#include <pathnmex.h>
Inheritance diagram for PathNameEx:
Public Member Functions | |
PathNameEx () | |
BOOL | CreateLocation () |
Creates the full directory path (as returned by GetLocation()) on the physical medium. This is useful when creating new directory structures. | |
PathNameEx (const PathName &rPath) | |
PathNameEx (const String_256 &strPath) | |
const String_256 & | GetDrive () |
BOOL | Remove () |
Static Protected Member Functions | |
static BOOL | RemoveRecursively (const String_256 &rPath) |
Removes a file or a whole directory tree from the the physical medium (in which case, the object should be pointing to the root directory of the tree you want deleted). |
Definition at line 117 of file pathnmex.h.
|
|
|
Definition at line 122 of file pathnmex.h. 00122 : PathName(rPath) {}
|
|
Definition at line 123 of file pathnmex.h. 00123 : PathName(strPath) {}
|
|
Creates the full directory path (as returned by GetLocation()) on the physical medium. This is useful when creating new directory structures. > BOOL PathNameEx::CreateLocation()
Definition at line 135 of file pathnmex.cpp. 00136 { 00137 PORTNOTETRACE("other","PathNameEx::CreateLocation - do nothing"); 00138 #ifndef EXCLUDE_FROM_XARALX 00139 if (!IsValid()) 00140 return FALSE; 00141 00142 // We'll walk the location string from left to right - if we come across non-existent directories, 00143 // we create them 00144 String_256 strLocation = GetLocation(FALSE); 00145 String_256 strDirPath = drivename; 00146 INT32 nPos = drivename.Length(); // start after the drivename 00147 while( nPos < strLocation.Length() ) 00148 { 00149 while( ( strLocation[nPos] != chPathSep ) && ( nPos < strLocation.Length() ) ) 00150 { 00151 strDirPath += strLocation[nPos]; 00152 nPos++; 00153 } 00154 // strDirPath has been added a directory, we check if it exists 00155 00156 if (_access((TCHAR*) strDirPath, 0) == -1) // not found, try to create the directory 00157 { 00158 if (_mkdir((TCHAR*) strDirPath)) 00159 { 00160 #ifdef _DEBUG 00161 TCHAR szMsg[256]; 00162 TCHAR szError[128]; 00163 switch (errno) 00164 { 00165 case EACCES: 00166 camStrcpy(szError, "access denied (EACCES)"); 00167 break; 00168 case ENOENT: 00169 camStrcpy(szError, "path not found (ENOENT)"); 00170 break; 00171 default: 00172 wsprintf(szError, "errno = %d", errno); 00173 } 00174 wsprintf(szMsg, "Create directory %s failed, %s", strDirPath, szError); 00175 ERROR3(szMsg); 00176 #endif 00177 return FALSE; 00178 } 00179 } 00180 strDirPath += chPathSep; // add a backslash in case there are further subdirectories 00181 nPos++; // move to the next position 00182 } 00183 #endif 00184 return TRUE; 00185 }
|
|
Definition at line 124 of file pathnmex.h. 00124 { return drivename;}
|
|
Definition at line 125 of file pathnmex.h. 00125 { return RemoveRecursively(GetPath());}
|
|
Removes a file or a whole directory tree from the the physical medium (in which case, the object should be pointing to the root directory of the tree you want deleted). > BOOL PathNameEx::RemoveRecursively()
Definition at line 208 of file pathnmex.cpp. 00209 { 00210 PORTNOTETRACE("other","PathNameEx::RemoveRecursively - do nothing"); 00211 #ifndef EXCLUDE_FROM_XARALX 00212 String_256 strFilename(rPath); 00213 strFilename.toLower(); 00214 // See if the path points to a file (the easy case) or a directory 00215 if (strFilename[strFilename.Length() - 1] == chPathSep) 00216 { 00217 strFilename.Remove(strFilename.Length() - 1, 1); 00218 goto DIRECTORY; 00219 } 00220 struct _stat fileData; 00221 if (_stat((TCHAR*) strFilename, &fileData)) 00222 { 00223 if (errno == ENOENT) 00224 { 00225 ERROR3("Filename or path not found"); 00226 } 00227 else 00228 { 00229 ERROR3("_stat() failed with an unknown error"); 00230 } 00231 return FALSE; 00232 } 00233 if (fileData.st_mode & _S_IFDIR) // directory 00234 { 00235 DIRECTORY: 00236 // Make sure the directory is not the current one 00237 TCHAR tchbuff[_MAX_PATH]; 00238 if (_getcwd(tchbuff, _MAX_PATH) == NULL) 00239 { 00240 ERROR3("Can't get working directory"); 00241 return FALSE; 00242 } 00243 if (strstr(_strlwr(tchbuff), (TCHAR*) strFilename)) 00244 { 00245 // change to upper dir (we should never attempt to delete the root directory!) 00246 PathName path(strFilename); 00247 if (_chdir((TCHAR*) String_256(path.GetLocation(FALSE)))) 00248 { 00249 ERROR3("Can't change directory"); 00250 return FALSE; 00251 } 00252 } 00253 // Try to remove it in the hope that it's empty 00254 if (_rmdir((TCHAR*) strFilename) == -1) 00255 { 00256 if (errno == ENOTEMPTY || errno == EACCES) 00257 { 00258 _finddata_t findData; 00259 String_256 strSearchPattern(strFilename); 00260 strSearchPattern += chPathSep; 00261 strSearchPattern += _T("*"); // add wildcard 00262 INT32 hSearch = _findfirst(strSearchPattern, &findData); 00263 if (hSearch == -1) 00264 return FALSE; 00265 do 00266 { 00267 if (!(strcmp(findData.name, _T(".")) && strcmp(findData.name, _T("..")))) 00268 continue; // skip this directory (.) or its parent (..) 00269 String_256 strFoundFile(strFilename); 00270 strFoundFile += chPathSep; 00271 strFoundFile += findData.name; 00272 RemoveRecursively(strFoundFile); 00273 } 00274 while (_findnext(hSearch, &findData) == 0); 00275 _findclose(hSearch); 00276 return (_rmdir((TCHAR*) strFilename) != -1); 00277 } 00278 else 00279 { 00280 return FALSE; // probably invalid path 00281 } 00282 } 00283 else 00284 return TRUE; // succedded 00285 } 00286 else if (fileData.st_mode & _S_IFREG) // file 00287 return (remove((TCHAR*) strFilename) != -1); 00288 else 00289 #endif 00290 return FALSE; 00291 }
|