CARMA C++
dbFFIO.h
Go to the documentation of this file.
1 #ifndef carma_monitor_dbFFIO_h
2 #define carma_monitor_dbFFIO_h
3 
8 #include <string.h>
9 #include <vector>
10 #include <fstream>
11 #include <complex>
12 #include <iostream>
13 #include <stdio.h>
14 #include <string>
15 
16 namespace carma {
17  namespace dbms {
18 
19 class dbFFIO {
20  public:
21  dbFFIO();
22  virtual ~dbFFIO();
23 
24 // Write the flat file header:
25 // <length of header in bytes> framecount signature
26 // Binary files don't use header length.
27  virtual void writeFFHeader(const long frameCount, const std::string &sig)=0;
28 
29  // Write instantaneous average to file.
30  virtual void dumpInstAverage(const long frameCount, const long tagID,
31  short blanking, short validity, short v,
32  int iSample)=0;
33  virtual void dumpInstAverage(const long frameCount, const long tagID,
34  short blanking, short validity, int v,
35  int iSample)=0;
36  virtual void dumpInstAverage(const long frameCount, const long tagID,
37  short blanking, short validity, long v,
38  int iSample)=0;
39  virtual void dumpInstAverage(const long frameCount, const long tagID,
40  short blanking, short validity, float v,
41  int iSample) = 0;
42  virtual void dumpInstAverage(const long frameCount, const long tagID,
43  short blanking, short validity, double v,
44  int iSample) = 0;
45  virtual void dumpInstAverage(const long frameCount, const long tagID,
46  short blanking, short validity,
47  const std::complex<float> &v,
48  int iSample)=0;
49  // Strings don't have iSamples, but it's easier to provide the argument.
50  virtual void dumpInstAverage(const long frameCount, const long tagID,
51  short blanking, short validity,
52  const std::string &v, int dmy=0)=0;
53 
55  // Returns values obtained when file was opened.
56  bool readFFHeader(long &frameCount, std::string &sig);
57 
58  virtual bool readInstAverage(long &frameCount, long &tagID,
59  short &blanking, short &validity,
60  short &v, int &iSample)=0;
61  virtual bool readInstAverage(long &frameCount, long &tagID,
62  short &blanking, short &validity,
63  int &v, int &iSample)=0;
64 
65  virtual bool readInstAverage(long &frameCount, long &tagID,
66  short &blanking, short &validity,
67  long &v, int &iSample)=0;
68  virtual bool readInstAverage(long &frameCount, long &tagID,
69  short &blanking, short &validity,
70  float &v, int &iSample)=0;
71  virtual bool readInstAverage(long &frameCount, long &tagID,
72  short &blanking, short &validity,
73  double &v, int &iSample)=0;
74  virtual bool readInstAverage(long &frameCount, long &tagID,
75  short &blanking, short &validity,
76  std::complex<float> &v, int &iSample)=0;
77  virtual bool readInstAverage(long &frameCount, long &tagID,
78  short &blanking, short &validity,
79  std::string &v)=0;
80 
82 // Write instantaneous average to file.
83  virtual void dumpLongAverage(const long frameCount, const long tagID,
84  short blanking, short validity,
85  short v,
86  short maxValue, short minValue,
87  int iSample,
88  short nValidSamples, int nTotalSamples)=0;
89  virtual void dumpLongAverage(const long frameCount, const long tagID,
90  short blanking, short validity,
91  int v,
92  int maxValue, int minValue,
93  int iSample,
94  short nValidSamples, int nTotalSamples)=0;
95  virtual void dumpLongAverage(const long frameCount, const long tagID,
96  short blanking, short validity,
97  long v,
98  long maxValue, long minValue,
99  int iSample,
100  short nValidSamples, int nTotalSamples)=0;
101  virtual void dumpLongAverage(const long frameCount, const long tagID,
102  short blanking, short validity, float v,
103  float maxValue, float minValue,
104  int iSample,
105  short nValidSamples, int nTotalSamples)=0;
106  virtual void dumpLongAverage(const long frameCount, const long tagID,
107  short blanking, short validity, double v,
108  double maxValue, double minValue,
109  int iSample,
110  short nValidSamples, int nTotalSamples)=0;
111  virtual void dumpLongAverage(const long frameCount, const long tagID,
112  short blanking, short validity,
113  const std::complex<float> &v,
114  const std::complex<float> maxValue,
115  const std::complex<float> minValue,
116  int iSample,
117  short nValidSamples, int nTotalSamples)=0;
118  virtual void dumpLongAverage(const long frameCount, const long tagID,
119  short blanking, short validity,
120  const std::string &v,
121  short nValidSamples, int nTotalSamples)=0;
122 
124 
125 
126  virtual bool readLongAverage(long &frameCount, long &tagID,
127  short &blanking, short &validity,
128  short &v,
129  short &maxValue, short &minValue,
130  int &iSample,
131  short &nValidSamples,
132  int &nTotalSamples)=0;
133  virtual bool readLongAverage(long &frameCount, long &tagID,
134  short &blanking, short &validity,
135  int &v,
136  int &maxValue, int &minValue,
137  int &iSample,
138  short &nValidSamples,
139  int &nTotalSamples)=0;
140  virtual bool readLongAverage(long &frameCount, long &tagID,
141  short &blanking, short &validity,
142  long &v,
143  long &maxValue, long &minValue,
144  int &iSample,
145  short &nValidSamples,
146  int &nTotalSamples)=0;
147  virtual bool readLongAverage(long &frameCount, long &tagID,
148  short &blanking, short &validity,
149  float &v,
150  float &maxValue, float &minValue,
151  int &iSample,
152  short &nValidSamples,
153  int &nTotalSamples)=0;
154  virtual bool readLongAverage(long &frameCount, long &tagID,
155  short &blanking, short &validity,
156  double &v,
157  double &maxValue, double &minValue,
158  int &iSample,
159  short &nValidSamples,
160  int &nTotalSamples)=0;
161  virtual bool readLongAverage(long &frameCount, long &tagID,
162  short &blanking, short &validity,
163  std::complex<float> &maxValue,
164  std::complex<float> &minValue,
165  std::complex<float> &v,
166  int &iSample,
167  short &nValidSamples,
168  int &nTotalSamples)=0;
169  virtual bool readLongAverage(long &frameCount, long &tagID,
170  short &blanking, short &validity,
171  std::string &v,
172  short &nValidSamples,
173  int &nTotalSamples)=0;
174 
176  // Types of records that are known.
177  enum RECORD_TYPE { RECORD_UNKNOWN=-1, RECORD_SHORT, RECORD_INTEGER,
178  RECORD_LONG, RECORD_FLOAT, RECORD_DOUBLE,
179  RECORD_COMPLEX, RECORD_STRING
180  };
181 
183  virtual bool open(const std::string &fileName, bool isWrite)=0;
184  bool open(const std::string &fileName, std::ios_base::openmode openmode);
185  virtual bool close();
186  virtual void flush();
187  inline bool isWrite(){return isWrite_ && isOpen();}
188  inline bool isRead(){return !isWrite_ && isOpen();}
189  inline bool eof()const {return file_.eof();}
190  bool isOpen(){return file_.is_open();}
191  virtual bool getRecordInfo(RECORD_TYPE &mvt,
192  unsigned &recordCount)=0;
193  static std::string valuetypeToString(int valuetype);
194  protected:
195  // Fill input buffer.
196  virtual bool fillBuffer() = 0;
197  // Write output buffer.
198  virtual void writeBuffer() = 0;
199 
200  protected:
201  std::string fileName_; // Name of opened file.
202  std::string askedFileName_; // Name of file that was asked for.
203  std::fstream file_;
204  bool isWrite_;
205  // Flat file header info.
206  long frameCount_;
207  std::string sig_;
208 };
209 
210 /*
211  * The format of the ASCII files generated should be the same as those
212  * produced by monitor/MonitorPointAccumulatorT.h and friends. The
213  * formating statements were taken from the original code.
214  * One difference is that a complete line is built before it is written.
215  * This should help minimize partially written lines in the output file.
216  */
217 
218 class dbFFIOa : public dbFFIO {
219  public:
220  dbFFIOa();
221  virtual ~dbFFIOa();
222 
223  virtual void writeFFHeader(const long frameCount, const std::string &sig);
224 
225  virtual void dumpInstAverage(const long frameCount, const long tagID,
226  short blanking, short validity, short v,
227  int iSample);
228  virtual void dumpInstAverage(const long frameCount, const long tagID,
229  short blanking, short validity, int v,
230  int iSample);
231  virtual void dumpInstAverage(const long frameCount, const long tagID,
232  short blanking, short validity, long v,
233  int iSample);
234  virtual void dumpInstAverage(const long frameCount, const long tagID,
235  short blanking, short validity, float v,
236  int iSample);
237  virtual void dumpInstAverage(const long frameCount, const long tagID,
238  short blanking, short validity, double v,
239  int iSample);
240  virtual void dumpInstAverage(const long frameCount, const long tagID,
241  short blanking, short validity,
242  const std::complex<float> &v,
243  int iSample);
244  virtual void dumpInstAverage(const long frameCount, const long tagID,
245  short blanking, short validity,
246  const std::string &v, int dmy=0);
247 
248  virtual bool readInstAverage(long &frameCount, long &tagID,
249  short &blanking, short &validity,
250  short &v, int &iSample);
251  virtual bool readInstAverage(long &frameCount, long &tagID,
252  short &blanking, short &validity,
253  int &v, int &iSample);
254  virtual bool readInstAverage(long &frameCount, long &tagID,
255  short &blanking, short &validity,
256  long &v, int &iSample);
257  virtual bool readInstAverage(long &frameCount, long &tagID,
258  short &blanking, short &validity,
259  float &v, int &iSample);
260  virtual bool readInstAverage(long &frameCount, long &tagID,
261  short &blanking, short &validity,
262  double &v, int &iSample);
263  virtual bool readInstAverage(long &frameCount, long &tagID,
264  short &blanking, short &validity,
265  std::complex<float> &v, int &iSample);
266  virtual bool readInstAverage(long &frameCount, long &tagID,
267  short &blanking, short &validity,
268  std::string &v);
270  virtual void dumpLongAverage(const long frameCount, const long tagID,
271  short blanking, short validity, short v,
272  short maxValue, short minValue,
273  int iSample,
274  short nValidSamples, int nTotalSamples);
275  virtual void dumpLongAverage(const long frameCount, const long tagID,
276  short blanking, short validity, int v,
277  int maxValue, int minValue,
278  int iSample,
279  short nValidSamples, int nTotalSamples);
280  virtual void dumpLongAverage(const long frameCount, const long tagID,
281  short blanking, short validity, long v,
282  long maxValue, long minValue,
283  int iSample,
284  short nValidSamples, int nTotalSamples);
285  virtual void dumpLongAverage(const long frameCount, const long tagID,
286  short blanking, short validity, float v,
287  float maxValue, float minValue,
288  int iSample,
289  short nValidSamples, int nTotalSamples);
290  virtual void dumpLongAverage(const long frameCount, const long tagID,
291  short blanking, short validity, double v,
292  double maxValue, double minValue,
293  int iSample,
294  short nValidSamples, int nTotalSamples);
295  virtual void dumpLongAverage(const long frameCount, const long tagID,
296  short blanking, short validity,
297  const std::complex<float> &v,
298  const std::complex<float> maxValue,
299  const std::complex<float> minValue,
300  int iSample,
301  short nValidSamples, int nTotalSamples);
302  virtual void dumpLongAverage(const long frameCount, const long tagID,
303  short blanking, short validity,
304  const std::string &v,
305  short nValidSamples, int nTotalSamples);
306 
307  virtual bool readLongAverage(long &frameCount, long &tagID,
308  short &blanking, short &validity,
309  short &v,
310  short &maxValue, short &minValue,
311  int &iSample,
312  short &nValidSamples,
313  int &nTotalSamples);
314  virtual bool readLongAverage(long &frameCount, long &tagID,
315  short &blanking, short &validity,
316  int &v,
317  int &maxValue, int &minValue,
318  int &iSample,
319  short &nValidSamples,
320  int &nTotalSamples);
321  virtual bool readLongAverage(long &frameCount, long &tagID,
322  short &blanking, short &validity,
323  long &v,
324  long &maxValue, long &minValue,
325  int &iSample,
326  short &nValidSamples,
327  int &nTotalSamples);
328  virtual bool readLongAverage(long &frameCount, long &tagID,
329  short &blanking, short &validity,
330  float &v,
331  float &maxValue, float &minValue,
332  int &iSample,
333  short &nValidSamples,
334  int &nTotalSamples);
335  virtual bool readLongAverage(long &frameCount, long &tagID,
336  short &blanking, short &validity,
337  double &v,
338  double &maxValue, double &minValue,
339  int &iSample,
340  short &nValidSamples,
341  int &nTotalSamples);
342  virtual bool readLongAverage(long &frameCount, long &tagID,
343  short &blanking, short &validity,
344  std::complex<float> &v,
345  std::complex<float> &maxValue,
346  std::complex<float> &minValue,
347  int &iSample,
348  short &nValidSamples,
349  int &nTotalSamples);
350  virtual bool readLongAverage(long &frameCount, long &tagID,
351  short &blanking, short &validity,
352  std::string &v,
353  short &nValidSamples,
354  int &nTotalSamples);
356  virtual bool open(const std::string &fileName, bool write);
357  virtual bool close();
358  virtual bool getRecordInfo(RECORD_TYPE &mvt,
359  unsigned &recordCount);
360  protected:
361  virtual bool fillBuffer();
362  virtual bool checkBuffer();
363  virtual void writeBuffer();
364  // This is taken from util/FileUtils.
365  unsigned int getMonitorDataFlatFileHeaderLength(const unsigned& sigLength) {
366  return (19 + sigLength);}
367 
368  void buildRecordFormat(const long frameCount, const long tagID,
369  short blanking, short validity, const char *fmt);
370 
371  bool getAverageProps(long &frameCount, long &tagID,
372  short &blanking, short &validity);
373 
374  // Convert tokens_ array into values.
375  bool tokenToValue(unsigned int index, short &v);
376  bool tokenToValue(unsigned int index, int &v);
377  bool tokenToValue(unsigned int index, long int &v);
378  bool tokenToValue(unsigned int index, float &v);
379  bool tokenToValue(unsigned int index, double &v);
380  bool tokenToValue(unsigned int index, std::complex<float> &v);
381  bool tokenToValue(unsigned int index, std::string &v);
382  bool getFFHeader();
383  private:
384  char format_[128]; // Holds the format string.
385  char lineBuffer_[512];
386  std::vector<const char *> tokens_; // Used to break apart an input line.
387 };
388 
389 /*
390  * Read/Write binary flat files.
391  */
392 class dbFFIOb : public dbFFIO {
393  public:
394  dbFFIOb();
395  virtual ~dbFFIOb();
396  virtual bool open(const std::string &fileName, bool write);
397  virtual bool close();
398  // Returns current record type and # of records.
399  // For input files, returns the number of records of the current type
400  // left to be processed. If recordCount would be 0, the next record
401  // header is read and that information is returned. If false is returned,
402  // no more records can be read or the file type is ASCII and therefore
403  // the information isn't available.
404  virtual bool getRecordInfo(RECORD_TYPE &mvt,
405  unsigned &recordCount);
406 
407  // This has to be the first thing written.
408  virtual void writeFFHeader(const long frameCount, const std::string &sig);
409 
414  static bool copyToASCII(const std::string &infileName, const
415  std::string &outfileName, bool isInstant);
417  virtual void dumpInstAverage(const long frameCount, const long tagID,
418  short blanking, short validity, short v,
419  int iSample);
420  virtual void dumpInstAverage(const long frameCount, const long tagID,
421  short blanking, short validity, int v,
422  int iSample);
423  virtual void dumpInstAverage(const long frameCount, const long tagID,
424  short blanking, short validity, long v,
425  int iSample);
426  virtual void dumpInstAverage(const long frameCount, const long tagID,
427  short blanking, short validity,
428  float v, int iSample);
429  virtual void dumpInstAverage(const long frameCount, const long tagID,
430  short blanking, short validity,
431  double v, int iSample);
432  virtual void dumpInstAverage(const long frameCount, const long tagID,
433  short blanking, short validity,
434  const std::complex<float> &v,
435  int iSample);
436  virtual void dumpInstAverage(const long frameCount, const long tagID,
437  short blanking, short validity,
438  const std::string &v, int dmy=0);
439 
440  virtual bool readInstAverage(long &frameCount, long &tagID,
441  short &blanking, short &validity,
442  short &v, int &iSample);
443  virtual bool readInstAverage(long &frameCount, long &tagID,
444  short &blanking, short &validity,
445  int &v, int &iSample);
446  virtual bool readInstAverage(long &frameCount, long &tagID,
447  short &blanking, short &validity,
448  long &v, int &iSample);
449  virtual bool readInstAverage(long &frameCount, long &tagID,
450  short &blanking, short &validity,
451  float &v, int &iSample);
452  virtual bool readInstAverage(long &frameCount, long &tagID,
453  short &blanking, short &validity,
454  double &v, int &iSample);
455  virtual bool readInstAverage(long &frameCount, long &tagID,
456  short &blanking, short &validity,
457  std::complex<float> &v, int &iSample);
458  virtual bool readInstAverage(long &frameCount, long &tagID,
459  short &blanking, short &validity,
460  std::string &v);
462  virtual void dumpLongAverage(const long frameCount, const long tagID,
463  short blanking, short validity, short v,
464  short maxValue, short minValue,
465  int iSample,
466  short nValidSamples, int nTotalSamples);
467  virtual void dumpLongAverage(const long frameCount, const long tagID,
468  short blanking, short validity, int v,
469  int maxValue, int minValue,
470  int iSample,
471  short nValidSamples, int nTotalSamples);
472  virtual void dumpLongAverage(const long frameCount, const long tagID,
473  short blanking, short validity, long v,
474  long maxValue, long minValue,
475  int iSample,
476  short nValidSamples, int nTotalSamples);
477  virtual void dumpLongAverage(const long frameCount, const long tagID,
478  short blanking, short validity, float v,
479  float maxValue, float minValue,
480  int iSample,
481  short nValidSamples, int nTotalSamples);
482  virtual void dumpLongAverage(const long frameCount, const long tagID,
483  short blanking, short validity, double v,
484  double maxValue, double minValue,
485  int iSample,
486  short nValidSamples, int nTotalSamples);
487  virtual void dumpLongAverage(const long frameCount, const long tagID,
488  short blanking, short validity,
489  const std::complex<float> &v,
490  const std::complex<float> maxValue,
491  const std::complex<float> minValue,
492  int iSample,
493  short nValidSamples, int nTotalSamples);
494  virtual void dumpLongAverage(const long frameCount, const long tagID,
495  short blanking, short validity,
496  const std::string &v,
497  short nValidSamples, int nTotalSamples);
498 
499  virtual bool readLongAverage(long &frameCount, long &tagID,
500  short &blanking, short &validity,
501  short &v,
502  short &maxValue, short &minValue,
503  int &iSample,
504  short &nValidSamples,
505  int &nTotalSamples);
506  virtual bool readLongAverage(long &frameCount, long &tagID,
507  short &blanking, short &validity,
508  int &v,
509  int &maxValue, int &minValue,
510  int &iSample,
511  short &nValidSamples,
512  int &nTotalSamples);
513  virtual bool readLongAverage(long &frameCount, long &tagID,
514  short &blanking, short &validity,
515  long &v,
516  long &maxValue, long &minValue,
517  int &iSample,
518  short &nValidSamples,
519  int &nTotalSamples);
520  virtual bool readLongAverage(long &frameCount, long &tagID,
521  short &blanking, short &validity,
522  float &v,
523  float &maxValue, float &minValue,
524  int &iSample,
525  short &nValidSamples,
526  int &nTotalSamples);
527  virtual bool readLongAverage(long &frameCount, long &tagID,
528  short &blanking, short &validity,
529  double &v,
530  double &maxValue, double &minValue,
531  int &iSample,
532  short &nValidSamples,
533  int &nTotalSamples);
534  virtual bool readLongAverage(long &frameCount, long &tagID,
535  short &blanking, short &validity,
536  std::complex<float> &v,
537  std::complex<float> &maxValue,
538  std::complex<float> &minValue,
539  int &iSample,
540  short &nValidSamples,
541  int &nTotalSamples);
542  virtual bool readLongAverage(long &frameCount, long &tagID,
543  short &blanking, short &validity,
544  std::string &v,
545  short &nValidSamples,
546  int &nTotalSamples);
548 
549  protected:
550  static bool copyInstantAverages(dbFFIOb &in, dbFFIOa &out);
551  static bool copyLongAverages(dbFFIOb &in, dbFFIOa &out);
552 
553  virtual bool fillBuffer();
554  // Make sure there are size bytes available for packing or unpacking.
555  virtual bool checkBuffer(RECORD_TYPE mvt, int size);
556  virtual bool checkBuffer(int size);
557  virtual void writeBuffer();
558  inline void pack(short v){pack(v, &byteArray_, &offset_);}
559  inline void pack(unsigned short v){pack(v, &byteArray_, &offset_);}
560  inline void pack(int v){pack(v, &byteArray_, &offset_);}
561  inline void pack(unsigned v){pack(v, &byteArray_, &offset_);}
562  inline void pack(long v){pack(v, &byteArray_, &offset_);}
563  inline void pack(float v){pack(v, &byteArray_, &offset_);}
564  inline void pack(double v){pack(v, &byteArray_, &offset_);}
565  inline void pack(const std::complex<float> &v)
566  {pack(v, &byteArray_, &offset_);}
567  inline void pack(const std::string &v){pack(v, &byteArray_, &offset_);}
568 
569  inline void unpack(short &v){unpack(v, byteArray_, &offset_);}
570  inline void unpack(unsigned short &v){unpack(v, byteArray_, &offset_);}
571  inline void unpack(int &v){unpack(v, byteArray_, &offset_);}
572  inline void unpack(unsigned &v){unpack(v, byteArray_, &offset_);}
573  inline void unpack(long &v){unpack(v, byteArray_, &offset_);}
574  inline void unpack(float &v){unpack(v, byteArray_, &offset_);}
575  inline void unpack(double &v){unpack(v, byteArray_, &offset_);}
576  inline void unpack(std::complex<float> &v){unpack(v, byteArray_, &offset_);}
577  inline void unpack(std::string &v){unpack(v, byteArray_, &offset_);}
578 
579  public:
580  // These routines were pretty much copied from util/Serializable.h.
581 
586  static inline void pack(int tmp, std::vector<char>* byteArray,
587  int* offset) {
588  // std::cout << "pack(int)" << std::endl;
589  int size = sizeof(tmp);
590  memcpy(&(*byteArray)[*offset], &tmp, size);
591  *offset += size;
592  }
593 
598  static inline void pack(unsigned tmp, std::vector<char>* byteArray,
599  int* offset) {
600  // std::cout << "pack(int)" << std::endl;
601  int size = sizeof(tmp);
602  memcpy(&(*byteArray)[*offset], &tmp, size);
603  *offset += size;
604  }
605 
610  static inline void pack(char tmp, std::vector<char>* byteArray,
611  int* offset) {
612  // std::cout << "pack(char)" << std::endl;
613  int size = sizeof(tmp);
614  memcpy(&(*byteArray)[*offset], &tmp, size);
615  *offset += size;
616  }
617 
618  static inline void pack(const std::string &v,
619  std::vector<char>* byteArray,
620  int* offset) {
621  // std::cout << "pack(char)" << std::endl;
622  int size = v.size() + 1;
623  memcpy(&(*byteArray)[*offset], v.c_str(), size);
624  *offset += size;
625  }
626 
631  static inline void pack(bool tmp, std::vector<char>* byteArray,
632  int* offset) {
633  // std::cout << "pack(bool)" << std::endl;
634  int size = sizeof(tmp);
635  memcpy(&(*byteArray)[*offset], &tmp, size);
636  *offset += size;
637  }
638 
643  static inline void pack(short tmp, std::vector<char>* byteArray,
644  int* offset) {
645  int size = sizeof(tmp);
646  memcpy(&(*byteArray)[*offset], &tmp, size);
647  *offset += size;
648  }
649 
650  static inline void pack(unsigned short tmp, std::vector<char>* byteArray,
651  int* offset)
652  {
653  // std::cout << "pack(short)" << std::endl;
654  int size = sizeof(tmp);
655  memcpy(&(*byteArray)[*offset], &tmp, size);
656  *offset += size;
657  }
658 
663  static inline void pack(long tmp, std::vector<char>* byteArray,
664  int* offset) {
665  // std::cout << "pack(long)" << std::endl;
666  int size = sizeof(tmp);
667  memcpy(&(*byteArray)[*offset], &tmp, size);
668  *offset += size;
669  }
670 
675  static inline void pack(float tmp, std::vector<char>* byteArray,
676  int* offset) {
677  // std::cout << "pack(float)" << std::endl;
678  int size = sizeof(tmp);
679  memcpy(&(*byteArray)[*offset], &tmp, size);
680  *offset += size;
681  }
682 
687  static inline void pack(double tmp, std::vector<char>* byteArray,
688  int* offset) {
689  // std::cout << "pack(double)" << std::endl;
690  int size = sizeof(tmp);
691  memcpy(&(*byteArray)[*offset], &tmp, size);
692  *offset += size;
693  }
694 
699  static inline void pack(const std::complex<float>& tmpc,
700  std::vector<char>* byteArray,
701  int* offset) {
702  //int start = byteArray_.size();
703  int size = sizeof(std::complex<float>);
704  int size2 = static_cast< int >(size * .5);
705  float tmp = tmpc.real();
706  memcpy(&(*byteArray)[*offset], &tmp, size2);
707  *offset += size2;
708 
709  tmp = tmpc.imag();
710  memcpy(&(*byteArray)[*offset], &tmp, size2);
711  *offset += size2;
712  }
713 
714  static inline void unpack(std::string &val,
715  const std::vector<char>& byteArray,
716  int* offset) {
717  val = &byteArray[*offset];
718  *offset += val.size() + 1;
719  }
720 
724  static inline void unpack(bool& val,
725  const std::vector<char>& byteArray,
726  int* offset) {
727  memcpy(&val, &byteArray[*offset], sizeof(val));
728  *offset += sizeof(val);
729  }
730 
734  static inline void unpack(short& val,
735  const std::vector<char>& byteArray,
736  int* offset) {
737  memcpy(&val, &byteArray[*offset], sizeof(val));
738  *offset += sizeof(val);
739  }
740 
741  static inline void unpack(unsigned short& val,
742  const std::vector<char>& byteArray,
743  int* offset) {
744  memcpy(&val, &byteArray[*offset], sizeof(val));
745  *offset += sizeof(val);
746  }
747 
751  static inline void unpack(int& val,
752  const std::vector<char>& byteArray,
753  int* offset) {
754  memcpy(&val, &byteArray[*offset], sizeof(val));
755  *offset += sizeof(val);
756  }
757 
761  static inline void unpack(unsigned& val,
762  const std::vector<char>& byteArray,
763  int* offset) {
764  memcpy(&val, &byteArray[*offset], sizeof(val));
765  *offset += sizeof(val);
766  }
767 
771  static inline void unpack(long& val,
772  const std::vector<char>& byteArray,
773  int* offset) {
774  memcpy(&val, &byteArray[*offset], sizeof(val));
775  *offset += sizeof(val);
776  }
777 
781  static inline void unpack(float& val,
782  const std::vector<char>& byteArray,
783  int* offset) {
784  memcpy(&val, &byteArray[*offset], sizeof(val));
785  *offset += sizeof(val);
786  }
787 
788 
792  static inline void unpack(double& val,
793  const std::vector<char>& byteArray,
794  int* offset) {
795  memcpy(&val, &byteArray[*offset], sizeof(val));
796  *offset += sizeof(val);
797  }
798 
802  static inline void unpack(std::complex<float>& tmpv,
803  const std::vector<char>& byteArray,
804  int* offset) {
805  int size = sizeof(std::complex<float>);
806  int size2 = static_cast< int >(size * .5);
807  float real;
808  float imag;
809  memcpy(&real, &byteArray[*offset], size2);
810  *offset += size2;
811  memcpy(&imag, &byteArray[*offset], size2);
812  *offset += size2;
813 
814  tmpv = std::complex<float>(real, imag);
815  }
816 
822  static inline void unpack(std::vector<short>& tmpv,
823  const std::vector<char>& byteArray,
824  int* offset) {
825  int size = tmpv.size() * sizeof(short);
826  memcpy(&tmpv[0], &byteArray[*offset], size);
827  //for (unsigned int idx = 0; idx < tmpv.size(); ++idx)
828  // tmpv[idx] = ntohf(tmpv[idx]);
829  *offset += size;
830  }
831 
837  static inline void unpack(std::vector<int>& tmpv,
838  const std::vector<char>& byteArray,
839  int* offset) {
840  int size = tmpv.size() * sizeof(int);
841  memcpy(&tmpv[0], &byteArray[*offset], size);
842  //for (unsigned int idx = 0; idx < tmpv.size(); ++idx)
843  // tmpv[idx] = ntohf(tmpv[idx]);
844  *offset += size;
845  }
846 
847 
853  static inline void unpack(std::vector<float>& tmpv,
854  const std::vector<char>& byteArray,
855  int* offset) {
856  int size = tmpv.size() * sizeof(float);
857  memcpy(&tmpv[0], &byteArray[*offset], size);
858  *offset += size;
859  }
860 
866  static inline void unpack(std::vector<std::complex<float> >& tmpv,
867  const std::vector<char>& byteArray,
868  int* offset) {
869  int length = tmpv.size();
870  int size2 = sizeof(float);
871 
872  float real;
873  float imag;
874  for (int idx = 0; idx < length; ++idx) {
875  memcpy(&real, &byteArray[*offset], size2);
876  *offset += size2;
877  memcpy(&imag, &byteArray[*offset], size2);
878  *offset += size2;
879  tmpv[idx] = std::complex<float>(real, imag);
880  }
881  }
882 
883  private:
884  void initb();
885  // Begin a series of records.
886  bool startRecordGroup(int mvt);
887  bool endRecordGroup(); // End a series of records.
888  // std::string typeToString(int)const;
889  // Called to track # of records & bytes processed.
890  void incRecordInfo(int size){recordCount_++;recordBytes_ += size;}
891  void decRecordInfo(int size){recordCount_--;recordBytes_ -= size;}
892  private:
893  // Holds serialized values.
894  std::vector<char> byteArray_;
895  int offset_; // Index into array of where next request goes.
896  // # of bytes read by last read + # bytes already there.
897  int bytesInBuffer_;
898  std::ostream::pos_type recordStart_;// Place in file where record starts.
899  int recordType_; // Type of record being read/written
900  unsigned recordCount_; // Count of # of records of this type.
901  unsigned recordBytes_; // # of bytes in this group of records.
902 };
903 
904  }
905 }
906 
907 #endif