File Text.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /******************************************************************************
  2. Use 'FileText' to handle text files management.
  3. /******************************************************************************/
  4. enum ENCODING : Byte // text encoding mode
  5. {
  6. ANSI , // 8-bit per character
  7. UTF_16 , // 16-bit per character
  8. UTF_8 , // variable-bit per character
  9. UTF_8_NAKED, // variable-bit per character with no Byte Order Mark (BOM)
  10. };
  11. enum INDENT : Byte // indentation mode
  12. {
  13. INDENT_NONE , // disabled
  14. INDENT_TABS , // use tabs
  15. INDENT_SPACES, // use spaces
  16. };
  17. /******************************************************************************/
  18. struct FileText
  19. {
  20. Int depth ; // depth level which affects indentation when using 'startLine' and 'putLine' methods
  21. Bool fix_new_line; // if replace "\n" characters with "\r\n" which are required in Windows operating system, default=true
  22. INDENT indent ; // indentation mode, default=INDENT_TABS
  23. // manage
  24. FileText& del ( ); // delete manually
  25. Bool read (C Str &name , const_mem_addr Cipher *cipher=null); // read from file , 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  26. Bool read (C UID &id , const_mem_addr Cipher *cipher=null); // read from file , 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  27. Bool read (C Str &name, Pak &pak ); // read from Pak file
  28. Bool read (C UID &id , Pak &pak ); // read from Pak file
  29. FileText& readMem( CPtr data, Int size, Int encoding= -1, const_mem_addr Cipher *cipher=null); // read from memory , 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed), 'encoding'=encoding of the memory (-1=autodetect)
  30. FileText& writeMem( ENCODING encoding=UTF_8, const_mem_addr Cipher *cipher=null); // write to memory , 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  31. Bool write (C Str &name, ENCODING encoding=UTF_8, const_mem_addr Cipher *cipher=null); // write to stdio file, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  32. Bool append (C Str &name, ENCODING encoding=UTF_8, const_mem_addr Cipher *cipher=null); // append stdio file, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  33. // get
  34. Bool end ()C {return _f.end ();} // if current position is at the end of the file
  35. Long size ()C {return _f.size();} // get file size
  36. Long pos ()C {return _f.pos ();} // get file position
  37. ENCODING encoding()C {return _code ;} // get file encoding
  38. // read
  39. FileText& skipLine( ); // skip text until end of line found
  40. FileText& fullLine(Str &s); Str fullLine() {Str s; fullLine(s); return s;} // read text until end of line found, includes starting white chars (spaces and tabs)
  41. FileText& getLine(Str &s); Str getLine() {Str s; getLine(s); return s;} // read text until end of line found, skips starting white chars (spaces and tabs)
  42. FileText& getLine(Str8 &s); // read text until end of line found, skips starting white chars (spaces and tabs)
  43. FileText& getAll (Str &s); Str getAll () {Str s; getAll (s); return s;} // read text until end of file
  44. Char getChar( ); // read a single character
  45. // write
  46. FileText& startLine( ); // write indentation according to 'indent'
  47. FileText& endLine( ); // write end of line marker
  48. FileText& putChar( Char8 c ); // write a single character
  49. FileText& putChar( Char c ); // write a single character
  50. FileText& putText(C Str &text); // write text
  51. FileText& putLine(C Str &text); // write indentation, text and end of line marker
  52. // operations
  53. FileText& rewind (); // reset to starting position, this can be useful when wanting to read previously written data
  54. Bool ok ()C {return _f.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 read/write call will fail then this will be set to false
  55. Bool flush () {return _f.flush ();} // flush all buffered data to the disk, false on fail
  56. Bool flushOK() {return _f.flushOK();} // flush all buffered data to the disk and check if no other errors occurred before - "flush() && ok()", false on fail
  57. Bool copy (File &dest); // copy the entire contents (from start to end) of this FileText into 'dest' file, including byte order mark (BOM) if present, false on fail
  58. Char posInfo(Long pos, VecI2 &col_line); // get character, its column and line indexes at specified 'pos' file offset (in raw bytes), this method will start reading each character in the file from the start, until 'pos' file offset
  59. #if EE_PRIVATE
  60. void zero();
  61. #endif
  62. FileText();
  63. #if !EE_PRIVATE
  64. private:
  65. #endif
  66. ENCODING _code;
  67. File _f;
  68. NO_COPY_CONSTRUCTOR(FileText);
  69. };
  70. /******************************************************************************/
  71. #if EE_PRIVATE
  72. struct FileTextEx : FileText
  73. {
  74. Str text;
  75. void get(Int &i); // get Int
  76. void get(Flt &f); // get Flt
  77. void get(Dbl &d); // get Dbl
  78. void get(Vec2 &v); // get Vec2
  79. void get(Vec &v); // get Vec
  80. void get(Vec4 &v); // get Vec4
  81. void get(VecI2 &v); // get VecI2
  82. void get(VecI &v); // get VecI
  83. void get(VecI4 &v); // get VecI4
  84. void get(VecB4 &v); // get VecB4
  85. Bool getBool (); // get Bool
  86. Int getInt (); // get Int
  87. UInt getUInt (); // get UInt
  88. Flt getFlt (); // get Flt
  89. Dbl getDbl (); // get Dbl
  90. Vec2 getVec2 (); // get Vec2
  91. Vec getVec (); // get Vec
  92. Vec4 getVec4 (); // get Vec4
  93. VecI2 getVecI2(); // get VecI2
  94. VecI getVecI (); // get VecI
  95. VecI4 getVecI4(); // get VecI4
  96. VecB4 getVecB4(); // get VecB4
  97. C Str& getWord (); // get word, skips starting spaces and empty lines, stops on one of the following '\n' ' ', returns 'text'
  98. C Str& getName (); // get name, skips everything until name surrounded with "" is encountered, returns 'text'
  99. Bool cur (C Str &name)C {return text==name;} // if last read data is equal to 'name'
  100. Bool getIn ( ); // try to get inside next '{' level
  101. void getOut( ); // get out from current '}' level
  102. Bool level ( ); // if still in current level
  103. };
  104. #endif
  105. /******************************************************************************/