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')
44 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
46 WCHAR* ptr = dirTableW[nDefault];
49 if ((*lpBuffer++ = *ptr++) == '\0')
56 DWORD CalculateEnvironmentSpace(void);
57 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
60 int SetDirA(char const *pPath, int index);
61 void FromEnvA(char *pEnv, int index);
62 inline const char *GetDefaultDirA(void)
64 return dirTableA[nDefault];
67 inline void SetDefaultDirA(char const *pPath, int index)
69 SetDirA(pPath, index);
72 int SetDirW(WCHAR const *pPath, int index);
73 inline const WCHAR *GetDefaultDirW(void)
75 return dirTableW[nDefault];
78 inline void SetDefaultDirW(WCHAR const *pPath, int index)
80 SetDirW(pPath, index);
83 inline const char *GetDirA(int index)
85 char *ptr = dirTableA[index];
87 /* simulate the existance of this drive */
96 inline const WCHAR *GetDirW(int index)
98 WCHAR *ptr = dirTableW[index];
100 /* simulate the existance of this drive */
101 ptr = szLocalBufferW;
102 ptr[0] = 'A' + index;
110 inline int DriveIndex(char chr)
112 if (chr == '\\' || chr == '/')
114 return (chr | 0x20)-'a';
118 int nDefault, bManageDirectory;
119 char *dirTableA[driveCount];
120 char szLocalBufferA[MAX_PATH+1];
121 WCHAR *dirTableW[driveCount];
122 WCHAR szLocalBufferW[MAX_PATH+1];
126 VDir::VDir(int bManageDir /* = 1 */)
129 bManageDirectory = bManageDir;
130 memset(dirTableA, 0, sizeof(dirTableA));
131 memset(dirTableW, 0, sizeof(dirTableW));
134 void VDir::Init(VDir* pDir, VMem *p)
139 char szBuffer[MAX_PATH*driveCount];
143 for (index = 0; index < driveCount; ++index) {
144 SetDirW(pDir->GetDirW(index), index);
146 nDefault = pDir->GetDefault();
149 nSave = bManageDirectory;
150 bManageDirectory = 0;
151 driveBits = GetLogicalDrives();
152 if (GetLogicalDriveStrings(sizeof(szBuffer), szBuffer)) {
153 char* pEnv = GetEnvironmentStrings();
154 char* ptr = szBuffer;
155 for (index = 0; index < driveCount; ++index) {
156 if (driveBits & (1<<index)) {
157 ptr += SetDirA(ptr, index) + 1;
158 FromEnvA(pEnv, index);
161 FreeEnvironmentStrings(pEnv);
164 bManageDirectory = nSave;
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::SetDefaultA(char const *pDefault)
216 char szBuffer[MAX_PATH+1];
219 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
220 if (*pDefault != '.' && pPtr != NULL)
223 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
227 int VDir::SetDirW(WCHAR const *pPath, int index)
230 char szBuffer[MAX_PATH+1];
232 if (index < driveCount && pPath != NULL) {
233 length = wcslen(pPath);
234 pMem->Free(dirTableW[index]);
235 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
240 if (chr != '\\' && chr != '/') {
244 WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
245 length = strlen(szBuffer);
246 pMem->Free(dirTableA[index]);
247 dirTableA[index] = (char*)pMem->Malloc(length+1);
248 if (dirTableA[index] != NULL) {
249 strcpy(dirTableA[index], szBuffer);
255 ::SetCurrentDirectoryW(pPath);
260 void VDir::SetDefaultW(WCHAR const *pDefault)
262 WCHAR szBuffer[MAX_PATH+1];
265 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
266 if (*pDefault != '.' && pPtr != NULL)
269 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
273 inline BOOL IsPathSep(char ch)
275 return (ch == '\\' || ch == '/');
278 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
283 * On WinNT GetFullPathName does not fail, (or at least always
284 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
285 * On Win98 GetFullPathName will set last error if it fails, but
286 * does not touch *Dest
289 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
292 inline bool IsSpecialFileName(const char* pName)
294 /* specical file names are devices that the system can open
295 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
296 * (x is a single digit, and names are case-insensitive)
298 char ch = (pName[0] & ~0x20);
302 if (((pName[1] & ~0x20) == 'U')
303 && ((pName[2] & ~0x20) == 'X')
307 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
308 ch = (pName[1] & ~0x20);
311 case 'L': /* CLOCK$ */
312 if (((pName[2] & ~0x20) == 'O')
313 && ((pName[3] & ~0x20) == 'C')
314 && ((pName[4] & ~0x20) == 'K')
319 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
320 if ((pName[2] & ~0x20) == 'M') {
321 if ((pName[3] >= '1') && (pName[3] <= '9')
325 else if ((pName[2] & ~0x20) == 'N') {
328 else if ((pName[3] & ~0x20) == 'I') {
329 if (((pName[4] & ~0x20) == 'N')
334 else if ((pName[3] & ~0x20) == 'O') {
335 if (((pName[4] & ~0x20) == 'U')
336 && ((pName[5] & ~0x20) == 'T')
346 if (((pName[1] & ~0x20) == 'U')
347 && ((pName[2] & ~0x20) == 'X')
348 && (pName[3] >= '1') && (pName[3] <= '9')
353 if (((pName[1] & ~0x20) == 'U')
354 && ((pName[2] & ~0x20) == 'L')
359 if (((pName[1] & ~0x20) == 'R')
360 && ((pName[2] & ~0x20) == 'N')
368 char *VDir::MapPathA(const char *pInName)
370 * possiblities -- relative path or absolute path with or without drive letter
373 char szBuffer[(MAX_PATH+1)*2];
374 char szlBuf[MAX_PATH+1];
375 int length = strlen(pInName);
378 return (char*)pInName;
380 if (length > MAX_PATH) {
381 strncpy(szlBuf, pInName, MAX_PATH);
382 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
383 /* absolute path - reduce length by 2 for drive specifier */
384 szlBuf[MAX_PATH-2] = '\0';
387 szlBuf[MAX_PATH] = '\0';
390 /* strlen(pInName) is now <= MAX_PATH */
392 if (pInName[1] == ':') {
393 /* has drive letter */
394 if (IsPathSep(pInName[2])) {
395 /* absolute with drive letter */
396 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
399 /* relative path with drive letter */
400 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
401 strcat(szBuffer, &pInName[2]);
402 if(strlen(szBuffer) > MAX_PATH)
403 szBuffer[MAX_PATH] = '\0';
405 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
409 /* no drive letter */
410 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
412 DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
415 strcpy(szBuffer, GetDefaultDirA());
416 if (IsPathSep(pInName[0])) {
418 strcpy(&szBuffer[2], pInName);
419 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
423 if (IsSpecialFileName(pInName)) {
424 return (char*)pInName;
427 strcat(szBuffer, pInName);
428 if (strlen(szBuffer) > MAX_PATH)
429 szBuffer[MAX_PATH] = '\0';
431 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
437 return szLocalBufferA;
440 int VDir::SetCurrentDirectoryA(char *lpBuffer)
443 int length, nRet = -1;
445 pPtr = MapPathA(lpBuffer);
446 length = strlen(pPtr);
447 if(length > 3 && IsPathSep(pPtr[length-1])) {
448 /* don't remove the trailing slash from 'x:\' */
449 pPtr[length-1] = '\0';
452 DWORD r = GetFileAttributesA(pPtr);
453 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
455 char szBuffer[(MAX_PATH+1)*2];
456 DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
457 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
464 DWORD VDir::CalculateEnvironmentSpace(void)
465 { /* the current directory environment strings are stored as '=D:=d:\path' */
468 for (index = 0; index < driveCount; ++index) {
469 if (dirTableA[index] != NULL) {
470 dwSize += strlen(dirTableA[index]) + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
476 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
477 { /* store the current directory environment strings as '=D:=d:\path' */
480 for (index = 0; index < driveCount; ++index) {
481 lpDirStr = dirTableA[index];
482 if (lpDirStr != NULL) {
484 lpStr[1] = lpDirStr[0];
486 CharUpper(&lpStr[1]);
489 strcpy(&lpStr[4], lpDirStr);
490 length = strlen(lpDirStr);
491 lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
492 if (length > 3 && IsPathSep(lpStr[-2])) {
493 lpStr[-2] = '\0'; /* remove the trailing path separator */
501 inline BOOL IsPathSep(WCHAR ch)
503 return (ch == '\\' || ch == '/');
506 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
511 * On WinNT GetFullPathName does not fail, (or at least always
512 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
513 * On Win98 GetFullPathName will set last error if it fails, but
514 * does not touch *Dest
517 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
520 inline bool IsSpecialFileName(const WCHAR* pName)
522 /* specical file names are devices that the system can open
523 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
524 * (x is a single digit, and names are case-insensitive)
526 WCHAR ch = (pName[0] & ~0x20);
530 if (((pName[1] & ~0x20) == 'U')
531 && ((pName[2] & ~0x20) == 'X')
535 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
536 ch = (pName[1] & ~0x20);
539 case 'L': /* CLOCK$ */
540 if (((pName[2] & ~0x20) == 'O')
541 && ((pName[3] & ~0x20) == 'C')
542 && ((pName[4] & ~0x20) == 'K')
547 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
548 if ((pName[2] & ~0x20) == 'M') {
549 if ((pName[3] >= '1') && (pName[3] <= '9')
553 else if ((pName[2] & ~0x20) == 'N') {
556 else if ((pName[3] & ~0x20) == 'I') {
557 if (((pName[4] & ~0x20) == 'N')
562 else if ((pName[3] & ~0x20) == 'O') {
563 if (((pName[4] & ~0x20) == 'U')
564 && ((pName[5] & ~0x20) == 'T')
574 if (((pName[1] & ~0x20) == 'U')
575 && ((pName[2] & ~0x20) == 'X')
576 && (pName[3] >= '1') && (pName[3] <= '9')
581 if (((pName[1] & ~0x20) == 'U')
582 && ((pName[2] & ~0x20) == 'L')
587 if (((pName[1] & ~0x20) == 'R')
588 && ((pName[2] & ~0x20) == 'N')
596 WCHAR* VDir::MapPathW(const WCHAR *pInName)
598 * possiblities -- relative path or absolute path with or without drive letter
601 WCHAR szBuffer[(MAX_PATH+1)*2];
602 WCHAR szlBuf[MAX_PATH+1];
603 int length = wcslen(pInName);
606 return (WCHAR*)pInName;
608 if (length > MAX_PATH) {
609 wcsncpy(szlBuf, pInName, MAX_PATH);
610 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
611 /* absolute path - reduce length by 2 for drive specifier */
612 szlBuf[MAX_PATH-2] = '\0';
615 szlBuf[MAX_PATH] = '\0';
618 /* strlen(pInName) is now <= MAX_PATH */
620 if (pInName[1] == ':') {
621 /* has drive letter */
622 if (IsPathSep(pInName[2])) {
623 /* absolute with drive letter */
624 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
627 /* relative path with drive letter */
628 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
629 wcscat(szBuffer, &pInName[2]);
630 if(wcslen(szBuffer) > MAX_PATH)
631 szBuffer[MAX_PATH] = '\0';
633 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
637 /* no drive letter */
638 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
640 DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
643 wcscpy(szBuffer, GetDefaultDirW());
644 if (IsPathSep(pInName[0])) {
646 wcscpy(&szBuffer[2], pInName);
647 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
651 if (IsSpecialFileName(pInName)) {
652 return (WCHAR*)pInName;
655 wcscat(szBuffer, pInName);
656 if (wcslen(szBuffer) > MAX_PATH)
657 szBuffer[MAX_PATH] = '\0';
659 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
664 return szLocalBufferW;
667 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
670 int length, nRet = -1;
672 pPtr = MapPathW(lpBuffer);
673 length = wcslen(pPtr);
674 if(length > 3 && IsPathSep(pPtr[length-1])) {
675 /* don't remove the trailing slash from 'x:\' */
676 pPtr[length-1] = '\0';
679 DWORD r = GetFileAttributesW(pPtr);
680 if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
682 WCHAR wBuffer[(MAX_PATH+1)*2];
683 DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
684 SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
691 #endif /* ___VDir_H___ */