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();
150 bManageDirectory = 0;
151 WCHAR szBuffer[MAX_PATH*driveCount];
152 if (GetLogicalDriveStringsW(sizeof(szBuffer), szBuffer)) {
153 WCHAR* pEnv = GetEnvironmentStringsW();
154 WCHAR* ptr = szBuffer;
155 for (index = 0; index < driveCount; ++index) {
156 if (driveBits & (1<<index)) {
157 ptr += SetDirW(ptr, index) + 1;
158 FromEnvW(pEnv, index);
161 FreeEnvironmentStringsW(pEnv);
164 bManageDirectory = bSave;
168 int VDir::SetDirA(char const *pPath, int index)
172 WCHAR wBuffer[MAX_PATH+1];
173 if (index < driveCount && pPath != NULL) {
174 length = strlen(pPath);
175 pMem->Free(dirTableA[index]);
176 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
181 if (chr != '\\' && chr != '/') {
185 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
186 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
187 length = wcslen(wBuffer);
188 pMem->Free(dirTableW[index]);
189 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
190 if (dirTableW[index] != NULL) {
191 wcscpy(dirTableW[index], wBuffer);
197 ::SetCurrentDirectoryA(pPath);
202 void VDir::FromEnvA(char *pEnv, int index)
203 { /* gets the directory for index from the environment variable. */
204 while (*pEnv != '\0') {
205 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
206 SetDirA(&pEnv[4], index);
210 pEnv += strlen(pEnv)+1;
214 void VDir::FromEnvW(WCHAR *pEnv, int index)
215 { /* gets the directory for index from the environment variable. */
216 while (*pEnv != '\0') {
217 if ((pEnv[0] == '=') && (DriveIndex((char)pEnv[1]) == index)) {
218 SetDirW(&pEnv[4], index);
222 pEnv += wcslen(pEnv)+1;
226 void VDir::SetDefaultA(char const *pDefault)
228 char szBuffer[MAX_PATH+1];
231 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
232 if (*pDefault != '.' && pPtr != NULL)
235 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
239 int VDir::SetDirW(WCHAR const *pPath, int index)
243 if (index < driveCount && pPath != NULL) {
244 length = wcslen(pPath);
245 pMem->Free(dirTableW[index]);
246 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
252 if (chr != '\\' && chr != '/') {
256 ansi = win32_ansipath(dirTableW[index]);
257 length = strlen(ansi);
258 pMem->Free(dirTableA[index]);
259 dirTableA[index] = (char*)pMem->Malloc(length+1);
260 if (dirTableA[index] != NULL) {
261 strcpy(dirTableA[index], ansi);
268 ::SetCurrentDirectoryW(pPath);
273 void VDir::SetDefaultW(WCHAR const *pDefault)
275 WCHAR szBuffer[MAX_PATH+1];
278 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
279 if (*pDefault != '.' && pPtr != NULL)
282 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
286 inline BOOL IsPathSep(char ch)
288 return (ch == '\\' || ch == '/');
291 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
296 * On WinNT GetFullPathName does not fail, (or at least always
297 * succeeds when the drive is valid) WinNT does set *Dest to NULL
298 * On Win98 GetFullPathName will set last error if it fails, but
299 * does not touch *Dest
302 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
305 inline bool IsSpecialFileName(const char* pName)
307 /* specical file names are devices that the system can open
308 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
309 * (x is a single digit, and names are case-insensitive)
311 char ch = (pName[0] & ~0x20);
315 if (((pName[1] & ~0x20) == 'U')
316 && ((pName[2] & ~0x20) == 'X')
320 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
321 ch = (pName[1] & ~0x20);
324 case 'L': /* CLOCK$ */
325 if (((pName[2] & ~0x20) == 'O')
326 && ((pName[3] & ~0x20) == 'C')
327 && ((pName[4] & ~0x20) == 'K')
332 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
333 if ((pName[2] & ~0x20) == 'M') {
334 if ((pName[3] >= '1') && (pName[3] <= '9')
338 else if ((pName[2] & ~0x20) == 'N') {
341 else if ((pName[3] & ~0x20) == 'I') {
342 if (((pName[4] & ~0x20) == 'N')
347 else if ((pName[3] & ~0x20) == 'O') {
348 if (((pName[4] & ~0x20) == 'U')
349 && ((pName[5] & ~0x20) == 'T')
359 if (((pName[1] & ~0x20) == 'U')
360 && ((pName[2] & ~0x20) == 'X')
361 && (pName[3] >= '1') && (pName[3] <= '9')
366 if (((pName[1] & ~0x20) == 'U')
367 && ((pName[2] & ~0x20) == 'L')
372 if (((pName[1] & ~0x20) == 'R')
373 && ((pName[2] & ~0x20) == 'N')
381 char *VDir::MapPathA(const char *pInName)
383 * possiblities -- relative path or absolute path with or without drive letter
386 char szBuffer[(MAX_PATH+1)*2];
387 char szlBuf[MAX_PATH+1];
388 int length = strlen(pInName);
391 return (char*)pInName;
393 if (length > MAX_PATH) {
394 strncpy(szlBuf, pInName, MAX_PATH);
395 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
396 /* absolute path - reduce length by 2 for drive specifier */
397 szlBuf[MAX_PATH-2] = '\0';
400 szlBuf[MAX_PATH] = '\0';
403 /* strlen(pInName) is now <= MAX_PATH */
405 if (pInName[1] == ':') {
406 /* has drive letter */
407 if (IsPathSep(pInName[2])) {
408 /* absolute with drive letter */
409 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
412 /* relative path with drive letter */
413 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
414 strcat(szBuffer, &pInName[2]);
415 if(strlen(szBuffer) > MAX_PATH)
416 szBuffer[MAX_PATH] = '\0';
418 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
422 /* no drive letter */
423 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
425 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
428 strcpy(szBuffer, GetDefaultDirA());
429 if (IsPathSep(pInName[0])) {
431 strcpy(&szBuffer[2], pInName);
432 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
436 if (IsSpecialFileName(pInName)) {
437 return (char*)pInName;
440 strcat(szBuffer, pInName);
441 if (strlen(szBuffer) > MAX_PATH)
442 szBuffer[MAX_PATH] = '\0';
444 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
450 return szLocalBufferA;
453 int VDir::SetCurrentDirectoryA(char *lpBuffer)
456 int length, nRet = -1;
458 pPtr = MapPathA(lpBuffer);
459 length = strlen(pPtr);
460 if(length > 3 && IsPathSep(pPtr[length-1])) {
461 /* don't remove the trailing slash from 'x:\' */
462 pPtr[length-1] = '\0';
465 DWORD r = GetFileAttributesA(pPtr);
466 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
468 char szBuffer[(MAX_PATH+1)*2];
469 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
470 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
477 DWORD VDir::CalculateEnvironmentSpace(void)
478 { /* the current directory environment strings are stored as '=D:=d:\path' */
481 for (index = 0; index < driveCount; ++index) {
482 if (dirTableA[index] != NULL) {
483 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
489 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
490 { /* store the current directory environment strings as '=D:=d:\path' */
493 for (index = 0; index < driveCount; ++index) {
494 lpDirStr = dirTableA[index];
495 if (lpDirStr != NULL) {
497 lpStr[1] = lpDirStr[0];
499 CharUpper(&lpStr[1]);
502 strcpy(&lpStr[4], lpDirStr);
503 length = strlen(lpDirStr);
504 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
505 if (length > 3 && IsPathSep(lpStr[-2])) {
506 lpStr[-2] = '\0'; /* remove the trailing path separator */
514 inline BOOL IsPathSep(WCHAR ch)
516 return (ch == '\\' || ch == '/');
519 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
524 * On WinNT GetFullPathName does not fail, (or at least always
525 * succeeds when the drive is valid) WinNT does set *Dest to NULL
526 * On Win98 GetFullPathName will set last error if it fails, but
527 * does not touch *Dest
530 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
533 inline bool IsSpecialFileName(const WCHAR* pName)
535 /* specical file names are devices that the system can open
536 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
537 * (x is a single digit, and names are case-insensitive)
539 WCHAR ch = (pName[0] & ~0x20);
543 if (((pName[1] & ~0x20) == 'U')
544 && ((pName[2] & ~0x20) == 'X')
548 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
549 ch = (pName[1] & ~0x20);
552 case 'L': /* CLOCK$ */
553 if (((pName[2] & ~0x20) == 'O')
554 && ((pName[3] & ~0x20) == 'C')
555 && ((pName[4] & ~0x20) == 'K')
560 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
561 if ((pName[2] & ~0x20) == 'M') {
562 if ((pName[3] >= '1') && (pName[3] <= '9')
566 else if ((pName[2] & ~0x20) == 'N') {
569 else if ((pName[3] & ~0x20) == 'I') {
570 if (((pName[4] & ~0x20) == 'N')
575 else if ((pName[3] & ~0x20) == 'O') {
576 if (((pName[4] & ~0x20) == 'U')
577 && ((pName[5] & ~0x20) == 'T')
587 if (((pName[1] & ~0x20) == 'U')
588 && ((pName[2] & ~0x20) == 'X')
589 && (pName[3] >= '1') && (pName[3] <= '9')
594 if (((pName[1] & ~0x20) == 'U')
595 && ((pName[2] & ~0x20) == 'L')
600 if (((pName[1] & ~0x20) == 'R')
601 && ((pName[2] & ~0x20) == 'N')
609 WCHAR* VDir::MapPathW(const WCHAR *pInName)
611 * possiblities -- relative path or absolute path with or without drive letter
614 WCHAR szBuffer[(MAX_PATH+1)*2];
615 WCHAR szlBuf[MAX_PATH+1];
616 int length = wcslen(pInName);
619 return (WCHAR*)pInName;
621 if (length > MAX_PATH) {
622 wcsncpy(szlBuf, pInName, MAX_PATH);
623 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
624 /* absolute path - reduce length by 2 for drive specifier */
625 szlBuf[MAX_PATH-2] = '\0';
628 szlBuf[MAX_PATH] = '\0';
631 /* strlen(pInName) is now <= MAX_PATH */
633 if (pInName[1] == ':') {
634 /* has drive letter */
635 if (IsPathSep(pInName[2])) {
636 /* absolute with drive letter */
637 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
640 /* relative path with drive letter */
641 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
642 wcscat(szBuffer, &pInName[2]);
643 if(wcslen(szBuffer) > MAX_PATH)
644 szBuffer[MAX_PATH] = '\0';
646 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
650 /* no drive letter */
651 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
653 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
656 wcscpy(szBuffer, GetDefaultDirW());
657 if (IsPathSep(pInName[0])) {
659 wcscpy(&szBuffer[2], pInName);
660 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
664 if (IsSpecialFileName(pInName)) {
665 return (WCHAR*)pInName;
668 wcscat(szBuffer, pInName);
669 if (wcslen(szBuffer) > MAX_PATH)
670 szBuffer[MAX_PATH] = '\0';
672 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
677 return szLocalBufferW;
680 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
683 int length, nRet = -1;
685 pPtr = MapPathW(lpBuffer);
686 length = wcslen(pPtr);
687 if(length > 3 && IsPathSep(pPtr[length-1])) {
688 /* don't remove the trailing slash from 'x:\' */
689 pPtr[length-1] = '\0';
692 DWORD r = GetFileAttributesW(pPtr);
693 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
695 WCHAR wBuffer[(MAX_PATH+1)*2];
696 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
697 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
704 #endif /* ___VDir_H___ */