textfile.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: textfile.h
  3. // Purpose: interface of wxTextFile
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. // TODO: document wxTextBuffer
  8. /**
  9. The line termination type.
  10. */
  11. enum wxTextFileType
  12. {
  13. wxTextFileType_None, //!< incomplete (the last line of the file only)
  14. wxTextFileType_Unix, //!< line is terminated with 'LF' = 0xA = 10 = '\\n'
  15. wxTextFileType_Dos, //!< line is terminated with 'CR' 'LF'
  16. wxTextFileType_Mac, //!< line is terminated with 'CR' = 0xD = 13 = '\\r'
  17. wxTextFileType_Os2 //!< line is terminated with 'CR' 'LF'
  18. };
  19. /**
  20. @class wxTextFile
  21. The wxTextFile is a simple class which allows to work with text files on line by
  22. line basis. It also understands the differences in line termination characters
  23. under different platforms and will not do anything bad to files with "non
  24. native" line termination sequences - in fact, it can be also used to modify the
  25. text files and change the line termination characters from one type (say DOS) to
  26. another (say Unix).
  27. One word of warning: the class is not at all optimized for big files and thus
  28. it will load the file entirely into memory when opened. Of course, you should
  29. not work in this way with large files (as an estimation, anything over 1 Megabyte
  30. is surely too big for this class). On the other hand, it is not a serious
  31. limitation for small files like configuration files or program sources
  32. which are well handled by wxTextFile.
  33. The typical things you may do with wxTextFile in order are:
  34. - Create and open it: this is done with either wxTextFile::Create or wxTextFile::Open
  35. function which opens the file (name may be specified either as the argument to
  36. these functions or in the constructor), reads its contents in memory (in the
  37. case of wxTextFile::Open()) and closes it.
  38. - Work with the lines in the file: this may be done either with "direct
  39. access" functions like wxTextFile::GetLineCount and wxTextFile::GetLine
  40. (@e operator[] does exactly the same but looks more like array addressing)
  41. or with "sequential access" functions which include wxTextFile::GetFirstLine,
  42. wxTextFile::GetNextLine and also wxTextFile::GetLastLine, wxTextFile::GetPrevLine.
  43. For the sequential access functions the current line number is maintained: it is
  44. returned by wxTextFile::GetCurrentLine and may be changed with wxTextFile::GoToLine.
  45. - Add/remove lines to the file: wxTextFile::AddLine and wxTextFile::InsertLine
  46. add new lines while wxTextFile::RemoveLine deletes the existing ones.
  47. wxTextFile::Clear resets the file to empty.
  48. - Save your changes: notice that the changes you make to the file will @b not be
  49. saved automatically; calling wxTextFile::Close or doing nothing discards them!
  50. To save the changes you must explicitly call wxTextFile::Write - here, you may
  51. also change the line termination type if you wish.
  52. @library{wxbase}
  53. @category{file}
  54. @see wxFile
  55. */
  56. class wxTextFile
  57. {
  58. public:
  59. /**
  60. Default type for current platform determined at compile time.
  61. */
  62. static const wxTextFileType typeDefault;
  63. /**
  64. Default constructor, use Create() or Open() with a file name parameter to
  65. initialize the object.
  66. */
  67. wxTextFile();
  68. /**
  69. Constructor does not load the file into memory, use Open() to do it.
  70. */
  71. wxTextFile(const wxString& strFile);
  72. /**
  73. Destructor does nothing.
  74. */
  75. virtual ~wxTextFile();
  76. /**
  77. Adds a line to the end of file.
  78. */
  79. void AddLine(const wxString& str, wxTextFileType type = typeDefault);
  80. /**
  81. Delete all lines from the file, set current line number to 0.
  82. */
  83. void Clear();
  84. /**
  85. Closes the file and frees memory, @b "losing all changes".
  86. Use Write() if you want to save them.
  87. */
  88. bool Close();
  89. /**
  90. Creates the file with the name which was given in the
  91. wxTextFile(const wxString&) constructor.
  92. The array of file lines is initially empty.
  93. It will fail if the file already exists, Open() should be used in this case.
  94. */
  95. bool Create();
  96. /**
  97. Creates the file with the given name.
  98. The array of file lines is initially empty.
  99. It will fail if the file already exists, Open() should be used in this case.
  100. */
  101. bool Create(const wxString& strFile);
  102. /**
  103. Returns @true if the current line is the last one.
  104. */
  105. bool Eof() const;
  106. /**
  107. Return @true if file exists - the name of the file should have been specified
  108. in the constructor before calling Exists().
  109. */
  110. bool Exists() const;
  111. /**
  112. Returns the current line: it has meaning only when you're using
  113. GetFirstLine()/GetNextLine() functions, it doesn't get updated when
  114. you're using "direct access" functions like GetLine().
  115. GetFirstLine() and GetLastLine() also change the value of the current line,
  116. as well as GoToLine().
  117. */
  118. size_t GetCurrentLine() const;
  119. /**
  120. Get the line termination string corresponding to given constant.
  121. @e typeDefault is the value defined during the compilation and corresponds
  122. to the native format of the platform, i.e. it will be @c wxTextFileType_Dos
  123. under Windows and @c wxTextFileType_Unix under Unix (including Mac OS
  124. X, the value @c wxTextFileType_Mac was only used for classic Mac OS
  125. versions).
  126. */
  127. static const wxChar* GetEOL(wxTextFileType type = typeDefault);
  128. /**
  129. This method together with GetNextLine() allows more "iterator-like"
  130. traversal of the list of lines, i.e. you may write something like:
  131. @code
  132. wxTextFile file;
  133. ...
  134. for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
  135. {
  136. // do something with the current line in str
  137. }
  138. // do something with the last line in str
  139. @endcode
  140. */
  141. wxString& GetFirstLine();
  142. /**
  143. Gets the last line of the file.
  144. Together with GetPrevLine() it allows to enumerate the lines
  145. in the file from the end to the beginning like this:
  146. @code
  147. wxTextFile file;
  148. ...
  149. for ( str = file.GetLastLine();
  150. file.GetCurrentLine() > 0;
  151. str = file.GetPrevLine() )
  152. {
  153. // do something with the current line in str
  154. }
  155. // do something with the first line in str
  156. @endcode
  157. */
  158. wxString& GetLastLine();
  159. /**
  160. Retrieves the line number @a n from the file.
  161. The returned line may be modified when non-const method is used but you
  162. shouldn't add line terminator at the end -- this will be done by
  163. wxTextFile itself.
  164. */
  165. //@{
  166. wxString& GetLine(size_t n);
  167. const wxString& GetLine(size_t n) const;
  168. //@}
  169. /**
  170. Get the number of lines in the file.
  171. */
  172. size_t GetLineCount() const;
  173. /**
  174. Get the type of the line (see also wxTextFile::GetEOL).
  175. */
  176. wxTextFileType GetLineType(size_t n) const;
  177. /**
  178. Get the name of the file.
  179. */
  180. const wxString& GetName() const;
  181. /**
  182. Gets the next line (see GetFirstLine() for the example).
  183. */
  184. wxString& GetNextLine();
  185. /**
  186. Gets the previous line in the file.
  187. */
  188. wxString& GetPrevLine();
  189. /**
  190. Changes the value returned by GetCurrentLine() and used by GetFirstLine()
  191. and GetNextLine().
  192. */
  193. void GoToLine(size_t n);
  194. /**
  195. Guess the type of file (which is supposed to be opened).
  196. If sufficiently many lines of the file are in DOS/Unix/Mac format,
  197. the corresponding value will be returned.
  198. If the detection mechanism fails @c wxTextFileType_None is returned.
  199. */
  200. wxTextFileType GuessType() const;
  201. /**
  202. Insert a line before the line number @a n.
  203. */
  204. void InsertLine(const wxString& str, size_t n,
  205. wxTextFileType type = typeDefault);
  206. /**
  207. Returns @true if the file is currently opened.
  208. */
  209. bool IsOpened() const;
  210. /**
  211. Opens the file with the name which was given in the wxTextFile(const wxString&)
  212. constructor and also loads file in memory on success.
  213. It will fail if the file does not exist, Create() should be used in this case.
  214. The @a conv argument is only meaningful in Unicode build of wxWidgets when
  215. it is used to convert the file to wide character representation.
  216. */
  217. bool Open(const wxMBConv& conv = wxConvAuto());
  218. /**
  219. Opens the file with the given name and also loads file in memory on success.
  220. It will fail if the file does not exist, Create() should be used in this case.
  221. The @a conv argument is only meaningful in Unicode build of wxWidgets when
  222. it is used to convert the file to wide character representation.
  223. */
  224. bool Open(const wxString& strFile, const wxMBConv& conv = wxConvAuto());
  225. /**
  226. Delete line number @a n from the file.
  227. */
  228. void RemoveLine(size_t n);
  229. /**
  230. Change the file on disk.
  231. The @a typeNew parameter allows you to change the file format
  232. (default argument means "don't change type") and may be used to convert,
  233. for example, DOS files to Unix.
  234. The @a conv argument is only meaningful in Unicode build of wxWidgets when
  235. it is used to convert all lines to multibyte representation before writing
  236. them to physical file.
  237. @return
  238. @true if operation succeeded, @false if it failed.
  239. */
  240. bool Write(wxTextFileType typeNew = wxTextFileType_None,
  241. const wxMBConv& conv = wxConvAuto());
  242. /**
  243. The same as GetLine().
  244. */
  245. wxString& operator[](size_t n) const;
  246. };