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;
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 const char *GetDirA(int index)
30 return dirTableA[index];
32 inline const WCHAR *GetDirW(int index)
34 return dirTableW[index];
36 inline int GetDefault(void) { return nDefault; };
38 inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
40 char* ptr = dirTableA[nDefault];
43 if ((*lpBuffer++ = *ptr++) == '\0')
48 inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
50 WCHAR* ptr = dirTableW[nDefault];
53 if ((*lpBuffer++ = *ptr++) == '\0')
60 DWORD CalculateEnvironmentSpace(void);
61 LPSTR BuildEnvironmentSpace(LPSTR lpStr);
64 int SetDirA(char const *pPath, int index);
65 void FromEnvA(char *pEnv, int index);
66 inline const char *GetDefaultDirA(void)
68 return dirTableA[nDefault];
71 inline void SetDefaultDirA(char const *pPath, int index)
73 SetDirA(pPath, index);
76 int SetDirW(WCHAR const *pPath, int index);
77 inline const WCHAR *GetDefaultDirW(void)
79 return dirTableW[nDefault];
82 inline void SetDefaultDirW(WCHAR const *pPath, int index)
84 SetDirW(pPath, index);
88 inline int DriveIndex(char chr)
90 return (chr | 0x20)-'a';
95 char *dirTableA[driveCount];
96 char szLocalBufferA[MAX_PATH+1];
97 WCHAR *dirTableW[driveCount];
98 WCHAR szLocalBufferW[MAX_PATH+1];
105 memset(dirTableA, 0, sizeof(dirTableA));
106 memset(dirTableW, 0, sizeof(dirTableW));
109 void VDir::Init(VDir* pDir, VMem *p)
113 char szBuffer[MAX_PATH*driveCount];
117 for (index = 0; index < driveCount; ++index) {
118 SetDirW(pDir->GetDirW(index), index);
120 nDefault = pDir->GetDefault();
123 driveBits = GetLogicalDrives();
124 if (GetLogicalDriveStrings(sizeof(szBuffer), szBuffer)) {
125 char* pEnv = GetEnvironmentStrings();
126 char* ptr = szBuffer;
127 for (index = 0; index < driveCount; ++index) {
128 if (driveBits & (1<<index)) {
129 ptr += SetDirA(ptr, index) + 1;
130 FromEnvA(pEnv, index);
133 FreeEnvironmentStrings(pEnv);
139 int VDir::SetDirA(char const *pPath, int index)
143 WCHAR wBuffer[MAX_PATH+1];
144 if (index < driveCount && pPath != NULL) {
145 length = strlen(pPath);
146 pMem->Free(dirTableA[index]);
147 ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
152 if (chr != '\\' && chr != '/') {
156 MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
157 wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
158 length = wcslen(wBuffer);
159 pMem->Free(dirTableW[index]);
160 dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
161 if (dirTableW[index] != NULL) {
162 wcscpy(dirTableW[index], wBuffer);
169 void VDir::FromEnvA(char *pEnv, int index)
170 { /* gets the directory for index from the environment variable. */
171 while (*pEnv != '\0') {
172 if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
173 SetDirA(&pEnv[4], index);
177 pEnv += strlen(pEnv)+1;
181 void VDir::SetDefaultA(char const *pDefault)
183 char szBuffer[MAX_PATH+1];
186 if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
187 if (*pDefault != '.' && pPtr != NULL)
190 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
194 int VDir::SetDirW(WCHAR const *pPath, int index)
197 char szBuffer[MAX_PATH+1];
199 if (index < driveCount && pPath != NULL) {
200 length = wcslen(pPath);
201 pMem->Free(dirTableW[index]);
202 ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
207 if (chr != '\\' && chr != '/') {
211 WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
212 length = strlen(szBuffer);
213 pMem->Free(dirTableA[index]);
214 dirTableA[index] = (char*)pMem->Malloc(length+1);
215 if (dirTableA[index] != NULL) {
216 strcpy(dirTableA[index], szBuffer);
223 void VDir::SetDefaultW(WCHAR const *pDefault)
225 WCHAR szBuffer[MAX_PATH+1];
228 if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
229 if (*pDefault != '.' && pPtr != NULL)
232 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
236 inline BOOL IsPathSep(char ch)
238 return (ch == '\\' || ch == '/');
241 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
246 * On WinNT GetFullPathName does not fail, (or at least always
247 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
248 * On Win98 GetFullPathName will set last error if it fails, but
249 * does not touch *Dest
252 GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
255 char *VDir::MapPathA(const char *pInName)
257 * possiblities -- relative path or absolute path with or without drive letter
260 char szBuffer[(MAX_PATH+1)*2];
261 char szlBuf[MAX_PATH+1];
263 if (strlen(pInName) > MAX_PATH) {
264 strncpy(szlBuf, pInName, MAX_PATH);
265 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
266 /* absolute path - reduce length by 2 for drive specifier */
267 szlBuf[MAX_PATH-2] = '\0';
270 szlBuf[MAX_PATH] = '\0';
273 /* strlen(pInName) is now <= MAX_PATH */
275 if (pInName[1] == ':') {
276 /* has drive letter */
277 if (IsPathSep(pInName[2])) {
278 /* absolute with drive letter */
279 strcpy(szLocalBufferA, pInName);
282 /* relative path with drive letter */
283 strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
284 strcat(szBuffer, &pInName[2]);
285 if(strlen(szBuffer) > MAX_PATH)
286 szBuffer[MAX_PATH] = '\0';
288 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
292 /* no drive letter */
293 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
295 strcpy(szLocalBufferA, pInName);
298 strcpy(szBuffer, GetDefaultDirA());
299 if (IsPathSep(pInName[0])) {
301 szLocalBufferA[0] = szBuffer[0];
302 szLocalBufferA[1] = szBuffer[1];
303 strcpy(&szLocalBufferA[2], pInName);
307 strcat(szBuffer, pInName);
308 if (strlen(szBuffer) > MAX_PATH)
309 szBuffer[MAX_PATH] = '\0';
311 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
316 return szLocalBufferA;
319 int VDir::SetCurrentDirectoryA(char *lpBuffer)
322 WIN32_FIND_DATA win32FD;
323 char szBuffer[MAX_PATH+1], *pPtr;
324 int length, nRet = -1;
326 GetFullPathNameA(MapPathA(lpBuffer), sizeof(szBuffer), szBuffer, &pPtr);
327 /* if the last char is a '\\' or a '/' then add
328 * an '*' before calling FindFirstFile
330 length = strlen(szBuffer);
331 if(length > 0 && IsPathSep(szBuffer[length-1])) {
332 szBuffer[length] = '*';
333 szBuffer[length+1] = '\0';
336 hHandle = FindFirstFileA(szBuffer, &win32FD);
337 if (hHandle != INVALID_HANDLE_VALUE) {
340 /* if an '*' was added remove it */
341 if(szBuffer[length] == '*')
342 szBuffer[length] = '\0';
344 SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
350 DWORD VDir::CalculateEnvironmentSpace(void)
351 { /* the current directory environment strings are stored as '=d=d:\path' */
354 for (index = 0; index < driveCount; ++index) {
355 if (dirTableA[index] != NULL) {
356 dwSize += strlen(dirTableA[index]) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
362 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
363 { /* store the current directory environment strings as '=d=d:\path' */
366 for (index = 0; index < driveCount; ++index) {
367 lpDirStr = dirTableA[index];
368 if (lpDirStr != NULL) {
370 lpStr[1] = lpDirStr[0];
372 strcpy(&lpStr[3], lpDirStr);
373 lpStr += strlen(lpDirStr) + 4; /* add 1 for trailing NULL and 3 for '=d=' */
379 inline BOOL IsPathSep(WCHAR ch)
381 return (ch == '\\' || ch == '/');
384 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
389 * On WinNT GetFullPathName does not fail, (or at least always
390 * succeeds when the drive is valid) WinNT does set *Dest to Nullch
391 * On Win98 GetFullPathName will set last error if it fails, but
392 * does not touch *Dest
395 GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
398 WCHAR* VDir::MapPathW(const WCHAR *pInName)
400 * possiblities -- relative path or absolute path with or without drive letter
403 WCHAR szBuffer[(MAX_PATH+1)*2];
404 WCHAR szlBuf[MAX_PATH+1];
406 if (wcslen(pInName) > MAX_PATH) {
407 wcsncpy(szlBuf, pInName, MAX_PATH);
408 if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {
409 /* absolute path - reduce length by 2 for drive specifier */
410 szlBuf[MAX_PATH-2] = '\0';
413 szlBuf[MAX_PATH] = '\0';
416 /* strlen(pInName) is now <= MAX_PATH */
418 if (pInName[1] == ':') {
419 /* has drive letter */
420 if (IsPathSep(pInName[2])) {
421 /* absolute with drive letter */
422 wcscpy(szLocalBufferW, pInName);
425 /* relative path with drive letter */
426 wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
427 wcscat(szBuffer, &pInName[2]);
428 if(wcslen(szBuffer) > MAX_PATH)
429 szBuffer[MAX_PATH] = '\0';
431 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
435 /* no drive letter */
436 if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
438 wcscpy(szLocalBufferW, pInName);
441 wcscpy(szBuffer, GetDefaultDirW());
442 if (IsPathSep(pInName[0])) {
444 szLocalBufferW[0] = szBuffer[0];
445 szLocalBufferW[1] = szBuffer[1];
446 wcscpy(&szLocalBufferW[2], pInName);
450 wcscat(szBuffer, pInName);
451 if (wcslen(szBuffer) > MAX_PATH)
452 szBuffer[MAX_PATH] = '\0';
454 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
458 return szLocalBufferW;
461 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
464 WIN32_FIND_DATAW win32FD;
465 WCHAR szBuffer[MAX_PATH+1], *pPtr;
466 int length, nRet = -1;
468 GetFullPathNameW(MapPathW(lpBuffer), (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr);
469 /* if the last char is a '\\' or a '/' then add
470 * an '*' before calling FindFirstFile
472 length = wcslen(szBuffer);
473 if(length > 0 && IsPathSep(szBuffer[length-1])) {
474 szBuffer[length] = '*';
475 szBuffer[length+1] = '\0';
478 hHandle = FindFirstFileW(szBuffer, &win32FD);
479 if (hHandle != INVALID_HANDLE_VALUE) {
482 /* if an '*' was added remove it */
483 if(szBuffer[length] == '*')
484 szBuffer[length] = '\0';
486 SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
492 #endif /* ___VDir_H___ */