3 * (c) 1999 Microsoft Corporation. All rights reserved.
4 * Portions (c) 1999 ActiveState Tool Corp, http://www.ActiveState.com/
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
13 const int driveCount = 30;
18 VDir(int bManageDir = 1);
21 void Init(VDir* pDir, VMem *pMem);
22 void SetDefaultA(char const *pDefault);
23 void SetDefaultW(WCHAR const *pDefault);
24 char* MapPathA(const char *pInName);
25 WCHAR* MapPathW(const WCHAR *pInName);
26 int SetCurrentDirectoryA(char *lpBuffer);
27 int SetCurrentDirectoryW(WCHAR *lpBuffer);
28 inline const char *GetDirA(int index)
30 return dirTableA[index];
32 inline const WCHAR *GetDirW(int index)
34 return dirTableW[index];
36 inline int GetDefault(void) { return nDefault; };
38 inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
40 char* ptr = dirTableA[nDefault];
43 if ((*lpBuffer++ = *ptr++) == '\0')
48 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
50 WCHAR* ptr = dirTableW[nDefault];
53 if ((*lpBuffer++ = *ptr++) == '\0')
60 DWORD CalculateEnvironmentSpace(void);
61 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
64 int SetDirA(char const *pPath, int index);
65 void FromEnvA(char *pEnv, int index);
66 inline const char *GetDefaultDirA(void)
68 return dirTableA[nDefault];
71 inline void SetDefaultDirA(char const *pPath, int index)
73 SetDirA(pPath, index);
76 int SetDirW(WCHAR const *pPath, int index);
77 inline const WCHAR *GetDefaultDirW(void)
79 return dirTableW[nDefault];
82 inline void SetDefaultDirW(WCHAR const *pPath, int index)
84 SetDirW(pPath, index);
88 inline int DriveIndex(char chr)
90 return (chr | 0x20)-'a';
94 int nDefault, bManageDirectory;
95 char *dirTableA[driveCount];
96 char szLocalBufferA[MAX_PATH+1];
97 WCHAR *dirTableW[driveCount];
98 WCHAR szLocalBufferW[MAX_PATH+1];
102 VDir::VDir(int bManageDir /* = 1 */)
105 bManageDirectory = bManageDir;
106 memset(dirTableA, 0, sizeof(dirTableA));
107 memset(dirTableW, 0, sizeof(dirTableW));
110 void VDir::Init(VDir* pDir, VMem *p)
115 char szBuffer[MAX_PATH*driveCount];
119 for (index = 0; index < driveCount; ++index) {
120 SetDirW(pDir->GetDirW(index), index);
122 nDefault = pDir->GetDefault();
125 nSave = bManageDirectory;
126 bManageDirectory = 0;
127 driveBits = GetLogicalDrives();
128 if (GetLogicalDriveStrings(sizeof(szBuffer), szBuffer)) {
129 char* pEnv = GetEnvironmentStrings();
130 char* ptr = szBuffer;
131 for (index = 0; index < driveCount; ++index) {
132 if (driveBits & (1<<index)) {
133 ptr += SetDirA(ptr, index) + 1;
134 FromEnvA(pEnv, index);
137 FreeEnvironmentStrings(pEnv);
140 bManageDirectory = nSave;
144 int VDir::SetDirA(char const *pPath, int index)
148 WCHAR wBuffer[MAX_PATH+1];
149 if (index < driveCount && pPath != NULL) {
150 length = strlen(pPath);
151 pMem->Free(dirTableA[index]);
152 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
157 if (chr != '\\' && chr != '/') {
161 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
162 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
163 length = wcslen(wBuffer);
164 pMem->Free(dirTableW[index]);
165 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
166 if (dirTableW[index] != NULL) {
167 wcscpy(dirTableW[index], wBuffer);
173 ::SetCurrentDirectoryA(pPath);
178 void VDir::FromEnvA(char *pEnv, int index)
179 { /* gets the directory for index from the environment variable. */
180 while (*pEnv != '\0') {
181 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
182 SetDirA(&pEnv[4], index);
186 pEnv += strlen(pEnv)+1;
190 void VDir::SetDefaultA(char const *pDefault)
192 char szBuffer[MAX_PATH+1];
195 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
196 if (*pDefault != '.' && pPtr != NULL)
199 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
203 int VDir::SetDirW(WCHAR const *pPath, int index)
206 char szBuffer[MAX_PATH+1];
208 if (index < driveCount && pPath != NULL) {
209 length = wcslen(pPath);
210 pMem->Free(dirTableW[index]);
211 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
216 if (chr != '\\' && chr != '/') {
220 WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
221 length = strlen(szBuffer);
222 pMem->Free(dirTableA[index]);
223 dirTableA[index] = (char*)pMem->Malloc(length+1);
224 if (dirTableA[index] != NULL) {
225 strcpy(dirTableA[index], szBuffer);
231 ::SetCurrentDirectoryW(pPath);
236 void VDir::SetDefaultW(WCHAR const *pDefault)
238 WCHAR szBuffer[MAX_PATH+1];
241 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
242 if (*pDefault != '.' && pPtr != NULL)
245 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
249 inline BOOL IsPathSep(char ch)
251 return (ch == '\\' || ch == '/');
254 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
259 * On WinNT GetFullPathName does not fail, (or at least always
260 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
261 * On Win98 GetFullPathName will set last error if it fails, but
262 * does not touch *Dest
265 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
268 char *VDir::MapPathA(const char *pInName)
270 * possiblities -- relative path or absolute path with or without drive letter
273 char szBuffer[(MAX_PATH+1)*2];
274 char szlBuf[MAX_PATH+1];
276 if (strlen(pInName) > MAX_PATH) {
277 strncpy(szlBuf, pInName, MAX_PATH);
278 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
279 /* absolute path - reduce length by 2 for drive specifier */
280 szlBuf[MAX_PATH-2] = '\0';
283 szlBuf[MAX_PATH] = '\0';
286 /* strlen(pInName) is now <= MAX_PATH */
288 if (pInName[1] == ':') {
289 /* has drive letter */
290 if (IsPathSep(pInName[2])) {
291 /* absolute with drive letter */
292 strcpy(szLocalBufferA, pInName);
295 /* relative path with drive letter */
296 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
297 strcat(szBuffer, &pInName[2]);
298 if(strlen(szBuffer) > MAX_PATH)
299 szBuffer[MAX_PATH] = '\0';
301 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
305 /* no drive letter */
306 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
308 strcpy(szLocalBufferA, pInName);
311 strcpy(szBuffer, GetDefaultDirA());
312 if (IsPathSep(pInName[0])) {
314 szLocalBufferA[0] = szBuffer[0];
315 szLocalBufferA[1] = szBuffer[1];
316 strcpy(&szLocalBufferA[2], pInName);
320 strcat(szBuffer, pInName);
321 if (strlen(szBuffer) > MAX_PATH)
322 szBuffer[MAX_PATH] = '\0';
324 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
329 return szLocalBufferA;
332 int VDir::SetCurrentDirectoryA(char *lpBuffer)
335 WIN32_FIND_DATA win32FD;
336 char szBuffer[MAX_PATH+1], *pPtr;
337 int length, nRet = -1;
339 GetFullPathNameA(MapPathA(lpBuffer), sizeof(szBuffer), szBuffer, &pPtr);
340 /* if the last char is a '\\' or a '/' then add
341 * an '*' before calling FindFirstFile
343 length = strlen(szBuffer);
344 if(length > 0 && IsPathSep(szBuffer[length-1])) {
345 szBuffer[length] = '*';
346 szBuffer[length+1] = '\0';
349 hHandle = FindFirstFileA(szBuffer, &win32FD);
350 if (hHandle != INVALID_HANDLE_VALUE) {
353 /* if an '*' was added remove it */
354 if(szBuffer[length] == '*')
355 szBuffer[length] = '\0';
357 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
363 DWORD VDir::CalculateEnvironmentSpace(void)
364 { /* the current directory environment strings are stored as '=d=d:\path' */
367 for (index = 0; index < driveCount; ++index) {
368 if (dirTableA[index] != NULL) {
369 dwSize += strlen(dirTableA[index]) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
375 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
376 { /* store the current directory environment strings as '=d=d:\path' */
379 for (index = 0; index < driveCount; ++index) {
380 lpDirStr = dirTableA[index];
381 if (lpDirStr != NULL) {
383 lpStr[1] = lpDirStr[0];
385 strcpy(&lpStr[3], lpDirStr);
386 lpStr += strlen(lpDirStr) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
392 inline BOOL IsPathSep(WCHAR ch)
394 return (ch == '\\' || ch == '/');
397 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
402 * On WinNT GetFullPathName does not fail, (or at least always
403 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
404 * On Win98 GetFullPathName will set last error if it fails, but
405 * does not touch *Dest
408 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
411 WCHAR* VDir::MapPathW(const WCHAR *pInName)
413 * possiblities -- relative path or absolute path with or without drive letter
416 WCHAR szBuffer[(MAX_PATH+1)*2];
417 WCHAR szlBuf[MAX_PATH+1];
419 if (wcslen(pInName) > MAX_PATH) {
420 wcsncpy(szlBuf, pInName, MAX_PATH);
421 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
422 /* absolute path - reduce length by 2 for drive specifier */
423 szlBuf[MAX_PATH-2] = '\0';
426 szlBuf[MAX_PATH] = '\0';
429 /* strlen(pInName) is now <= MAX_PATH */
431 if (pInName[1] == ':') {
432 /* has drive letter */
433 if (IsPathSep(pInName[2])) {
434 /* absolute with drive letter */
435 wcscpy(szLocalBufferW, pInName);
438 /* relative path with drive letter */
439 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
440 wcscat(szBuffer, &pInName[2]);
441 if(wcslen(szBuffer) > MAX_PATH)
442 szBuffer[MAX_PATH] = '\0';
444 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
448 /* no drive letter */
449 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
451 wcscpy(szLocalBufferW, pInName);
454 wcscpy(szBuffer, GetDefaultDirW());
455 if (IsPathSep(pInName[0])) {
457 szLocalBufferW[0] = szBuffer[0];
458 szLocalBufferW[1] = szBuffer[1];
459 wcscpy(&szLocalBufferW[2], pInName);
463 wcscat(szBuffer, pInName);
464 if (wcslen(szBuffer) > MAX_PATH)
465 szBuffer[MAX_PATH] = '\0';
467 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
471 return szLocalBufferW;
474 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
477 WIN32_FIND_DATAW win32FD;
478 WCHAR szBuffer[MAX_PATH+1], *pPtr;
479 int length, nRet = -1;
481 GetFullPathNameW(MapPathW(lpBuffer), (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr);
482 /* if the last char is a '\\' or a '/' then add
483 * an '*' before calling FindFirstFile
485 length = wcslen(szBuffer);
486 if(length > 0 && IsPathSep(szBuffer[length-1])) {
487 szBuffer[length] = '*';
488 szBuffer[length+1] = '\0';
491 hHandle = FindFirstFileW(szBuffer, &win32FD);
492 if (hHandle != INVALID_HANDLE_VALUE) {
495 /* if an '*' was added remove it */
496 if(szBuffer[length] == '*')
497 szBuffer[length] = '\0';
499 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
505 #endif /* ___VDir_H___ */