binstream.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifdef NICE_USELIB_LINAL
  35. #include <cstddef>
  36. #include "LinAl/matrix.h"
  37. #endif
  38. namespace NICE {
  39. // ----------------------------------------------------------------------------
  40. // User classes. Use ibinstream and obinstream analogously to ifstream and
  41. // ofstream respectively. They read and write files based in a binary fashion.
  42. // ----------------------------------------------------------------------------
  43. /**
  44. * Input binary stream class.
  45. *
  46. * @author Frank Mattern, Ferid Bajramovic
  47. */
  48. class ibinstream {
  49. public:
  50. virtual void read(char* data, unsigned int length) = 0;
  51. virtual ~ibinstream() {}
  52. ibinstream &operator>>(bool &n) {
  53. read((char*) &n, sizeof(bool)); return *this;
  54. }
  55. ibinstream &operator>>(int &n) {
  56. read((char*) &n, sizeof(int)); return *this;
  57. }
  58. ibinstream &operator>>(unsigned int &n) {
  59. read((char*) &n, sizeof(unsigned int)); return *this;
  60. }
  61. ibinstream &operator>>(char &n) {
  62. read((char*) &n, sizeof(char)); return *this;
  63. }
  64. ibinstream &operator>>(signed char &n) {
  65. read((char*) &n, sizeof(signed char)); return *this;
  66. }
  67. ibinstream &operator>>(unsigned char &n) {
  68. read((char*) &n, sizeof(unsigned char)); return *this;
  69. }
  70. ibinstream &operator>>(short &n) {
  71. read((char*) &n, sizeof(short)); return *this;
  72. }
  73. ibinstream &operator>>(unsigned short &n) {
  74. read((char*) &n, sizeof(unsigned short)); return *this;
  75. }
  76. ibinstream &operator>>(long &n) {
  77. read((char*) &n, sizeof(long)); return *this;
  78. }
  79. ibinstream &operator>>(unsigned long &n) {
  80. read((char*) &n, sizeof(unsigned long)); return *this;
  81. }
  82. ibinstream &operator>>(long long &n) {
  83. read((char*) &n, sizeof(long long)); return *this;
  84. }
  85. ibinstream &operator>>(unsigned long long &n) {
  86. read((char*) &n, sizeof(unsigned long long)); return *this;
  87. }
  88. ibinstream &operator>>(float &n) {
  89. read((char*) &n, sizeof(float)); return *this;
  90. }
  91. ibinstream &operator>>(double &n) {
  92. read((char*) &n, sizeof(double)); return *this;
  93. }
  94. ibinstream &operator>>(long double &n) {
  95. read((char*) &n, sizeof(long double)); return *this;
  96. }
  97. ibinstream &operator>>(char *n) {
  98. int l=0; read((char*) &l, sizeof(int)); read((char*) n, l); return *this;
  99. }
  100. ibinstream &operator>>(std::string &n) {
  101. int l=0;
  102. read((char*) &l, sizeof(int));
  103. char buf[l];
  104. read((char*) buf, l);
  105. n.assign(buf,l);
  106. return *this;
  107. }
  108. template<class T>
  109. ibinstream &operator>>(std::vector<T> &n) {
  110. int l=0;
  111. read((char*) &l, sizeof(int));
  112. n.resize(l);
  113. for(uint i=0;i<n.size();i++)
  114. (*this) >> n[i];
  115. return *this;
  116. }
  117. };
  118. /**
  119. * Output binary stream class.
  120. *
  121. * @author Frank Mattern, Ferid Bajramovic
  122. */
  123. class obinstream {
  124. public:
  125. virtual void write(char* data, unsigned int length) = 0;
  126. virtual ~obinstream() {}
  127. obinstream &operator<<(bool n) {
  128. write((char*) &n, sizeof(bool)); return *this;
  129. }
  130. obinstream &operator<<(int n) {
  131. write((char*) &n, sizeof(int)); return *this;
  132. }
  133. obinstream &operator<<(unsigned int n) {
  134. write((char*) &n, sizeof(unsigned int)); return *this;
  135. }
  136. obinstream &operator<<(char n) {
  137. write((char*) &n, sizeof(char)); return *this;
  138. }
  139. obinstream &operator<<(signed char n) {
  140. write((char*) &n, sizeof(signed char)); return *this;
  141. }
  142. obinstream &operator<<(unsigned char n) {
  143. write((char*) &n, sizeof(unsigned char)); return *this;
  144. }
  145. obinstream &operator<<(short n) {
  146. write((char*) &n, sizeof(short)); return *this;
  147. }
  148. obinstream &operator<<(unsigned short n) {
  149. write((char*) &n, sizeof(unsigned short)); return *this;
  150. }
  151. obinstream &operator<<(long n) {
  152. write((char*) &n, sizeof(long)); return *this;
  153. }
  154. obinstream &operator<<(unsigned long n) {
  155. write((char*) &n, sizeof(unsigned long)); return *this;
  156. }
  157. obinstream &operator<<(long long n) {
  158. write((char*) &n, sizeof(long long)); return *this;
  159. }
  160. obinstream &operator<<(unsigned long long n) {
  161. write((char*) &n, sizeof(unsigned long long)); return *this;
  162. }
  163. obinstream &operator<<(float n) {
  164. write((char*) &n, sizeof(float)); return *this;
  165. }
  166. obinstream &operator<<(double n) {
  167. write((char*) &n, sizeof(double)); return *this;
  168. }
  169. obinstream &operator<<(long double n) {
  170. write((char*) &n, sizeof(long double)); return *this;
  171. }
  172. obinstream &operator<<(const char *n) {
  173. int l=(int)strlen(n)+1;
  174. write((char*) &l, sizeof(int));
  175. write((char*) n, l);
  176. return *this;
  177. }
  178. obinstream &operator<<(const std::string &n) {
  179. int l=(int)n.size();
  180. write((char*) &l, sizeof(int));
  181. write((char*) n.c_str(), l);
  182. return *this;
  183. }
  184. template<class T>
  185. obinstream &operator<<(const std::vector<T> &n) {
  186. int l=n.size();
  187. write((char*) &l, sizeof(int));
  188. for(uint i=0;i<n.size();i++)
  189. *this << n[i];
  190. return *this;
  191. }
  192. };
  193. /**
  194. * Input binary stream class, reading from binary files.
  195. *
  196. * @author Frank Mattern, Ferid Bajramovic
  197. */
  198. class ifbinstream : public ibinstream {
  199. private:
  200. std::ifstream stream;
  201. public:
  202. ifbinstream() {}
  203. ifbinstream(const char* name,
  204. std::ios_base::openmode open_mode = std::ios::in)
  205. : stream(name, open_mode) {}
  206. virtual void read(char* data, unsigned int length) {
  207. stream.read(data, length);
  208. }
  209. inline void open(const char* name,
  210. std::ios_base::openmode open_mode = std::ios::out) {
  211. stream.open(name, open_mode);
  212. }
  213. inline void close() {
  214. stream.close();
  215. }
  216. inline bool good() const {
  217. return stream.good();
  218. }
  219. inline bool fail() const {
  220. return stream.fail();
  221. }
  222. };
  223. /**
  224. * Output binary stream class, writing to binary files.
  225. *
  226. * @author Frank Mattern, Ferid Bajramovic
  227. */
  228. class ofbinstream : public obinstream {
  229. private:
  230. std::ofstream stream;
  231. public:
  232. ofbinstream() {}
  233. ofbinstream( const char* name,
  234. std::ios_base::openmode mode = std::ios::out|std::ios::binary)
  235. : stream(name, mode) {}
  236. virtual void write(char* data, unsigned int length) {
  237. stream.write(data, length);
  238. }
  239. inline void open(const char* name,
  240. std::ios_base::openmode open_mode = std::ios::out) {
  241. stream.open(name, open_mode);
  242. }
  243. inline void close() {
  244. stream.close();
  245. }
  246. inline bool good() const {
  247. return stream.good();
  248. }
  249. inline bool fail() const {
  250. return stream.fail();
  251. }
  252. };
  253. #ifdef NICE_USELIB_LINAL
  254. template <class Tp>
  255. inline ibinstream& operator>>(ibinstream& s, LinAl::Ref<Tp> r) {
  256. Tp i;
  257. s>>i;
  258. r=i;
  259. return s;
  260. }
  261. template <class Tp>
  262. inline obinstream& operator<<(obinstream& s, const LinAl::Ref<Tp>& r) {
  263. s<<(Tp)r;
  264. return s;
  265. }
  266. template <class Tp>
  267. inline ibinstream& operator>>(ibinstream& s, LinAl::MatrixC<Tp> &A) {
  268. int nrows, ncols;
  269. s >> nrows >> ncols;
  270. A.resize(nrows, ncols);
  271. for(int r = 0; r < nrows; r++)
  272. for(int c = 0; c < ncols; c++) {
  273. s >> A(r,c);
  274. }
  275. return s;
  276. }
  277. template <class Tp>
  278. inline obinstream& operator<<(obinstream &s, const LinAl::MatrixC<Tp> &A) {
  279. s << A.nr() << A.nc();
  280. for(int i = 0; i < A.nr(); i++) {
  281. for(int j = 0; j < A.nc(); j++) {
  282. s << A(i,j);
  283. }
  284. }
  285. return s;
  286. }
  287. // #else
  288. // #ifndef LINAL_WARNING
  289. // #warning LinAl addons will not be compiled.
  290. // #define LINAL_WARNING
  291. // #endif
  292. #endif
  293. } // namespace
  294. #endif // BINSTREAM_H