File.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /******************************************************************************
  2. Use 'File' to handle binary files management.
  3. /******************************************************************************/
  4. #if EE_PRIVATE
  5. enum FILE_TYPE : Byte // File Type
  6. {
  7. FILE_NONE , // none
  8. FILE_STD_READ , // stdio in read mode, '_handle' is a system handle, can be switched to FILE_STD_WRITE if '_writable'
  9. FILE_STD_WRITE, // stdio in write mode, '_handle' is a system handle, can be switched to FILE_STD_READ
  10. FILE_MEM , // memory, always readable, writable if '_writable'
  11. FILE_MEMB , // Memb , always readable+writable
  12. };
  13. #endif
  14. enum FILE_PATH : Byte // File Path Type
  15. {
  16. FILE_CUR , // relative to 'CurDir'
  17. FILE_DATA, // relative to 'DataPath'
  18. #if EE_PRIVATE
  19. FILE_ANDROID_ASSET, // Android Asset, can be accessed by memory ('_aasset' is "AAsset*") or by stdio ('_handle' is a system handle)
  20. #endif
  21. };
  22. struct File
  23. {
  24. // manage
  25. #if EE_PRIVATE
  26. void zeroNoBuf( );
  27. void zero ( );
  28. void delBuf ( );
  29. Bool setBuf (Int size); // set the buffer to be at least of 'size', false on fail
  30. void close ( ); // close the file without releasing helper memory
  31. #endif
  32. File& del(); // manually delete the file object (this does not delete the file on the disk, it only closes the handle of the C++ file object and releases used memory)
  33. File& append (C Str &name, const_mem_addr Cipher *cipher=null); // append stdio file, reading is allowed in this mode, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), if a file doesn't exist then a new file is created, if a file already exists then that file is opened, its contents are preserved, position is set to the end
  34. File& write (C Str &name, const_mem_addr Cipher *cipher=null); // write to stdio file, reading is allowed in this mode, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), if a file doesn't exist then a new file is created, if a file already exists then that file is opened, its contents are destroyed, position and size are set to 0
  35. File& readStd (C Str &name, const_mem_addr Cipher *cipher=null); // read stdio file, writing is not allowed in this mode, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  36. File& read (C Str &name, const_mem_addr Cipher *cipher=null); // read Pak or stdio file, writing is not allowed in this mode, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  37. File& read (C UID &id , const_mem_addr Cipher *cipher=null); // read Pak or stdio file, writing is not allowed in this mode, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  38. File& read (C Str &name, C Pak &pak ); // read Pak file, writing is not allowed in this mode, Exit on fail
  39. File& read (C UID &id , C Pak &pak ); // read Pak file, writing is not allowed in this mode, Exit on fail
  40. File& read (C Str &name, C PakSet &paks ); // read Pak file, writing is not allowed in this mode, Exit on fail
  41. File& read (C UID &id , C PakSet &paks ); // read Pak file, writing is not allowed in this mode, Exit on fail
  42. File& read (C PakFile &file, C Pak &pak ); // read Pak file, writing is not allowed in this mode, Exit on fail
  43. Bool appendTry (C Str &name, const_mem_addr Cipher *cipher=null); // try to append stdio file, reading is allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), if a file doesn't exist then a new file is created, if a file already exists then that file is opened, its contents are preserved, position is set to the end
  44. Bool writeTry (C Str &name, const_mem_addr Cipher *cipher=null); // try to write to stdio file, reading is allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), if a file doesn't exist then a new file is created, if a file already exists then that file is opened, its contents are destroyed, position and size are set to 0
  45. Bool readStdTry(C Str &name, const_mem_addr Cipher *cipher=null); // try to read stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  46. Bool readTry (C Str &name, const_mem_addr Cipher *cipher=null); // try to read Pak or stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  47. Bool readTry (C UID &id , const_mem_addr Cipher *cipher=null); // try to read Pak or stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  48. Bool readTry (C Str &name, C Pak &pak ); // try to read Pak file, writing is not allowed in this mode, false on fail
  49. Bool readTry (C UID &id , C Pak &pak ); // try to read Pak file, writing is not allowed in this mode, false on fail
  50. Bool readTry (C Str &name, C PakSet &paks ); // try to read Pak file, writing is not allowed in this mode, false on fail
  51. Bool readTry (C UID &id , C PakSet &paks ); // try to read Pak file, writing is not allowed in this mode, false on fail
  52. Bool readTry (C PakFile &file, C Pak &pak ); // try to read Pak file, writing is not allowed in this mode, false on fail
  53. #if EE_PRIVATE
  54. #if ANDROID
  55. Bool readStdTryEx(C Str &name, const_mem_addr Cipher *cipher, UInt max_buf_size=UINT_MAX, Bool *processed=null); // try to read stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  56. #else
  57. Bool readStdTryEx(C Str &name, const_mem_addr Cipher *cipher, UInt max_buf_size=UINT_MAX); // try to read stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  58. #endif
  59. Bool readTryEx (C Str &name, const_mem_addr Cipher *cipher, Bool *processed ); // try to read Pak or stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'processed'=if original file had to be processed
  60. Bool readTryEx (C UID &id , const_mem_addr Cipher *cipher, Bool *processed ); // try to read Pak or stdio file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'processed'=if original file had to be processed
  61. Bool readTryEx (C Str &name, C PakSet &paks, const_mem_addr Cipher *cipher, Bool *processed ); // try to read Pak file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'processed'=if original file had to be processed
  62. Bool readTryEx (C UID &id , C PakSet &paks, const_mem_addr Cipher *cipher, Bool *processed ); // try to read Pak file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'processed'=if original file had to be processed
  63. Bool readTryEx (C PakFile &file, C Pak &pak , const_mem_addr Cipher *cipher, Bool *processed ); // try to read Pak file, writing is not allowed in this mode, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'processed'=if original file had to be processed
  64. Bool readTryRaw (C PakFile &file, C Pak &pak ); // try to read Pak file, writing is not allowed in this mode, false on fail, this reads file in raw mode (does not decompress files)
  65. #endif
  66. File& writeMemFixed( Int size , const_mem_addr Cipher *cipher=null); // start writing to fixed memory file , reading is allowed in this mode, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  67. File& writeMem (UInt block_elms=64*1024, const_mem_addr Cipher *cipher=null); // start writing to resizable memory file , reading is allowed in this mode, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  68. File& readMem (CPtr data, Int size , const_mem_addr Cipher *cipher=null); // start reading from fixed memory address, writing is not allowed in this mode, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  69. // get / set
  70. #if EE_PRIVATE
  71. Long posFile ()C; // get actual position in file (takes into account buffering)
  72. Long posAbs ()C {return _pos+ _offset;} // get absolute position in file
  73. Int posCipher()C {return _pos+_cipher_offset;} // get offset to be used in cipher, can be Int (and not Long) because Cipher operates on Int offset only
  74. Ptr memFast () {return (Byte*)_mem+posAbs();}
  75. Ptr mem () ; // get raw memory pointer for FILE_MEM
  76. UInt memUsage ()C; // get memory usage
  77. void cipher (Cipher *cipher); // adjust file cipher
  78. void cipherOffset (Int offset); // adjust file cipher offset
  79. void cipherOffsetClear( ) {cipherOffset(-pos());} // adjust file cipher offset so that "posCipher()==0 -> pos+cipher_offset==0 -> cipher_offset=-pos", this will result in encryption being always the same, regardless of current location
  80. #endif
  81. Bool is ( )C {return _type!=0 ;} // if file is opened
  82. Bool pos (Long pos); // set position, false on fail
  83. Long pos ( )C {return _pos ;} // get position
  84. Long size( )C {return _size ;} // get size
  85. Long left( )C {return _size-_pos ;} // get size left (number of bytes from current position to the end of the file)
  86. Bool end ( )C {return _pos>=_size ;} // if current position is at the end of the file
  87. Bool skip(Long n ) {return pos(_pos+n);} // skip 'n' bytes going forward
  88. Bool ok ( )C {return _ok ;} // check if no errors occurred during reading/writing. When a new file is opened this will be set to true by default, if any 'put' or 'get' call will fail then this will be set to false
  89. // put / get
  90. #if EE_PRIVATE
  91. Int putReturnSize(CPtr data, Int size); // write from 'data' memory to file, returns number of bytes written (can be less than 'size'), this method doesn't set 'ok' to false on error
  92. Int getReturnSize( Ptr data, Int size); // read from file to 'data' memory, returns number of bytes read (can be less than 'size'), this method doesn't set 'ok' to false on error
  93. Bool getFast ( Ptr data, Int size); // read from file to 'data' memory, false on fail without zeroing the memory like 'get' does, if error occurs then 'ok' will be set to false
  94. T1(TYPE) Bool getFast (TYPE &t ) {return getFast(&t, SIZE(TYPE) );} // read raw memory to 't' object , if error occurs then 'ok' will be set to false
  95. T1(TYPE) Bool getFastN (TYPE *t, Int n ) {return getFast( t, SIZE(TYPE)*n);} // read raw array to 'n' number of 't' objects , if error occurs then 'ok' will be set to false
  96. #endif
  97. Bool get( Ptr data, Int size); // read from file to 'data' memory, false on fail, if error occurs then 'ok' will be set to false
  98. Bool put(CPtr data, Int size); // write from 'data' memory to file, false on fail, if error occurs then 'ok' will be set to false
  99. T1(TYPE) Bool put (C TYPE &t ) {return put(&t, SIZE(TYPE) );} // write raw memory of 't' object
  100. T1(TYPE) Bool get ( TYPE &t ) {return get(&t, SIZE(TYPE) );} // read raw memory to 't' object
  101. T1(TYPE) Bool putN (C TYPE *t, Int n) {return put( t, SIZE(TYPE)*n);} // write raw array of 'n' number of 't' objects
  102. T1(TYPE) Bool getN ( TYPE *t, Int n) {return get( t, SIZE(TYPE)*n);} // read raw array to 'n' number of 't' objects
  103. T1(TYPE) File& operator<<(C TYPE &t ) { put(t); return T;} // write raw memory of 't' object
  104. T1(TYPE) File& operator>>( TYPE &t ) { get(t); return T;} // read raw memory to 't' object
  105. File& operator<<(C Str8 &s ) {return putStr(s); } // write string
  106. File& operator<<(C Str &s ) {return putStr(s); } // write string
  107. File& operator>>( Str8 &s ) {return getStr(s); } // read string
  108. File& operator>>( Str &s ) {return getStr(s); } // read string
  109. File& putBool ( Bool b) {T<<b; return T;} Bool getBool () {Bool b; T>>b; return b;} // write/read Bool
  110. File& putSByte ( SByte b) {T<<b; return T;} SByte getSByte () {SByte b; T>>b; return b;} // write/read SByte
  111. File& putByte ( Byte b) {T<<b; return T;} Byte getByte () {Byte b; T>>b; return b;} // write/read Byte
  112. File& putShort ( Short i) {T<<i; return T;} Short getShort () {Short i; T>>i; return i;} // write/read Short
  113. File& putUShort( UShort u) {T<<u; return T;} UShort getUShort() {UShort u; T>>u; return u;} // write/read UShort
  114. File& putInt ( Int i) {T<<i; return T;} Int getInt () {Int i; T>>i; return i;} // write/read Int
  115. File& putUInt ( UInt u) {T<<u; return T;} UInt getUInt () {UInt u; T>>u; return u;} // write/read UInt
  116. File& putLong ( Long i) {T<<i; return T;} Long getLong () {Long i; T>>i; return i;} // write/read Long
  117. File& putULong ( ULong u) {T<<u; return T;} ULong getULong () {ULong u; T>>u; return u;} // write/read ULong
  118. File& putFlt ( Flt f) {T<<f; return T;} Flt getFlt () {Flt f; T>>f; return f;} // write/read Float
  119. File& putDbl ( Dbl d) {T<<d; return T;} Dbl getDbl () {Dbl d; T>>d; return d;} // write/read Double
  120. File& putUID (C UID &i) {T<<i; return T;} UID getUID () {UID i; T>>i; return i;} // write/read UID
  121. #if EE_PRIVATE
  122. Short getBEShort (); // read BigEndian Short
  123. UShort getBEUShort(); // read BigEndian UShort
  124. Int getBEInt24 (); // read BigEndian Int24
  125. UInt getBEUInt24(); // read BigEndian UInt24
  126. Int getBEInt (); // read BigEndian Int
  127. UInt getBEUInt (); // read BigEndian UInt
  128. Long getBELong (); // read BigEndian Long
  129. ULong getBEULong (); // read BigEndian ULong
  130. #endif
  131. File& putStr(CChar8 *t); // write string
  132. File& putStr(CChar *t); // write string
  133. File& putStr(C Str8 &s); // write string
  134. File& putStr(C Str &s); // write string
  135. Str getStr( ); // read string, this method can be used to read previously written strings using all 'putStr' methods
  136. File& getStr( Str8 &s); // read string, this method can be used to read previously written strings using all 'putStr' methods
  137. File& getStr( Str &s); // read string, this method can be used to read previously written strings using all 'putStr' methods
  138. File& skipStr( ); // skip string, this method can be used to skip previously written strings using all 'putStr' methods, this method works by reading a string without storing it
  139. File& getStr(Char8 *t, Int t_elms); // read string into 't' array, 't_elms'=number of elements in the array, this method can be used to read previously written strings using all 'putStr' methods
  140. File& getStr(Char *t, Int t_elms); // read string into 't' array, 't_elms'=number of elements in the array, this method can be used to read previously written strings using all 'putStr' methods
  141. template<Int elms> File& getStr(Char8 (&t)[elms] ) {return getStr(t, elms);} // read string into 't' array, this method can be used to read previously written strings using all 'putStr' methods
  142. template<Int elms> File& getStr(Char (&t)[elms] ) {return getStr(t, elms);} // read string into 't' array, this method can be used to read previously written strings using all 'putStr' methods
  143. File& putAsset (CChar *name); // write asset location, this method is optimized for saving asset locations, typically assets are stored with file name based on UID, in such case this method can detect that and save the location using fewer bytes
  144. File& putAsset (C UID &id ); // write asset location, this method is optimized for saving asset locations, typically assets are stored with file name based on UID, in such case this method can detect that and save the location using fewer bytes
  145. File& getAsset ( Str &s ); // read asset location, this method can be used to read previously written asset location using all 'putAsset' methods, returns "" on fail
  146. Str getAsset ( ); // read asset location, this method can be used to read previously written asset location using all 'putAsset' methods, returns "" on fail
  147. UID getAssetID( ); // read asset location, this method can be used to read previously written asset location using all 'putAsset' methods, returns 'UIDZero' on fail
  148. // compress / decompress
  149. File& cmpIntV ( Int i); // compress any Int to variable number of bytes (1..5) depending on the value of the Int (values -64..63 will require only 1 byte, -8192..8191 will require 2 bytes, -1048576..1048575 will require 3 bytes, bigger values will require more bytes)
  150. File& decIntV ( Int &i); Int decIntV () {Int i; decIntV (i); return i;} // decompress any Int from variable number of bytes (1..5)
  151. File& cmpUIntV ( UInt u); // compress any UInt to variable number of bytes (1..5) depending on the value of the UInt (values 0..127 will require only 1 byte, 128..16383 will require 2 bytes, 16384..2097151 will require 3 bytes, bigger values will require more bytes)
  152. File& decUIntV ( UInt &u); UInt decUIntV () {UInt u; decUIntV (u); return u;} // decompress any UInt from variable number of bytes (1..5)
  153. File& cmpLongV ( Long l); // compress any Long to variable number of bytes (1..9) depending on the value of the Long (values -64..63 will require only 1 byte, -8192..8191 will require 2 bytes, -1048576..1048575 will require 3 bytes, bigger values will require more bytes)
  154. File& decLongV ( Long &l); Long decLongV () {Long l; decLongV (l); return l;} // decompress any Long from variable number of bytes (1..9)
  155. #if EE_PRIVATE
  156. File& cmpULongVMax(ULong u); // compress any ULong to 9 bytes, this is useful if data is expected to be loaded using 'decULongV' and the value was not yet known at the moment when writing data, in which case space could have been allocated using "cmpULongV(-1)" which would use max possible size, after that 'cmpULongVMax' can be called in place of 'cmpULongV' which will fill all bytes to correct value
  157. #endif
  158. File& cmpULongV ( ULong u); // compress any ULong to variable number of bytes (1..9) depending on the value of the ULong (values 0..127 will require only 1 byte, 128..16383 will require 2 bytes, 16384..2097151 will require 3 bytes, bigger values will require more bytes)
  159. File& decULongV ( ULong &u); ULong decULongV () {ULong u; decULongV (u); return u;} // decompress any ULong from variable number of bytes (1..9)
  160. File& cmpFlt3cm (C Flt &r); // compress Float in range -83,800 .. 83,800 to 3 bytes with precision of 0.01 (this covers range of -83..83 kilometers with 1 centimeter precision)
  161. File& decFlt3cm ( Flt &r); Flt decFlt3cm () {Flt r; decFlt3cm (r); return r;} // decompress Float in range -83,800 .. 83,800 from 3 bytes with precision of 0.01 (this covers range of -83..83 kilometers with 1 centimeter precision)
  162. File& cmpSatFlt1(C Flt &r); // compress Saturated Float in range 0..1 to 1 byte (having 256 possible values, and precision of ~0.00400)
  163. File& cmpSatFlt2(C Flt &r); // compress Saturated Float in range 0..1 to 2 bytes (having 65536 possible values, and precision of ~0.00001)
  164. File& decSatFlt1( Flt &r); Flt decSatFlt1() {Flt r; decSatFlt1(r); return r;} // decompress Saturated Float in range 0..1 from 1 byte
  165. File& decSatFlt2( Flt &r); Flt decSatFlt2() {Flt r; decSatFlt2(r); return r;} // decompress Saturated Float in range 0..1 from 2 bytes
  166. File& cmpAngle1 (C Flt &r); // compress Angle Float (0..PI2) to 1 byte (having 256 possible values, and precision of ~0.02000)
  167. File& cmpAngle2 (C Flt &r); // compress Angle Float (0..PI2) to 2 bytes (having 65536 possible values, and precision of ~0.00009)
  168. File& decAngle1 ( Flt &r); Flt decAngle1 () {Flt r; decAngle1 (r); return r;} // decompress Angle Float from 1 byte
  169. File& decAngle2 ( Flt &r); Flt decAngle2 () {Flt r; decAngle2 (r); return r;} // decompress Angle Float from 2 bytes
  170. File& cmpDir2 (C Vec &v); // compress Normalized Direction Vector to 2 bytes
  171. File& cmpDir3 (C Vec &v); // compress Normalized Direction Vector to 3 bytes
  172. File& decDir2 ( Vec &v); Vec decDir2 () {Vec v; decDir2 (v); return v;} // decompress Normalized Direction Vector from 2 bytes
  173. File& decDir3 ( Vec &v); Vec decDir3 () {Vec v; decDir3 (v); return v;} // decompress Normalized Direction Vector from 3 bytes
  174. File& cmpOrient2(C Matrix3 &m); // compress Orientation to 2 bytes (its scale is ignored, only rotation is saved)
  175. File& cmpOrient3(C Matrix3 &m); // compress Orientation to 3 bytes (its scale is ignored, only rotation is saved)
  176. File& cmpOrient4(C Matrix3 &m); // compress Orientation to 4 bytes (its scale is ignored, only rotation is saved)
  177. File& decOrient2( Matrix3 &m); Matrix3 decOrient2() {Matrix3 m; decOrient2(m); return m;} // decompress Orientation from 2 bytes
  178. File& decOrient3( Matrix3 &m); Matrix3 decOrient3() {Matrix3 m; decOrient3(m); return m;} // decompress Orientation from 3 bytes
  179. File& decOrient4( Matrix3 &m); Matrix3 decOrient4() {Matrix3 m; decOrient4(m); return m;} // decompress Orientation from 4 bytes
  180. // hashes
  181. UInt crc32 (Long max_size=-1); // get file CRC32 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  182. UInt xxHash32 (Long max_size=-1); // get file xxHash32 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  183. UInt xxHash64_32(Long max_size=-1); // get file xxHash64-32 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  184. ULong xxHash64 (Long max_size=-1); // get file xxHash64 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  185. UInt spookyHash32 (Long max_size=-1); // get file Spooky Hash32 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  186. ULong spookyHash64 (Long max_size=-1); // get file Spooky Hash64 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  187. UID spookyHash128 (Long max_size=-1); // get file Spooky Hash128 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  188. ULong metroHash64 (Long max_size=-1); // get file Metro Hash64 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  189. UID metroHash128 (Long max_size=-1); // get file Metro Hash128 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  190. UID md5 (Long max_size=-1); // get file MD5 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  191. SHA1::Hash sha1 (Long max_size=-1); // get file SHA1 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  192. SHA2::Hash sha2 (Long max_size=-1); // get file SHA2 from current position to the end, this reads the data and modifies file position, 'max_size'=number of bytes to process (-1=all remaining)
  193. // operations
  194. File& reset ( ); // reset all written data to the memory, this method affects only files which have been created using 'writeMem', this method sets file position and size to zero, but keeps the file in mode available for writing new data
  195. Bool flush ( ); // flush all buffered data to the disk , false on fail, this method doesn't set 'ok' to false on error
  196. Bool flushOK( ); // flush all buffered data to the disk and check if no other errors occurred before - "flush() && ok()", false on fail
  197. Bool equal (File &f , Long max_size=-1); // if has the same data as 'f' file (only data from current positions to the end are compared, and not from the start to the end), 'max_size'=number of bytes to compare (-1=all remaining)
  198. Bool copy (File &dest, Long max_size=-1); // copy file to 'dest', false on fail (only data from current position to the end are copied , and not from the start to the end), 'max_size'=number of bytes to copy (-1=all remaining)
  199. Bool size (Long size); // set file size , false on fail
  200. Bool trim ( ) {return size(pos());} // set file size to current position, false on fail
  201. #if EE_PRIVATE
  202. Bool copy (File &dest, DataCallback &callback, Long max_size=-1); // copy with callback file to 'dest', false on fail (only data from the current position to the end are copied, and not from the start to the end), 'max_size'=number of bytes to copy (-1=all remaining)
  203. Bool copyEncrypt(File &dest, Cipher &cipher , Long max_size=-1); // copy and encrypt file to 'dest', false on fail (only data from the current position to the end are copied, and not from the start to the end), 'max_size'=number of bytes to copy (-1=all remaining)
  204. Bool copyToAndDiscard(Mems<Byte> &dest); // copy remaining contents of this file to 'dest' and discard this file data after, false on fail
  205. Bool sync();
  206. Bool flushDo ();
  207. void clearBuf();
  208. Bool discardBuf(Bool flush);
  209. void limit(ULong &total_size, ULong &applied_offset, Long new_size); // temporarily limit current file to '0..new_size' size, current position will be mapped to 0
  210. void unlimit(ULong &total_size, ULong &applied_offset ); // unlimit previously limited file
  211. T2(TA, TB ) File& putMulti(C TA &a, C TB &b);
  212. T2(TA, TB ) File& getMulti( TA &a, TB &b);
  213. T3(TA, TB, TC ) File& putMulti(C TA &a, C TB &b, C TC &c);
  214. T3(TA, TB, TC ) File& getMulti( TA &a, TB &b, TC &c);
  215. T4(TA, TB, TC, TD ) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d);
  216. T4(TA, TB, TC, TD ) File& getMulti( TA &a, TB &b, TC &c, TD &d);
  217. T5(TA, TB, TC, TD, TE ) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d, C TE &e);
  218. T5(TA, TB, TC, TD, TE ) File& getMulti( TA &a, TB &b, TC &c, TD &d, TE &e);
  219. T6(TA, TB, TC, TD, TE, TF ) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d, C TE &e, C TF &f);
  220. T6(TA, TB, TC, TD, TE, TF ) File& getMulti( TA &a, TB &b, TC &c, TD &d, TE &e, TF &f);
  221. T7(TA, TB, TC, TD, TE, TF, TG ) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d, C TE &e, C TF &f, C TG &g);
  222. T7(TA, TB, TC, TD, TE, TF, TG ) File& getMulti( TA &a, TB &b, TC &c, TD &d, TE &e, TF &f, TG &g);
  223. T8(TA, TB, TC, TD, TE, TF, TG, TH ) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d, C TE &e, C TF &f, C TG &g, C TH &h);
  224. T8(TA, TB, TC, TD, TE, TF, TG, TH ) File& getMulti( TA &a, TB &b, TC &c, TD &d, TE &e, TF &f, TG &g, TH &h);
  225. T9(TA, TB, TC, TD, TE, TF, TG, TH, TI) File& putMulti(C TA &a, C TB &b, C TC &c, C TD &d, C TE &e, C TF &f, C TG &g, C TH &h, C TI &i);
  226. T9(TA, TB, TC, TD, TE, TF, TG, TH, TI) File& getMulti( TA &a, TB &b, TC &c, TD &d, TE &e, TF &f, TG &g, TH &h, TI &i);
  227. // fast but unsafe save/load methods for multiple elements at the same time
  228. T2(TYPE_FROM, TYPE_TO) File& putRange(C TYPE_FROM &from, C TYPE_TO &to) {put(&from, UIntPtr(&to)-UIntPtr(&from)+SIZE(to)); return T;}
  229. T2(TYPE_FROM, TYPE_TO) File& getRange( TYPE_FROM &from, TYPE_TO &to) {get(&from, UIntPtr(&to)-UIntPtr(&from)+SIZE(to)); return T;}
  230. #endif
  231. ~File() {del();}
  232. File();
  233. explicit File(C Str &name, const_mem_addr Cipher *cipher=null); // read Pak or stdio file, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  234. explicit File(C UID &id , const_mem_addr Cipher *cipher=null); // read Pak or stdio file, Exit on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  235. explicit File(C Str &name, C Pak &pak ); // read Pak file, Exit on fail
  236. explicit File(C PakFile &file, C Pak &pak ); // read Pak file, Exit on fail
  237. explicit File( CPtr data, Int size, const_mem_addr Cipher *cipher=null); // start reading from memory address (writing is not allowed in this mode), 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  238. #if !EE_PRIVATE
  239. private:
  240. #endif
  241. Byte _type;
  242. Bool _writable, _ok;
  243. FILE_PATH _path;
  244. Int _buf_pos, _buf_len, _buf_size, _cipher_offset;
  245. ULong _offset, _pos, _size;
  246. C Pak *_pak;
  247. Cipher *_cipher;
  248. Ptr _buf;
  249. union
  250. {
  251. Int _handle;
  252. Ptr _mem;
  253. Memb<Byte> _memb;
  254. };
  255. #if ANDROID
  256. Ptr _aasset;
  257. #endif
  258. NO_COPY_CONSTRUCTOR(File);
  259. #if EE_PRIVATE
  260. #define FILE_MEMB_UNION 1 // if 'File._memb' is stored in an union and can be accessed only when 'File._type' matches FILE_MEMB, union will give smaller 'File' class size, no union will allow to reuse previously allocated MEMB memory
  261. File& _decIntV ( Int &i); // deprecated - do not use
  262. Str _getStr2 ( ); // deprecated - do not use
  263. File& _getStr2 ( Str &s); // deprecated - do not use
  264. File& _getStr2 ( Str8 &s); // deprecated - do not use
  265. File& _putStr (C Str8 &s); File& _putStr(CChar8 *t) {return _putStr(Str8(t));} // write string, deprecated - do not use
  266. File& _putStr (C Str &s); File& _putStr(CChar *t) {return _putStr(Str (t));} // write string, deprecated - do not use
  267. Str _getStr ( ); // read string , deprecated - do not use
  268. File& _getStr ( Str8 &s); // read string , deprecated - do not use
  269. File& _getStr ( Str &s); // read string , deprecated - do not use
  270. Str8 _getStr8 ( ); // read string , deprecated - do not use, with 8-bit per character
  271. Str _getStr16( ); // read string , deprecated - do not use, with 16-bit per character
  272. File& _putAsset(CChar *t); // write asset location, deprecated - do not use
  273. Str _getAsset( ); // read asset location, deprecated - do not use
  274. File& _getStr2(Char8 *t, Int t_elms); // read string into 't' array, 't_elms'=number of elements in the array, deprecated - do not use
  275. File& _getStr2(Char *t, Int t_elms); // read string into 't' array, 't_elms'=number of elements in the array, deprecated - do not use
  276. template<Int elms> File& _getStr2(Char8 (&t)[elms] ) {return _getStr2(t, elms);} // read string into 't' array, , deprecated - do not use
  277. template<Int elms> File& _getStr2(Char (&t)[elms] ) {return _getStr2(t, elms);} // read string into 't' array, , deprecated - do not use
  278. File& _getStr(Char *t, Int t_elms); // read string into 't' array, 't_elms'=number of elements in the array, deprecated - do not use
  279. template<Int elms> File& _getStr(Char (&t)[elms] ) {return _getStr(t, elms);} // read string into 't' array, , deprecated - do not use
  280. #endif
  281. };
  282. /******************************************************************************/