datstrm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: datstrm.h
  3. // Purpose: interface of wxDataInputStream and wxDataOutputStream
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxDataOutputStream
  9. This class provides functions that write binary data types in a portable
  10. way.
  11. Data can be written in either big-endian or little-endian format,
  12. little-endian being the default on all architectures but BigEndianOrdered()
  13. can be used to change this. The default format for the floating point types
  14. is 80 bit "extended precision" unless @c wxUSE_APPLE_IEEE was turned off
  15. during the library compilation, in which case extended precision is not
  16. available at all. You can call UseBasicPrecisions() to change this and
  17. use the standard IEEE 754 32 bit single precision format for floats and
  18. standard 64 bit double precision format for doubles. This is recommended
  19. for the new code for better interoperability with other software that
  20. typically uses standard IEEE 754 formats for its data, the use of extended
  21. precision by default is solely due to backwards compatibility.
  22. If you want to write data to text files (or streams) use wxTextOutputStream
  23. instead.
  24. The "<<" operator is overloaded and you can use this class like a standard
  25. C++ iostream. See wxDataInputStream for its usage and caveats.
  26. @library{wxbase}
  27. @category{streams}
  28. @see wxDataInputStream
  29. */
  30. class wxDataOutputStream
  31. {
  32. public:
  33. /**
  34. Constructs a datastream object from an output stream.
  35. Only write methods will be available.
  36. Note that the @a conv parameter is only available in Unicode builds of wxWidgets.
  37. @param stream
  38. The output stream.
  39. @param conv
  40. Charset conversion object used to encoding Unicode strings
  41. before writing them to the stream in Unicode mode (see
  42. WriteString() for a detailed description). Note that you must not
  43. destroy @a conv before you destroy this wxDataOutputStream
  44. instance! It is recommended to use the default value (UTF-8).
  45. */
  46. wxDataOutputStream(wxOutputStream& stream,
  47. const wxMBConv& conv = wxConvUTF8);
  48. /**
  49. Destroys the wxDataOutputStream object.
  50. */
  51. ~wxDataOutputStream();
  52. /**
  53. If @a be_order is @true, all data will be written in big-endian order,
  54. e.g. for reading on a Sparc or from Java-Streams (which always use
  55. big-endian order), otherwise data will be written in little-endian
  56. order.
  57. */
  58. void BigEndianOrdered(bool be_order);
  59. /**
  60. Returns the current text conversion class used for
  61. writing strings.
  62. */
  63. wxMBConv *GetConv() const;
  64. /**
  65. Sets the text conversion class used for writing strings.
  66. */
  67. void SetConv( const wxMBConv &conv );
  68. /**
  69. Disables the use of extended precision format for floating point
  70. numbers.
  71. This method disables the use of 80 bit extended precision format for
  72. the @c float and @c double values written to the stream, which is used
  73. by default (unless @c wxUSE_APPLE_IEEE was set to @c 0 when building
  74. the library, in which case the extended format support is not available
  75. at all and this function does nothing).
  76. After calling it, @c float values will be written out in one of IEEE
  77. 754 "basic formats", i.e. 32 bit single precision format for floats and
  78. 64 bit double precision format for doubles.
  79. @since 2.9.5
  80. */
  81. void UseBasicPrecisions();
  82. /**
  83. Explicitly request the use of extended precision for floating point
  84. numbers.
  85. This function allows the application code to explicitly request the use
  86. of 80 bit extended precision format for the floating point numbers.
  87. This is the case by default but using this function explicitly ensures
  88. that the compilation of code relying on producing the output stream
  89. using extended precision would fail when using a version of wxWidgets
  90. compiled with @c wxUSE_APPLE_IEEE==0 and so not supporting this format
  91. at all.
  92. @since 2.9.5
  93. */
  94. void UseExtendedPrecision();
  95. /**
  96. Writes the single byte @a i8 to the stream.
  97. */
  98. void Write8(wxUint8 i8);
  99. /**
  100. Writes an array of bytes to the stream. The number of bytes to write is
  101. specified with the @a size variable.
  102. */
  103. void Write8(const wxUint8* buffer, size_t size);
  104. /**
  105. Writes the 16 bit unsigned integer @a i16 to the stream.
  106. */
  107. void Write16(wxUint16 i16);
  108. /**
  109. Writes an array of 16 bit unsigned integer to the stream. The number of
  110. 16 bit unsigned integer to write is specified with the @a size variable.
  111. */
  112. void Write16(const wxUint16* buffer, size_t size);
  113. /**
  114. Writes the 32 bit unsigned integer @a i32 to the stream.
  115. */
  116. void Write32(wxUint32 i32);
  117. /**
  118. Writes an array of 32 bit unsigned integer to the stream. The number of
  119. 32 bit unsigned integer to write is specified with the @a size variable.
  120. */
  121. void Write32(const wxUint32* buffer, size_t size);
  122. /**
  123. Writes the 64 bit unsigned integer @a i64 to the stream.
  124. */
  125. void Write64(wxUint64 i64);
  126. /**
  127. Writes an array of 64 bit unsigned integer to the stream. The number of
  128. 64 bit unsigned integer to write is specified with the @a size variable.
  129. */
  130. void Write64(const wxUint64* buffer, size_t size);
  131. /**
  132. Writes the float @a f to the stream.
  133. If UseBasicPrecisions() had been called, the value is written out using
  134. the standard IEEE 754 32 bit single precision format. Otherwise, this
  135. method uses the same format as WriteDouble(), i.e. 80 bit extended
  136. precision representation.
  137. @since 2.9.5
  138. */
  139. void WriteFloat(float f);
  140. /**
  141. Writes an array of float to the stream. The number of floats to write is
  142. specified by the @a size variable.
  143. @since 2.9.5
  144. */
  145. void WriteFloat(const float* buffer, size_t size);
  146. /**
  147. Writes the double @a d to the stream.
  148. The output format is either 80 bit extended precision or, if
  149. UseBasicPrecisions() had been called, standard IEEE 754 64 bit double
  150. precision.
  151. */
  152. void WriteDouble(double d);
  153. /**
  154. Writes an array of double to the stream. The number of doubles to write is
  155. specified by the @a size variable.
  156. */
  157. void WriteDouble(const double* buffer, size_t size);
  158. /**
  159. Writes @a string to the stream. Actually, this method writes the size
  160. of the string before writing @a string itself.
  161. In ANSI build of wxWidgets, the string is written to the stream in
  162. exactly same way it is represented in memory. In Unicode build,
  163. however, the string is first converted to multibyte representation with
  164. @e conv object passed to stream's constructor (consequently, ANSI
  165. applications can read data written by Unicode application, as long as
  166. they agree on encoding) and this representation is written to the
  167. stream. UTF-8 is used by default.
  168. */
  169. void WriteString(const wxString& string);
  170. };
  171. /**
  172. @class wxDataInputStream
  173. This class provides functions that read binary data types in a portable
  174. way.
  175. Please see wxDataOutputStream for the discussion of the format expected by
  176. this stream on input, notably for the floating point values.
  177. If you want to read data from text files (or streams) use wxTextInputStream
  178. instead.
  179. The ">>" operator is overloaded and you can use this class like a standard
  180. C++ iostream. Note, however, that the arguments are the fixed size types
  181. wxUint32, wxInt32 etc and on a typical 32-bit computer, none of these match
  182. to the "long" type (wxInt32 is defined as signed int on 32-bit
  183. architectures) so that you cannot use long. To avoid problems (here and
  184. elsewhere), make use of the wxInt32, wxUint32, etc types.
  185. For example:
  186. @code
  187. wxFileInputStream input( "mytext.dat" );
  188. wxDataInputStream store( input );
  189. wxUint8 i1;
  190. float f2;
  191. wxString line;
  192. store >> i1; // read a 8 bit integer.
  193. store >> i1 >> f2; // read a 8 bit integer followed by float.
  194. store >> line; // read a text line
  195. @endcode
  196. @library{wxbase}
  197. @category{streams}
  198. @see wxDataOutputStream
  199. */
  200. class wxDataInputStream
  201. {
  202. public:
  203. /**
  204. Constructs a datastream object from an input stream.
  205. Only read methods will be available.
  206. Note that the @a conv parameter is only available in Unicode builds of wxWidgets.
  207. @param stream
  208. The input stream.
  209. @param conv
  210. Charset conversion object used to decode strings in Unicode
  211. mode (see ReadString() for a detailed description). Note that you
  212. must not destroy @a conv before you destroy this wxDataInputStream
  213. instance!
  214. */
  215. wxDataInputStream(wxInputStream& stream,
  216. const wxMBConv& conv = wxConvUTF8 );
  217. /**
  218. Destroys the wxDataInputStream object.
  219. */
  220. ~wxDataInputStream();
  221. /**
  222. If @a be_order is @true, all data will be read in big-endian order,
  223. such as written by programs on a big endian architecture (e.g. Sparc)
  224. or written by Java-Streams (which always use big-endian order).
  225. */
  226. void BigEndianOrdered(bool be_order);
  227. /**
  228. Returns the current text conversion class used for
  229. reading strings.
  230. */
  231. wxMBConv *GetConv() const;
  232. /**
  233. Reads a single byte from the stream.
  234. */
  235. wxUint8 Read8();
  236. /**
  237. Reads bytes from the stream in a specified buffer. The number of bytes
  238. to read is specified by the @a size variable.
  239. */
  240. void Read8(wxUint8* buffer, size_t size);
  241. /**
  242. Reads a 16 bit unsigned integer from the stream.
  243. */
  244. wxUint16 Read16();
  245. /**
  246. Reads 16 bit unsigned integers from the stream in a specified buffer.
  247. The number of 16 bit unsigned integers to read is specified by the
  248. @a size variable.
  249. */
  250. void Read16(wxUint16* buffer, size_t size);
  251. /**
  252. Reads a 32 bit unsigned integer from the stream.
  253. */
  254. wxUint32 Read32();
  255. /**
  256. Reads 32 bit unsigned integers from the stream in a specified buffer.
  257. The number of 32 bit unsigned integers to read is specified by the
  258. @a size variable.
  259. */
  260. void Read32(wxUint32* buffer, size_t size);
  261. /**
  262. Reads a 64 bit unsigned integer from the stream.
  263. */
  264. wxUint64 Read64();
  265. /**
  266. Reads 64 bit unsigned integers from the stream in a specified buffer.
  267. The number of 64 bit unsigned integers to read is specified by the
  268. @a size variable.
  269. */
  270. void Read64(wxUint64* buffer, size_t size);
  271. /**
  272. Reads a float from the stream.
  273. Notice that if UseBasicPrecisions() hadn't been called, this function
  274. simply reads a double and truncates it to float as by default the same
  275. (80 bit extended precision) representation is used for both float and
  276. double values.
  277. @since 2.9.5
  278. */
  279. float ReadFloat();
  280. /**
  281. Reads float data from the stream in a specified buffer.
  282. The number of floats to read is specified by the @a size variable.
  283. @since 2.9.5
  284. */
  285. void ReadFloat(float* buffer, size_t size);
  286. /**
  287. Reads a double from the stream.
  288. The expected format is either 80 bit extended precision or, if
  289. UseBasicPrecisions() had been called, standard IEEE 754 64 bit double
  290. precision.
  291. */
  292. double ReadDouble();
  293. /**
  294. Reads double data from the stream in a specified buffer.
  295. The number of doubles to read is specified by the @a size variable.
  296. */
  297. void ReadDouble(double* buffer, size_t size);
  298. /**
  299. Reads a string from a stream. Actually, this function first reads a
  300. long integer specifying the length of the string (without the last null
  301. character) and then reads the string.
  302. In Unicode build of wxWidgets, the function first reads multibyte
  303. (char*) string from the stream and then converts it to Unicode using
  304. the @e conv object passed to constructor and returns the result as
  305. wxString. You are responsible for using the same converter as when
  306. writing the stream.
  307. @see wxDataOutputStream::WriteString()
  308. */
  309. wxString ReadString();
  310. /**
  311. Sets the text conversion class used for reading strings.
  312. */
  313. void SetConv( const wxMBConv &conv );
  314. /**
  315. Disables the use of extended precision format for floating point
  316. numbers.
  317. This method disables the use of 80 bit extended precision format for
  318. the @c float and @c double values read from the stream, which is used
  319. by default (unless @c wxUSE_APPLE_IEEE was set to @c 0 when building
  320. the library, in which case the extended format support is not available
  321. at all and this function does nothing).
  322. After calling it, @c float values will be expected to appear in one of
  323. IEEE 754 "basic formats", i.e. 32 bit single precision format for
  324. floats and 64 bit double precision format for doubles in the input.
  325. @since 2.9.5
  326. */
  327. void UseBasicPrecisions();
  328. /**
  329. Explicitly request the use of extended precision for floating point
  330. numbers.
  331. This function allows the application code to explicitly request the use
  332. of 80 bit extended precision format for the floating point numbers.
  333. This is the case by default but using this function explicitly ensures
  334. that the compilation of code relying on reading the input containing
  335. numbers in extended precision format would fail when using a version of
  336. wxWidgets compiled with @c wxUSE_APPLE_IEEE==0 and so not supporting
  337. this format at all.
  338. @since 2.9.5
  339. */
  340. void UseExtendedPrecision();
  341. };