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.
14 * Allow one slot for each possible drive letter
15 * and one additional slot for a UNC name
17 const int driveCount = ('Z'-'A')+1+1;
22 VDir(int bManageDir = 1);
25 void Init(VDir* pDir, VMem *pMem);
26 void SetDefaultA(char const *pDefault);
27 void SetDefaultW(WCHAR const *pDefault);
28 char* MapPathA(const char *pInName);
29 WCHAR* MapPathW(const WCHAR *pInName);
30 int SetCurrentDirectoryA(char *lpBuffer);
31 int SetCurrentDirectoryW(WCHAR *lpBuffer);
32 inline int GetDefault(void) { return nDefault; };
34 inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
36 char* ptr = dirTableA[nDefault];
39 if ((*lpBuffer++ = *ptr++) == '\0')
43 return /* unused */ NULL;
45 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
47 WCHAR* ptr = dirTableW[nDefault];
50 if ((*lpBuffer++ = *ptr++) == '\0')
54 return /* unused */ NULL;
57 DWORD CalculateEnvironmentSpace(void);
58 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
61 int SetDirA(char const *pPath, int index);
62 int SetDirW(WCHAR const *pPath, int index);
63 void FromEnvA(char *pEnv, int index);
64 void FromEnvW(WCHAR *pEnv, int index);
66 inline const char *GetDefaultDirA(void)
68 return dirTableA[nDefault];
70 inline void SetDefaultDirA(char const *pPath, int index)
72 SetDirA(pPath, index);
75 inline const WCHAR *GetDefaultDirW(void)
77 return dirTableW[nDefault];
79 inline void SetDefaultDirW(WCHAR const *pPath, int index)
81 SetDirW(pPath, index);
84 inline const char *GetDirA(int index)
86 char *ptr = dirTableA[index];
88 /* simulate the existance of this drive */
97 inline const WCHAR *GetDirW(int index)
99 WCHAR *ptr = dirTableW[index];
101 /* simulate the existance of this drive */
102 ptr = szLocalBufferW;
103 ptr[0] = 'A' + index;
111 inline int DriveIndex(char chr)
113 if (chr == '\\' || chr == '/')
115 return (chr | 0x20)-'a';
119 int nDefault, bManageDirectory;
120 char *dirTableA[driveCount];
121 char szLocalBufferA[MAX_PATH+1];
122 WCHAR *dirTableW[driveCount];
123 WCHAR szLocalBufferW[MAX_PATH+1];
127 VDir::VDir(int bManageDir /* = 1 */)
130 bManageDirectory = bManageDir;
131 memset(dirTableA, 0, sizeof(dirTableA));
132 memset(dirTableW, 0, sizeof(dirTableW));
135 void VDir::Init(VDir* pDir, VMem *p)
141 for (index = 0; index < driveCount; ++index) {
142 SetDirW(pDir->GetDirW(index), index);
144 nDefault = pDir->GetDefault();
147 int bSave = bManageDirectory;
148 DWORD driveBits = GetLogicalDrives();
151 memset(&osver, 0, sizeof(osver));
152 osver.dwOSVersionInfoSize = sizeof(osver);
153 GetVersionEx(&osver);
155 bManageDirectory = 0;
156 if (osver.dwMajorVersion < 5) {
157 char szBuffer[MAX_PATH*driveCount];
158 if (GetLogicalDriveStringsA(sizeof(szBuffer), szBuffer)) {
159 char* pEnv = (char*)GetEnvironmentStringsA();
160 char* ptr = szBuffer;
161 for (index = 0; index < driveCount; ++index) {
162 if (driveBits & (1<<index)) {
163 ptr += SetDirA(ptr, index) + 1;
164 FromEnvA(pEnv, index);
167 FreeEnvironmentStringsA(pEnv);
171 else { /* Windows 2000 or later */
172 WCHAR szBuffer[MAX_PATH*driveCount];
173 if (GetLogicalDriveStringsW(sizeof(szBuffer), szBuffer)) {
174 WCHAR* pEnv = GetEnvironmentStringsW();
175 WCHAR* ptr = szBuffer;
176 for (index = 0; index < driveCount; ++index) {
177 if (driveBits & (1<<index)) {
178 ptr += SetDirW(ptr, index) + 1;
179 FromEnvW(pEnv, index);
182 FreeEnvironmentStringsW(pEnv);
186 bManageDirectory = bSave;
190 int VDir::SetDirA(char const *pPath, int index)
194 WCHAR wBuffer[MAX_PATH+1];
195 if (index < driveCount && pPath != NULL) {
196 length = strlen(pPath);
197 pMem->Free(dirTableA[index]);
198 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
203 if (chr != '\\' && chr != '/') {
207 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
208 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
209 length = wcslen(wBuffer);
210 pMem->Free(dirTableW[index]);
211 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
212 if (dirTableW[index] != NULL) {
213 wcscpy(dirTableW[index], wBuffer);
219 ::SetCurrentDirectoryA(pPath);
224 void VDir::FromEnvA(char *pEnv, int index)
225 { /* gets the directory for index from the environment variable. */
226 while (*pEnv != '\0') {
227 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
228 SetDirA(&pEnv[4], index);
232 pEnv += strlen(pEnv)+1;
236 void VDir::FromEnvW(WCHAR *pEnv, int index)
237 { /* gets the directory for index from the environment variable. */
238 while (*pEnv != '\0') {
239 if ((pEnv[0] == '=') && (DriveIndex((char)pEnv[1]) == index)) {
240 SetDirW(&pEnv[4], index);
244 pEnv += wcslen(pEnv)+1;
248 void VDir::SetDefaultA(char const *pDefault)
250 char szBuffer[MAX_PATH+1];
253 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
254 if (*pDefault != '.' && pPtr != NULL)
257 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
261 int VDir::SetDirW(WCHAR const *pPath, int index)
265 if (index < driveCount && pPath != NULL) {
266 length = wcslen(pPath);
267 pMem->Free(dirTableW[index]);
268 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
274 if (chr != '\\' && chr != '/') {
278 ansi = win32_ansipath(dirTableW[index]);
279 length = strlen(ansi);
280 pMem->Free(dirTableA[index]);
281 dirTableA[index] = (char*)pMem->Malloc(length+1);
282 if (dirTableA[index] != NULL) {
283 strcpy(dirTableA[index], ansi);
290 ::SetCurrentDirectoryW(pPath);
295 void VDir::SetDefaultW(WCHAR const *pDefault)
297 WCHAR szBuffer[MAX_PATH+1];
300 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
301 if (*pDefault != '.' && pPtr != NULL)
304 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
308 inline BOOL IsPathSep(char ch)
310 return (ch == '\\' || ch == '/');
313 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
318 * On WinNT GetFullPathName does not fail, (or at least always
319 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
320 * On Win98 GetFullPathName will set last error if it fails, but
321 * does not touch *Dest
324 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
327 inline bool IsSpecialFileName(const char* pName)
329 /* specical file names are devices that the system can open
330 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
331 * (x is a single digit, and names are case-insensitive)
333 char ch = (pName[0] & ~0x20);
337 if (((pName[1] & ~0x20) == 'U')
338 && ((pName[2] & ~0x20) == 'X')
342 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
343 ch = (pName[1] & ~0x20);
346 case 'L': /* CLOCK$ */
347 if (((pName[2] & ~0x20) == 'O')
348 && ((pName[3] & ~0x20) == 'C')
349 && ((pName[4] & ~0x20) == 'K')
354 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
355 if ((pName[2] & ~0x20) == 'M') {
356 if ((pName[3] >= '1') && (pName[3] <= '9')
360 else if ((pName[2] & ~0x20) == 'N') {
363 else if ((pName[3] & ~0x20) == 'I') {
364 if (((pName[4] & ~0x20) == 'N')
369 else if ((pName[3] & ~0x20) == 'O') {
370 if (((pName[4] & ~0x20) == 'U')
371 && ((pName[5] & ~0x20) == 'T')
381 if (((pName[1] & ~0x20) == 'U')
382 && ((pName[2] & ~0x20) == 'X')
383 && (pName[3] >= '1') && (pName[3] <= '9')
388 if (((pName[1] & ~0x20) == 'U')
389 && ((pName[2] & ~0x20) == 'L')
394 if (((pName[1] & ~0x20) == 'R')
395 && ((pName[2] & ~0x20) == 'N')
403 char *VDir::MapPathA(const char *pInName)
405 * possiblities -- relative path or absolute path with or without drive letter
408 char szBuffer[(MAX_PATH+1)*2];
409 char szlBuf[MAX_PATH+1];
410 int length = strlen(pInName);
413 return (char*)pInName;
415 if (length > MAX_PATH) {
416 strncpy(szlBuf, pInName, MAX_PATH);
417 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
418 /* absolute path - reduce length by 2 for drive specifier */
419 szlBuf[MAX_PATH-2] = '\0';
422 szlBuf[MAX_PATH] = '\0';
425 /* strlen(pInName) is now <= MAX_PATH */
427 if (pInName[1] == ':') {
428 /* has drive letter */
429 if (IsPathSep(pInName[2])) {
430 /* absolute with drive letter */
431 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
434 /* relative path with drive letter */
435 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
436 strcat(szBuffer, &pInName[2]);
437 if(strlen(szBuffer) > MAX_PATH)
438 szBuffer[MAX_PATH] = '\0';
440 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
444 /* no drive letter */
445 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
447 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
450 strcpy(szBuffer, GetDefaultDirA());
451 if (IsPathSep(pInName[0])) {
453 strcpy(&szBuffer[2], pInName);
454 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
458 if (IsSpecialFileName(pInName)) {
459 return (char*)pInName;
462 strcat(szBuffer, pInName);
463 if (strlen(szBuffer) > MAX_PATH)
464 szBuffer[MAX_PATH] = '\0';
466 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
472 return szLocalBufferA;
475 int VDir::SetCurrentDirectoryA(char *lpBuffer)
478 int length, nRet = -1;
480 pPtr = MapPathA(lpBuffer);
481 length = strlen(pPtr);
482 if(length > 3 && IsPathSep(pPtr[length-1])) {
483 /* don't remove the trailing slash from 'x:\' */
484 pPtr[length-1] = '\0';
487 DWORD r = GetFileAttributesA(pPtr);
488 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
490 char szBuffer[(MAX_PATH+1)*2];
491 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
492 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
499 DWORD VDir::CalculateEnvironmentSpace(void)
500 { /* the current directory environment strings are stored as '=D:=d:\path' */
503 for (index = 0; index < driveCount; ++index) {
504 if (dirTableA[index] != NULL) {
505 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
511 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
512 { /* store the current directory environment strings as '=D:=d:\path' */
515 for (index = 0; index < driveCount; ++index) {
516 lpDirStr = dirTableA[index];
517 if (lpDirStr != NULL) {
519 lpStr[1] = lpDirStr[0];
521 CharUpper(&lpStr[1]);
524 strcpy(&lpStr[4], lpDirStr);
525 length = strlen(lpDirStr);
526 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
527 if (length > 3 && IsPathSep(lpStr[-2])) {
528 lpStr[-2] = '\0'; /* remove the trailing path separator */
536 inline BOOL IsPathSep(WCHAR ch)
538 return (ch == '\\' || ch == '/');
541 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
546 * On WinNT GetFullPathName does not fail, (or at least always
547 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
548 * On Win98 GetFullPathName will set last error if it fails, but
549 * does not touch *Dest
552 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
555 inline bool IsSpecialFileName(const WCHAR* pName)
557 /* specical file names are devices that the system can open
558 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
559 * (x is a single digit, and names are case-insensitive)
561 WCHAR ch = (pName[0] & ~0x20);
565 if (((pName[1] & ~0x20) == 'U')
566 && ((pName[2] & ~0x20) == 'X')
570 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
571 ch = (pName[1] & ~0x20);
574 case 'L': /* CLOCK$ */
575 if (((pName[2] & ~0x20) == 'O')
576 && ((pName[3] & ~0x20) == 'C')
577 && ((pName[4] & ~0x20) == 'K')
582 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
583 if ((pName[2] & ~0x20) == 'M') {
584 if ((pName[3] >= '1') && (pName[3] <= '9')
588 else if ((pName[2] & ~0x20) == 'N') {
591 else if ((pName[3] & ~0x20) == 'I') {
592 if (((pName[4] & ~0x20) == 'N')
597 else if ((pName[3] & ~0x20) == 'O') {
598 if (((pName[4] & ~0x20) == 'U')
599 && ((pName[5] & ~0x20) == 'T')
609 if (((pName[1] & ~0x20) == 'U')
610 && ((pName[2] & ~0x20) == 'X')
611 && (pName[3] >= '1') && (pName[3] <= '9')
616 if (((pName[1] & ~0x20) == 'U')
617 && ((pName[2] & ~0x20) == 'L')
622 if (((pName[1] & ~0x20) == 'R')
623 && ((pName[2] & ~0x20) == 'N')
631 WCHAR* VDir::MapPathW(const WCHAR *pInName)
633 * possiblities -- relative path or absolute path with or without drive letter
636 WCHAR szBuffer[(MAX_PATH+1)*2];
637 WCHAR szlBuf[MAX_PATH+1];
638 int length = wcslen(pInName);
641 return (WCHAR*)pInName;
643 if (length > MAX_PATH) {
644 wcsncpy(szlBuf, pInName, MAX_PATH);
645 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
646 /* absolute path - reduce length by 2 for drive specifier */
647 szlBuf[MAX_PATH-2] = '\0';
650 szlBuf[MAX_PATH] = '\0';
653 /* strlen(pInName) is now <= MAX_PATH */
655 if (pInName[1] == ':') {
656 /* has drive letter */
657 if (IsPathSep(pInName[2])) {
658 /* absolute with drive letter */
659 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
662 /* relative path with drive letter */
663 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
664 wcscat(szBuffer, &pInName[2]);
665 if(wcslen(szBuffer) > MAX_PATH)
666 szBuffer[MAX_PATH] = '\0';
668 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
672 /* no drive letter */
673 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
675 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
678 wcscpy(szBuffer, GetDefaultDirW());
679 if (IsPathSep(pInName[0])) {
681 wcscpy(&szBuffer[2], pInName);
682 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
686 if (IsSpecialFileName(pInName)) {
687 return (WCHAR*)pInName;
690 wcscat(szBuffer, pInName);
691 if (wcslen(szBuffer) > MAX_PATH)
692 szBuffer[MAX_PATH] = '\0';
694 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
699 return szLocalBufferW;
702 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
705 int length, nRet = -1;
707 pPtr = MapPathW(lpBuffer);
708 length = wcslen(pPtr);
709 if(length > 3 && IsPathSep(pPtr[length-1])) {
710 /* don't remove the trailing slash from 'x:\' */
711 pPtr[length-1] = '\0';
714 DWORD r = GetFileAttributesW(pPtr);
715 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
717 WCHAR wBuffer[(MAX_PATH+1)*2];
718 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
719 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
726 #endif /* ___VDir_H___ */