IO.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /******************************************************************************
  2. Use 'Drive' and 'GetDrives' to access your device drives information.
  3. Use 'FileInfo' to access information about files and folders.
  4. Use 'FileFind' to find files.
  5. Use 'DataPath' to specify additional search path for accessing files.
  6. Use 'F*' functions to handle any file operations.
  7. /******************************************************************************/
  8. // DRIVE
  9. /******************************************************************************/
  10. enum DRIVE_TYPE : Byte // Drive Type
  11. {
  12. DRIVE_UNDEFINED, // undefined
  13. DRIVE_DISK , // disk (HDD/SSD)
  14. DRIVE_OPTICAL , // optical (CD/DVD/Blu-Ray)
  15. DRIVE_USB , // removable (USB)
  16. DRIVE_SD_CARD , // removable (SD Card)
  17. };
  18. struct Drive // Drive in the Operating System
  19. {
  20. DRIVE_TYPE type; // drive type
  21. Str path, // for example "C:\" "D:\" on Windows, and "\" "Volumes\Bootcamp\" on Mac
  22. name; // for example "Hard Disk Drive", "Macintosh HD", "BOOTCAMP"
  23. };
  24. /******************************************************************************/
  25. // FILE INFO
  26. /******************************************************************************/
  27. enum FSTD_TYPE : Byte // Standard File Type
  28. {
  29. FSTD_NONE , // none
  30. FSTD_DRIVE, // drive
  31. FSTD_DIR , // folder
  32. FSTD_FILE , // file
  33. FSTD_LINK , // symbolic link, this type can occur on Unix systems (this includes retrieving a list of files from Unix FTP server)
  34. };
  35. enum FATTRIB_FLAG // File Attribute Flags
  36. {
  37. FATTRIB_READ_ONLY=0x1, // read-only
  38. FATTRIB_HIDDEN =0x2, // hidden
  39. };
  40. struct FileInfo // File Information
  41. {
  42. FSTD_TYPE type ; // file type
  43. Byte attrib ; // file attributes, FATTRIB_FLAG
  44. DateTime modify_time_utc; // file modification time (UTC time zone)
  45. Long size ; // file size
  46. #if EE_PRIVATE
  47. #if WINDOWS
  48. void from(WIN32_FIND_DATA &fd);
  49. #endif
  50. void zero() {Zero(T);}
  51. #endif
  52. Bool getSystem(C Str &name); // get info from 'name' file/folder in the system, false on fail
  53. Bool get (C Str &name); // get info from 'name' file/folder , false on fail, search is performed additionally in 'Paks' and 'DataPath'
  54. friend Bool operator==(C FileInfo &f0, C FileInfo &f1); // warning: file dates are compared with 1 second tolerance (because some file systems have lower precision for times), file attributes are not taken into account in this function
  55. friend Bool operator!=(C FileInfo &f0, C FileInfo &f1) {return !(f0==f1);} // warning: file dates are compared with 1 second tolerance (because some file systems have lower precision for times), file attributes are not taken into account in this function
  56. FileInfo( );
  57. FileInfo(C Str &name) {get(name);}
  58. FileInfo(C PakFile &pf );
  59. };
  60. struct FileInfoSystem : FileInfo // System File Information, this is the same thing as 'FileInfo' except the default constructor always uses 'getSystem' method
  61. {
  62. FileInfoSystem(C Str &name) {getSystem(name);}
  63. };
  64. /******************************************************************************/
  65. // FILE FIND
  66. /******************************************************************************/
  67. STRUCT(FileFind , FileInfo) // File Finder
  68. //{
  69. Str name; // currently encountered file base name (does not include path), this is valid when "Bool operator()" returns true
  70. // operations
  71. void find (C Str &path, C Str &ext=S); // start searching in 'path', accept only 'ext' extensions (set empty for all)
  72. void findDrives( ); // start searching for drives
  73. // get
  74. Bool operator()(); // if has file for processing
  75. C Str& path ()C {return _path ;} // get searching path
  76. Str pathName ()C {return _path+ name;} // get encountered file full path name
  77. DRIVE_TYPE driveType ()C {return _drive_type;} // get encountered drive type (valid when current file is of FSTD_DRIVE type)
  78. ~FileFind();
  79. FileFind();
  80. explicit FileFind(C Str &path, C Str &ext=S);
  81. private:
  82. #if EE_PRIVATE
  83. enum STATE : Byte
  84. {
  85. NONE ,
  86. FILE_WAITING,
  87. NEED_CHECK ,
  88. };
  89. #endif
  90. Str _path, _ext;
  91. Byte _state, _drive;
  92. DRIVE_TYPE _drive_type;
  93. #if EE_PRIVATE
  94. PLATFORM(Ptr, DIR*) _handle;
  95. #else
  96. Ptr _handle;
  97. #endif
  98. #if EE_PRIVATE
  99. #if WINDOWS
  100. Bool findValid(WIN32_FIND_DATA &fd);
  101. #endif
  102. void zero ();
  103. void del ();
  104. Bool findNext();
  105. #endif
  106. NO_COPY_CONSTRUCTOR(FileFind);
  107. };
  108. /******************************************************************************/
  109. struct BackgroundFileFind
  110. {
  111. struct File : FileInfo
  112. {
  113. Str name;
  114. };
  115. static Int Compare(C File &a, C File &b) {return ComparePath(a.name, b.name);}
  116. // get
  117. Bool finished( )C {return !_thread.active();} // if finished finding files
  118. Bool getFiles(Memc<File> &files); // get new files found since last call to 'getFiles' and till this moment, returns true if any files were returned
  119. // operations
  120. void find (C Str &path, Bool (*filter)(C Str &name)=null); // start finding files in path
  121. void clear( ); // stop current search and clear all files
  122. #if EE_PRIVATE
  123. Bool update();
  124. #endif
  125. BackgroundFileFind& del();
  126. ~BackgroundFileFind() {del();}
  127. BackgroundFileFind() {}
  128. explicit BackgroundFileFind(C Str &path, Bool (*filter)(C Str &name)=null) {find(path, filter);}
  129. private:
  130. UInt _find_id;
  131. Memc<File> _files;
  132. Memc<Str> _paths;
  133. SyncLock _lock;
  134. Thread _thread;
  135. Bool (*_filter)(C Str &name);
  136. };
  137. /******************************************************************************/
  138. // MAIN
  139. /******************************************************************************/
  140. enum FILE_OVERWRITE_MODE
  141. {
  142. FILE_OVERWRITE_NEVER , // never overwrite
  143. FILE_OVERWRITE_ALWAYS , // always overwrite
  144. FILE_OVERWRITE_DIFFERENT, // overwrite only if size or modification time (with 1 second tolerance) is different
  145. };
  146. /******************************************************************************/
  147. C Str& DataPath( ); // get additional search path, used for opening files
  148. void DataPath(C Str &path); // set additional search path, used for opening files, given 'path' will have automatically added '/' character at the end if not present, and will be converted from relative to global (for example DataPath("../data") will set a complete path like "d:/game/data/" ending with '/' character - this is only an example)
  149. void GetDrives(MemPtr<Drive> drives); // get Drives in the system
  150. Bool GetDriveSize(C Str &path, Long *free=null, Long *total=null); // get drive size in bytes of specified path. Warning: on some platforms, if you don't have write permission to the specified path, then 'free' value may be incorrect, always use a path where you actually intend to write data, false is returned on fail
  151. Str CurDir( ); // get current working directory, by default it's always set to path of the application executable file (an exception is iOS platform, there the path is set inside the executable, since the executable is actually a folder)
  152. void CurDir(C Str &dir); // set current working directory
  153. Str MakeFullPath(C Str &path, FILE_PATH type=FILE_CUR, Bool keep_empty=true); // if the path is not 'FullPath', then full path is returned as if 'path' was relative to 'type', 'keep_empty'=if the 'path' is empty then don't make it a full path but return an empty path
  154. Bool FEqual ( C Str &a , C Str &b , Cipher * a_cipher=null, Cipher * b_cipher=null ); // if files have the same data
  155. Bool FCopy ( C Str &src, C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *src_cipher=null, Cipher *dest_cipher=null, CChar *safe_overwrite_suffix=null); // copy 'src' file to 'dest' file , false on fail, 'safe_overwrite_suffix'=if specified then 'SafeOverwrite' function will be used
  156. Bool FCopy ( Pak &pak, C PakFile &src, C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *dest_cipher=null, CChar *safe_overwrite_suffix=null); // copy 'src' pak file to 'dest' file , false on fail, 'safe_overwrite_suffix'=if specified then 'SafeOverwrite' function will be used
  157. Bool FCopy ( Pak &pak, C Str &src, C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *dest_cipher=null, CChar *safe_overwrite_suffix=null); // copy 'src' pak file to 'dest' file , false on fail, 'safe_overwrite_suffix'=if specified then 'SafeOverwrite' function will be used
  158. Bool FCopy ( Pak &src , C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *dest_cipher=null ); // extract 'src' pak files to 'dest' folder, false on fail
  159. Bool FCopyDir (C Str &src , C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *src_cipher=null, Cipher *dest_cipher=null ); // copy 'src' folder to 'dest' folder, false on fail
  160. Bool FReplaceDir (C Str &src , C Str &dest, FILE_OVERWRITE_MODE overwrite=FILE_OVERWRITE_ALWAYS, Cipher *src_cipher=null, Cipher *dest_cipher=null ); // copy 'src' folder to 'dest' folder and remove all contents of 'dest' which are not present in 'src', false on fail
  161. Str FFirst (C Str &name, C Str &ext=S ); // find first non-existing file, sample usage: FFirst("path/name ", "bmp") will return "path/name 0.bmp", "path/name 1.bmp", "path/name 2.bmp", ..
  162. Str FFirstUp (C Str &name ); // find first existing file/folder by going up in the path, for example when using FFirstUp("c:/folder1/folder2/file.ext") and in the case when only "c:/folder1" exists, but not "c:/folder1/folder2/file.ext", then "c:/folder1" will be returned. empty string is returned when nothing was found
  163. Bool FTimeUTC (C Str &name, C DateTime &time_utc); // set file/folder modification time (in UTC time zone), false on fail
  164. Bool FAttrib (C Str &name, UInt attrib ); // set file attributes, 'attrib'=FATTRIB_FLAG, false on fail
  165. UInt FAttrib (C Str &name ); // get file attributes FATTRIB_FLAG
  166. Long FSize (C Str &name ); // get file/folder size in the system, -1 on fail
  167. Bool FExistSystem(C Str &name ); // if file/folder exists in the system
  168. Bool FExist (C Str &name ); // if file/folder exists , search is performed additionaly in 'Paks' and 'DataPath'
  169. Bool FRename (C Str &src , C Str &dest ); // rename file/folder, false on fail, if 'dest' file already exists then it will be overwritten by 'src'
  170. Bool FMoveDir (C Str &src , C Str &dest ); // move folder, false on fail
  171. Bool FDelFile (C Str &name ); // delete file , false on fail
  172. Bool FDelDir (C Str &name ); // delete empty folder, false on fail (this will delete the folder only if it's empty, this will fail if it's not empty)
  173. Bool FDelDirs (C Str &name ); // delete folder, false on fail (this will delete the folder and all elements inside it)
  174. Bool FDelInside (C Str &name ); // delete inside folder, false on fail (this will delete all elements inside provided path, but not the path itself)
  175. Bool FDel (C Str &name ); // delete file/folder, false on fail (this function will call 'FDelFile' or 'FDelDirs' depending on if the element is a file or folder)
  176. Bool FRecycle (C Str &name, Bool hidden=true ); // recycle file/folder, false on fail, 'hidden'=if recycle with no OS UI and no confirmations (this function will move the element into system's "Recycle Bin")
  177. Bool FCreateDir (C Str &name ); // create folder, false on fail
  178. Bool FCreateDirs (C Str &name ); // create series of folders, usage: FCreateDirs("c:\a\b\c") creates "a", then "b" and "c", false on fail
  179. Bool SafeOverwrite(File &src, C Str &dest, C DateTime *modify_time_utc=null, Cipher *dest_cipher=null, C Str &suffix="@new", ReadWriteSync *rws=null); // safely overwrite 'dest' file from 'src' data, this first writes the full data (starting from current 'src' position) to a temporary name made from dest+suffix, after the file is written successfully, its modification time is set to 'modify_time_utc' if not null, after that the file name is changed to the final 'dest' replacing any existing file at that location, 'rws'=(optional) if specified then it will have its 'enterWrite' called just before replacing the file and 'leaveWrite' afterwards, false on fail
  180. Bool SafeOverwrite(FileText &src, C Str &dest, C DateTime *modify_time_utc=null, Cipher *dest_cipher=null, C Str &suffix="@new", ReadWriteSync *rws=null); // safely overwrite 'dest' file from 'src' data, this first writes the full data (starting from zero 'src' position) to a temporary name made from dest+suffix, after the file is written successfully, its modification time is set to 'modify_time_utc' if not null, after that the file name is changed to the final 'dest' replacing any existing file at that location, 'rws'=(optional) if specified then it will have its 'enterWrite' called just before replacing the file and 'leaveWrite' afterwards, false on fail
  181. enum FILE_LIST_MODE
  182. {
  183. FILE_LIST_CONTINUE, // return this enum if you want to continue listing files
  184. FILE_LIST_SKIP , // return this enum if currently processed element is a folder and you don't want to list elements inside it
  185. FILE_LIST_BREAK , // return this enum if you don't want to continue listing files, but return from the recursion as soon as possible
  186. };
  187. void FList(C Str &path, FILE_LIST_MODE func(C FileFind &ff, Ptr user), Ptr user=null); // list all elements inside specified path (in recursive mode) and report them into custom 'func' callback function, 'user'=custom user data
  188. T1(TYPE) void FList(C Str &path, FILE_LIST_MODE func(C FileFind &ff, TYPE *user), TYPE *user=null); // list all elements inside specified path (in recursive mode) and report them into custom 'func' callback function, 'user'=custom user data
  189. T1(TYPE) void FList(C Str &path, FILE_LIST_MODE func(C FileFind &ff, TYPE &user), TYPE &user ); // list all elements inside specified path (in recursive mode) and report them into custom 'func' callback function, 'user'=custom user data
  190. Bool CreateSymLink(C Str &name, C Str &target); // create a symbolic link, link will be created at 'name' location and it will be pointing to 'target' file, this function is used only on Unix systems and only for symbolic links, if you wish to create a shortcut then please use 'CreateShortcut' function instead, false on fail
  191. Str DecodeSymLink(File &f ); // this function is meant for resolving target file of a symbolic link, to use it pass an opened 'f' file of FSTD_LINK type to this function and it will return the target path, S on fail
  192. /******************************************************************************/
  193. #if EE_PRIVATE
  194. void InitIO();
  195. void FlushIO();
  196. Bool UnixReadFile(CChar8 *file, Char8 *data, Int size);
  197. #endif
  198. /******************************************************************************/