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.
13 const int driveCount = 30;
18 VDir(int bManageDir = 1);
21 void Init(VDir* pDir, VMem *pMem);
22 void SetDefaultA(char const *pDefault);
23 void SetDefaultW(WCHAR const *pDefault);
24 char* MapPathA(const char *pInName);
25 WCHAR* MapPathW(const WCHAR *pInName);
26 int SetCurrentDirectoryA(char *lpBuffer);
27 int SetCurrentDirectoryW(WCHAR *lpBuffer);
28 inline int GetDefault(void) { return nDefault; };
30 inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
32 char* ptr = dirTableA[nDefault];
35 if ((*lpBuffer++ = *ptr++) == '\0')
40 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
42 WCHAR* ptr = dirTableW[nDefault];
45 if ((*lpBuffer++ = *ptr++) == '\0')
52 DWORD CalculateEnvironmentSpace(void);
53 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
56 int SetDirA(char const *pPath, int index);
57 void FromEnvA(char *pEnv, int index);
58 inline const char *GetDefaultDirA(void)
60 return dirTableA[nDefault];
63 inline void SetDefaultDirA(char const *pPath, int index)
65 SetDirA(pPath, index);
68 int SetDirW(WCHAR const *pPath, int index);
69 inline const WCHAR *GetDefaultDirW(void)
71 return dirTableW[nDefault];
74 inline void SetDefaultDirW(WCHAR const *pPath, int index)
76 SetDirW(pPath, index);
79 inline const char *GetDirA(int index)
81 char *ptr = dirTableA[index];
83 /* simulate the existance of this drive */
92 inline const WCHAR *GetDirW(int index)
94 WCHAR *ptr = dirTableW[index];
96 /* simulate the existance of this drive */
106 inline int DriveIndex(char chr)
108 return (chr | 0x20)-'a';
112 int nDefault, bManageDirectory;
113 char *dirTableA[driveCount];
114 char szLocalBufferA[MAX_PATH+1];
115 WCHAR *dirTableW[driveCount];
116 WCHAR szLocalBufferW[MAX_PATH+1];
120 VDir::VDir(int bManageDir /* = 1 */)
123 bManageDirectory = bManageDir;
124 memset(dirTableA, 0, sizeof(dirTableA));
125 memset(dirTableW, 0, sizeof(dirTableW));
128 void VDir::Init(VDir* pDir, VMem *p)
133 char szBuffer[MAX_PATH*driveCount];
137 for (index = 0; index < driveCount; ++index) {
138 SetDirW(pDir->GetDirW(index), index);
140 nDefault = pDir->GetDefault();
143 nSave = bManageDirectory;
144 bManageDirectory = 0;
145 driveBits = GetLogicalDrives();
146 if (GetLogicalDriveStrings(sizeof(szBuffer), szBuffer)) {
147 char* pEnv = GetEnvironmentStrings();
148 char* ptr = szBuffer;
149 for (index = 0; index < driveCount; ++index) {
150 if (driveBits & (1<<index)) {
151 ptr += SetDirA(ptr, index) + 1;
152 FromEnvA(pEnv, index);
155 FreeEnvironmentStrings(pEnv);
158 bManageDirectory = nSave;
162 int VDir::SetDirA(char const *pPath, int index)
166 WCHAR wBuffer[MAX_PATH+1];
167 if (index < driveCount && pPath != NULL) {
168 length = strlen(pPath);
169 pMem->Free(dirTableA[index]);
170 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
175 if (chr != '\\' && chr != '/') {
179 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
180 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
181 length = wcslen(wBuffer);
182 pMem->Free(dirTableW[index]);
183 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
184 if (dirTableW[index] != NULL) {
185 wcscpy(dirTableW[index], wBuffer);
191 ::SetCurrentDirectoryA(pPath);
196 void VDir::FromEnvA(char *pEnv, int index)
197 { /* gets the directory for index from the environment variable. */
198 while (*pEnv != '\0') {
199 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
200 SetDirA(&pEnv[4], index);
204 pEnv += strlen(pEnv)+1;
208 void VDir::SetDefaultA(char const *pDefault)
210 char szBuffer[MAX_PATH+1];
213 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
214 if (*pDefault != '.' && pPtr != NULL)
217 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
221 int VDir::SetDirW(WCHAR const *pPath, int index)
224 char szBuffer[MAX_PATH+1];
226 if (index < driveCount && pPath != NULL) {
227 length = wcslen(pPath);
228 pMem->Free(dirTableW[index]);
229 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
234 if (chr != '\\' && chr != '/') {
238 WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
239 length = strlen(szBuffer);
240 pMem->Free(dirTableA[index]);
241 dirTableA[index] = (char*)pMem->Malloc(length+1);
242 if (dirTableA[index] != NULL) {
243 strcpy(dirTableA[index], szBuffer);
249 ::SetCurrentDirectoryW(pPath);
254 void VDir::SetDefaultW(WCHAR const *pDefault)
256 WCHAR szBuffer[MAX_PATH+1];
259 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
260 if (*pDefault != '.' && pPtr != NULL)
263 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
267 inline BOOL IsPathSep(char ch)
269 return (ch == '\\' || ch == '/');
272 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
277 * On WinNT GetFullPathName does not fail, (or at least always
278 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
279 * On Win98 GetFullPathName will set last error if it fails, but
280 * does not touch *Dest
283 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
286 inline bool IsSpecialFileName(const char* pName)
288 /* specical file names are devices that the system can open
289 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
290 * (x is a single digit, and names are case-insensitive)
292 char ch = (pName[0] & ~0x20);
296 if (((pName[1] & ~0x20) == 'U')
297 && ((pName[2] & ~0x20) == 'X')
301 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
302 ch = (pName[1] & ~0x20);
305 case 'L': /* CLOCK$ */
306 if (((pName[2] & ~0x20) == 'O')
307 && ((pName[3] & ~0x20) == 'C')
308 && ((pName[4] & ~0x20) == 'K')
313 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
314 if ((pName[2] & ~0x20) == 'M') {
315 if ((pName[3] >= '1') && (pName[3] <= '9')
319 else if ((pName[2] & ~0x20) == 'N') {
322 else if ((pName[3] & ~0x20) == 'I') {
323 if (((pName[4] & ~0x20) == 'N')
328 else if ((pName[3] & ~0x20) == 'O') {
329 if (((pName[4] & ~0x20) == 'U')
330 && ((pName[5] & ~0x20) == 'T')
340 if (((pName[1] & ~0x20) == 'U')
341 && ((pName[2] & ~0x20) == 'X')
342 && (pName[3] >= '1') && (pName[3] <= '9')
347 if (((pName[1] & ~0x20) == 'U')
348 && ((pName[2] & ~0x20) == 'L')
353 if (((pName[1] & ~0x20) == 'R')
354 && ((pName[2] & ~0x20) == 'N')
362 char *VDir::MapPathA(const char *pInName)
364 * possiblities -- relative path or absolute path with or without drive letter
367 char szBuffer[(MAX_PATH+1)*2];
368 char szlBuf[MAX_PATH+1];
370 if (strlen(pInName) > MAX_PATH) {
371 strncpy(szlBuf, pInName, MAX_PATH);
372 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
373 /* absolute path - reduce length by 2 for drive specifier */
374 szlBuf[MAX_PATH-2] = '\0';
377 szlBuf[MAX_PATH] = '\0';
380 /* strlen(pInName) is now <= MAX_PATH */
382 if (pInName[1] == ':') {
383 /* has drive letter */
384 if (IsPathSep(pInName[2])) {
385 /* absolute with drive letter */
386 strcpy(szLocalBufferA, pInName);
389 /* relative path with drive letter */
390 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
391 strcat(szBuffer, &pInName[2]);
392 if(strlen(szBuffer) > MAX_PATH)
393 szBuffer[MAX_PATH] = '\0';
395 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
399 /* no drive letter */
400 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
402 strcpy(szLocalBufferA, pInName);
405 strcpy(szBuffer, GetDefaultDirA());
406 if (IsPathSep(pInName[0])) {
408 szLocalBufferA[0] = szBuffer[0];
409 szLocalBufferA[1] = szBuffer[1];
410 strcpy(&szLocalBufferA[2], pInName);
414 if (IsSpecialFileName(pInName)) {
415 return (char*)pInName;
418 strcat(szBuffer, pInName);
419 if (strlen(szBuffer) > MAX_PATH)
420 szBuffer[MAX_PATH] = '\0';
422 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
428 return szLocalBufferA;
431 int VDir::SetCurrentDirectoryA(char *lpBuffer)
434 WIN32_FIND_DATA win32FD;
435 char szBuffer[MAX_PATH+1], *pPtr;
436 int length, nRet = -1;
438 GetFullPathNameA(MapPathA(lpBuffer), sizeof(szBuffer), szBuffer, &pPtr);
439 /* if the last char is a '\\' or a '/' then add
440 * an '*' before calling FindFirstFile
442 length = strlen(szBuffer);
443 if(length > 0 && IsPathSep(szBuffer[length-1])) {
444 szBuffer[length] = '*';
445 szBuffer[length+1] = '\0';
448 hHandle = FindFirstFileA(szBuffer, &win32FD);
449 if (hHandle != INVALID_HANDLE_VALUE) {
452 /* if an '*' was added remove it */
453 if(szBuffer[length] == '*')
454 szBuffer[length] = '\0';
456 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
462 DWORD VDir::CalculateEnvironmentSpace(void)
463 { /* the current directory environment strings are stored as '=d=d:\path' */
466 for (index = 0; index < driveCount; ++index) {
467 if (dirTableA[index] != NULL) {
468 dwSize += strlen(dirTableA[index]) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
474 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
475 { /* store the current directory environment strings as '=d=d:\path' */
478 for (index = 0; index < driveCount; ++index) {
479 lpDirStr = dirTableA[index];
480 if (lpDirStr != NULL) {
482 lpStr[1] = lpDirStr[0];
484 strcpy(&lpStr[3], lpDirStr);
485 lpStr += strlen(lpDirStr) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
491 inline BOOL IsPathSep(WCHAR ch)
493 return (ch == '\\' || ch == '/');
496 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
501 * On WinNT GetFullPathName does not fail, (or at least always
502 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
503 * On Win98 GetFullPathName will set last error if it fails, but
504 * does not touch *Dest
507 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
510 inline bool IsSpecialFileName(const WCHAR* pName)
512 /* specical file names are devices that the system can open
513 * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
514 * (x is a single digit, and names are case-insensitive)
516 WCHAR ch = (pName[0] & ~0x20);
520 if (((pName[1] & ~0x20) == 'U')
521 && ((pName[2] & ~0x20) == 'X')
525 case 'C': /* CLOCK$, COMx, CON, CONIN$ CONOUT$ */
526 ch = (pName[1] & ~0x20);
529 case 'L': /* CLOCK$ */
530 if (((pName[2] & ~0x20) == 'O')
531 && ((pName[3] & ~0x20) == 'C')
532 && ((pName[4] & ~0x20) == 'K')
537 case 'O': /* COMx, CON, CONIN$ CONOUT$ */
538 if ((pName[2] & ~0x20) == 'M') {
539 if ((pName[3] >= '1') && (pName[3] <= '9')
543 else if ((pName[2] & ~0x20) == 'N') {
546 else if ((pName[3] & ~0x20) == 'I') {
547 if (((pName[4] & ~0x20) == 'N')
552 else if ((pName[3] & ~0x20) == 'O') {
553 if (((pName[4] & ~0x20) == 'U')
554 && ((pName[5] & ~0x20) == 'T')
564 if (((pName[1] & ~0x20) == 'U')
565 && ((pName[2] & ~0x20) == 'X')
566 && (pName[3] >= '1') && (pName[3] <= '9')
571 if (((pName[1] & ~0x20) == 'U')
572 && ((pName[2] & ~0x20) == 'L')
577 if (((pName[1] & ~0x20) == 'R')
578 && ((pName[2] & ~0x20) == 'N')
586 WCHAR* VDir::MapPathW(const WCHAR *pInName)
588 * possiblities -- relative path or absolute path with or without drive letter
591 WCHAR szBuffer[(MAX_PATH+1)*2];
592 WCHAR szlBuf[MAX_PATH+1];
594 if (wcslen(pInName) > MAX_PATH) {
595 wcsncpy(szlBuf, pInName, MAX_PATH);
596 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
597 /* absolute path - reduce length by 2 for drive specifier */
598 szlBuf[MAX_PATH-2] = '\0';
601 szlBuf[MAX_PATH] = '\0';
604 /* strlen(pInName) is now <= MAX_PATH */
606 if (pInName[1] == ':') {
607 /* has drive letter */
608 if (IsPathSep(pInName[2])) {
609 /* absolute with drive letter */
610 wcscpy(szLocalBufferW, pInName);
613 /* relative path with drive letter */
614 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
615 wcscat(szBuffer, &pInName[2]);
616 if(wcslen(szBuffer) > MAX_PATH)
617 szBuffer[MAX_PATH] = '\0';
619 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
623 /* no drive letter */
624 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
626 wcscpy(szLocalBufferW, pInName);
629 wcscpy(szBuffer, GetDefaultDirW());
630 if (IsPathSep(pInName[0])) {
632 szLocalBufferW[0] = szBuffer[0];
633 szLocalBufferW[1] = szBuffer[1];
634 wcscpy(&szLocalBufferW[2], pInName);
638 if (IsSpecialFileName(pInName)) {
639 return (WCHAR*)pInName;
642 wcscat(szBuffer, pInName);
643 if (wcslen(szBuffer) > MAX_PATH)
644 szBuffer[MAX_PATH] = '\0';
646 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
651 return szLocalBufferW;
654 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
657 WIN32_FIND_DATAW win32FD;
658 WCHAR szBuffer[MAX_PATH+1], *pPtr;
659 int length, nRet = -1;
661 GetFullPathNameW(MapPathW(lpBuffer), (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr);
662 /* if the last char is a '\\' or a '/' then add
663 * an '*' before calling FindFirstFile
665 length = wcslen(szBuffer);
666 if(length > 0 && IsPathSep(szBuffer[length-1])) {
667 szBuffer[length] = '*';
668 szBuffer[length+1] = '\0';
671 hHandle = FindFirstFileW(szBuffer, &win32FD);
672 if (hHandle != INVALID_HANDLE_VALUE) {
675 /* if an '*' was added remove it */
676 if(szBuffer[length] == '*')
677 szBuffer[length] = '\0';
679 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
685 #endif /* ___VDir_H___ */