source: trunk/gsinstaller/FilePath.cpp@ 1502

Last change on this file since 1502 was 1498, checked in by cs025, 24 years ago

Further changes for uninstaller

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1#include "FilePath.h"
2#include "File.h"
3
4#include <string.h>
5#include <ctype.h>
6#include <stdarg.h>
7#include <stdio.h>
8#include <dir.h>
9
10static char divider = '\\';
11static char dividerStr[] = "\\";
12
13bool __pathIsAValidRoot(string path)
14{ DWORD driveMask;
15 int driveNum;
16
17 if (path.length() > 3)
18 { return false;
19 }
20
21 if (path[1] != ':' || !isalpha(path[0]))
22 { return false;
23 }
24 if (isupper(path[0]))
25 { driveNum = path[0] - 'A';
26 }
27 else
28 { driveNum = path[0] - 'a';
29 }
30 if (driveNum > 25)
31 { return false;
32 }
33 driveMask = GetLogicalDrives();
34 if ((driveMask & (1 << driveNum)) != (1 << driveNum))
35 { return false;
36 }
37 return true;
38}
39
40FilePath::FilePath()
41{
42}
43
44FilePath::FilePath(int npaths, ...)
45{ va_list list;
46 string* pathArray;
47
48 pathArray = new string[npaths];
49
50 // initialise use of variable parameters
51 va_start(list, npaths);
52
53 if (pathArray)
54 { for (int i = 0; i < npaths; i ++)
55 { // get next parameter (guaranteed to be a char * by caller!!!)
56 pathArray[i] = va_arg(list, char *);
57
58 // discount leading slashes
59 if (pathArray[i][0] == divider)
60 { pathArray[i] = pathArray[i].substr(1);
61 }
62
63 // get rid of terminating slashes if present
64 if (pathArray[i][pathArray[i].length() - 1] == divider)
65 { pathArray[i] = pathArray[i].substr(0, pathArray[i].length() - 1);
66 }
67 }
68 }
69
70 // end of variable arguments
71 va_end(list);
72
73 // construct path in the usual manner
74 if (npaths > 0)
75 { this->path = pathArray[0];
76 }
77 for (int i = 1; i < npaths; i ++)
78 { // don't add slashes if suppressed
79 if (pathArray[i][0] != '!')
80 { this->path.append("\\");
81 this->path.append(pathArray[i]);
82 }
83 // if slashes suppressed, skip the magic character
84 else
85 { this->path.append(pathArray[i].substr(1));
86 }
87 }
88
89 // destroy temporary array
90 if (pathArray)
91 { delete[] pathArray;
92 }
93
94 // set if this is a valid root (unlikely in this circumstance)
95 this->is_aRoot = __pathIsAValidRoot(this->path);
96}
97
98void FilePath::_init(string filePath, string subPath)
99{ // copy the directory path
100 this->path = filePath;
101
102 // if it didn't have a concluding divider, then add one; note that if
103 // filePath is empty, we're not actually going to add any divider at
104 // all
105 if (filePath.length() > 0 &&
106 this->path[filePath.length()-1] != divider)
107 { this->path.append(dividerStr);
108 }
109
110 // copy in the subdirectory path, eliminating any leading divider
111 if (subPath[0] != divider)
112 { this->path.append(subPath);
113 }
114 else
115 { this->path.append(subPath.substr(1));
116 }
117
118 // set if this is a valid root (unlikely when joining two paths)
119 this->is_aRoot = __pathIsAValidRoot(this->path);
120}
121
122/**
123 * Construct a Filepath from a directory and a sub-directory path
124 */
125FilePath::FilePath(string filePath, string subPath)
126{ this->_init(filePath, subPath);
127}
128
129FilePath::FilePath(FilePath &path, string subPath)
130{ this->_init(path.pathString(), subPath);
131}
132
133FilePath::FilePath(string filePath)
134{ if (filePath.length() < 3)
135 { this->path = filePath;
136 this->path.append("\\");
137 }
138 else
139 { this->path = filePath;
140
141 // trim trailing directory separators
142 if (this->path[this->path.length() - 1] == '\\')
143 { this->path = this->path.substr(0, this->path.length() - 1);
144 }
145 }
146
147 // check for a valid root
148 this->is_aRoot = __pathIsAValidRoot(this->path);
149}
150
151bool FilePath::isEmpty()
152{ return this->path.length() == 0;
153}
154
155bool FilePath::isRoot()
156{ return this->is_aRoot;
157}
158
159bool FilePath::equals(FilePath &other)
160{ if (this->path.compare(other.path) == 0)
161 { return true;
162 }
163 return false;
164}
165
166bool FilePath::equals(const char *other)
167{ if (strcmp(this->path.c_str(), other) == 0)
168 { return true;
169 }
170 return false;
171}
172
173/**
174 * Create a filepath of the drive letter/colon only
175 */
176FilePath *FilePath::rootDrive()
177{ FilePath *reply = this->root();
178
179 if (reply->path.length() >= 2 &&
180 reply->path[1] == ':' &&
181 isalpha(reply->path[0])) // if this is a drive letter destination
182 { reply->path = reply->path.substr(0, 2);
183 }
184 else
185 { reply->path = "";
186 }
187 return reply;
188}
189
190/**
191 * Create a filepath of the root of the given location
192 */
193FilePath *FilePath::root()
194{ // TODO: cope with network destinations
195 FilePath *reply = new FilePath(this->path);
196
197 unsigned int truncate = reply->path.find(divider);
198 if (truncate != -1) // if it's got a divider then skip past it;
199 // we will assume that it's a drive letter first
200 { truncate ++;
201 reply->path = reply->path.substr(0, truncate);
202 }
203 // if we've got a drive, then we'll use it plus the divider
204 else if (reply->path[1] == ':' && reply->path.length() == 2 &&
205 isalpha(reply->path[0]))
206 { reply->path = reply->path + dividerStr;
207 }
208 else
209 { reply->path = "";
210 }
211 return reply;
212}
213
214/**
215 * Just a quick check on the existence of a file.
216 */
217bool FilePath::exists()
218{ File f(this->path);
219
220 return f.exists();
221}
222
223/**
224 * Get the immediate parent of the given filepath
225 */
226FilePath *FilePath::parent()
227{ // TODO: cope with network roots
228 FilePath *reply = new FilePath(this->path);
229
230 // get the last divider
231 unsigned int truncate = reply->path.find_last_of(divider);
232
233 // if it exists, then we can do a normal get parent
234 if (truncate != -1)
235 { // if it's the leftmost one as well then we'll skip it to
236 // give a parent of <drive>:\ as this patches some problems
237 // with windows calls! Note that a parent call to the returned
238 // object in this case will return itself
239 if (truncate == reply->path.find(divider))
240 { truncate ++;
241 }
242 reply->path = reply->path.substr(0, truncate);
243 }
244 // if we've got a drive letter only, then append the divider to
245 // patch problematic windows calls; again subsequent parent
246 // calls to our returned object would in effect return itself
247 else if (reply->path[1] == ':' && reply->path.length() == 2 &&
248 isalpha(reply->path[0]))
249 { reply->path = reply->path + dividerStr;
250 }
251 // a non-rooted object; return blank
252 else
253 { reply->path = "";
254 }
255 reply->is_aRoot = __pathIsAValidRoot(reply->path);
256 return reply;
257}
258
259FilePath *FilePath::append(FilePath &subPath)
260{ return new FilePath(this->path, subPath.path);
261}
262
263FilePath *FilePath::append(char *subPath)
264{ return new FilePath(this->path, subPath);
265}
266
267const char *FilePath::cString()
268{ return this->path.c_str();
269}
270
271string FilePath::pathString()
272{ return this->path;
273}
274
275bool FilePath::ensureWriteablePath()
276{ //TODO: don't assume that all roots are writeable!
277 if (this->isRoot())
278 { return true;
279 }
280
281 // does this path exist?
282 File thisFile(this->path);
283 bool reply = false;
284
285 // if not, ensure writeable parent
286 if (thisFile.exists() == false)
287 { FilePath *parent;
288
289 // create parent reference
290 parent = this->parent();
291 if (parent->equals(thisFile.getFileName()))
292 { delete parent;
293 return true;
294 }
295
296 // if parent is writeable then try to make the directory
297 if (parent->ensureWriteablePath())
298 { // make this directory; if it succeeded we reply positively
299 if (mkdir(this->path.c_str()) == 0)
300 { reply = true;
301 }
302 }
303 // destroy parent reference
304 delete parent;
305 }
306 else if (thisFile.isDirectory() == true)
307 { // is this writeable by user?; if so we're okay
308 if (thisFile.isWriteable())
309 { reply = true;
310 }
311 }
312
313 return reply;
314}
Note: See TracBrowser for help on using the repository browser.