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.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
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 NT 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)
264 char szBuffer[MAX_PATH+1];
266 if (index < driveCount && pPath != NULL) {
267 length = wcslen(pPath);
268 pMem->Free(dirTableW[index]);
269 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
274 if (chr != '\\' && chr != '/') {
278 WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
279 length = strlen(szBuffer);
280 pMem->Free(dirTableA[index]);
281 dirTableA[index] = (char*)pMem->Malloc(length+1);
282 if (dirTableA[index] != NULL) {
283 strcpy(dirTableA[index], szBuffer);
289 ::SetCurrentDirectoryW(pPath);
294 void VDir::SetDefaultW(WCHAR const *pDefault)
296 WCHAR szBuffer[MAX_PATH+1];
299 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
300 if (*pDefault != '.' && pPtr != NULL)
303 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
307 inline BOOL IsPathSep(char ch)
309 return (ch == '\\' || ch == '/');
312 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
317 * On WinNT GetFullPathName does not fail, (or at least always
318 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
319 * On Win98 GetFullPathName will set last error if it fails, but
320 * does not touch *Dest
323 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
326 inline bool IsSpecialFileName(const char* pName)
328 /* specical file names are devices that the system can open
329 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
330 * (x is a single digit, and names are case-insensitive)
332 char ch = (pName[0] & ~0x20);
336 if (((pName[1] & ~0x20) == 'U')
337 && ((pName[2] & ~0x20) == 'X')
341 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
342 ch = (pName[1] & ~0x20);
345 case 'L': /* CLOCK$ */
346 if (((pName[2] & ~0x20) == 'O')
347 && ((pName[3] & ~0x20) == 'C')
348 && ((pName[4] & ~0x20) == 'K')
353 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
354 if ((pName[2] & ~0x20) == 'M') {
355 if ((pName[3] >= '1') && (pName[3] <= '9')
359 else if ((pName[2] & ~0x20) == 'N') {
362 else if ((pName[3] & ~0x20) == 'I') {
363 if (((pName[4] & ~0x20) == 'N')
368 else if ((pName[3] & ~0x20) == 'O') {
369 if (((pName[4] & ~0x20) == 'U')
370 && ((pName[5] & ~0x20) == 'T')
380 if (((pName[1] & ~0x20) == 'U')
381 && ((pName[2] & ~0x20) == 'X')
382 && (pName[3] >= '1') && (pName[3] <= '9')
387 if (((pName[1] & ~0x20) == 'U')
388 && ((pName[2] & ~0x20) == 'L')
393 if (((pName[1] & ~0x20) == 'R')
394 && ((pName[2] & ~0x20) == 'N')
402 char *VDir::MapPathA(const char *pInName)
404 * possiblities -- relative path or absolute path with or without drive letter
407 char szBuffer[(MAX_PATH+1)*2];
408 char szlBuf[MAX_PATH+1];
409 int length = strlen(pInName);
412 return (char*)pInName;
414 if (length > MAX_PATH) {
415 strncpy(szlBuf, pInName, MAX_PATH);
416 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
417 /* absolute path - reduce length by 2 for drive specifier */
418 szlBuf[MAX_PATH-2] = '\0';
421 szlBuf[MAX_PATH] = '\0';
424 /* strlen(pInName) is now <= MAX_PATH */
426 if (pInName[1] == ':') {
427 /* has drive letter */
428 if (IsPathSep(pInName[2])) {
429 /* absolute with drive letter */
430 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
433 /* relative path with drive letter */
434 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
435 strcat(szBuffer, &pInName[2]);
436 if(strlen(szBuffer) > MAX_PATH)
437 szBuffer[MAX_PATH] = '\0';
439 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
443 /* no drive letter */
444 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
446 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
449 strcpy(szBuffer, GetDefaultDirA());
450 if (IsPathSep(pInName[0])) {
452 strcpy(&szBuffer[2], pInName);
453 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
457 if (IsSpecialFileName(pInName)) {
458 return (char*)pInName;
461 strcat(szBuffer, pInName);
462 if (strlen(szBuffer) > MAX_PATH)
463 szBuffer[MAX_PATH] = '\0';
465 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
471 return szLocalBufferA;
474 int VDir::SetCurrentDirectoryA(char *lpBuffer)
477 int length, nRet = -1;
479 pPtr = MapPathA(lpBuffer);
480 length = strlen(pPtr);
481 if(length > 3 && IsPathSep(pPtr[length-1])) {
482 /* don't remove the trailing slash from 'x:\' */
483 pPtr[length-1] = '\0';
486 DWORD r = GetFileAttributesA(pPtr);
487 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
489 char szBuffer[(MAX_PATH+1)*2];
490 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
491 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
498 DWORD VDir::CalculateEnvironmentSpace(void)
499 { /* the current directory environment strings are stored as '=D:=d:\path' */
502 for (index = 0; index < driveCount; ++index) {
503 if (dirTableA[index] != NULL) {
504 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
510 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
511 { /* store the current directory environment strings as '=D:=d:\path' */
514 for (index = 0; index < driveCount; ++index) {
515 lpDirStr = dirTableA[index];
516 if (lpDirStr != NULL) {
518 lpStr[1] = lpDirStr[0];
520 CharUpper(&lpStr[1]);
523 strcpy(&lpStr[4], lpDirStr);
524 length = strlen(lpDirStr);
525 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
526 if (length > 3 && IsPathSep(lpStr[-2])) {
527 lpStr[-2] = '\0'; /* remove the trailing path separator */
535 inline BOOL IsPathSep(WCHAR ch)
537 return (ch == '\\' || ch == '/');
540 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
545 * On WinNT GetFullPathName does not fail, (or at least always
546 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
547 * On Win98 GetFullPathName will set last error if it fails, but
548 * does not touch *Dest
551 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
554 inline bool IsSpecialFileName(const WCHAR* pName)
556 /* specical file names are devices that the system can open
557 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
558 * (x is a single digit, and names are case-insensitive)
560 WCHAR ch = (pName[0] & ~0x20);
564 if (((pName[1] & ~0x20) == 'U')
565 && ((pName[2] & ~0x20) == 'X')
569 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
570 ch = (pName[1] & ~0x20);
573 case 'L': /* CLOCK$ */
574 if (((pName[2] & ~0x20) == 'O')
575 && ((pName[3] & ~0x20) == 'C')
576 && ((pName[4] & ~0x20) == 'K')
581 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
582 if ((pName[2] & ~0x20) == 'M') {
583 if ((pName[3] >= '1') && (pName[3] <= '9')
587 else if ((pName[2] & ~0x20) == 'N') {
590 else if ((pName[3] & ~0x20) == 'I') {
591 if (((pName[4] & ~0x20) == 'N')
596 else if ((pName[3] & ~0x20) == 'O') {
597 if (((pName[4] & ~0x20) == 'U')
598 && ((pName[5] & ~0x20) == 'T')
608 if (((pName[1] & ~0x20) == 'U')
609 && ((pName[2] & ~0x20) == 'X')
610 && (pName[3] >= '1') && (pName[3] <= '9')
615 if (((pName[1] & ~0x20) == 'U')
616 && ((pName[2] & ~0x20) == 'L')
621 if (((pName[1] & ~0x20) == 'R')
622 && ((pName[2] & ~0x20) == 'N')
630 WCHAR* VDir::MapPathW(const WCHAR *pInName)
632 * possiblities -- relative path or absolute path with or without drive letter
635 WCHAR szBuffer[(MAX_PATH+1)*2];
636 WCHAR szlBuf[MAX_PATH+1];
637 int length = wcslen(pInName);
640 return (WCHAR*)pInName;
642 if (length > MAX_PATH) {
643 wcsncpy(szlBuf, pInName, MAX_PATH);
644 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
645 /* absolute path - reduce length by 2 for drive specifier */
646 szlBuf[MAX_PATH-2] = '\0';
649 szlBuf[MAX_PATH] = '\0';
652 /* strlen(pInName) is now <= MAX_PATH */
654 if (pInName[1] == ':') {
655 /* has drive letter */
656 if (IsPathSep(pInName[2])) {
657 /* absolute with drive letter */
658 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
661 /* relative path with drive letter */
662 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
663 wcscat(szBuffer, &pInName[2]);
664 if(wcslen(szBuffer) > MAX_PATH)
665 szBuffer[MAX_PATH] = '\0';
667 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
671 /* no drive letter */
672 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
674 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
677 wcscpy(szBuffer, GetDefaultDirW());
678 if (IsPathSep(pInName[0])) {
680 wcscpy(&szBuffer[2], pInName);
681 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
685 if (IsSpecialFileName(pInName)) {
686 return (WCHAR*)pInName;
689 wcscat(szBuffer, pInName);
690 if (wcslen(szBuffer) > MAX_PATH)
691 szBuffer[MAX_PATH] = '\0';
693 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
698 return szLocalBufferW;
701 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
704 int length, nRet = -1;
706 pPtr = MapPathW(lpBuffer);
707 length = wcslen(pPtr);
708 if(length > 3 && IsPathSep(pPtr[length-1])) {
709 /* don't remove the trailing slash from 'x:\' */
710 pPtr[length-1] = '\0';
713 DWORD r = GetFileAttributesW(pPtr);
714 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
716 WCHAR wBuffer[(MAX_PATH+1)*2];
717 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
718 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
725 #endif /* ___VDir_H___ */