QDP++
qdp_io.h
Go to the documentation of this file.
1// -*- C++ -*-
2
6
7#ifndef QDP_IO_H
8#define QDP_IO_H
9
10#include <string>
11#include <iostream>
12#include <fstream>
13#include <sstream>
14#include "qdp_byteorder.h"
15#include <vector>
16#include <map>
17#include <list>
18
19#if 1
20#include <complex>
21#endif
22
23namespace QDP
24{
31
32 //--------------------------------------------------------------------------------
34
40
42 {
43 public:
44 TextReader();
45
49 virtual ~TextReader() {}
50
52
55 virtual bool fail();
56
57 // Readers for builtin types
58 virtual void read(std::string& result);
59 virtual void read(char& result);
60 virtual void read(int& result);
61 virtual void read(unsigned int& result);
62 virtual void read(short int& result);
63 virtual void read(unsigned short int& result);
64 virtual void read(long int& result);
65 virtual void read(unsigned long int& result);
66 virtual void read(long long int& result);
67 virtual void read(float& result);
68 virtual void read(double& result);
69 virtual void read(bool& result);
70
71 protected:
73
77 template< typename T>
78 void
79 readPrimitive(T& output);
80
82 virtual std::istream& getIstream() = 0;
83 };
84
85
86 // Different bindings for same operators
87 TextReader& operator>>(TextReader& txt, std::string& input);
88 TextReader& operator>>(TextReader& txt, char& input);
89 TextReader& operator>>(TextReader& txt, int& input);
90 TextReader& operator>>(TextReader& txt, unsigned int& input);
91 TextReader& operator>>(TextReader& txt, short int& input);
92 TextReader& operator>>(TextReader& txt, unsigned short int& input);
93 TextReader& operator>>(TextReader& txt, long int& input);
94 TextReader& operator>>(TextReader& txt, unsigned long int& input);
95 TextReader& operator>>(TextReader& txt, long long int& input);
96 TextReader& operator>>(TextReader& txt, float& input);
97 TextReader& operator>>(TextReader& txt, double& input);
98 TextReader& operator>>(TextReader& txt, bool& input);
99
100
101 //--------------------------------------------------------------------------------
103
109
111 {
112 public:
114
117
122 explicit TextBufferReader(const std::string& p);
123
125
128 void open(const std::string& p);
129
131 std::string str() const;
132
134 std::string strPrimaryNode() const;
135
136 protected:
138 std::istream& getIstream() {return f;}
139
140 private:
141 std::istringstream f;
142 };
143
144
145 //--------------------------------------------------------------------------------
147
153
155 {
156 public:
158
163
168 explicit TextFileReader(const std::string& p);
169
171
174 void open(const std::string& p);
175
177 void close();
178
180
183 bool is_open();
184
185 protected:
187 std::istream& getIstream() {return f;}
188
189 private:
190 std::ifstream f;
191 };
192
193
194 //--------------------------------------------------------------------------------
196
203 {
204 public:
205 TextWriter();
206
210 virtual ~TextWriter() {}
211
213 virtual void flush() = 0;
214
216
219 virtual bool fail();
220
221 // Overloaded Writer Functions
222 virtual void write(const std::string& output);
223 virtual void write(const char* output);
224 virtual void write(const char& output);
225 virtual void write(const int& output);
226 virtual void write(const unsigned int& output);
227 virtual void write(const short int& output);
228 virtual void write(const unsigned short int& output);
229 virtual void write(const long int& output);
230 virtual void write(const unsigned long int& output);
231 virtual void write(const long long int& output);
232 virtual void write(const float& output);
233 virtual void write(const double& output);
234 virtual void write(const bool& output);
235
236 protected:
238
242 template< typename T>
243 void
244 writePrimitive(const T& output);
245
247 virtual std::ostream& getOstream() = 0;
248 };
249
250
251 // Different bindings for same operators
252 TextWriter& operator<<(TextWriter& txt, const std::string& output);
253 TextWriter& operator<<(TextWriter& txt, const char* output);
254 TextWriter& operator<<(TextWriter& txt, char output);
255 TextWriter& operator<<(TextWriter& txt, int output);
256 TextWriter& operator<<(TextWriter& txt, unsigned int output);
257 TextWriter& operator<<(TextWriter& txt, short int output);
258 TextWriter& operator<<(TextWriter& txt, unsigned short int output);
259 TextWriter& operator<<(TextWriter& txt, long int output);
260 TextWriter& operator<<(TextWriter& txt, unsigned long int output);
261 TextWriter& operator<<(TextWriter& txt, long long int output);
262 TextWriter& operator<<(TextWriter& txt, float output);
263 TextWriter& operator<<(TextWriter& txt, double output);
264 TextWriter& operator<<(TextWriter& txt, bool output);
265
266
267 //--------------------------------------------------------------------------------
269
276 {
277 public:
279
282
287 explicit TextBufferWriter(const std::string& p);
288
290 void open(const std::string& s);
291
293 void flush() {}
294
296 std::string str() const;
297
299 std::string strPrimaryNode() const;
300
301 protected:
303 std::ostream& getOstream() {return f;}
304
305 private:
306 std::ostringstream f;
307 };
308
309
310 //--------------------------------------------------------------------------------
312
319 {
320 public:
322
327
332 explicit TextFileWriter(const std::string& p);
333
335
338 bool is_open();
339
344 void open(const std::string& p);
345
347 void close();
348
350 void flush();
351
352 protected:
354 std::ostream& getOstream() {return f;}
355
356 private:
357 std::ofstream f;
358 };
359
360
361 //--------------------------------------------------------------------------------
363
372 {
373 public:
374 typedef std::istream::pos_type pos_type; // position in buffer
375 typedef std::istream::off_type off_type; // offset in buffer
376
377 public:
381 virtual ~BinaryReader() {}
382
384
389 virtual void readArrayPrimaryNode(char* output, size_t nbytes, size_t nmemb);
390
392
397 virtual void readArray(char* output, size_t nbytes, size_t nmemb);
398
401
406 virtual void readArrayLittleEndian(char* output, size_t nbytes, size_t nmemb);
407
408 virtual void readArrayPrimaryNodeLittleEndian(char* output, size_t nbytes, size_t nmemb);
409
410 // Overloaded reader functions
411 virtual void readDesc(std::string& result);
412
414
415 virtual void read(std::string& result, size_t nbytes);
416
417 virtual void read(char& result);
418 virtual void read(int& result);
419 virtual void read(unsigned int& result);
420 virtual void read(short int& result);
421 virtual void read(unsigned short int& result);
422 virtual void read(long int& result);
423 virtual void read(unsigned long int& result);
424 virtual void read(long long int& result);
425 virtual void read(float& result);
426 virtual void read(double& result);
427 virtual void read(bool& result);
428
430
433 virtual bool fail();
434
437
439 virtual void resetChecksum();
440
442 virtual pos_type currentPosition();
443
445
446 virtual void seek(pos_type off);
447
449
450 virtual void seekBegin(off_type off);
451
453
454 virtual void seekRelative(off_type off);
455
457
458 virtual void seekEnd(off_type off);
459
461
462 virtual void rewind();
463
464 protected:
466
470 template< typename T>
471 void
472 readPrimitive(T& output);
473
476
478 virtual std::istream& getIstream() = 0;
479 };
480
481
482 // Telephone book of basic primitives
483 void readDesc(BinaryReader& bin, std::string& input);
484 void read(BinaryReader& bin, std::string& input, size_t maxBytes);
485 void read(BinaryReader& bin, char& input);
486 void read(BinaryReader& bin, int& input);
487 void read(BinaryReader& bin, unsigned int& input);
488 void read(BinaryReader& bin, short int& input);
489 void read(BinaryReader& bin, unsigned short int& input);
490 void read(BinaryReader& bin, long int& input);
491 void read(BinaryReader& bin, unsigned long int& input);
492 void read(BinaryReader& bin, long long int& input);
493 void read(BinaryReader& bin, float& input);
494 void read(BinaryReader& bin, double& input);
495 void read(BinaryReader& bin, bool& input);
496
497 // Different bindings for same operators
498 BinaryReader& operator>>(BinaryReader& bin, char& input);
499 BinaryReader& operator>>(BinaryReader& bin, int& input);
500 BinaryReader& operator>>(BinaryReader& bin, unsigned int& input);
501 BinaryReader& operator>>(BinaryReader& bin, short int& input);
502 BinaryReader& operator>>(BinaryReader& bin, unsigned short int& input);
503 BinaryReader& operator>>(BinaryReader& bin, long int& input);
504 BinaryReader& operator>>(BinaryReader& bin, unsigned long int& input);
505 BinaryReader& operator>>(BinaryReader& bin, long long int& input);
506 BinaryReader& operator>>(BinaryReader& bin, float& input);
507 BinaryReader& operator>>(BinaryReader& bin, double& input);
508 BinaryReader& operator>>(BinaryReader& bin, bool& input);
509
510#if 1
512 void read(BinaryReader& bin, std::complex<float>& param);
513 void read(BinaryReader& bin, std::complex<double>& param);
514#endif
515
516
518
528 template<class T>
529 inline
531 {
532 int n;
533 read(bin, n); // the size is always written, even if 0
534 d.resize(n);
535
536 for(int i=0; i < d.size(); ++i)
537 read(bin, d[i]);
538 }
539
540
542
553 template<class T>
554 inline
555 void read(BinaryReader& bin, multi1d<T>& d, int num)
556 {
557 for(int i=0; i < num; ++i)
558 read(bin, d[i]);
559 }
560
562
574 template<class T>
575 inline
576 void read(BinaryReader& bin, multi2d<T>& d, int num1, int num2)
577 {
578 for(int i=0; i < num2; ++i)
579 for(int j=0; j < num1; ++j)
580 read(bin, d[j][i]);
581
582 }
583
584
586
596 template<class T>
597 inline
599 {
600 int n1;
601 int n2;
602 read(bin, n1); // the size is always written, even if 0
603 read(bin, n2); // the size is always written, even if 0
604 d.resize(n1,n2);
605
606 for(int i=0; i < d.size1(); ++i)
607 for(int j=0; j < d.size2(); ++j)
608 {
609 read(bin, d[j][i]);
610 }
611 }
612
613
615
625 template<class T>
626 inline
628 {
629 int n1;
630 int n2;
631 int n3;
632 read(bin, n3); // the size is always written, even if 0
633 read(bin, n2); // the size is always written, even if 0
634 read(bin, n1); // the size is always written, even if 0
635
636 // Destructively resize the array
637 d.resize(n3,n2,n1);
638
639 for(int i=0; i < d.size1(); ++i)
640 {
641 for(int j=0; j < d.size2(); ++j)
642 {
643 for(int k=0; k < d.size3(); ++k)
644 {
645 read(bin, d[k][j][i]);
646 }
647 }
648 }
649 }
650
651
653
663 template<class T>
664 inline
666 {
667 int n1;
668 int n2;
669 int n3;
670 int n4;
671 read(bin, n4); // the size is always written, even if 0
672 read(bin, n3); // the size is always written, even if 0
673 read(bin, n2); // the size is always written, even if 0
674 read(bin, n1); // the size is always written, even if 0
675
676 // Destructively resize the array
677 d.resize(n4,n3,n2,n1);
678
679 for(int i=0; i < d.size1(); ++i)
680 {
681 for(int j=0; j < d.size2(); ++j)
682 {
683 for(int k=0; k < d.size3(); ++k)
684 {
685 for(int l=0; l < d.size4(); ++l)
686 {
687 read(bin, d[l][k][j][i]);
688 }
689 }
690 }
691 }
692 }
693
694
696
706 template<class T>
707 inline
709 {
710 int n1;
711 int n2;
712 int n3;
713 int n4;
714 int n5;
715 read(bin, n5); // the size is always written, even if 0
716 read(bin, n4); // the size is always written, even if 0
717 read(bin, n3); // the size is always written, even if 0
718 read(bin, n2); // the size is always written, even if 0
719 read(bin, n1); // the size is always written, even if 0
720
721 // Destructively resize the array
722 d.resize(n5,n4,n3,n2,n1);
723
724 for(int i=0; i < d.size1(); ++i)
725 {
726 for(int j=0; j < d.size2(); ++j)
727 {
728 for(int k=0; k < d.size3(); ++k)
729 {
730 for(int l=0; l < d.size4(); ++l)
731 {
732 for(int m=0; m < d.size5(); ++m)
733 {
734 read(bin, d[m][l][k][j][i]);
735 }
736 }
737 }
738 }
739 }
740 }
741
742
744 template<class T>
745 inline
747 {
748 multi1d<int> siz;
749 read(bin, siz); // read the array of the sizes
750
751 d.resize(siz);
752
753 for(int i=0; i < d.numElem(); ++i)
754 read(bin, d.getElem(i));
755 }
756
757
759
769 template<class T>
770 inline
771 void read(BinaryReader& bin, std::vector<T>& d)
772 {
773 int n;
774 read(bin, n); // the size is always written, even if 0
775 d.resize(n);
776
777 for(int i=0; i < d.size(); ++i)
778 read(bin, d[i]);
779 }
780
781
783
793 template<class T>
794 inline
795 void read(BinaryReader& bin, std::list<T>& d)
796 {
797 d.clear();
798
799 int n;
800 read(bin, n); // the size is always written, even if 0
801
802 for(int i=0; i < n; ++i)
803 {
804 T thingy;
805 read(bin, thingy);
806
807 d.push_back(thingy);
808 }
809 }
810
811
813
823 template<class T>
824 inline
826 {
827 int n;
828 read(bin, n); // the size is always written, even if 0
829 d.resize(n);
830
831 for(int i=1; i <= d.size(); ++i)
832 read(bin, d[i]);
833 }
834
835
837
847 template<typename K, typename V>
848 inline
849 void read(BinaryReader& bin, std::map<K,V>& d)
850 {
851 d.clear();
852
853 int n;
854 read(bin, n); // the size is always written, even if 0
855
856 for(int i=0; i < n; ++i)
857 {
858 K key;
859 read(bin, key);
860
861 V val;
862 read(bin, val);
863
864 d.insert(std::make_pair(key,val));
865 }
866 }
867
868
870
880 template<typename T1, typename T2>
881 inline
882 void read(BinaryReader& bin, std::pair<T1,T2>& d)
883 {
884 T1 f;
885 read(bin, f);
886
887 T2 s;
888 read(bin, s);
889
890 d = std::make_pair(f,s);
891 }
892
893
894 //--------------------------------------------------------------------------------
896
905 {
906 public:
908
910 explicit BinaryBufferReader(const std::string& s);
911
914
916 void open(const std::string& s);
917
919 std::string str() const;
920
922 std::string strPrimaryNode() const;
923
925 void clear();
926
927 protected:
930
932 std::istream& getIstream() {return f;}
933
934 private:
936 QDPUtil::n_uint32_t checksum;
937 std::istringstream f;
938 };
939
940
941 //--------------------------------------------------------------------------------
943
952 {
953 public:
955
960
965 explicit BinaryFileReader(const std::string& p);
966
968
971 bool is_open();
972
974
977 void open(const std::string& p);
978
980 void close();
981
982 protected:
985
987 std::istream& getIstream() {return f;}
988
989 private:
991 QDPUtil::n_uint32_t checksum;
992 std::ifstream f;
993 };
994
995
996 //--------------------------------------------------------------------------------
998
1010 {
1011 public:
1012 typedef std::ostream::pos_type pos_type; // position in buffer
1013 typedef std::ostream::off_type off_type; // offset in buffer
1014
1015 public:
1017 BinaryWriter();
1018
1022 virtual ~BinaryWriter() {}
1023
1025 virtual void flush() = 0;
1026
1028
1033 virtual void writeArrayPrimaryNode(const char* output, size_t nbytes, size_t nmemb);
1034
1036
1041 virtual void writeArray(const char* output, size_t nbytes, size_t nmemb);
1042
1043 // Overloaded Writer Functions
1044
1046 virtual void writeDesc(const std::string& output);
1047
1051 virtual void write(const std::string& output);
1055 virtual void write(const char* output);
1056 virtual void write(const char& output);
1057 virtual void write(const int& output);
1058 virtual void write(const unsigned int& output);
1059 virtual void write(const short int& output);
1060 virtual void write(const unsigned short int& output);
1061 virtual void write(const long int& output);
1062 virtual void write(const unsigned long int& output);
1063 virtual void write(const long long int& output);
1064 virtual void write(const float& output);
1065 virtual void write(const double& output);
1066 virtual void write(const bool& output);
1067
1069
1072 virtual bool fail();
1073
1076
1078 virtual void resetChecksum();
1079
1081 virtual pos_type currentPosition();
1082
1084
1085 virtual void seek(pos_type off);
1086
1088
1089 virtual void seekBegin(off_type off);
1090
1092
1093 virtual void seekRelative(off_type off);
1094
1096
1097 virtual void seekEnd(off_type off);
1098
1100
1101 virtual void rewind();
1102
1103 protected:
1104
1106
1110 template< typename T>
1111 void
1112 writePrimitive(const T& output);
1113
1116
1118 virtual std::ostream& getOstream() = 0;
1119 };
1120
1121
1122 // Telephone book of basic primitives
1123 void writeDesc(BinaryWriter& bin, const std::string& output);
1124 void write(BinaryWriter& bin, const std::string& output);
1125 void write(BinaryWriter& bin, const char* output);
1126 void write(BinaryWriter& bin, char output);
1127 void write(BinaryWriter& bin, int output);
1128 void write(BinaryWriter& bin, unsigned int output);
1129 void write(BinaryWriter& bin, short int output);
1130 void write(BinaryWriter& bin, unsigned short int output);
1131 void write(BinaryWriter& bin, long int output);
1132 void write(BinaryWriter& bin, unsigned long int output);
1133 void write(BinaryWriter& bin, long long int output);
1134 void write(BinaryWriter& bin, float output);
1135 void write(BinaryWriter& bin, double output);
1136 void write(BinaryWriter& bin, bool output);
1137
1138 // Different bindings for same operators
1139 BinaryWriter& operator<<(BinaryWriter& bin, const std::string& output);
1140 BinaryWriter& operator<<(BinaryWriter& bin, const char* output);
1142 BinaryWriter& operator<<(BinaryWriter& bin, int output);
1143 BinaryWriter& operator<<(BinaryWriter& bin, unsigned int output);
1144 BinaryWriter& operator<<(BinaryWriter& bin, short int output);
1145 BinaryWriter& operator<<(BinaryWriter& bin, unsigned short int output);
1146 BinaryWriter& operator<<(BinaryWriter& bin, long int output);
1147 BinaryWriter& operator<<(BinaryWriter& bin, unsigned long int output);
1148 BinaryWriter& operator<<(BinaryWriter& bin, long long int output);
1149 BinaryWriter& operator<<(BinaryWriter& bin, float output);
1150 BinaryWriter& operator<<(BinaryWriter& bin, double output);
1151 BinaryWriter& operator<<(BinaryWriter& bin, bool output);
1152
1153#if 1
1155 void write(BinaryWriter& bin, const std::complex<float>& param);
1156 void write(BinaryWriter& bin, const std::complex<double>& param);
1157#endif
1158
1160
1167 template<class T>
1168 inline
1169 void write(BinaryWriter& bin, const multi1d<T>& d)
1170 {
1171 write(bin, d.size()); // always write the size
1172 for(int i=0; i < d.size(); ++i)
1173 write(bin, d[i]);
1174 }
1175
1177
1185 template<class T>
1186 inline
1187 void write(BinaryWriter& bin, const multi1d<T>& d, int num)
1188 {
1189 for(int i=0; i < num; ++i)
1190 write(bin, d[i]);
1191 }
1192
1193
1195 template<class T>
1196 inline
1197 void write(BinaryWriter& bin, const multi2d<T>& d)
1198 {
1199 write(bin, d.size2()); // always write the size
1200 write(bin, d.size1()); // always write the size
1201
1202 for(int i=0; i < d.size1(); ++i)
1203 for(int j=0; j < d.size2(); ++j)
1204 {
1205 write(bin, d[j][i]);
1206 }
1207
1208 }
1209
1210
1211
1213 template<class T>
1214 inline
1215 void write(BinaryWriter& bin, const multi3d<T>& d)
1216 {
1217 write(bin, d.size3()); // always write the size
1218 write(bin, d.size2()); // always write the size
1219 write(bin, d.size1()); // always write the size
1220
1221 for(int i=0; i < d.size1(); ++i)
1222 for(int j=0; j < d.size2(); ++j)
1223 for(int k=0; k < d.size3(); ++k)
1224 write(bin, d[k][j][i]);
1225 }
1226
1227
1229 template<class T>
1230 inline
1231 void write(BinaryWriter& bin, const multi4d<T>& d)
1232 {
1233 write(bin, d.size4()); // always write the size
1234 write(bin, d.size3()); // always write the size
1235 write(bin, d.size2()); // always write the size
1236 write(bin, d.size1()); // always write the size
1237
1238 for(int i=0; i < d.size1(); ++i)
1239 for(int j=0; j < d.size2(); ++j)
1240 for(int k=0; k < d.size3(); ++k)
1241 for(int l=0; l < d.size4(); ++l)
1242 write(bin, d[l][k][j][i]);
1243 }
1244
1245
1247 template<class T>
1248 inline
1249 void write(BinaryWriter& bin, const multi5d<T>& d)
1250 {
1251 write(bin, d.size5()); // always write the size
1252 write(bin, d.size4()); // always write the size
1253 write(bin, d.size3()); // always write the size
1254 write(bin, d.size2()); // always write the size
1255 write(bin, d.size1()); // always write the size
1256
1257 for(int i=0; i < d.size1(); ++i)
1258 for(int j=0; j < d.size2(); ++j)
1259 for(int k=0; k < d.size3(); ++k)
1260 for(int l=0; l < d.size4(); ++l)
1261 for(int m=0; m < d.size5(); ++m)
1262 write(bin, d[m][l][k][j][i]);
1263 }
1264
1265
1267 template<class T>
1268 inline
1269 void write(BinaryWriter& bin, const multiNd<T>& d)
1270 {
1271 write(bin, d.size()); // write the array of the sizes
1272
1273 for(int i=0; i < d.numElem(); ++i)
1274 write(bin, d.getElem(i));
1275 }
1276
1277
1279
1286 template<class T>
1287 inline
1288 void write(BinaryWriter& bin, const std::vector<T>& d)
1289 {
1290 int n = d.size();
1291 write(bin, n); // always write the size
1292 for(int i=0; i < d.size(); ++i)
1293 write(bin, d[i]);
1294 }
1295
1296
1298
1305 template<class T>
1306 inline
1307 void write(BinaryWriter& bin, const std::list<T>& d)
1308 {
1309 int n = d.size();
1310 write(bin, n); // always write the size
1311 for(typename std::list<T>::const_iterator p = d.begin(); p != d.end(); ++p)
1312 write(bin, *p);
1313 }
1314
1315
1317
1324 template<class T>
1325 inline
1326 void write(BinaryWriter& bin, const Array1dO<T>& d)
1327 {
1328 int n = d.size();
1329 write(bin, n); // always write the size
1330 for(int i=1; i <= d.size(); ++i)
1331 write(bin, d[i]);
1332 }
1333
1334
1336
1343 template<typename K, typename V>
1344 inline
1345 void write(BinaryWriter& bin, const std::map<K,V>& d)
1346 {
1347 int n = d.size();
1348 write(bin, n); // always write the size
1349
1350 for(typename std::map<K,V>::const_iterator v = d.begin();
1351 v != d.end();
1352 ++v)
1353 {
1354 write(bin, v->first);
1355 write(bin, v->second);
1356 }
1357 }
1358
1359
1361
1368 template<typename T1, typename T2>
1369 inline
1370 void write(BinaryWriter& bin, const std::pair<T1,T2>& d)
1371 {
1372 write(bin, d.first);
1373 write(bin, d.second);
1374 }
1375
1376
1377 //--------------------------------------------------------------------------------
1379
1390 {
1391 public:
1393
1395 explicit BinaryBufferWriter(const std::string& s);
1396
1399
1401 void open(const std::string& s);
1402
1404 std::string str() const;
1405
1407 std::string strPrimaryNode() const;
1408
1410 void flush() {}
1411
1413 void clear();
1414
1415 protected:
1418
1420 std::ostream& getOstream() {return f;}
1421
1422 private:
1424 QDPUtil::n_uint32_t checksum;
1425 std::ostringstream f;
1426 };
1427
1428 //--------------------------------------------------------------------------------
1430
1441 {
1442 public:
1443 explicit BinaryFileWriter();
1444
1449
1454 explicit BinaryFileWriter(const std::string& p);
1455
1457
1460 bool is_open();
1461
1466 void open(const std::string& p);
1467
1469 void close();
1470
1472 void flush();
1473
1474 protected:
1477
1479 std::ostream& getOstream() {return f;}
1480
1481 private:
1483 QDPUtil::n_uint32_t checksum;
1484 std::ofstream f;
1485 };
1486
1487
1488 //--------------------------------------------------------------------------------
1490
1499 {
1500 public:
1501 typedef std::iostream::pos_type pos_type; // position in buffer
1502 typedef std::iostream::off_type off_type; // offset in buffer
1503
1504 public:
1509
1511
1514 virtual bool fail();
1515
1518
1520 virtual void resetChecksum();
1521
1523 virtual pos_type currentPosition();
1524
1526
1527 virtual void seek(pos_type off);
1528
1530
1531 virtual void seekBegin(off_type off);
1532
1534
1535 virtual void seekRelative(off_type off);
1536
1538
1539 virtual void seekEnd(off_type off);
1540
1542
1543 virtual void rewind();
1544
1545
1546 protected:
1549
1551 virtual std::istream& getIstream() = 0;
1552
1554 virtual std::ostream& getOstream() = 0;
1555
1557 virtual std::iostream& getIOstream() = 0;
1558 };
1559
1560
1561 //--------------------------------------------------------------------------------
1563
1570 {
1571 public:
1573
1575 explicit BinaryBufferReaderWriter(const std::string& s);
1576
1579
1581 void open(const std::string& s);
1582
1584 std::string str() const;
1585
1587 std::string strPrimaryNode() const;
1588
1590 void flush() {}
1591
1593 void clear();
1594
1595 protected:
1598
1600 std::istream& getIstream() {return f;}
1601
1603 std::ostream& getOstream() {return f;}
1604
1606 std::iostream& getIOstream() {return f;}
1607
1608 private:
1610 QDPUtil::n_uint32_t checksum;
1611 std::stringstream f;
1612 };
1613
1614
1615 //--------------------------------------------------------------------------------
1617
1626 {
1627 public:
1629
1634
1639 explicit BinaryFileReaderWriter(const std::string& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
1640
1642
1645 bool is_open();
1646
1648
1651 void open(const std::string& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
1652
1654 void close();
1655
1657 void flush();
1658
1659 protected:
1662
1664 std::istream& getIstream() {return f;}
1665
1667 std::ostream& getOstream() {return f;}
1668
1670 std::iostream& getIOstream() {return f;}
1671
1672 private:
1674 QDPUtil::n_uint32_t checksum;
1675 std::fstream f;
1676 };
1677
1678 // end of group io
1680} // namespace QDP
1681
1682#endif
1d array, with 1-based indices
Definition qdp_arrays.h:18
int size() const
Definition qdp_arrays.h:38
void resize(int n)
Definition qdp_arrays.h:37
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:1565
void flush()
Flushes the buffer.
Definition qdp_io.h:1590
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:1603
void clear()
Clear the buffer.
Definition qdp_io.cc:1577
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:1547
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:1554
~BinaryBufferReaderWriter()
Closes the buffer.
Definition qdp_io.cc:1545
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:1600
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:1597
std::iostream & getIOstream()
Get the internal inpu/output stream.
Definition qdp_io.h:1606
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:929
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:942
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:931
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:932
void clear()
Clear the buffer.
Definition qdp_io.cc:954
~BinaryBufferReader()
Closes the buffer.
Definition qdp_io.cc:922
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:924
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:1420
~BinaryBufferWriter()
Closes the buffer.
Definition qdp_io.cc:1352
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:1354
void clear()
Clear the buffer.
Definition qdp_io.cc:1384
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:1372
void flush()
Flushes the buffer.
Definition qdp_io.h:1410
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:1361
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:1417
void close()
Closes the last file opened.
Definition qdp_io.cc:1608
void open(const std::string &p, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Opens a file for reading.
Definition qdp_io.cc:1597
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:1664
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:1667
std::iostream & getIOstream()
Get the internal inpu/output stream.
Definition qdp_io.h:1670
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:1618
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:1661
void flush()
Flushes the buffer.
Definition qdp_io.cc:1633
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:987
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:991
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:984
void open(const std::string &p)
Opens a file for reading.
Definition qdp_io.cc:970
void close()
Closes the last file opened.
Definition qdp_io.cc:981
QDPUtil::n_uint32_t & internalChecksum()
Get the current checksum to modify.
Definition qdp_io.h:1476
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:1422
void close()
Closes the last file opened.
Definition qdp_io.cc:1409
void flush()
Flushes the buffer.
Definition qdp_io.cc:1437
void open(const std::string &p)
Definition qdp_io.cc:1399
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:1479
Binary input/output base class.
Definition qdp_io.h:1499
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
std::iostream::off_type off_type
Definition qdp_io.h:1502
virtual std::iostream & getIOstream()=0
Get the internal input/output stream.
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:1482
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:1476
virtual ~BinaryReaderWriter()
Definition qdp_io.h:1508
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:1493
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:1453
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:1529
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:1502
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:1511
virtual std::ostream & getOstream()=0
Get the internal output stream.
std::iostream::pos_type pos_type
Definition qdp_io.h:1501
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:1465
virtual std::istream & getIstream()=0
Get the internal input stream.
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:1520
Binary input base class.
Definition qdp_io.h:372
std::istream::pos_type pos_type
Definition qdp_io.h:374
virtual void read(std::string &result, size_t nbytes)
Read some max number of characters - 1 upto and excluding a newline.
Definition qdp_io.cc:630
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:564
virtual void readArray(char *output, size_t nbytes, size_t nmemb)
Read data on the primary node and broadcast to all nodes.
Definition qdp_io.cc:706
virtual ~BinaryReader()
Definition qdp_io.h:381
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:591
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:600
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:582
void readPrimitive(T &output)
The universal data-reader.
Definition qdp_io.cc:701
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:547
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:536
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:553
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
virtual void readArrayLittleEndian(char *output, size_t nbytes, size_t nmemb)
Definition qdp_io.cc:734
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:524
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:573
virtual void readArrayPrimaryNodeLittleEndian(char *output, size_t nbytes, size_t nmemb)
Definition qdp_io.cc:754
virtual void readDesc(std::string &result)
Definition qdp_io.cc:610
virtual std::istream & getIstream()=0
Get the internal input stream.
std::istream::off_type off_type
Definition qdp_io.h:375
virtual void readArrayPrimaryNode(char *output, size_t nbytes, size_t nmemb)
Read data on the primary node only.
Definition qdp_io.cc:715
Binary writer base class.
Definition qdp_io.h:1010
virtual void writeArrayPrimaryNode(const char *output, size_t nbytes, size_t nmemb)
Write data on the primary node only.
Definition qdp_io.cc:1159
virtual void writeDesc(const std::string &output)
Writes a fixed length of characters like an array.
Definition qdp_io.cc:1089
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:1080
BinaryWriter()
Default constructor.
Definition qdp_io.cc:1012
virtual void flush()=0
Flushes the buffer.
void writePrimitive(const T &output)
The universal data-writer.
Definition qdp_io.cc:1154
virtual void writeArray(const char *output, size_t nbytes, size_t nmemb)
Write data from the primary node.
Definition qdp_io.cc:1182
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:1033
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
virtual std::ostream & getOstream()=0
Get the internal output stream.
virtual ~BinaryWriter()
Definition qdp_io.h:1022
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:1053
std::ostream::pos_type pos_type
Definition qdp_io.h:1012
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:1015
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:1044
virtual void write(const std::string &output)
Definition qdp_io.cc:1096
std::ostream::off_type off_type
Definition qdp_io.h:1013
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:1062
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:1188
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:1027
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:1071
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:196
~TextBufferReader()
Shutdown the buffer.
Definition qdp_io.cc:219
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:138
void open(const std::string &p)
Opens a buffer for reading.
Definition qdp_io.cc:189
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:207
void flush()
Flushes the buffer.
Definition qdp_io.h:293
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:442
~TextBufferWriter()
Shutdown the buffer.
Definition qdp_io.cc:465
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:453
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:303
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:435
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:247
std::istream & getIstream()
Get the internal input stream.
Definition qdp_io.h:187
void open(const std::string &p)
Opens a file for reading.
Definition qdp_io.cc:228
void close()
Closes the last file opened.
Definition qdp_io.cc:237
void flush()
Flushes the buffer.
Definition qdp_io.cc:508
std::ostream & getOstream()
Get the internal output stream.
Definition qdp_io.h:354
void open(const std::string &p)
Definition qdp_io.cc:474
void close()
Closes the last file opened.
Definition qdp_io.cc:483
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:493
Text input class.
Definition qdp_io.h:42
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:18
virtual std::istream & getIstream()=0
Get the internal input stream.
virtual void read(std::string &result)
Definition qdp_io.cc:30
virtual ~TextReader()
Definition qdp_io.h:49
void readPrimitive(T &output)
The universal data-reader.
Definition qdp_io.cc:111
Text output base class.
Definition qdp_io.h:203
virtual void write(const std::string &output)
Definition qdp_io.cc:356
virtual ~TextWriter()
Definition qdp_io.h:210
void writePrimitive(const T &output)
The universal data-writer.
Definition qdp_io.cc:422
virtual std::ostream & getOstream()=0
Get the internal output stream.
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:271
virtual void flush()=0
Flushes the object.
Container for a multi-dimensional 1D array.
Definition qdp_multi.h:25
int size() const
Size of array.
Definition qdp_multi.h:60
void resize(int ns1)
Resize routine, call a templated resize, using *this to disambiguate.
Definition qdp_multi.h:56
Container for a multi-dimensional 2D array.
Definition qdp_multi.h:640
void resize(int ns2, int ns1)
Allocate mem for the array.
Definition qdp_multi.h:657
int size1() const
Size of array.
Definition qdp_multi.h:673
int size2() const
Definition qdp_multi.h:674
Container for a multi-dimensional 3D array.
Definition qdp_multi.h:733
int size3() const
Definition qdp_multi.h:772
int size1() const
Size of array.
Definition qdp_multi.h:770
int size2() const
Definition qdp_multi.h:771
void resize(int ns3, int ns2, int ns1)
Allocate mem for the array.
Definition qdp_multi.h:750
Container for a multi-dimensional 4D array.
Definition qdp_multi.h:832
int size1() const
Size of array.
Definition qdp_multi.h:869
int size4() const
Definition qdp_multi.h:872
int size3() const
Definition qdp_multi.h:871
int size2() const
Definition qdp_multi.h:870
void resize(int ns4, int ns3, int ns2, int ns1)
Allocate mem for the array.
Definition qdp_multi.h:849
Container for a multi-dimensional 5D array.
Definition qdp_multi.h:928
int size2() const
Definition qdp_multi.h:966
int size1() const
Size of array.
Definition qdp_multi.h:965
int size4() const
Definition qdp_multi.h:968
int size5() const
Definition qdp_multi.h:969
void resize(int ns5, int ns4, int ns3, int ns2, int ns1)
Allocate mem for the array.
Definition qdp_multi.h:945
int size3() const
Definition qdp_multi.h:967
Container for a generic N dimensional array.
Definition qdp_multi.h:1026
void resize(const multi1d< int > &_nz)
Allocate mem for the array.
Definition qdp_multi.h:1042
T & getElem(int off)
Return ref to an element with index flattened over indices.
Definition qdp_multi.h:1227
int size(int i) const
Size of i-th array index. Indices run from left to right in operator().
Definition qdp_multi.h:1062
int numElem() const
Number of elements in the array.
Definition qdp_multi.h:1070
TextWriter & operator<<(TextWriter &txt, const std::string &output)
Definition qdp_io.cc:283
TextReader & operator>>(TextReader &txt, std::string &input)
Definition qdp_io.cc:121
void write(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1204
void readDesc(BinaryReader &bin, std::string &input)
Definition qdp_io.cc:774
void writeDesc(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1200
void read(BinaryReader &bin, std::string &input, size_t maxBytes)
Definition qdp_io.cc:778
unsigned int n_uint32_t
Yet another random number generator.
Byte manipulation.