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;
18 const int driveLetterCount = ('Z'-'A')+1;
23 VDir(int bManageDir = 1);
26 void Init(VDir* pDir, VMem *pMem);
27 void SetDefaultA(char const *pDefault);
28 void SetDefaultW(WCHAR const *pDefault);
29 char* MapPathA(const char *pInName);
30 WCHAR* MapPathW(const WCHAR *pInName);
31 int SetCurrentDirectoryA(char *lpBuffer);
32 int SetCurrentDirectoryW(WCHAR *lpBuffer);
33 inline int GetDefault(void) { return nDefault; };
35 inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
37 char* ptr = dirTableA[nDefault];
40 if ((*lpBuffer++ = *ptr++) == '\0')
44 return /* unused */ NULL;
46 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
48 WCHAR* ptr = dirTableW[nDefault];
51 if ((*lpBuffer++ = *ptr++) == '\0')
55 return /* unused */ NULL;
58 DWORD CalculateEnvironmentSpace(void);
59 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
62 int SetDirA(char const *pPath, int index);
63 int SetDirW(WCHAR const *pPath, int index);
64 void FromEnvA(char *pEnv, int index);
65 void FromEnvW(WCHAR *pEnv, int index);
67 inline const char *GetDefaultDirA(void)
69 return dirTableA[nDefault];
71 inline void SetDefaultDirA(char const *pPath, int index)
73 SetDirA(pPath, index);
76 inline const WCHAR *GetDefaultDirW(void)
78 return dirTableW[nDefault];
80 inline void SetDefaultDirW(WCHAR const *pPath, int index)
82 SetDirW(pPath, index);
85 inline const char *GetDirA(int index)
87 char *ptr = dirTableA[index];
89 /* simulate the existence of this drive */
98 inline const WCHAR *GetDirW(int index)
100 WCHAR *ptr = dirTableW[index];
102 /* simulate the existence of this drive */
103 ptr = szLocalBufferW;
104 ptr[0] = 'A' + index;
112 inline int DriveIndex(char chr)
114 if (chr == '\\' || chr == '/')
116 return (chr | 0x20)-'a';
120 int nDefault, bManageDirectory;
121 char *dirTableA[driveCount];
122 char szLocalBufferA[MAX_PATH+1];
123 WCHAR *dirTableW[driveCount];
124 WCHAR szLocalBufferW[MAX_PATH+1];
128 VDir::VDir(int bManageDir /* = 1 */)
131 bManageDirectory = bManageDir;
132 memset(dirTableA, 0, sizeof(dirTableA));
133 memset(dirTableW, 0, sizeof(dirTableW));
136 void VDir::Init(VDir* pDir, VMem *p)
142 for (index = 0; index < driveCount; ++index) {
143 SetDirW(pDir->GetDirW(index), index);
145 nDefault = pDir->GetDefault();
148 int bSave = bManageDirectory;
149 DWORD driveBits = GetLogicalDrives();
151 bManageDirectory = 0;
152 WCHAR szBuffer[MAX_PATH*driveCount];
153 if (GetLogicalDriveStringsW(sizeof(szBuffer), szBuffer)) {
154 WCHAR* pEnv = GetEnvironmentStringsW();
155 WCHAR* ptr = szBuffer;
156 for (index = 0; index < driveCount; ++index) {
157 if (driveBits & (1<<index)) {
158 ptr += SetDirW(ptr, index) + 1;
159 FromEnvW(pEnv, index);
162 FreeEnvironmentStringsW(pEnv);
165 bManageDirectory = bSave;
169 int VDir::SetDirA(char const *pPath, int index)
173 WCHAR wBuffer[MAX_PATH+1];
174 if (index < driveCount && pPath != NULL) {
175 length = strlen(pPath);
176 pMem->Free(dirTableA[index]);
177 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
182 if (chr != '\\' && chr != '/') {
186 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
187 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
188 length = wcslen(wBuffer);
189 pMem->Free(dirTableW[index]);
190 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
191 if (dirTableW[index] != NULL) {
192 wcscpy(dirTableW[index], wBuffer);
198 ::SetCurrentDirectoryA(pPath);
203 void VDir::FromEnvA(char *pEnv, int index)
204 { /* gets the directory for index from the environment variable. */
205 while (*pEnv != '\0') {
206 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
207 SetDirA(&pEnv[4], index);
211 pEnv += strlen(pEnv)+1;
215 void VDir::FromEnvW(WCHAR *pEnv, int index)
216 { /* gets the directory for index from the environment variable. */
217 while (*pEnv != '\0') {
218 if ((pEnv[0] == '=') && (DriveIndex((char)pEnv[1]) == index)) {
219 SetDirW(&pEnv[4], index);
223 pEnv += wcslen(pEnv)+1;
227 void VDir::SetDefaultA(char const *pDefault)
229 char szBuffer[MAX_PATH+1];
232 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
233 if (*pDefault != '.' && pPtr != NULL)
236 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
240 int VDir::SetDirW(WCHAR const *pPath, int index)
244 if (index < driveCount && pPath != NULL) {
245 length = wcslen(pPath);
246 pMem->Free(dirTableW[index]);
247 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
253 if (chr != '\\' && chr != '/') {
257 ansi = win32_ansipath(dirTableW[index]);
258 length = strlen(ansi);
259 pMem->Free(dirTableA[index]);
260 dirTableA[index] = (char*)pMem->Malloc(length+1);
261 if (dirTableA[index] != NULL) {
262 strcpy(dirTableA[index], ansi);
269 ::SetCurrentDirectoryW(pPath);
274 void VDir::SetDefaultW(WCHAR const *pDefault)
276 WCHAR szBuffer[MAX_PATH+1];
279 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
280 if (*pDefault != '.' && pPtr != NULL)
283 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
287 inline BOOL IsPathSep(char ch)
289 return (ch == '\\' || ch == '/');
292 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
297 * On WinNT GetFullPathName does not fail, (or at least always
298 * succeeds when the drive is valid) WinNT does set *Dest to NULL
299 * On Win98 GetFullPathName will set last error if it fails, but
300 * does not touch *Dest
303 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
306 inline bool IsSpecialFileName(const char* pName)
308 /* specical file names are devices that the system can open
309 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
310 * (x is a single digit, and names are case-insensitive)
312 char ch = (pName[0] & ~0x20);
316 if (((pName[1] & ~0x20) == 'U')
317 && ((pName[2] & ~0x20) == 'X')
321 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
322 ch = (pName[1] & ~0x20);
325 case 'L': /* CLOCK$ */
326 if (((pName[2] & ~0x20) == 'O')
327 && ((pName[3] & ~0x20) == 'C')
328 && ((pName[4] & ~0x20) == 'K')
333 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
334 if ((pName[2] & ~0x20) == 'M') {
335 if ((pName[3] >= '1') && (pName[3] <= '9')
339 else if ((pName[2] & ~0x20) == 'N') {
342 else if ((pName[3] & ~0x20) == 'I') {
343 if (((pName[4] & ~0x20) == 'N')
348 else if ((pName[3] & ~0x20) == 'O') {
349 if (((pName[4] & ~0x20) == 'U')
350 && ((pName[5] & ~0x20) == 'T')
360 if (((pName[1] & ~0x20) == 'U')
361 && ((pName[2] & ~0x20) == 'X')
362 && (pName[3] >= '1') && (pName[3] <= '9')
367 if (((pName[1] & ~0x20) == 'U')
368 && ((pName[2] & ~0x20) == 'L')
373 if (((pName[1] & ~0x20) == 'R')
374 && ((pName[2] & ~0x20) == 'N')
382 char *VDir::MapPathA(const char *pInName)
384 * possiblities -- relative path or absolute path with or without drive letter
388 char szBuffer[(MAX_PATH+1)*2];
389 char szlBuf[MAX_PATH+1];
390 int length = strlen(pInName);
393 return (char*)pInName;
395 if (length > MAX_PATH) {
396 strncpy(szlBuf, pInName, MAX_PATH);
397 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
398 /* absolute path - reduce length by 2 for drive specifier */
399 szlBuf[MAX_PATH-2] = '\0';
402 szlBuf[MAX_PATH] = '\0';
405 /* strlen(pInName) is now <= MAX_PATH */
407 if (length > 1 && pInName[1] == ':') {
408 /* has drive letter */
409 if (length > 2 && IsPathSep(pInName[2])) {
410 /* absolute with drive letter */
411 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
414 /* relative path with drive letter */
415 driveIndex = DriveIndex(*pInName);
416 if (driveIndex < 0 || driveIndex >= driveLetterCount)
417 return (char *)pInName;
418 strcpy(szBuffer, GetDirA(driveIndex));
419 strcat(szBuffer, &pInName[2]);
420 if(strlen(szBuffer) > MAX_PATH)
421 szBuffer[MAX_PATH] = '\0';
423 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
427 /* no drive letter */
428 if (length > 1 && IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
430 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
433 strcpy(szBuffer, GetDefaultDirA());
434 if (IsPathSep(pInName[0])) {
436 strcpy(&szBuffer[2], pInName);
437 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
441 if (IsSpecialFileName(pInName)) {
442 return (char*)pInName;
445 strcat(szBuffer, pInName);
446 if (strlen(szBuffer) > MAX_PATH)
447 szBuffer[MAX_PATH] = '\0';
449 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
455 return szLocalBufferA;
458 int VDir::SetCurrentDirectoryA(char *lpBuffer)
461 int length, nRet = -1;
463 pPtr = MapPathA(lpBuffer);
464 length = strlen(pPtr);
465 if(length > 3 && IsPathSep(pPtr[length-1])) {
466 /* don't remove the trailing slash from 'x:\' */
467 pPtr[length-1] = '\0';
470 DWORD r = GetFileAttributesA(pPtr);
471 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
473 char szBuffer[(MAX_PATH+1)*2];
474 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
475 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
482 DWORD VDir::CalculateEnvironmentSpace(void)
483 { /* the current directory environment strings are stored as '=D:=d:\path' */
486 for (index = 0; index < driveCount; ++index) {
487 if (dirTableA[index] != NULL) {
488 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
494 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
495 { /* store the current directory environment strings as '=D:=d:\path' */
498 for (index = 0; index < driveCount; ++index) {
499 lpDirStr = dirTableA[index];
500 if (lpDirStr != NULL) {
502 lpStr[1] = lpDirStr[0];
504 CharUpper(&lpStr[1]);
507 strcpy(&lpStr[4], lpDirStr);
508 length = strlen(lpDirStr);
509 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
510 if (length > 3 && IsPathSep(lpStr[-2])) {
511 lpStr[-2] = '\0'; /* remove the trailing path separator */
519 inline BOOL IsPathSep(WCHAR ch)
521 return (ch == '\\' || ch == '/');
524 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
529 * On WinNT GetFullPathName does not fail, (or at least always
530 * succeeds when the drive is valid) WinNT does set *Dest to NULL
531 * On Win98 GetFullPathName will set last error if it fails, but
532 * does not touch *Dest
535 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
538 inline bool IsSpecialFileName(const WCHAR* pName)
540 /* specical file names are devices that the system can open
541 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
542 * (x is a single digit, and names are case-insensitive)
544 WCHAR ch = (pName[0] & ~0x20);
548 if (((pName[1] & ~0x20) == 'U')
549 && ((pName[2] & ~0x20) == 'X')
553 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
554 ch = (pName[1] & ~0x20);
557 case 'L': /* CLOCK$ */
558 if (((pName[2] & ~0x20) == 'O')
559 && ((pName[3] & ~0x20) == 'C')
560 && ((pName[4] & ~0x20) == 'K')
565 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
566 if ((pName[2] & ~0x20) == 'M') {
567 if ((pName[3] >= '1') && (pName[3] <= '9')
571 else if ((pName[2] & ~0x20) == 'N') {
574 else if ((pName[3] & ~0x20) == 'I') {
575 if (((pName[4] & ~0x20) == 'N')
580 else if ((pName[3] & ~0x20) == 'O') {
581 if (((pName[4] & ~0x20) == 'U')
582 && ((pName[5] & ~0x20) == 'T')
592 if (((pName[1] & ~0x20) == 'U')
593 && ((pName[2] & ~0x20) == 'X')
594 && (pName[3] >= '1') && (pName[3] <= '9')
599 if (((pName[1] & ~0x20) == 'U')
600 && ((pName[2] & ~0x20) == 'L')
605 if (((pName[1] & ~0x20) == 'R')
606 && ((pName[2] & ~0x20) == 'N')
614 WCHAR* VDir::MapPathW(const WCHAR *pInName)
616 * possiblities -- relative path or absolute path with or without drive letter
620 WCHAR szBuffer[(MAX_PATH+1)*2];
621 WCHAR szlBuf[MAX_PATH+1];
622 int length = wcslen(pInName);
625 return (WCHAR*)pInName;
627 if (length > MAX_PATH) {
628 wcsncpy(szlBuf, pInName, MAX_PATH);
629 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
630 /* absolute path - reduce length by 2 for drive specifier */
631 szlBuf[MAX_PATH-2] = '\0';
634 szlBuf[MAX_PATH] = '\0';
637 /* strlen(pInName) is now <= MAX_PATH */
639 if (length > 1 && pInName[1] == ':') {
640 /* has drive letter */
641 if (IsPathSep(pInName[2])) {
642 /* absolute with drive letter */
643 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
646 /* relative path with drive letter */
647 driveIndex = DriveIndex(*pInName);
648 if (driveIndex < 0 || driveIndex >= driveLetterCount)
649 return (WCHAR *)pInName;
650 wcscpy(szBuffer, GetDirW(driveIndex));
651 wcscat(szBuffer, &pInName[2]);
652 if(wcslen(szBuffer) > MAX_PATH)
653 szBuffer[MAX_PATH] = '\0';
655 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
659 /* no drive letter */
660 if (length > 1 && IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
662 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
665 wcscpy(szBuffer, GetDefaultDirW());
666 if (IsPathSep(pInName[0])) {
668 wcscpy(&szBuffer[2], pInName);
669 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
673 if (IsSpecialFileName(pInName)) {
674 return (WCHAR*)pInName;
677 wcscat(szBuffer, pInName);
678 if (wcslen(szBuffer) > MAX_PATH)
679 szBuffer[MAX_PATH] = '\0';
681 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
686 return szLocalBufferW;
689 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
692 int length, nRet = -1;
694 pPtr = MapPathW(lpBuffer);
695 length = wcslen(pPtr);
696 if(length > 3 && IsPathSep(pPtr[length-1])) {
697 /* don't remove the trailing slash from 'x:\' */
698 pPtr[length-1] = '\0';
701 DWORD r = GetFileAttributesW(pPtr);
702 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
704 WCHAR wBuffer[(MAX_PATH+1)*2];
705 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
706 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
713 #endif /* ___VDir_H___ */