00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef _CATFileSystem_H_
00015 #define _CATFileSystem_H_
00016
00017 #include "CATInternal.h"
00018 #include "CATMutex.h"
00019 #include "CATPlatform.h"
00020 #include "CATStreamFile.h"
00021
00022 typedef void* CATFINDHANDLE;
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class CATFileSystem
00034 {
00035 friend CATPlatform;
00036 public:
00037
00038
00039 virtual CATResult Initialize() = 0;
00040
00041
00042
00043
00044
00045
00046
00047
00048 virtual CATResult FileExists( const CATString& pathname ) = 0;
00049
00050
00051
00052
00053
00054
00055
00056
00057 virtual CATResult DirExists ( const CATString& pathname ) = 0;
00058
00059
00060
00061
00062
00063
00064 virtual CATResult CreateDir ( const CATString& pathname ) = 0;
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 virtual CATResult PathExists( const CATString& pathname ) = 0;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 virtual CATResult FindFirst ( const CATString& searchMask,
00092 CATString& firstFile,
00093 CATFINDHANDLE& findHandle) = 0;
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 virtual CATResult FindNext ( CATString& nextFile,
00107 CATFINDHANDLE findHandle) = 0;
00108
00109
00110
00111
00112
00113
00114
00115 virtual CATResult FindEnd (CATFINDHANDLE& findHandle) = 0;
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 virtual CATResult OpenFile( const CATString& filename,
00126 CATStream::OPEN_MODE mode,
00127 CATStream*& stream) = 0;
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 virtual CATResult OpenCachedFile( const CATString& filename,
00138 CATStream*& stream)
00139 {
00140 return OpenFile(filename, CATStream::READ_ONLY,stream);
00141 }
00142
00143
00144
00145
00146
00147
00148 virtual CATResult ReleaseFile(CATStream*& stream) = 0;
00149
00150
00151
00152 virtual bool IsFileReadOnly(const CATString& path) = 0;
00153
00154
00155
00156
00157
00158
00159
00160 CATString GetFullPath( const CATString& path )
00161 {
00162 return BuildPath(this->fBasePath, path);
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 static CATString BuildPath ( const CATString& directory,
00172 const CATString& filename,
00173 bool appendSep = false)
00174 {
00175 CATString fullPath;
00176
00177 if (directory.IsEmpty() == false)
00178 {
00179 fullPath = directory;
00180 if ((directory.GetWChar(directory.LengthCalc() - 1) != CAT_PATHSEPERATOR) &&
00181 ((filename.IsEmpty() == true) || (filename.GetWChar(0) != CAT_PATHSEPERATOR)))
00182 {
00183 fullPath << CAT_PATHSEPERATOR;
00184 }
00185 }
00186
00187 fullPath << filename;
00188
00189 if (appendSep)
00190 {
00191 if (fullPath.GetWChar( fullPath.Length() - 1) != CAT_PATHSEPERATOR)
00192 {
00193 fullPath << CAT_PATHSEPERATOR;
00194 }
00195 }
00196
00197 return fullPath;
00198 }
00199
00200 static CATString& EnsureTerminator(CATString& termPath)
00201 {
00202 if (termPath.IsEmpty() || (termPath.GetWChar( termPath.Length() - 1) != CAT_PATHSEPERATOR))
00203 {
00204 termPath << CAT_PATHSEPERATOR;
00205 }
00206 return termPath;
00207 }
00208
00209
00210
00211
00212 static CATString GetFileExtension (const CATString& path)
00213 {
00214 CATString extension;
00215 bool found = false;
00216
00217 CATUInt32 offset = -1;
00218 if (path.ReverseFind(CAT_PATHSEPERATOR,offset))
00219 {
00220
00221 found = path.Find( '.',offset);
00222 }
00223 else
00224 {
00225 offset = 0;
00226 found = path.Find('.',offset);
00227 }
00228
00229 if (found)
00230 {
00231 extension = path.Right(offset+1);
00232 }
00233 return extension;
00234 }
00235
00236
00237 static CATString StripFileExtension (const CATString& path)
00238 {
00239 CATString noExtension = path;
00240
00241 bool found = false;
00242
00243 CATUInt32 offset = -1;
00244 CATUInt32 testOffset = -1;
00245
00246 if (path.ReverseFind(CAT_PATHSEPERATOR,testOffset))
00247 {
00248
00249 while (path.Find('.',testOffset))
00250 {
00251 offset = testOffset;
00252 found = true;
00253 testOffset++;
00254 }
00255 }
00256 else
00257 {
00258 while (path.Find('.',testOffset))
00259 {
00260 offset = testOffset;
00261 found = true;
00262 testOffset++;
00263 }
00264 }
00265
00266 if (found)
00267 {
00268 noExtension = path.Left(offset);
00269 }
00270 return noExtension;
00271 }
00272
00273
00274
00275 static CATString SanitizeFilename( const CATString& filename,
00276 bool allowExtSep = true,
00277 bool allowPathSep = false,
00278 bool allowDriveSep = false)
00279 {
00280 const CATUInt32 numChars = 6;
00281 wchar_t illegalChars[numChars] = { '*','|','?','<','>','/' };
00282
00283 CATString safeString;
00284 CATUInt32 nameLen = filename.LengthCalc();
00285
00286 for (CATUInt32 i=0; i < nameLen; i++)
00287 {
00288 wchar_t curChar = filename.GetWChar(i);
00289 bool skipChar = false;
00290
00291 for (CATUInt32 j=0; j<numChars; j++)
00292 {
00293 if (curChar == illegalChars[j])
00294 skipChar = true;
00295 }
00296
00297
00298 skipChar |= ((curChar == '.') && (!allowExtSep));
00299
00300
00301 skipChar |= ((curChar == CAT_PATHSEPERATOR) && (!allowPathSep));
00302
00303 #ifdef kDRIVESEPERATOR
00304 skipChar |= ((curChar == kDRIVESEPERATOR) && (!allowDriveSep));
00305 #endif
00306
00307 if (!skipChar)
00308 {
00309 safeString << curChar;
00310 }
00311 }
00312
00313 return safeString;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 static CATResult SplitPath ( const CATString& fullPath,
00332 CATString& directory,
00333 CATString& filename,
00334 bool keepTrailingSep = true)
00335 {
00336 CATResult result = CAT_SUCCESS;
00337 CATUInt32 offset = -1;
00338 directory = "";
00339 filename = "";
00340
00341 if (fullPath.IsEmpty())
00342 {
00343 return CATRESULT(CAT_ERR_PATH_EMPTY);
00344 }
00345
00346 if (fullPath.ReverseFind(CAT_PATHSEPERATOR,offset))
00347 {
00348 directory = fullPath.Left(offset + (keepTrailingSep ? 1 : 0));
00349 filename = fullPath.Right(offset + 1);
00350 }
00351 else
00352 {
00353 #ifdef kDRIVESEPERATOR
00354 offset = -1;
00355 if (fullPath.ReverseFind(kDRIVESEPERATOR, offset))
00356 {
00357 directory = fullPath.Left(offset + 1);
00358 filename = fullPath.Right(offset + 1);
00359 }
00360 else
00361 #endif
00362 {
00363 filename = fullPath;
00364 }
00365 }
00366
00367 if (directory.IsEmpty() != true)
00368 {
00369 if ( directory.GetWChar(directory.LengthCalc() - 1) != CAT_PATHSEPERATOR)
00370 {
00371 directory << CAT_PATHSEPERATOR;
00372 }
00373 }
00374
00375 if (filename.IsEmpty())
00376 {
00377 result = CATRESULTFILE(CAT_STAT_PATH_NO_FILE, fullPath);
00378 }
00379 else if (directory.IsEmpty())
00380 {
00381 result = CATRESULTFILE(CAT_STAT_PATH_NO_DIRECTORY, fullPath);
00382 }
00383
00384 return result;
00385
00386 }
00387
00388
00389 virtual CATString GetBase()
00390 {
00391 return fBasePath;
00392 }
00393
00394 protected:
00395
00396
00397
00398
00399
00400 CATFileSystem(const CATString& basePath = "")
00401 {
00402 fBasePath = basePath;
00403
00404 if ((fBasePath.IsEmpty() == false) && (fBasePath.GetWChar(fBasePath.Length() - 1) != CAT_PATHSEPERATOR))
00405 {
00406 fBasePath << CAT_PATHSEPERATOR;
00407 }
00408 }
00409
00410
00411
00412 virtual ~CATFileSystem()
00413 {
00414
00415 fFSLock.Wait();
00416 fFSLock.Release();
00417 }
00418
00419
00420 CATString RemoveBasePath(CATString& fullPath)
00421 {
00422 if (fBasePath.CompareNoCase(fullPath,fBasePath.Length()) == 0)
00423 {
00424 return fullPath.Right(fBasePath.Length());
00425 }
00426 else
00427 {
00428 return fullPath;
00429 }
00430 }
00431
00432 CATMutex fFSLock;
00433 CATString fBasePath;
00434 };
00435
00436
00437 #endif // _CATFileSystem_H_
00438
00439