binstream.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // ============================================================================
  2. // binstream, C++ iostream classes for binary in and output
  3. // Copyright (C) 2006 Frank Mattern
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. // ============================================================================
  19. //
  20. // File : binstream.h
  21. // Revision : $Revision: 1.3 $
  22. // Revision_date : $Date: 2011/10/18 14:15:29 $
  23. // Author(s) : Frank Mattern
  24. //
  25. // ============================================================================
  26. #ifndef BINSTREAM_H
  27. #define BINSTREAM_H 1
  28. // standard C++ with new header file names and std:: namespace
  29. #include <iostream>
  30. #include <fstream>
  31. #include <vector>
  32. #include <core/basics/types.h>
  33. #include <string.h>
  34. #include "CrossplatformDefines.h"
  35. #ifdef NICE_USELIB_LINAL
  36. #include <cstddef>
  37. #include "LinAl/matrix.h"
  38. #endif
  39. namespace NICE {
  40. // ----------------------------------------------------------------------------
  41. // User classes. Use ibinstream and obinstream analogously to ifstream and
  42. // ofstream respectively. They read and write files based in a binary fashion.
  43. // ----------------------------------------------------------------------------
  44. /**
  45. * Input binary stream class.
  46. *
  47. * @author Frank Mattern, Ferid Bajramovic
  48. */
  49. class ibinstream {
  50. public:
  51. virtual void read(char* data, unsigned int length) = 0;
  52. virtual ~ibinstream() {}
  53. ibinstream &operator>>(bool &n) {
  54. read((char*) &n, sizeof(bool)); return *this;
  55. }
  56. ibinstream &operator>>(int &n) {
  57. read((char*) &n, sizeof(int)); return *this;
  58. }
  59. ibinstream &operator>>(unsigned int &n) {
  60. read((char*) &n, sizeof(unsigned int)); return *this;
  61. }
  62. ibinstream &operator>>(char &n) {
  63. read((char*) &n, sizeof(char)); return *this;
  64. }
  65. ibinstream &operator>>(signed char &n) {
  66. read((char*) &n, sizeof(signed char)); return *this;
  67. }
  68. ibinstream &operator>>(unsigned char &n) {
  69. read((char*) &n, sizeof(unsigned char)); return *this;
  70. }
  71. ibinstream &operator>>(short &n) {
  72. read((char*) &n, sizeof(short)); return *this;
  73. }
  74. ibinstream &operator>>(unsigned short &n) {
  75. read((char*) &n, sizeof(unsigned short)); return *this;
  76. }
  77. ibinstream &operator>>(long &n) {
  78. read((char*) &n, sizeof(long)); return *this;
  79. }
  80. ibinstream &operator>>(unsigned long &n) {
  81. read((char*) &n, sizeof(unsigned long)); return *this;
  82. }
  83. ibinstream &operator>>(long long &n) {
  84. read((char*) &n, sizeof(long long)); return *this;
  85. }
  86. ibinstream &operator>>(unsigned long long &n) {
  87. read((char*) &n, sizeof(unsigned long long)); return *this;
  88. }
  89. ibinstream &operator>>(float &n) {
  90. read((char*) &n, sizeof(float)); return *this;
  91. }
  92. ibinstream &operator>>(double &n) {
  93. read((char*) &n, sizeof(double)); return *this;
  94. }
  95. ibinstream &operator>>(long double &n) {
  96. read((char*) &n, sizeof(long double)); return *this;
  97. }
  98. ibinstream &operator>>(char *n) {
  99. int l=0; read((char*) &l, sizeof(int)); read((char*) n, l); return *this;
  100. }
  101. ibinstream &operator>>(std::string &n) {
  102. int l=0;
  103. read((char*) &l, sizeof(int));
  104. #ifdef WIN32
  105. char* buf = new char[l];
  106. read( buf, l);
  107. n.assign(buf,l);
  108. #else
  109. char buf[l];
  110. read((char*) buf, l);
  111. n.assign(buf,l);
  112. #endif
  113. return *this;
  114. }
  115. template<class T>
  116. ibinstream &operator>>(std::vector<T> &n) {
  117. int l=0;
  118. read((char*) &l, sizeof(int));
  119. n.resize(l);
  120. for(uint i=0;i<n.size();i++)
  121. (*this) >> n[i];
  122. return *this;
  123. }
  124. };
  125. /**
  126. * Output binary stream class.
  127. *
  128. * @author Frank Mattern, Ferid Bajramovic
  129. */
  130. class obinstream {
  131. public:
  132. virtual void write(char* data, unsigned int length) = 0;
  133. virtual ~obinstream() {}
  134. obinstream &operator<<(bool n) {
  135. write((char*) &n, sizeof(bool)); return *this;
  136. }
  137. obinstream &operator<<(int n) {
  138. write((char*) &n, sizeof(int)); return *this;
  139. }
  140. obinstream &operator<<(unsigned int n) {
  141. write((char*) &n, sizeof(unsigned int)); return *this;
  142. }
  143. obinstream &operator<<(char n) {
  144. write((char*) &n, sizeof(char)); return *this;
  145. }
  146. obinstream &operator<<(signed char n) {
  147. write((char*) &n, sizeof(signed char)); return *this;
  148. }
  149. obinstream &operator<<(unsigned char n) {
  150. write((char*) &n, sizeof(unsigned char)); return *this;
  151. }
  152. obinstream &operator<<(short n) {
  153. write((char*) &n, sizeof(short)); return *this;
  154. }
  155. obinstream &operator<<(unsigned short n) {
  156. write((char*) &n, sizeof(unsigned short)); return *this;
  157. }
  158. obinstream &operator<<(long n) {
  159. write((char*) &n, sizeof(long)); return *this;
  160. }
  161. obinstream &operator<<(unsigned long n) {
  162. write((char*) &n, sizeof(unsigned long)); return *this;
  163. }
  164. obinstream &operator<<(long long n) {
  165. write((char*) &n, sizeof(long long)); return *this;
  166. }
  167. obinstream &operator<<(unsigned long long n) {
  168. write((char*) &n, sizeof(unsigned long long)); return *this;
  169. }
  170. obinstream &operator<<(float n) {
  171. write((char*) &n, sizeof(float)); return *this;
  172. }
  173. obinstream &operator<<(double n) {
  174. write((char*) &n, sizeof(double)); return *this;
  175. }
  176. obinstream &operator<<(long double n) {
  177. write((char*) &n, sizeof(long double)); return *this;
  178. }
  179. obinstream &operator<<(const char *n) {
  180. int l=(int)strlen(n)+1;
  181. write((char*) &l, sizeof(int));
  182. write((char*) n, l);
  183. return *this;
  184. }
  185. obinstream &operator<<(const std::string &n) {
  186. int l=(int)n.size();
  187. write((char*) &l, sizeof(int));
  188. write((char*) n.c_str(), l);
  189. return *this;
  190. }
  191. template<class T>
  192. obinstream &operator<<(const std::vector<T> &n) {
  193. int l=n.size();
  194. write((char*) &l, sizeof(int));
  195. for(uint i=0;i<n.size();i++)
  196. *this << n[i];
  197. return *this;
  198. }
  199. };
  200. /**
  201. * Input binary stream class, reading from binary files.
  202. *
  203. * @author Frank Mattern, Ferid Bajramovic
  204. */
  205. class ifbinstream : public ibinstream {
  206. private:
  207. std::ifstream stream;
  208. public:
  209. ifbinstream() {}
  210. ifbinstream(const char* name,
  211. std::ios_base::openmode open_mode = std::ios::in)
  212. : stream(name, open_mode) {}
  213. virtual void read(char* data, unsigned int length) {
  214. stream.read(data, length);
  215. }
  216. inline void open(const char* name,
  217. std::ios_base::openmode open_mode = std::ios::out) {
  218. stream.open(name, open_mode);
  219. }
  220. inline void close() {
  221. stream.close();
  222. }
  223. inline bool good() const {
  224. return stream.good();
  225. }
  226. inline bool fail() const {
  227. return stream.fail();
  228. }
  229. };
  230. /**
  231. * Output binary stream class, writing to binary files.
  232. *
  233. * @author Frank Mattern, Ferid Bajramovic
  234. */
  235. class ofbinstream : public obinstream {
  236. private:
  237. std::ofstream stream;
  238. public:
  239. ofbinstream() {}
  240. ofbinstream( const char* name,
  241. std::ios_base::openmode mode = std::ios::out|std::ios::binary)
  242. : stream(name, mode) {}
  243. virtual void write(char* data, unsigned int length) {
  244. stream.write(data, length);
  245. }
  246. inline void open(const char* name,
  247. std::ios_base::openmode open_mode = std::ios::out) {
  248. stream.open(name, open_mode);
  249. }
  250. inline void close() {
  251. stream.close();
  252. }
  253. inline bool good() const {
  254. return stream.good();
  255. }
  256. inline bool fail() const {
  257. return stream.fail();
  258. }
  259. };
  260. #ifdef NICE_USELIB_LINAL
  261. template <class Tp>
  262. inline ibinstream& operator>>(ibinstream& s, LinAl::Ref<Tp> r) {
  263. Tp i;
  264. s>>i;
  265. r=i;
  266. return s;
  267. }
  268. template <class Tp>
  269. inline obinstream& operator<<(obinstream& s, const LinAl::Ref<Tp>& r) {
  270. s<<(Tp)r;
  271. return s;
  272. }
  273. template <class Tp>
  274. inline ibinstream& operator>>(ibinstream& s, LinAl::MatrixC<Tp> &A) {
  275. int nrows, ncols;
  276. s >> nrows >> ncols;
  277. A.resize(nrows, ncols);
  278. for(int r = 0; r < nrows; r++)
  279. for(int c = 0; c < ncols; c++) {
  280. s >> A(r,c);
  281. }
  282. return s;
  283. }
  284. template <class Tp>
  285. inline obinstream& operator<<(obinstream &s, const LinAl::MatrixC<Tp> &A) {
  286. s << A.nr() << A.nc();
  287. for(int i = 0; i < A.nr(); i++) {
  288. for(int j = 0; j < A.nc(); j++) {
  289. s << A(i,j);
  290. }
  291. }
  292. return s;
  293. }
  294. //#else
  295. //#ifndef LINAL_WARNING
  296. //#pragma message NICE_WARNING("LinAl addons will not be compiled.")
  297. //#define LINAL_WARNING
  298. //#endif
  299. #endif
  300. } // namespace
  301. #endif // BINSTREAM_H