txtstrm.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: txtstrm.h
  3. // Purpose: interface of wxTextInputStream
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxTextInputStream
  9. This class provides functions that reads text data using an input stream,
  10. allowing you to read text, floats, and integers.
  11. The wxTextInputStream correctly reads text files (or streams) in DOS,
  12. Macintosh and Unix formats and reports a single newline char as a line
  13. ending.
  14. wxTextInputStream::operator>>() is overloaded and you can use this class
  15. like a standard C++ iostream. Note, however, that the arguments are the
  16. fixed size types wxUint32, wxInt32 etc and on a typical 32-bit computer,
  17. none of these match to the "long" type (wxInt32 is defined as int on 32-bit
  18. architectures) so that you cannot use long. To avoid problems (here and
  19. elsewhere), make use of wxInt32, wxUint32 and similar types.
  20. If you're scanning through a file using wxTextInputStream, you should check
  21. for @c EOF @b before reading the next item (word / number), because
  22. otherwise the last item may get lost. You should however be prepared to
  23. receive an empty item (empty string / zero number) at the end of file,
  24. especially on Windows systems. This is unavoidable because most (but not
  25. all) files end with whitespace (i.e. usually a newline).
  26. For example:
  27. @code
  28. wxFileInputStream input( "mytext.txt" );
  29. wxTextInputStream text( input );
  30. wxUint8 i1;
  31. float f2;
  32. wxString line;
  33. text >> i1; // read a 8 bit integer.
  34. text >> i1 >> f2; // read a 8 bit integer followed by float.
  35. text >> line; // read a text line
  36. @endcode
  37. @library{wxbase}
  38. @category{streams}
  39. @see wxTextOutputStream
  40. */
  41. class wxTextInputStream
  42. {
  43. public:
  44. /**
  45. Constructs a text stream associated to the given input stream.
  46. @param stream
  47. The underlying input stream.
  48. @param sep
  49. The initial string separator characters.
  50. @param conv
  51. <b>In Unicode build only:</b> The encoding converter used to
  52. convert the bytes in the underlying input stream to characters.
  53. */
  54. wxTextInputStream(wxInputStream& stream, const wxString& sep = " \t",
  55. const wxMBConv& conv = wxConvAuto());
  56. /**
  57. Destructor.
  58. */
  59. ~wxTextInputStream();
  60. /**
  61. Returns a pointer to the underlying input stream object.
  62. @since 2.9.2
  63. */
  64. const wxInputStream& GetInputStream() const;
  65. /**
  66. Reads a character, returns 0 if there are no more characters in the
  67. stream.
  68. */
  69. wxChar GetChar();
  70. /**
  71. Reads a unsigned 16 bit integer from the stream.
  72. See Read8() for the description of the @a base parameter.
  73. */
  74. wxUint16 Read16(int base = 10);
  75. /**
  76. Reads a signed 16 bit integer from the stream.
  77. See Read8() for the description of the @a base parameter.
  78. */
  79. wxInt16 Read16S(int base = 10);
  80. /**
  81. Reads a 32 bit unsigned integer from the stream.
  82. See Read8() for the description of the @a base parameter.
  83. */
  84. wxUint32 Read32(int base = 10);
  85. /**
  86. Reads a 32 bit signed integer from the stream.
  87. See Read8() for the description of the @a base parameter.
  88. */
  89. wxInt32 Read32S(int base = 10);
  90. /**
  91. Reads a single unsigned byte from the stream, given in base @a base.
  92. The value of @a base must be comprised between 2 and 36, inclusive, or
  93. be a special value 0 which means that the usual rules of C numbers are
  94. applied: if the number starts with @c 0x it is considered to be in base
  95. 16, if it starts with 0 - in base 8 and in base 10 otherwise. Note that
  96. you may not want to specify the base 0 if you are parsing the numbers
  97. which may have leading zeroes as they can yield unexpected (to the user
  98. not familiar with C) results.
  99. */
  100. wxUint8 Read8(int base = 10);
  101. /**
  102. Reads a single signed byte from the stream.
  103. See Read8() for the description of the @a base parameter.
  104. */
  105. wxInt8 Read8S(int base = 10);
  106. /**
  107. Reads a double (IEEE encoded) from the stream.
  108. */
  109. double ReadDouble();
  110. /**
  111. Reads a line from the input stream and returns it (without the end of
  112. line character).
  113. */
  114. wxString ReadLine();
  115. /**
  116. @deprecated Use ReadLine() or ReadWord() instead.
  117. Same as ReadLine().
  118. */
  119. wxString ReadString();
  120. /**
  121. Reads a word (a sequence of characters until the next separator) from
  122. the input stream.
  123. @see SetStringSeparators()
  124. */
  125. wxString ReadWord();
  126. /**
  127. Sets the characters which are used to define the word boundaries in
  128. ReadWord().
  129. The default separators are the @c space and @c TAB characters.
  130. */
  131. void SetStringSeparators(const wxString& sep);
  132. };
  133. /**
  134. Specifies the end-of-line characters to use with wxTextOutputStream.
  135. */
  136. typedef enum
  137. {
  138. /**
  139. Specifies wxTextOutputStream to use the native end-of-line characters.
  140. */
  141. wxEOL_NATIVE,
  142. /**
  143. Specifies wxTextOutputStream to use Unix end-of-line characters.
  144. */
  145. wxEOL_UNIX,
  146. /**
  147. Specifies wxTextOutputStream to use Mac end-of-line characters.
  148. */
  149. wxEOL_MAC,
  150. /**
  151. Specifies wxTextOutputStream to use DOS end-of-line characters.
  152. */
  153. wxEOL_DOS
  154. } wxEOL;
  155. /**
  156. @class wxTextOutputStream
  157. This class provides functions that write text data using an output stream,
  158. allowing you to write text, floats, and integers.
  159. You can also simulate the C++ @c std::cout class:
  160. @code
  161. wxFFileOutputStream output( stderr );
  162. wxTextOutputStream cout( output );
  163. cout << "This is a text line" << endl;
  164. cout << 1234;
  165. cout << 1.23456;
  166. @endcode
  167. The wxTextOutputStream writes text files (or streams) on DOS, Macintosh and
  168. Unix in their native formats (concerning the line ending).
  169. @library{wxbase}
  170. @category{streams}
  171. @see wxTextInputStream
  172. */
  173. class wxTextOutputStream
  174. {
  175. public:
  176. /**
  177. Constructs a text stream object associated to the given output stream.
  178. @param stream
  179. The output stream.
  180. @param mode
  181. The end-of-line mode. One of ::wxEOL_NATIVE, ::wxEOL_DOS,
  182. ::wxEOL_MAC and ::wxEOL_UNIX.
  183. @param conv
  184. <b>In Unicode build only:</b> The object used to convert
  185. Unicode text into ASCII characters written to the output stream.
  186. */
  187. wxTextOutputStream(wxOutputStream& stream,
  188. wxEOL mode = wxEOL_NATIVE,
  189. const wxMBConv& conv = wxConvAuto());
  190. /**
  191. Destroys the wxTextOutputStream object.
  192. Also calls Flush().
  193. */
  194. virtual ~wxTextOutputStream();
  195. /**
  196. Flushes the stream.
  197. This method should be called when using stateful encodings (currently
  198. the only example of such encoding in wxWidgets is wxMBConvUTF7) to
  199. write the end of the encoded data to the stream.
  200. @since 2.9.0
  201. */
  202. void Flush();
  203. /**
  204. Returns a pointer to the underlying output stream object.
  205. @since 2.9.2
  206. */
  207. const wxOutputStream& GetOutputStream() const;
  208. /**
  209. Returns the end-of-line mode. One of ::wxEOL_DOS, ::wxEOL_MAC and
  210. ::wxEOL_UNIX.
  211. */
  212. wxEOL GetMode();
  213. /**
  214. Writes a character to the stream.
  215. */
  216. wxTextOutputStream& PutChar(wxChar c);
  217. /**
  218. Set the end-of-line mode. One of ::wxEOL_NATIVE, ::wxEOL_DOS,
  219. ::wxEOL_MAC and ::wxEOL_UNIX.
  220. */
  221. void SetMode(wxEOL mode = wxEOL_NATIVE);
  222. /**
  223. Writes the 16 bit integer @a i16 to the stream.
  224. */
  225. void Write16(wxUint16 i16);
  226. /**
  227. Writes the 32 bit integer @a i32 to the stream.
  228. */
  229. void Write32(wxUint32 i32);
  230. /**
  231. Writes the single byte @a i8 to the stream.
  232. */
  233. void Write8(wxUint8 i8);
  234. /**
  235. Writes the double @a f to the stream using the IEEE format.
  236. */
  237. virtual void WriteDouble(double f);
  238. /**
  239. Writes @a string as a line. Depending on the end-of-line mode the end of
  240. line ('\\n') characters in the string are converted to the correct line
  241. ending terminator.
  242. */
  243. virtual void WriteString(const wxString& string);
  244. };