binio.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* -*-C++-*-
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Lesser General Public
  4. * License as published by the Free Software Foundation; either
  5. * version 2.1 of the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Lesser General Public
  13. * License along with this library; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * binio.h - Binary stream I/O classes
  17. * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
  18. */
  19. #ifndef H_BINIO_BINIO
  20. #define H_BINIO_BINIO
  21. #ifdef _MSC_VER
  22. #pragma warning (disable:4244)
  23. #pragma warning (disable:4996)
  24. #pragma warning (disable:4267)
  25. #endif
  26. /***** Configuration *****/
  27. // BINIO_ENABLE_STRING - Build std::string supporting methods
  28. //
  29. // Set to 1 to build std::string supporting methods. You need the STL to
  30. // do this.
  31. #define BINIO_ENABLE_STRING 1
  32. // BINIO_ENABLE_IOSTREAM - Build iostream wrapper classes
  33. //
  34. // Set to 1 to build the iostream wrapper classes. You need the standard
  35. // C++ library to do this.
  36. #define BINIO_ENABLE_IOSTREAM 1
  37. // BINIO_ISO_STDLIB - Build with ISO C++ standard library compliance
  38. //
  39. // Set to 1 to build for the ISO standard C++ library (i.e. namespaces, STL and
  40. // templatized iostream). Set to 0 to build for the traditional C++ library.
  41. #define BINIO_ISO_STDLIB 1
  42. // BINIO_WITH_MATH - Build with 'math.h' dependency to allow float conversions
  43. //
  44. // Set to 1 to also build routines that depend on the 'math.h' standard C header
  45. // file (this sometimes also implies a 'libm' or 'libmath' dependency). These
  46. // routines are needed in order to write IEEE-754 floating-point numbers on a
  47. // system that doesn't support this format natively. For only reading these
  48. // numbers, however, these routines are not needed. If set to 0, writing
  49. // IEEE-754 numbers on an incompatible system will be disabled.
  50. //#define BINIO_WITH_MATH 1
  51. /***** Implementation *****/
  52. #ifdef _MSC_VER
  53. # pragma warning(disable: 4250)
  54. #else
  55. # define __int64 long long
  56. #endif
  57. #if BINIO_ENABLE_STRING
  58. #include <string>
  59. #endif
  60. class binio {
  61. public:
  62. typedef enum {
  63. BigEndian = 1 << 0,
  64. FloatIEEE = 1 << 1
  65. } Flag;
  66. typedef enum {
  67. NoError = 0,
  68. Fatal = 1 << 0,
  69. Unsupported = 1 << 1,
  70. NotOpen = 1 << 2,
  71. Denied = 1 << 3,
  72. NotFound = 1 << 4,
  73. Eof = 1 << 5
  74. } ErrorCode;
  75. typedef enum { Set, Add, End } Offset;
  76. typedef enum { Single, Double } FType;
  77. typedef int Error;
  78. binio();
  79. virtual ~binio();
  80. void setFlag(Flag f, bool set = true);
  81. bool getFlag(Flag f);
  82. Error error();
  83. bool eof();
  84. virtual void seek(long, Offset = Set) = 0;
  85. virtual long pos() = 0;
  86. protected:
  87. typedef __int64 Int;
  88. typedef long double Float;
  89. typedef unsigned char Byte; // has to be unsigned!
  90. typedef int Flags;
  91. Flags my_flags;
  92. static const Flags system_flags;
  93. Error err;
  94. // Some math.h emulation functions...
  95. #if !BINIO_WITH_MATH
  96. Float pow(Float base, signed int exp);
  97. Float ldexp(Float x, signed int exp) {
  98. return x * pow(2, exp);
  99. }
  100. #endif
  101. private:
  102. static const Flags detect_system_flags();
  103. };
  104. class binistream: virtual public binio {
  105. public:
  106. binistream();
  107. virtual ~binistream();
  108. Int readInt(unsigned int size);
  109. Float readFloat(FType ft);
  110. unsigned long readString(char *str, unsigned long amount);
  111. unsigned long readString(char *str, unsigned long maxlen, const char delim);
  112. #if BINIO_ENABLE_STRING
  113. std::string readString(const char delim = '\0');
  114. #endif
  115. Int peekInt(unsigned int size);
  116. Float peekFloat(FType ft);
  117. bool ateof();
  118. void ignore(unsigned long amount = 1);
  119. protected:
  120. virtual Byte getByte() = 0;
  121. private:
  122. Float ieee_single2float(Byte *data);
  123. Float ieee_double2float(Byte *data);
  124. };
  125. class binostream: virtual public binio {
  126. public:
  127. binostream();
  128. virtual ~binostream();
  129. void writeInt(Int val, unsigned int size);
  130. void writeFloat(Float f, FType ft);
  131. unsigned long writeString(const char *str, unsigned long amount = 0);
  132. #if BINIO_ENABLE_STRING
  133. unsigned long writeString(const std::string &str);
  134. #endif
  135. protected:
  136. virtual void putByte(Byte) = 0;
  137. private:
  138. void float2ieee_single(Float f, Byte *data);
  139. void float2ieee_double(Float f, Byte *data);
  140. };
  141. class binstream: public binistream, public binostream {
  142. public:
  143. binstream();
  144. virtual ~binstream();
  145. };
  146. #endif