00001
00009 #ifndef __FILE_H
00010 #define __FILE_H
00011
00012 #include <fcntl.h>
00013
00014 #include "GMem.h"
00015 #include "GStream.h"
00016 #include "GArray.h"
00017
00018 #ifdef WIN32
00019
00020
00021
00022
00023
00024
00025 typedef HANDLE OsFile;
00026 #define INVALID_HANDLE INVALID_HANDLE_VALUE
00027 #define ValidHandle(hnd) ((hnd) != INVALID_HANDLE_VALUE)
00028
00029 #define O_READ GENERIC_READ
00030 #define O_WRITE GENERIC_WRITE
00031 #define O_READWRITE (GENERIC_READ | GENERIC_WRITE)
00032 #define O_SHARE 0x01000000
00033 #define O_NO_CACHE 0x00800000
00034
00035 #else
00036
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <dirent.h>
00040 #include <unistd.h>
00041
00042 typedef int OsFile;
00043 #define INVALID_HANDLE -1
00044 #define ValidHandle(hnd) ((hnd) >= 0)
00045
00046 #define O_READ O_RDONLY
00047 #define O_WRITE O_WRONLY
00048 #ifdef MAC
00049 #define O_SHARE O_SHLOCK
00050 #else
00051 #define O_SHARE 0
00052 #endif
00053 #define O_READWRITE O_RDWR
00054
00055 #endif
00056
00058
00059 #define FileDev (GFileSystem::GetInstance())
00060 #define MAX_PATH 260
00061
00062
00063 #define VT_NONE 0
00064 #define VT_3_5FLOPPY 1
00065 #define VT_5_25FLOPPY 2
00066 #define VT_HARDDISK 3
00067 #define VT_CDROM 4
00068 #define VT_RAMDISK 5
00069 #define VT_REMOVABLE 6
00070 #define VT_FOLDER 7
00071 #define VT_FILE 8
00072 #define VT_DESKTOP 9
00073 #define VT_NETWORK_NEIGHBOURHOOD 10
00074 #define VT_NETWORK_MACHINE 11
00075 #define VT_NETWORK_SHARE 12
00076 #define VT_NETWORK_PRINTER 13
00077 #define VT_NETWORK_GROUP 14 // e.g. workgroup
00078 #define VT_MAX 15
00079
00080
00081 #define VA_CASE_SENSITIVE 0x0001
00082 #define VA_CASE_PRESERVED 0x0002
00083 #define VA_UNICODE_ON_DISK 0x0004
00084 #define VA_LFN_API 0x4000
00085 #define VA_COMPRESSED 0x8000
00086
00087
00088 #define FA_NORMAL 0x0000
00089 #define FA_READONLY 0x0001
00090 #define FA_HIDDEN 0x0002
00091 #define FA_SYSTEM 0x0004
00092 #define FA_VOLUME 0x0008
00093 #define FA_DIRECTORY 0x0010
00094 #define FA_ARCHIVE 0x0020
00095
00097
00098
00100 class LgiClass GDirectory
00101 {
00102 public:
00103 virtual ~GDirectory() { }
00104
00108 virtual int First
00109 (
00111 const char *Name,
00114 const char *Pattern = LGI_ALL_FILES
00115 ) = 0;
00116
00119 virtual int Next() = 0;
00120
00123 virtual int Close() = 0;
00124
00127 virtual bool Path
00128 (
00129
00130 char *s,
00131
00132 int BufSize
00133 ) = 0;
00134
00136 virtual long GetAttributes() = 0;
00137
00139 virtual char *GetName() = 0;
00140
00142 virtual int GetUser
00143 (
00145 bool Group
00146 ) = 0;
00147
00149 virtual const uint64 GetCreationTime() = 0;
00150
00152 virtual const uint64 GetLastAccessTime() = 0;
00153
00155 virtual const uint64 GetLastWriteTime() = 0;
00156
00158 virtual const uint64 GetSize() = 0;
00159
00161 virtual bool IsDir() = 0;
00162
00164 virtual bool IsReadOnly() = 0;
00165
00168 virtual bool IsHidden() = 0;
00169
00171 virtual GDirectory *Clone() = 0;
00172
00174 virtual int GetType() = 0;
00175
00177 bool ConvertToTime(char *Str, uint64 Time);
00178
00180 bool ConvertToDate(char *Str, uint64 Time);
00181 };
00182
00184 class LgiClass GVolume
00185 {
00186 friend class GFileSystem;
00187
00188 protected:
00189 char *_Name;
00190 char *_Path;
00191 int _Type;
00192 int _Flags;
00193 int64 _Size;
00194 int64 _Free;
00195
00196 public:
00197 GVolume();
00198 virtual ~GVolume();
00199
00200 char *Name() { return _Name; }
00201 char *Path() { return _Path; }
00202 int Type() { return _Type; }
00203 int Flags() { return _Flags; }
00204 uint64 Size() { return _Size; }
00205 uint64 Free() { return _Free; }
00206
00207 virtual bool IsMounted() = 0;
00208 virtual bool SetMounted(bool Mount) = 0;
00209 virtual GVolume *First() = 0;
00210 virtual GVolume *Next() = 0;
00211 virtual GDirectory *GetContents() = 0;
00212 };
00213
00214
00216 class LgiClass GDirImpl : public GDirectory
00217 {
00218 class GDirImplPrivate *d;
00219
00220 public:
00221 GDirImpl();
00222 ~GDirImpl();
00223
00224 int First(const char *Path, const char *Pattern);
00225 int Next();
00226 int Close();
00227 bool Path(char *s, int len = -1);
00228 GDirectory *Clone();
00229
00230 long GetAttributes();
00231 bool IsDir();
00232 bool IsHidden();
00233 bool IsReadOnly();
00234 char *GetName();
00235 const uint64 GetCreationTime();
00236 const uint64 GetLastAccessTime();
00237 const uint64 GetLastWriteTime();
00238 const uint64 GetSize();
00239 int GetUser(bool Group);
00240 int GetType();
00241 };
00242
00243 typedef int (*CopyFileCallback)(void *token, int64 Done, int64 Total);
00244
00246 class LgiClass GFileSystem
00247 {
00248 friend class GFile;
00249 static GFileSystem *Instance;
00250 class GFileSystemPrivate *d;
00251
00252 GVolume *Root;
00253
00254 public:
00255 GFileSystem();
00256 ~GFileSystem();
00257
00258 #ifdef WIN32
00259 static bool Win9x;
00260 #endif
00261
00263 static GFileSystem *GetInstance() { return Instance; }
00264
00267 void OnDeviceChange(char *Reserved = 0);
00268
00270 GVolume *GetRootVolume();
00271
00273 GDirectory *GetDir();
00274
00276 bool Copy
00277 (
00279 char *From,
00281 char *To,
00283 int *Status = 0,
00285 CopyFileCallback Callback = 0,
00287 void *Token = 0
00288 );
00289
00291 bool Delete(char *FileName, bool ToTrash = true);
00293 bool Delete
00294 (
00296 GArray<char*> &Files,
00298 GArray<int> *Status = 0,
00300 bool ToTrash = true
00301 );
00302
00304 bool CreateDirectory(char *PathName);
00305
00307 bool RemoveDirectory
00308 (
00310 char *PathName,
00312 bool Recurse = false
00313 );
00314
00315 bool SetCurrentDirectory(char *PathName);
00316 bool GetCurrentDirectory(char *PathName, int Length);
00317
00319 bool Move(char *OldName, char *NewName);
00320 };
00321
00322 #ifdef BEOS
00323
00324 #define GFileOps() \
00325 GFilePre char GFilePost; \
00326 GFilePre int8 GFilePost; \
00327 GFilePre uint8 GFilePost; \
00328 GFilePre int16 GFilePost; \
00329 GFilePre uint16 GFilePost; \
00330 GFilePre signed int GFilePost; \
00331 GFilePre unsigned int GFilePost; \
00332 GFilePre signed long GFilePost; \
00333 GFilePre unsigned long GFilePost; \
00334 GFilePre int64 GFilePost; \
00335 GFilePre uint64 GFilePost; \
00336 GFilePre double GFilePost
00337
00338 #else
00339
00340 #define GFileOps() \
00341 GFilePre char GFilePost; \
00342 GFilePre uint8 GFilePost; \
00343 GFilePre int16 GFilePost; \
00344 GFilePre uint16 GFilePost; \
00345 GFilePre signed int GFilePost; \
00346 GFilePre unsigned int GFilePost; \
00347 GFilePre signed long GFilePost; \
00348 GFilePre unsigned long GFilePost; \
00349 GFilePre int64 GFilePost; \
00350 GFilePre uint64 GFilePost; \
00351 GFilePre double GFilePost
00352
00353 #endif
00354
00356 class LgiClass GFile : public GStream
00357 {
00358 protected:
00359 class GFilePrivate *d;
00360
00361 int SwapRead(uchar *Buf, int Size);
00362 int SwapWrite(uchar *Buf, int Size);
00363
00364 public:
00365 GFile();
00366 virtual ~GFile();
00367
00368 OsFile Handle();
00369
00372 int Open
00373 (
00375 const char *Name,
00377 int Attrib
00378 );
00379
00381 bool IsOpen();
00382
00384 int GetError();
00385
00387 int Close();
00388
00390 int GetOpenMode();
00391
00393 int GetBlockSize();
00394
00397 int64 GetPos();
00398
00401 int64 SetPos(int64 Pos);
00402
00405 int64 GetSize();
00406
00409 int64 SetSize(int64 Size);
00410
00413 int Read(void *Buffer, int Size, int Flags = 0);
00414
00417 int Write(const void *Buffer, int Size, int Flags = 0);
00418
00420 virtual char *GetName();
00421
00423 virtual int64 Seek(int64 To, int Whence);
00424
00426 virtual bool Eof();
00427
00429 virtual void SetStatus(bool s = false);
00430
00433 virtual bool GetStatus();
00434
00437 virtual void SetSwap(bool s);
00438
00440 virtual bool GetSwap();
00441
00442
00443 virtual int ReadStr(char *Buf, int Size);
00444 virtual int WriteStr(char *Buf, int Size);
00445
00446
00447 #define GFilePre virtual GFile &operator >> (
00448 #define GFilePost &i)
00449 GFileOps();
00450 #undef GFilePre
00451 #undef GFilePost
00452
00453 #define GFilePre virtual GFile &operator << (
00454 #define GFilePost i)
00455 GFileOps();
00456 #undef GFilePre
00457 #undef GFilePost
00458 };
00459
00460
00461 LgiFunc int64 LgiFileSize(char *FileName);
00462 LgiFunc bool FileExists(const char *File);
00463 LgiFunc bool DirExists(const char *Dir);
00464 LgiFunc bool ResolveShortcut(char *LinkFile, char *Path, int Len);
00465 LgiFunc void WriteStr(GFile &f, char *s);
00466 LgiFunc char *ReadStr(GFile &f DeclDebugArgs);
00467 LgiFunc int SizeofStr(char *s);
00468 LgiFunc char *ReadTextFile(char *File);
00469 LgiFunc bool LgiTrimDir(char *Path);
00470 LgiFunc bool LgiIsRelativePath(const char *Path);
00471 LgiFunc char *LgiMakeRelativePath(char *Base, char *Path);
00472 LgiFunc bool LgiMakePath(char *Str, int StrBufLen, const char *Dir, const char *File);
00473 LgiFunc char *LgiGetExtension(const char *File);
00474 LgiFunc bool LgiIsFileNameExecutable(char *FileName);
00475 LgiFunc bool LgiIsFileExecutable(char *FileName, GStreamI *f, int64 Start, int64 Len);
00476 LgiFunc const char *GetErrorName(int e);
00477
00479 LgiFunc bool LgiGetDriveInfo(char *Path, uint64 *Free, uint64 *Size = 0, uint64 *Available = 0);
00480
00481 #endif