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 && pEnv[2] == ':' && pEnv[3] == '=') {
208 SetDirA(&pEnv[4], index);
212 pEnv += strlen(pEnv)+1;
216 void VDir::FromEnvW(WCHAR *pEnv, int index)
217 { /* gets the directory for index from the environment variable. */
218 while (*pEnv != '\0') {
219 if ((pEnv[0] == '=') && (DriveIndex((char)pEnv[1]) == index)
220 && pEnv[2] == ':' && pEnv[3] == '=') {
221 SetDirW(&pEnv[4], index);
225 pEnv += wcslen(pEnv)+1;
229 void VDir::SetDefaultA(char const *pDefault)
231 char szBuffer[MAX_PATH+1];
234 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
235 if (*pDefault != '.' && pPtr != NULL)
238 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
242 int VDir::SetDirW(WCHAR const *pPath, int index)
246 if (index < driveCount && pPath != NULL) {
247 length = wcslen(pPath);
248 pMem->Free(dirTableW[index]);
249 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
255 if (chr != '\\' && chr != '/') {
259 ansi = win32_ansipath(dirTableW[index]);
260 length = strlen(ansi);
261 pMem->Free(dirTableA[index]);
262 dirTableA[index] = (char*)pMem->Malloc(length+1);
263 if (dirTableA[index] != NULL) {
264 strcpy(dirTableA[index], ansi);
271 ::SetCurrentDirectoryW(pPath);
276 void VDir::SetDefaultW(WCHAR const *pDefault)
278 WCHAR szBuffer[MAX_PATH+1];
281 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
282 if (*pDefault != '.' && pPtr != NULL)
285 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
289 inline BOOL IsPathSep(char ch)
291 return (ch == '\\' || ch == '/');
294 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
299 * On WinNT GetFullPathName does not fail, (or at least always
300 * succeeds when the drive is valid) WinNT does set *Dest to NULL
301 * On Win98 GetFullPathName will set last error if it fails, but
302 * does not touch *Dest
305 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
308 inline bool IsSpecialFileName(const char* pName)
310 /* specical file names are devices that the system can open
311 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
312 * (x is a single digit, and names are case-insensitive)
314 char ch = (pName[0] & ~0x20);
318 if (((pName[1] & ~0x20) == 'U')
319 && ((pName[2] & ~0x20) == 'X')
323 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
324 ch = (pName[1] & ~0x20);
327 case 'L': /* CLOCK$ */
328 if (((pName[2] & ~0x20) == 'O')
329 && ((pName[3] & ~0x20) == 'C')
330 && ((pName[4] & ~0x20) == 'K')
335 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
336 if ((pName[2] & ~0x20) == 'M') {
337 if ((pName[3] >= '1') && (pName[3] <= '9')
341 else if ((pName[2] & ~0x20) == 'N') {
344 else if ((pName[3] & ~0x20) == 'I') {
345 if (((pName[4] & ~0x20) == 'N')
350 else if ((pName[3] & ~0x20) == 'O') {
351 if (((pName[4] & ~0x20) == 'U')
352 && ((pName[5] & ~0x20) == 'T')
362 if (((pName[1] & ~0x20) == 'U')
363 && ((pName[2] & ~0x20) == 'X')
364 && (pName[3] >= '1') && (pName[3] <= '9')
369 if (((pName[1] & ~0x20) == 'U')
370 && ((pName[2] & ~0x20) == 'L')
375 if (((pName[1] & ~0x20) == 'R')
376 && ((pName[2] & ~0x20) == 'N')
384 char *VDir::MapPathA(const char *pInName)
386 * possiblities -- relative path or absolute path with or without drive letter
390 char szBuffer[(MAX_PATH+1)*2];
391 char szlBuf[MAX_PATH+1];
392 int length = strlen(pInName);
395 return (char*)pInName;
397 if (length > MAX_PATH) {
398 strncpy(szlBuf, pInName, MAX_PATH);
399 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
400 /* absolute path - reduce length by 2 for drive specifier */
401 szlBuf[MAX_PATH-2] = '\0';
404 szlBuf[MAX_PATH] = '\0';
407 /* strlen(pInName) is now <= MAX_PATH */
409 if (length > 1 && pInName[1] == ':') {
410 /* has drive letter */
411 if (length > 2 && IsPathSep(pInName[2])) {
412 /* absolute with drive letter */
413 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
416 /* relative path with drive letter */
417 driveIndex = DriveIndex(*pInName);
418 if (driveIndex < 0 || driveIndex >= driveLetterCount)
419 return (char *)pInName;
420 strcpy(szBuffer, GetDirA(driveIndex));
421 strcat(szBuffer, &pInName[2]);
422 if(strlen(szBuffer) > MAX_PATH)
423 szBuffer[MAX_PATH] = '\0';
425 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
429 /* no drive letter */
430 if (length > 1 && IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
432 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
435 strcpy(szBuffer, GetDefaultDirA());
436 if (IsPathSep(pInName[0])) {
438 strcpy(&szBuffer[2], pInName);
439 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
443 if (IsSpecialFileName(pInName)) {
444 return (char*)pInName;
447 strcat(szBuffer, pInName);
448 if (strlen(szBuffer) > MAX_PATH)
449 szBuffer[MAX_PATH] = '\0';
451 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
457 return szLocalBufferA;
460 int VDir::SetCurrentDirectoryA(char *lpBuffer)
463 int length, nRet = -1;
465 pPtr = MapPathA(lpBuffer);
466 length = strlen(pPtr);
467 if(length > 3 && IsPathSep(pPtr[length-1])) {
468 /* don't remove the trailing slash from 'x:\' */
469 pPtr[length-1] = '\0';
472 DWORD r = GetFileAttributesA(pPtr);
473 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
475 char szBuffer[(MAX_PATH+1)*2];
476 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
477 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
484 DWORD VDir::CalculateEnvironmentSpace(void)
485 { /* the current directory environment strings are stored as '=D:=d:\path' */
488 for (index = 0; index < driveCount; ++index) {
489 if (dirTableA[index] != NULL) {
490 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
496 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
497 { /* store the current directory environment strings as '=D:=d:\path' */
500 for (index = 0; index < driveCount; ++index) {
501 lpDirStr = dirTableA[index];
502 if (lpDirStr != NULL) {
504 lpStr[1] = lpDirStr[0];
506 CharUpper(&lpStr[1]);
509 strcpy(&lpStr[4], lpDirStr);
510 length = strlen(lpDirStr);
511 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
512 if (length > 3 && IsPathSep(lpStr[-2])) {
513 lpStr[-2] = '\0'; /* remove the trailing path separator */
521 inline BOOL IsPathSep(WCHAR ch)
523 return (ch == '\\' || ch == '/');
526 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
531 * On WinNT GetFullPathName does not fail, (or at least always
532 * succeeds when the drive is valid) WinNT does set *Dest to NULL
533 * On Win98 GetFullPathName will set last error if it fails, but
534 * does not touch *Dest
537 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
540 inline bool IsSpecialFileName(const WCHAR* pName)
542 /* specical file names are devices that the system can open
543 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
544 * (x is a single digit, and names are case-insensitive)
546 WCHAR ch = (pName[0] & ~0x20);
550 if (((pName[1] & ~0x20) == 'U')
551 && ((pName[2] & ~0x20) == 'X')
555 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
556 ch = (pName[1] & ~0x20);
559 case 'L': /* CLOCK$ */
560 if (((pName[2] & ~0x20) == 'O')
561 && ((pName[3] & ~0x20) == 'C')
562 && ((pName[4] & ~0x20) == 'K')
567 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
568 if ((pName[2] & ~0x20) == 'M') {
569 if ((pName[3] >= '1') && (pName[3] <= '9')
573 else if ((pName[2] & ~0x20) == 'N') {
576 else if ((pName[3] & ~0x20) == 'I') {
577 if (((pName[4] & ~0x20) == 'N')
582 else if ((pName[3] & ~0x20) == 'O') {
583 if (((pName[4] & ~0x20) == 'U')
584 && ((pName[5] & ~0x20) == 'T')
594 if (((pName[1] & ~0x20) == 'U')
595 && ((pName[2] & ~0x20) == 'X')
596 && (pName[3] >= '1') && (pName[3] <= '9')
601 if (((pName[1] & ~0x20) == 'U')
602 && ((pName[2] & ~0x20) == 'L')
607 if (((pName[1] & ~0x20) == 'R')
608 && ((pName[2] & ~0x20) == 'N')
616 WCHAR* VDir::MapPathW(const WCHAR *pInName)
618 * possiblities -- relative path or absolute path with or without drive letter
622 WCHAR szBuffer[(MAX_PATH+1)*2];
623 WCHAR szlBuf[MAX_PATH+1];
624 int length = wcslen(pInName);
627 return (WCHAR*)pInName;
629 if (length > MAX_PATH) {
630 wcsncpy(szlBuf, pInName, MAX_PATH);
631 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
632 /* absolute path - reduce length by 2 for drive specifier */
633 szlBuf[MAX_PATH-2] = '\0';
636 szlBuf[MAX_PATH] = '\0';
639 /* strlen(pInName) is now <= MAX_PATH */
641 if (length > 1 && pInName[1] == ':') {
642 /* has drive letter */
643 if (IsPathSep(pInName[2])) {
644 /* absolute with drive letter */
645 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
648 /* relative path with drive letter */
649 driveIndex = DriveIndex(*pInName);
650 if (driveIndex < 0 || driveIndex >= driveLetterCount)
651 return (WCHAR *)pInName;
652 wcscpy(szBuffer, GetDirW(driveIndex));
653 wcscat(szBuffer, &pInName[2]);
654 if(wcslen(szBuffer) > MAX_PATH)
655 szBuffer[MAX_PATH] = '\0';
657 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
661 /* no drive letter */
662 if (length > 1 && IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
664 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
667 wcscpy(szBuffer, GetDefaultDirW());
668 if (IsPathSep(pInName[0])) {
670 wcscpy(&szBuffer[2], pInName);
671 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
675 if (IsSpecialFileName(pInName)) {
676 return (WCHAR*)pInName;
679 wcscat(szBuffer, pInName);
680 if (wcslen(szBuffer) > MAX_PATH)
681 szBuffer[MAX_PATH] = '\0';
683 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
688 return szLocalBufferW;
691 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
694 int length, nRet = -1;
696 pPtr = MapPathW(lpBuffer);
697 length = wcslen(pPtr);
698 if(length > 3 && IsPathSep(pPtr[length-1])) {
699 /* don't remove the trailing slash from 'x:\' */
700 pPtr[length-1] = '\0';
703 DWORD r = GetFileAttributesW(pPtr);
704 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
706 WCHAR wBuffer[(MAX_PATH+1)*2];
707 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
708 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
715 #endif /* ___VDir_H___ */