summaryrefslogtreecommitdiff
path: root/cpp/src/Slice/Util.cpp
blob: 663a0ef952a1d9de6dada909a01d525430ac89ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Slice/Util.h>
#include <IceUtil/Unicode.h>
#include <climits>

#ifdef __hpux
#  include <unistd.h>
#endif

using namespace std;
using namespace Slice;

string
Slice::getCwd()
{
#ifdef _WIN32
    wchar_t cwdbuf[_MAX_PATH];
    if(_wgetcwd(cwdbuf, _MAX_PATH) != NULL)
    {
        return IceUtil::wstringToString(cwdbuf);
    }
#else
    char cwdbuf[PATH_MAX];
    if(::getcwd(cwdbuf, PATH_MAX) != NULL)
    {
        return cwdbuf;
    }
#endif
    return "";
}

bool
Slice::isAbsolute(const string& path)
{
#ifdef _WIN32
        if(path[0] == '\\' || path[0] == '/' || path.size() > 1 && isalpha(path[0]) && path[1] == ':')
#else
        if(path[0] == '/')
#endif
        {
            return true;
        }

        return false;
}

string
Slice::normalizePath(const string& path, bool removeDriveLetter)
{
    string result = path;
    replace(result.begin(), result.end(), '\\', '/');
    string::size_type pos;
    while((pos = result.find("//")) != string::npos)
    {
        result.replace(pos, 2, "/");
    }
    pos = 0;
    while((pos = result.find("/..", pos)) != string::npos)
    {
        string::size_type last = result.find_last_of("/", pos - 1);
        if(last != string::npos && result.substr(last, 4) != "/../")
        {
            result.erase(last, pos - last + 3);
            pos = last;
        }
        else
        {
            ++pos;
        }
    }

    if(removeDriveLetter && result.size() > 1 && isalpha(result[0]) && result[1] == ':')
    {
        result = result.substr(2);
    }

    return result;
}