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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// **********************************************************************
//
// Copyright (c) 2003-2009 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 <IceUtil/DisableWarnings.h>
#include <IceUtil/FileUtil.h>
#include <IceUtil/Unicode.h>
#include <climits>
#ifdef __BCPLUSPLUS__
# include <dir.h>
# include <io.h>
#endif
using namespace std;
//
// Detemine if path is an absolute path
//
bool
IceUtilInternal::isAbsolutePath(const string& path)
{
size_t i = 0;
size_t size = path.size();
// Skip whitespace
while(i < size && isspace(static_cast<unsigned char>(path[i])))
{
++i;
}
#ifdef _WIN32
// We need at least 3 non whitespace character to have
// and absolute path
if(i + 3 > size)
{
return false;
}
// Check for X:\ path ('\' may have been converted to '/')
if((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z'))
{
return path[i + 1] == ':' && (path[i + 2] == '\\' || path[i + 2] == '/');
}
// Check for UNC path
return (path[i] == '\\' && path[i + 1] == '\\') || path[i] == '/';
#else
if(i >= size)
{
return false;
}
return path[i] == '/';
#endif
}
//
// Detemine if a directory exists.
//
bool
IceUtilInternal::directoryExists(const string& path)
{
IceUtilInternal::structstat st;
if(IceUtilInternal::stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
{
return false;
}
return true;
}
//
// Determine if a regular file exists.
//
bool
IceUtilInternal::fileExists(const string& path)
{
IceUtilInternal::structstat st;
if(IceUtilInternal::stat(path, &st) != 0 || !S_ISREG(st.st_mode))
{
return false;
}
return true;
}
#ifdef _WIN32
//
// Stat
//
int
IceUtilInternal::stat(const string& path, structstat* buffer)
{
return _wstat(IceUtil::stringToWstring(path).c_str(), buffer);
}
int
IceUtilInternal::remove(const string& path)
{
return ::_wremove(IceUtil::stringToWstring(path).c_str());
}
int
IceUtilInternal::rename(const string& from, const string& to)
{
return ::_wrename(IceUtil::stringToWstring(from).c_str(), IceUtil::stringToWstring(to).c_str());
}
int
IceUtilInternal::rmdir(const string& path)
{
return ::_wrmdir(IceUtil::stringToWstring(path).c_str());
}
int
IceUtilInternal::mkdir(const string& path, int)
{
return ::_wmkdir(IceUtil::stringToWstring(path).c_str());
}
FILE*
IceUtilInternal::fopen(const string& path, const string& mode)
{
return ::_wfopen(IceUtil::stringToWstring(path).c_str(), IceUtil::stringToWstring(mode).c_str());
}
int
IceUtilInternal::open(const string& path, int flags)
{
return ::_wopen(IceUtil::stringToWstring(path).c_str(), flags);
}
int
IceUtilInternal::getcwd(string& cwd)
{
wchar_t cwdbuf[_MAX_PATH];
if(_wgetcwd(cwdbuf, _MAX_PATH) == NULL)
{
return -1;
}
cwd = IceUtil::wstringToString(cwdbuf);
return 0;
}
#else
//
// Stat
//
int
IceUtilInternal::stat(const string& path, structstat* buffer)
{
return stat(path.c_str(), buffer);
}
int
IceUtilInternal::remove(const string& path)
{
return ::remove(path.c_str());
}
int
IceUtilInternal::rename(const string& from, const string& to)
{
return ::rename(from.c_str(), to.c_str());
}
int
IceUtilInternal::rmdir(const string& path)
{
return ::rmdir(path.c_str());
}
int
IceUtilInternal::mkdir(const string& path, int perm)
{
return ::mkdir(path.c_str(), perm);
}
FILE*
IceUtilInternal::fopen(const string& path, const string& mode)
{
return ::fopen(path.c_str(), mode.c_str());
}
int
IceUtilInternal::open(const string& path, int flags)
{
return ::open(path.c_str(), flags);
}
int
IceUtilInternal::getcwd(string& cwd)
{
char cwdbuf[PATH_MAX];
if(::getcwd(cwdbuf, PATH_MAX) == NULL)
{
return -1;
}
cwd = cwdbuf;
return 0;
}
#endif
|