RSTT  3.2.0
Regional Seismic Travel Time
All Classes Namespaces Files Functions Variables Typedefs Friends Macros
DataBuffer.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government retains certain rights in this software.
6 //-
7 //- BSD Open Source License
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37 
38 #ifndef DATABUFFER_H
39 #define DATABUFFER_H
40 
41 // **** _SYSTEM INCLUDES_ ******************************************************
42 
43 #include <iostream>
44 #include <cstring>
45 #include <fstream>
46 #include <sstream>
47 #include <string>
48 
49 using namespace std;
50 
51 // **** _LOCAL INCLUDES_ *******************************************************
52 
53 #include "UtilGlobals.h"
54 #include "CPPUtils.h"
55 
56 // **** _BEGIN SLBM NAMESPACE_ **************************************************
57 
58 namespace util {
59 
60 // **** _CLASS CONSTANTS_ ******************************************************
61 // **** _FORWARD REFERENCES_ ***************************************************
62 
63 // **** _CLASS DEFINITION_ *****************************************************
64 //
67 //
77 //
78 // *****************************************************************************
79 //class PGL_EXP_IMP DataBuffer : public DataObject
81 {
82  public:
83 
84  // **** _PUBLIC LIFECYCLES_ ************************************************
85 
87  //- Public Default Constructor.
88 
89  DataBuffer(bool doPad);
90  //- Standard constructor that sets the padding option to doPad.
91 
92  DataBuffer(string * str);
93  //- Standard constructor that uses a string ptr provided.
94 
95  DataBuffer(const DataBuffer& db);
96  //- Copy Constructor.
97 
98  virtual ~DataBuffer();
99  //- Public destructor
100 
101  // **** _PUBLIC OPERATORS_ *************************************************
102 
104  //- Assignment operator
105 
106  // **** _PUBLIC METHODS_ ***************************************************
107 
108  void dumpBuffer();
109  //- Debug dump of the data.
110 
111  void writeToFile(ofstream& ofs);
112  void writeToFile(fstream& ofs);
113  //- Write the buffer to the output stream ofs
114 
115  void readFromFile(fstream& ifs, int num_bytes);
116  void readFromFile(ifstream& ifs, int num_bytes);
117  void readFromFile(fstream& ifs, int64 filePos,
118  int num_bytes);
119  void readFromFile(ifstream& ifs, int64 filePos,
120  int num_bytes);
121  //- read the num_bytes from the input file stream (ifs) into the buffer.
122  //- The second form sets the streams input file pointer to file_pos as the
123  //- point to begin reading.
124 
125  const string& readMD5HashKey();
126  //- Returns the md5 hash key assumed to be stored at the beginning of this
127  //- DataBuffer (32 bytes). Don't call this function if a hash key was not
128  //- stored at the beginning of the buffer. The buffer pointer (dbDataPos)
129  //- is left unchanged on exit.
130 
132  //- Writes an md5 hash key generated from this DataBuffer into the first 32
133  //- locations of the DataBuffer. This function assumes that the first 32
134  //- positions of the DataBuffer were reserved for the hexadecimal string.
135 
137  //- Returns an MD5 hash key string that represents all the data
138  //- stored in this data buffer.
139 
140  void align4Byte();
141  void align8Byte();
142  //- These functions ensure that the buffer position pointer (dbDataPos) is
143  //- aligned on a 4 or 8 byte boundary, respectivly.
144 
145  void checkBufferSize(int sincr);
146  //- This function checks to make sure that the buffer is large enough to
147  //- contain sincr more bytes ... if not it is resized so that it can.
148 
149  void clear();
150  //- Clears the buffer.
151 
152  const string& readString();
153  void readString(string& str, int num_chars);
154  void readCharArray(char* array, int num_chars);
155  //- Read string data. readString assumes the string length immediately
156  //- precedes the string data. The other two functions read num_chars of
157  //- characters at a time into a string or character array.
158 
159  uByte readByte();
160  uByte readByte(int pos);
161  void readByteArray(uByte* array, int num_bytes);
162  //- Read uByte data. readByte reads one uByte from the DataBuffer and
163  //- updates buffer iterator. The second form specifies where to read but
164  //- does not update the iterator. The last form reads num_bytes into the
165  //- input array.
166 
167  bool readBool();
168  bool readBool(int pos);
169  void readBoolArray(bool* array, int num_bools);
170  //- Read bool data. readBool reads one bool from the DataBuffer and
171  //- updates buffer iterator. The second form specifies where to read but
172  //- does not update the iterator. The last form reads num_bool into the
173  //- input array.
174 
175  int readInt32();
176  int readInt32(int pos);
177  int readRawInt32();
178  void readIntArray(int* array, int num_ints);
179  //- Read int data. readInt32 reads one int from the DataBuffer and
180  //- updates buffer iterator. The second form specifies where to read but
181  //- does not update the iterator. The third form is like the first except
182  //- that byte alignment is assumed to be ok. This is faster but not as safe.
183  //- The last form reads num_ints into the input array.
184 
185  int64 readInt64();
186  int64 readInt64(int pos);
187  int64 readRawInt64();
188  void readLongArray(int64* array, int num_longs);
189  //- Read long data. readInt64 reads one long from the DataBuffer and
190  //- updates buffer iterator. The second form specifies where to read but
191  //- does not update the iterator. The third form is like the first except
192  //- that byte alignment is assumed to be ok. This is faster but not as safe.
193  //- The last form reads num_longs into the input array.
194 
195  float readFloat();
196  float readFloat(int pos);
197  float readRawFloat();
198  void readFloatArray(float* array, int num_floats);
199  //- Read float data. readFloat reads one float from the DataBuffer and
200  //- updates buffer iterator. The second form specifies where to read but
201  //- does not update the iterator. The third form is like the first except
202  //- that byte alignment is assumed to be ok. This is faster but not as safe.
203  //- The last form reads num_floats into the input array.
204 
205  double readDouble();
206  double readDouble(int pos);
207  double readRawDouble();
208  void readDoubleArray(double* array, int num_doubles);
209  //- Read double data. readDouble reads one double from the DataBuffer and
210  //- updates buffer iterator. The second form specifies where to read but
211  //- does not update the iterator. The third form is like the first except
212  //- that byte alignment is assumed to be ok. This is faster but not as safe.
213  //- The last form reads num_doubles into the input array.
214 
215  void writeString(const string& in_string);
216  void writeString(char* char_string);
217  void writeCharArray(const char* array, int num_chars);
218  //- Write string data to 'this' DataBuffer. The two writeString functions
219  //- first writes the the input string length immediately followed by the
220  //- string data. The last function writes num_chars from the input character
221  //- array into 'this' DataBuffer.
222 
223  void writeByte(uByte b);
224  void writeByte(uByte b, int pos);
225  void writeRawByte(uByte b);
226  void writeByteArray(const uByte* array, int num_bytes);
227  //- Write uByte data. writeByte writes one uByte to the DataBuffer and
228  //- updates buffer iterator. The second form specifies where to write but
229  //- does not update the iterator. The third form is like the first except
230  //- that buffer size checking is not performed and is assumed to be ok. The
231  //- last form writes num_bytes from the input array into the 'this'
232  //- DataBuffer.
233 
234  void writeBool(bool b);
235  void writeBool(bool b, int pos);
236  void writeRawBool(bool b);
237  void writeBoolArray(const bool* array, int num_bools);
238  //- Write bool data. writeBool writes one bool to the DataBuffer and
239  //- updates buffer iterator. The second form specifies where to write but
240  //- does not update the iterator. The third form is like the first except
241  //- that buffer size checking is not performed and is assumed to be ok. The
242  //- last form writes num_bools from the input array into the 'this'
243  //- DataBuffer.
244 
245  void writeInt32(int i);
246  void writeInt32(int i, int pos);
247  void writeRawInt32(int i);
248  void writeIntArray(const int* array, int num_ints);
249  //- Write int data. writeInt32 writes one int to the DataBuffer and
250  //- updates buffer iterator. The second form specifies where to write but
251  //- does not update the iterator. The third form is like the first except
252  //- that byte alignment and buffer size checking is not performed and is
253  //- assumed to be ok. This is faster but not as safe. The last form writes
254  //- num_ints from the input array into 'this' DataBuffer.
255 
256  void writeInt64(int64 i);
257  void writeInt64(int64 i, int pos);
258  void writeRawInt64(int64 i);
259  void writeLongArray(const int64* array, int num_longs);
260  //- Write long data. writeInt64 writes one long to the DataBuffer and
261  //- updates buffer iterator. The second form specifies where to write but
262  //- does not update the iterator. The third form is like the first except
263  //- that byte alignment and buffer size checking is not performed and is
264  //- assumed to be ok. This is faster but not as safe. The last form writes
265  //- num_longs from the input array into 'this' DataBuffer.
266 
267  void writeFloat(float f);
268  void writeFloat(float f, int pos);
269  void writeRawFloat(float f);
270  void writeFloatArray(const float* array, int num_floats);
271  //- Write float data. writeFloat writes one float to the DataBuffer and
272  //- updates buffer iterator. The second form specifies where to write but
273  //- does not update the iterator. The third form is like the first except
274  //- that byte alignment and buffer size checking is not performed and is
275  //- assumed to be ok. This is faster but not as safe. The last form writes
276  //- num_floats from the input array into 'this' DataBuffer.
277 
278  void writeDouble(double d);
279  void writeDouble(double d, int pos);
280  void writeRawDouble(double d);
281  void writeDoubleArray(const double* array, int num_doubles);
282  //- Write double data. writeDouble writes one double to the DataBuffer and
283  //- updates buffer iterator. The second form specifies where to write but
284  //- does not update the iterator. The third form is like the first except
285  //- that byte alignment and buffer size checking is not performed and is
286  //- assumed to be ok. This is faster but not as safe. The last form writes
287  //- num_doubles from the input array into 'this' DataBuffer.
288 
289  static void reverseBOArray(int n, char* a, int s);
290  //- Reverses each s-byte element of array a containing n elements
291  //- (s*n bytes). The element size s must be 2, 4, or 8.
292 
293  static void reverseBO2Array(int n, char* a);
294  static void reverseBO4Array(int n, char* a);
295  static void reverseBO8Array(int n, char* a);
296  //- Reverses the byte order of each element of a where a is an array of n
297  //- 2, 4, or 8 byte elements. Used to convert between big-and
298  //- little-endian formats.
299 
300  static void reverseBO2(char* d);
301  static void reverseBO4(char* d);
302  static void reverseBO8(char* d);
303  //- Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used
304  //- to convert between big-and little-endian formats.
305 
306  // **** _PUBLIC PROPERTIES_ ************************************************
307 
308  static string class_name();
309  virtual string get_class_name() const;
310  virtual int class_size() const;
311  //- Return class name and size.
312 
313  void setSize(int num_bytes);
314  //- This function reserves num_bytes in the buffer to minimize reallocation.
315 
316  int size();
317  //- Returns the DataBuffer size.
318 
319  void resetPos();
320  void setPosToEnd();
321  int getPos() const;
322  //- Reset, set, and get the current iterator position.
323 
324  void incrementPos(int increment);
325  void decrementPos(int decrement);
326  //- Increment / decrement the iterator position.
327 
328  string getData() const;
329  //- Return a const reference to 'this' DataBuffers data.
330 
331  int getCapacity() const;
332  //- Return the allocated capacity of this DataBuffer.
333 
334  char* getPosPointer();
335  //- Return a pointer at the current iterator location in the DataBuffers
336  //- data.
337 
338  char* getPosPointer(int pos);
339  //- Return a pointer to position pos in 'this' DataBuffers data.
340 
341  void setByteOrderReverse(bool bor);
342  void byteOrderReverseOn();
343  void byteOrderReverseOff();
344  //- Sets the byte order reverse flag to bor (1st function), true (on
345  //- function) or false (off function).
346 
347  static int getAllocationReqSize();
348  //- Returns the default DataBuffer allocation size.
349 
350  // **** _PUBLIC INQUIRIES_ *************************************************
351 
352  bool isByteOrderReversed() const;
353  //- Returns true if the byte order in reads and writes is reversed for all
354  //- 2, 4, or 8 byte intrinsincs (short, int, long, float, double, etc.).
355 
356  // **** _PUBLIC DATA_ ******************************************************
357 
359  static const int ALLOCATION_REQ_SIZE;
360 
362  void reserve(int sze);
363 
364  protected:
365 
366  // **** _PRIVATE LIFECYCLES_ ***********************************************
367  // **** _PROTECTED OPERATORS_ ***********************************************
368  // **** _PROTECTED METHODS_ *************************************************
369 
370  // **** _PROTECTED DATA_ ****************************************************
371 
373  string* dbData;
374 
377 
379  int dbSize;
380 
383  bool dbPad;
384 
390  bool dbReverse;
391 
395  bool dbOwnStr;
396 
397 }; // DataBuffer End Definition
398 
399 // **** _INLINE FUNCTION IMPLEMENTATIONS_ **************************************
400 
401 // **** _FUNCTION DESCRIPTION_ *************************************************
402 //
404 //
405 // *****************************************************************************
406 inline string DataBuffer::class_name()
407 {
408  return "DataBuffer";
409 }
410 
411 // **** _FUNCTION DESCRIPTION_ *************************************************
412 //
414 //
415 // *****************************************************************************
416 inline string DataBuffer::get_class_name() const
417 {
418  return class_name();
419 }
420 
421 // **** _FUNCTION DESCRIPTION_ *************************************************
422 //
424 //
425 // *****************************************************************************
426 inline int DataBuffer::class_size() const
427 {
428  return (int) sizeof(DataBuffer);
429 }
430 
431 // **** _FUNCTION DESCRIPTION_ *************************************************
432 //
435 //
436 // *****************************************************************************
437 inline void DataBuffer::align4Byte()
438 {
439  // get position from an 4 byte alignment location
440 
441  int sm = dbDataPos % sizeof(int);
442 
443  // If not zero and dbPad is not set then add pad
444 
445  if (sm && dbPad) dbDataPos += sizeof(int) - sm;
446 }
447 
448 // **** _FUNCTION DESCRIPTION_ *************************************************
449 //
452 //
453 // *****************************************************************************
454 inline void DataBuffer::align8Byte()
455 {
456  // get position from an 8 byte alignment location
457 
458  int sm = dbDataPos % sizeof(double);
459 
460  // If not zero and dbPad is not set then add pad
461 
462  if (sm && dbPad) dbDataPos += sizeof(double) - sm;
463 }
464 
465 // **** _FUNCTION DESCRIPTION_ *************************************************
466 //
470 //
471 // *****************************************************************************
472 inline void DataBuffer::checkBufferSize(int sincr)
473 {
474  // append spaces to dbData if its current size is less than the position
475  // pointer + sincr.
476 
477  if (dbDataPos + sincr > (int) dbData->size())
478  dbData->append(dbDataPos + sincr - dbData->size(), ' ');
479 }
480 
481 // **** _FUNCTION DESCRIPTION_ *************************************************
482 //
484 //
485 // *****************************************************************************
486 inline void DataBuffer::clear()
487 {
488  (*dbData) = "";
489  dbDataPos = 0;
490  dbSize = 0;
491 }
492 
493 //----------
494 //---------------------------- READ FUNCTIONS ------------------------//
495 //----------
496 
497 // **** _FUNCTION DESCRIPTION_ *************************************************
498 //
502 //
503 // *****************************************************************************
504 inline const string& DataBuffer::readString()
505 {
506  int len;
507  static string s;
508 
509  // read the data
510 
511  len = readInt32();
512  s = dbData->substr(dbDataPos, len);
513  dbDataPos += len;
514 
515  return s;
516 }
517 
518 // **** _FUNCTION DESCRIPTION_ *************************************************
519 //
521 //
522 // *****************************************************************************
523 inline void DataBuffer::readString(string& str, int num_chars)
524 {
525  // read the data
526 
527  str = dbData->substr(dbDataPos, num_chars);
528  dbDataPos += num_chars;
529 }
530 
531 // **** _FUNCTION DESCRIPTION_ *************************************************
532 //
534 //
535 // *****************************************************************************
536 inline void DataBuffer::readCharArray(char* array, int num_chars)
537 {
538  // read the data
539 
540  memcpy(array, &(*dbData)[dbDataPos], num_chars);
541  dbDataPos += num_chars;
542 }
543 
544 // **** _FUNCTION DESCRIPTION_ *************************************************
545 //
547 //
548 // *****************************************************************************
549 inline uByte DataBuffer::readByte()
550 {
551  // read the data
552 
553  uByte b = *((uByte*) &(*dbData)[dbDataPos]);
554  dbDataPos += sizeof(uByte);
555  return b;
556 }
557 
558 // **** _FUNCTION DESCRIPTION_ *************************************************
559 //
562 //
563 // *****************************************************************************
564 inline uByte DataBuffer::readByte(int pos)
565 {
566  // read the data
567 
568  uByte b = *((uByte*) &(*dbData)[pos]);
569  return b;
570 }
571 
572 // **** _FUNCTION DESCRIPTION_ *************************************************
573 //
575 //
576 // *****************************************************************************
577 inline void DataBuffer::readByteArray(uByte* array, int num_bytes)
578 {
579  // read the data
580 
581  memcpy(array, &(*dbData)[dbDataPos], num_bytes);
582  dbDataPos += num_bytes;
583 }
584 
585 // **** _FUNCTION DESCRIPTION_ *************************************************
586 //
588 //
589 // *****************************************************************************
590 inline bool DataBuffer::readBool()
591 {
592  // read the data
593 
594  bool b = *((bool*) &(*dbData)[dbDataPos]);
595  dbDataPos += sizeof(bool);
596  return b;
597 }
598 
599 // **** _FUNCTION DESCRIPTION_ *************************************************
600 //
603 //
604 // *****************************************************************************
605 inline bool DataBuffer::readBool(int pos)
606 {
607  // read the data
608 
609  bool b = *((bool*) &(*dbData)[pos]);
610  return b;
611 }
612 
613 // **** _FUNCTION DESCRIPTION_ *************************************************
614 //
616 //
617 // *****************************************************************************
618 inline void DataBuffer::readBoolArray(bool* array, int num_bools)
619 {
620  // read the data
621 
622  memcpy(array, &(*dbData)[dbDataPos], num_bools);
623  dbDataPos += num_bools;
624 }
625 
626 // **** _FUNCTION DESCRIPTION_ *************************************************
627 //
630 //
631 // *****************************************************************************
632 inline int DataBuffer::readInt32()
633 {
634  // check alignment
635 
636  align4Byte();
637 
638  // read the data
639 
640  return readRawInt32();
641 }
642 
643 // **** _FUNCTION DESCRIPTION_ *************************************************
644 //
647 //
648 // *****************************************************************************
649 inline int DataBuffer::readInt32(int pos)
650 {
651  // read the data
652 
653  int i = *((int*) &(*dbData)[pos]);
654  if (dbReverse) reverseBO4((char*) &i);
655  return i;
656 }
657 
658 // **** _FUNCTION DESCRIPTION_ *************************************************
659 //
662 //
663 // *****************************************************************************
664 inline int DataBuffer::readRawInt32()
665 {
666  // read the data
667 
668  int i = *((int*) &(*dbData)[dbDataPos]);
669  if (dbReverse) reverseBO4((char*) &i);
670  dbDataPos += sizeof(int);
671  return i;
672 }
673 
674 // **** _FUNCTION DESCRIPTION_ *************************************************
675 //
677 //
678 // *****************************************************************************
679 inline void DataBuffer::readIntArray(int* array, int num_ints)
680 {
681  int num_bytes = num_ints * sizeof(int);
682 
683  // check alignment
684 
685  align4Byte();
686 
687  // read the data
688  memcpy(array, &(*dbData)[dbDataPos], num_bytes);
689  if (dbReverse) reverseBO4Array(num_ints, (char*) array);
690  dbDataPos += num_bytes;
691 }
692 
693 // **** _FUNCTION DESCRIPTION_ *************************************************
694 //
697 //
698 // *****************************************************************************
699 inline int64 DataBuffer::readInt64()
700 {
701  // check alignment
702 
703  align8Byte();
704 
705  // read the data
706 
707  return readRawInt64();
708 }
709 
710 // **** _FUNCTION DESCRIPTION_ *************************************************
711 //
714 //
715 // *****************************************************************************
716 inline int64 DataBuffer::readInt64(int pos)
717 {
718  // read the data
719 
720  int64 i = *((int64*) &(*dbData)[pos]);
721  if (dbReverse) reverseBO8((char*) &i);
722  return i;
723 }
724 
725 // **** _FUNCTION DESCRIPTION_ *************************************************
726 //
729 //
730 // *****************************************************************************
731 inline int64 DataBuffer::readRawInt64()
732 {
733  // read the data
734 
735  int64 i = *((int64*) &(*dbData)[dbDataPos]);
736  if (dbReverse) reverseBO8((char*) &i);
737  dbDataPos += sizeof(int64);
738  return i;
739 }
740 
741 // **** _FUNCTION DESCRIPTION_ *************************************************
742 //
744 //
745 // *****************************************************************************
746 inline void DataBuffer::readLongArray(int64* array, int num_longs)
747 {
748  int num_bytes = num_longs * sizeof(int64);
749 
750  // check alignment
751 
752  align8Byte();
753 
754  // read the data
755 
756  memcpy(array, &(*dbData)[dbDataPos], num_bytes);
757  if (dbReverse) reverseBO8Array(num_longs, (char*) array);
758  dbDataPos += num_bytes;
759 }
760 
761 // **** _FUNCTION DESCRIPTION_ *************************************************
762 //
765 //
766 // *****************************************************************************
767 inline float DataBuffer::readFloat()
768 {
769  // check alignment
770 
771  align4Byte();
772 
773  // read the data
774 
775  return readRawFloat();
776 }
777 
778 // **** _FUNCTION DESCRIPTION_ *************************************************
779 //
782 //
783 // *****************************************************************************
784 inline float DataBuffer::readFloat(int pos)
785 {
786  // read the data
787 
788  float f = *((float*) &(*dbData)[pos]);
789  if (dbReverse) reverseBO4((char*) &f);
790  return f;
791 }
792 
793 // **** _FUNCTION DESCRIPTION_ *************************************************
794 //
797 //
798 // *****************************************************************************
799 inline float DataBuffer::readRawFloat()
800 {
801  // read the data
802 
803  float f = *((float*) &(*dbData)[dbDataPos]);
804  if (dbReverse) reverseBO4((char*) &f);
805  dbDataPos += sizeof(float);
806  return f;
807 }
808 
809 // **** _FUNCTION DESCRIPTION_ *************************************************
810 //
812 //
813 // *****************************************************************************
814 inline void DataBuffer::readFloatArray(float* array, int num_floats)
815 {
816  int num_bytes = num_floats * sizeof(float);
817 
818  // check alignment
819 
820  align4Byte();
821 
822  // read the data
823 
824  memcpy(array, &(*dbData)[dbDataPos], num_bytes);
825  if (dbReverse) reverseBO4Array(num_floats, (char*) array);
826  dbDataPos += num_bytes;
827 }
828 
829 // **** _FUNCTION DESCRIPTION_ *************************************************
830 //
833 //
834 // *****************************************************************************
835 inline double DataBuffer::readDouble()
836 {
837  // check alignment
838 
839  align8Byte();
840 
841  // read the data
842 
843  return readRawDouble();
844 }
845 
846 // **** _FUNCTION DESCRIPTION_ *************************************************
847 //
850 //
851 // *****************************************************************************
852 inline double DataBuffer::readDouble(int pos)
853 {
854  // read the data
855 
856  double d = *((double*) &(*dbData)[pos]);
857  if (dbReverse) reverseBO8((char*) &d);
858  return d;
859 }
860 
861 // **** _FUNCTION DESCRIPTION_ *************************************************
862 //
865 //
866 // *****************************************************************************
867 inline double DataBuffer::readRawDouble()
868 {
869  // read the data
870 
871  double d = *((double*) &(*dbData)[dbDataPos]);
872  if (dbReverse) reverseBO8((char*) &d);
873  dbDataPos += sizeof(double);
874  return d;
875 }
876 
877 // **** _FUNCTION DESCRIPTION_ *************************************************
878 //
880 //
881 // *****************************************************************************
882 inline void DataBuffer::readDoubleArray(double* array, int num_doubles)
883 {
884  int num_bytes = num_doubles * sizeof(double);
885 
886  // check alignment
887 
888  align8Byte();
889 
890  // read the data
891 
892  memcpy(array, &(*dbData)[dbDataPos], num_bytes);
893  if (dbReverse) reverseBO8Array(num_doubles, (char*) array);
894  dbDataPos += num_bytes;
895 }
896 
897 //----------
898 //---------------------------- WRITE FUNCTIONS ------------------------//
899 //----------
900 
901 // **** _FUNCTION DESCRIPTION_ *************************************************
902 //
904 //
905 // *****************************************************************************
906 inline void DataBuffer::writeString(const string& in_string)
907 {
908  // get total size
909 
910  int sz = (int) in_string.size() + sizeof(int);
911 
912  // check alignment
913 
914  align4Byte();
915 
916  // check buffer size
917 
918  checkBufferSize(sz);
919 
920  // write the data
921 
922  writeRawInt32((int) in_string.size());
923 
924  // In Sun's V5.2 CC compiler, string replace seems to have a bug or
925  // odd behavior that is not understood. Whenever a replace occurs
926  // using the following line, the strings size() is reduced by
927  // dbDataPos bytes??? So we will use memcpy instead!
928  // dbData.replace(dbDataPos, dbDataPos + in_string.size(), in_string);
929  memcpy(&(*dbData)[dbDataPos], &in_string[0], in_string.size());
930 
931  dbDataPos += (int) in_string.size();
932 }
933 
934 // **** _FUNCTION DESCRIPTION_ *************************************************
935 //
937 //
938 // *****************************************************************************
939 inline void DataBuffer::writeString(char* char_string)
940 {
941  string tmp_string = char_string;
942  writeString(tmp_string);
943 }
944 
945 // **** _FUNCTION DESCRIPTION_ *************************************************
946 //
948 //
949 // *****************************************************************************
950 inline void DataBuffer::writeCharArray(const char* array, int num_chars)
951 {
952  // check buffer size
953 
954  checkBufferSize(num_chars);
955 
956  // write data
957 
958  memcpy(&(*dbData)[dbDataPos], array, num_chars);
959  dbDataPos += num_chars;
960 }
961 
962 // **** _FUNCTION DESCRIPTION_ *************************************************
963 //
966 //
967 // *****************************************************************************
968 inline void DataBuffer::writeByte(uByte b)
969 {
970  // check buffer size
971 
972  checkBufferSize(sizeof(uByte));
973 
974  // write the data
975 
976  writeRawByte(b);
977 }
978 
979 // **** _FUNCTION DESCRIPTION_ *************************************************
980 //
983 //
984 // *****************************************************************************
985 inline void DataBuffer::writeByte(uByte b, int pos)
986 {
987  // write the data
988 
989  *((uByte*) &(*dbData)[pos]) = b;
990 }
991 
992 // **** _FUNCTION DESCRIPTION_ *************************************************
993 //
997 //
998 // *****************************************************************************
999 inline void DataBuffer::writeRawByte(uByte b)
1000 {
1001  // write the data
1002 
1003  *((uByte*) &(*dbData)[dbDataPos]) = b;
1004  dbDataPos += sizeof(uByte);
1005 }
1006 
1007 // **** _FUNCTION DESCRIPTION_ *************************************************
1008 //
1010 //
1011 // *****************************************************************************
1012 inline void DataBuffer::writeByteArray(const uByte* array, int num_bytes)
1013 {
1014  // check buffer size
1015 
1016  checkBufferSize(num_bytes);
1017 
1018  // write data
1019 
1020  memcpy(&(*dbData)[dbDataPos], array, num_bytes);
1021  dbDataPos += num_bytes;
1022 }
1023 
1024 // **** _FUNCTION DESCRIPTION_ *************************************************
1025 //
1028 //
1029 // *****************************************************************************
1030 inline void DataBuffer::writeBool(bool b)
1031 {
1032  // check buffer size
1033 
1034  checkBufferSize(sizeof(bool));
1035 
1036  // write the data
1037 
1038  writeRawBool(b);
1039 }
1040 
1041 // **** _FUNCTION DESCRIPTION_ *************************************************
1042 //
1045 //
1046 // *****************************************************************************
1047 inline void DataBuffer::writeBool(bool b, int pos)
1048 {
1049  // write the data
1050 
1051  *((bool*) &(*dbData)[pos]) = b;
1052 }
1053 
1054 // **** _FUNCTION DESCRIPTION_ *************************************************
1055 //
1059 //
1060 // *****************************************************************************
1061 inline void DataBuffer::writeRawBool(bool b)
1062 {
1063  // write the data
1064 
1065  *((bool*) &(*dbData)[dbDataPos]) = b;
1066  dbDataPos += sizeof(bool);
1067 }
1068 
1069 // **** _FUNCTION DESCRIPTION_ *************************************************
1070 //
1072 //
1073 // *****************************************************************************
1074 inline void DataBuffer::writeBoolArray(const bool* array, int num_bools)
1075 {
1076  // check buffer size
1077 
1078  checkBufferSize(num_bools);
1079 
1080  // write data
1081 
1082  memcpy(&(*dbData)[dbDataPos], array, num_bools);
1083  dbDataPos += num_bools;
1084 }
1085 
1086 // **** _FUNCTION DESCRIPTION_ *************************************************
1087 //
1090 //
1091 // *****************************************************************************
1092 inline void DataBuffer::writeInt32(int i)
1093 {
1094  // check alignment
1095 
1096  align4Byte();
1097 
1098  // check buffer size
1099 
1100  checkBufferSize(sizeof(int));
1101 
1102  // write the data
1103 
1104  writeRawInt32(i);
1105 }
1106 
1107 // **** _FUNCTION DESCRIPTION_ *************************************************
1108 //
1111 //
1112 // *****************************************************************************
1113 inline void DataBuffer::writeInt32(int i, int pos)
1114 {
1115  // write the data
1116 
1117  if (dbReverse) reverseBO4((char*) &i);
1118  *((int*) &(*dbData)[pos]) = i;
1119 }
1120 
1121 // **** _FUNCTION DESCRIPTION_ *************************************************
1122 //
1126 //
1127 // *****************************************************************************
1128 inline void DataBuffer::writeRawInt32(int i)
1129 {
1130  // write the data
1131 
1132  if (dbReverse) reverseBO4((char*) &i);
1133  *((int*) &(*dbData)[dbDataPos]) = i;
1134  dbDataPos += sizeof(int);
1135 }
1136 
1137 // **** _FUNCTION DESCRIPTION_ *************************************************
1138 //
1140 //
1141 // *****************************************************************************
1142 inline void DataBuffer::writeIntArray(const int* array, int num_ints)
1143 {
1144  int num_bytes = num_ints * sizeof(int);
1145 
1146  // check alignment
1147 
1148  align4Byte();
1149 
1150  // check buffer size
1151 
1152  checkBufferSize(num_bytes);
1153 
1154  // write data
1155 
1156  memcpy(&(*dbData)[dbDataPos], array, num_bytes);
1157  if (dbReverse) reverseBO4Array(num_ints, &(*dbData)[dbDataPos]);
1158  dbDataPos += num_bytes;
1159 }
1160 
1161 // **** _FUNCTION DESCRIPTION_ *************************************************
1162 //
1165 //
1166 // *****************************************************************************
1167 inline void DataBuffer::writeInt64(int64 i)
1168 {
1169  // check alignment
1170 
1171  align8Byte();
1172 
1173  // check buffer size
1174 
1175  checkBufferSize(sizeof(int64));
1176 
1177  // write the data
1178 
1179  writeRawInt64(i);
1180 }
1181 
1182 // **** _FUNCTION DESCRIPTION_ *************************************************
1183 //
1186 //
1187 // *****************************************************************************
1188 inline void DataBuffer::writeInt64(int64 i, int pos)
1189 {
1190  // write the data
1191 
1192  if (dbReverse) reverseBO8((char*) &i);
1193  *((int64*) &(*dbData)[pos]) = i;
1194 }
1195 
1196 // **** _FUNCTION DESCRIPTION_ *************************************************
1197 //
1201 //
1202 // *****************************************************************************
1203 inline void DataBuffer::writeRawInt64 (int64 i)
1204 {
1205  // write the data
1206 
1207  if (dbReverse) reverseBO8((char*) &i);
1208  *((int64*) &(*dbData)[dbDataPos]) = i;
1209  dbDataPos += sizeof(int64);
1210 }
1211 
1212 // **** _FUNCTION DESCRIPTION_ *************************************************
1213 //
1215 //
1216 // *****************************************************************************
1217 inline void DataBuffer::writeLongArray(const int64* array, int num_longs)
1218 {
1219  int num_bytes = num_longs * sizeof(int64);
1220 
1221  // check alignment
1222 
1223  align8Byte();
1224 
1225  // check buffer size
1226 
1227  checkBufferSize(num_bytes);
1228 
1229  // write data
1230 
1231  memcpy(&(*dbData)[dbDataPos], array, num_bytes);
1232  if (dbReverse) reverseBO8Array(num_longs, &(*dbData)[dbDataPos]);
1233  dbDataPos += num_bytes;
1234 }
1235 
1236 // **** _FUNCTION DESCRIPTION_ *************************************************
1237 //
1240 //
1241 // *****************************************************************************
1242 inline void DataBuffer::writeFloat(float f)
1243 {
1244  // check alignment
1245 
1246  align4Byte();
1247 
1248  // check buffer size
1249 
1250  checkBufferSize(sizeof(float));
1251 
1252  // write the data
1253 
1254  writeRawFloat(f);
1255 }
1256 
1257 // **** _FUNCTION DESCRIPTION_ *************************************************
1258 //
1261 //
1262 // *****************************************************************************
1263 inline void DataBuffer::writeFloat(float f, int pos)
1264 {
1265  // write the data
1266 
1267  if (dbReverse) reverseBO4((char*) &f);
1268  *((float*) &(*dbData)[pos]) = f;
1269 }
1270 
1271 // **** _FUNCTION DESCRIPTION_ *************************************************
1272 //
1276 //
1277 // *****************************************************************************
1278 inline void DataBuffer::writeRawFloat(float f)
1279 {
1280  // write the data
1281 
1282  if (dbReverse) reverseBO4((char*) &f);
1283  *((float*) &(*dbData)[dbDataPos]) = f;
1284  dbDataPos += sizeof(float);
1285 }
1286 
1287 // **** _FUNCTION DESCRIPTION_ *************************************************
1288 //
1290 //
1291 // *****************************************************************************
1292 inline void DataBuffer::writeFloatArray(const float* array, int num_floats)
1293 {
1294  int num_bytes = num_floats * sizeof(float);
1295 
1296  // check alignment
1297 
1298  align4Byte();
1299 
1300  // check buffer size
1301 
1302  checkBufferSize(num_bytes);
1303 
1304  // write data
1305 
1306  memcpy(&(*dbData)[dbDataPos], array, num_bytes);
1307  if (dbReverse) reverseBO4Array(num_floats, &(*dbData)[dbDataPos]);
1308  dbDataPos += num_bytes;
1309 }
1310 
1311 // **** _FUNCTION DESCRIPTION_ *************************************************
1312 //
1315 //
1316 // *****************************************************************************
1317 inline void DataBuffer::writeDouble(double d)
1318 {
1319  // check alignment
1320 
1321  align8Byte();
1322 
1323  // check buffer size
1324 
1325  checkBufferSize(sizeof(double));
1326 
1327  // write data
1328 
1329  writeRawDouble(d);
1330 }
1331 
1332 // **** _FUNCTION DESCRIPTION_ *************************************************
1333 //
1336 //
1337 // *****************************************************************************
1338 inline void DataBuffer::writeDouble(double d, int pos)
1339 {
1340  // write the data
1341 
1342  if (dbReverse) reverseBO8((char*) &d);
1343  *((double*) &(*dbData)[pos]) = d;
1344 }
1345 
1346 // **** _FUNCTION DESCRIPTION_ *************************************************
1347 //
1351 //
1352 // *****************************************************************************
1353 inline void DataBuffer::writeRawDouble(double d)
1354 {
1355  // write data
1356 
1357  if (dbReverse) reverseBO8((char*) &d);
1358  *((double*) &(*dbData)[dbDataPos]) = d;
1359  dbDataPos += sizeof(double);
1360 }
1361 
1362 // **** _FUNCTION DESCRIPTION_ *************************************************
1363 //
1366 //
1367 // *****************************************************************************
1368 inline void DataBuffer::writeDoubleArray(const double* array, int num_doubles)
1369 {
1370  int num_bytes = num_doubles * sizeof(double);
1371 
1372  // check alignment
1373 
1374  align8Byte();
1375 
1376  // check buffer size
1377 
1378  checkBufferSize(num_bytes);
1379 
1380  // write data
1381 
1382  memcpy(&(*dbData)[dbDataPos], array, num_bytes);
1383  if (dbReverse) reverseBO8Array(num_doubles, &(*dbData)[dbDataPos]);
1384  dbDataPos += num_bytes;
1385 }
1386 
1387 //----------
1388 //---------------------------- PROPERTY FUNCTIONS ------------------------//
1389 //----------
1390 
1391 // **** _FUNCTION DESCRIPTION_ *************************************************
1392 //
1394 //
1395 // *****************************************************************************
1396 inline void DataBuffer::setSize(int num_bytes)
1397 {
1398  dbData->resize(num_bytes);
1399 }
1400 
1401 // **** _FUNCTION DESCRIPTION_ *************************************************
1402 //
1404 //
1405 // *****************************************************************************
1406 inline int DataBuffer::size()
1407 {
1408  // return dbData.size();
1409 
1410  if (dbDataPos > dbSize)
1411  dbSize = dbDataPos;
1412 
1413  return dbSize;
1414 }
1415 
1416 // **** _FUNCTION DESCRIPTION_ *************************************************
1417 //
1419 //
1420 // *****************************************************************************
1421 inline void DataBuffer::resetPos()
1422 {
1423  if (dbDataPos > dbSize)
1424  dbSize = dbDataPos;
1425 
1426  dbDataPos = 0;
1427 }
1428 
1429 // **** _FUNCTION DESCRIPTION_ *************************************************
1430 //
1434 //
1435 // *****************************************************************************
1436 inline void DataBuffer::setPosToEnd()
1437 {
1438  if (dbDataPos > dbSize)
1439  dbSize = dbDataPos;
1440 
1441  dbDataPos = dbSize;
1442 }
1443 
1444 // **** _FUNCTION DESCRIPTION_ *************************************************
1445 //
1447 //
1448 // *****************************************************************************
1449 inline int DataBuffer::getPos() const
1450 {
1451  return dbDataPos;
1452 }
1453 
1454 // **** _FUNCTION DESCRIPTION_ *************************************************
1455 //
1459 //
1460 // *****************************************************************************
1461 inline void DataBuffer::incrementPos(int increment)
1462 {
1463  dbDataPos += increment;
1464 }
1465 
1466 // **** _FUNCTION DESCRIPTION_ *************************************************
1467 //
1470 //
1471 // *****************************************************************************
1472 inline void DataBuffer::decrementPos(int decrement)
1473 {
1474  dbDataPos -= decrement;
1475 }
1476 
1477 // **** _FUNCTION DESCRIPTION_ *************************************************
1478 //
1480 //
1481 // *****************************************************************************
1482 inline string DataBuffer::getData() const
1483 {
1484  return *dbData;
1485 }
1486 
1487 // **** _FUNCTION DESCRIPTION_ *************************************************
1488 //
1490 //
1491 // *****************************************************************************
1492 inline int DataBuffer::getCapacity() const
1493 {
1494  return (int) dbData->capacity();
1495 }
1496 
1497 // **** _FUNCTION DESCRIPTION_ *************************************************
1498 //
1500 //
1501 // *****************************************************************************
1502 inline char* DataBuffer::getPosPointer()
1503 {
1504  char* p = &(*dbData)[dbDataPos];
1505  return p;
1506 }
1507 
1508 // **** _FUNCTION DESCRIPTION_ *************************************************
1509 //
1511 //
1512 // *****************************************************************************
1513 inline char* DataBuffer::getPosPointer(int pos)
1514 {
1515  char* p = &(*dbData)[pos];
1516  return p;
1517 }
1518 
1519 // **** _FUNCTION DESCRIPTION_ *************************************************
1520 //
1522 //
1523 // *****************************************************************************
1524 inline void DataBuffer::reverseBO2(char* d)
1525 {
1526  char tmp = d[0];
1527  d[0] = d[1];
1528  d[1] = tmp;
1529 }
1530 
1531 // **** _FUNCTION DESCRIPTION_ *************************************************
1532 //
1534 //
1535 // *****************************************************************************
1536 inline void DataBuffer::reverseBO4(char* d)
1537 {
1538  char tmp = d[0];
1539  d[0] = d[3];
1540  d[3] = tmp;
1541 
1542  tmp = d[1];
1543  d[1] = d[2];
1544  d[2] = tmp;
1545 }
1546 
1547 // **** _FUNCTION DESCRIPTION_ *************************************************
1548 //
1550 //
1551 // *****************************************************************************
1552 inline void DataBuffer::reverseBO8(char* d)
1553 {
1554  char tmp = d[0];
1555  d[0] = d[7];
1556  d[7] = tmp;
1557 
1558  tmp = d[1];
1559  d[1] = d[6];
1560  d[6] = tmp;
1561 
1562  tmp = d[2];
1563  d[2] = d[5];
1564  d[5] = tmp;
1565 
1566  tmp = d[3];
1567  d[3] = d[4];
1568  d[4] = tmp;
1569 }
1570 
1571 // **** _FUNCTION DESCRIPTION_ *************************************************
1572 //
1574 //
1575 // *****************************************************************************
1576 inline void DataBuffer::setByteOrderReverse(bool bor)
1577 {
1578  dbReverse = bor;
1579 }
1580 
1581 // **** _FUNCTION DESCRIPTION_ *************************************************
1582 //
1584 //
1585 // *****************************************************************************
1586 inline void DataBuffer::byteOrderReverseOn()
1587 {
1588  dbReverse = true;
1589 }
1590 
1591 // **** _FUNCTION DESCRIPTION_ *************************************************
1592 //
1594 //
1595 // *****************************************************************************
1596 inline void DataBuffer::byteOrderReverseOff()
1597 {
1598  dbReverse = false;
1599 }
1600 
1601 // **** _FUNCTION DESCRIPTION_ *************************************************
1602 //
1605 //
1606 // *****************************************************************************
1607 inline bool DataBuffer::isByteOrderReversed() const
1608 {
1609  return dbReverse;
1610 }
1611 
1612 //} // end namespace pgl
1613 
1614 // **** _FUNCTION DESCRIPTION_ *************************************************
1615 //
1617 //
1618 // *****************************************************************************
1619 inline void DataBuffer::reserve(int sze)
1620 {
1621  dbData->reserve(sze);
1622 }
1623 
1624 } // end util namespace
1625 
1626 #endif // DATABUFFER_H
#define UTIL_EXP
Definition: UtilGlobals.h:66
A byte array container used to hold binary data in the same manner as disk based file system.
Definition: DataBuffer.h:81
int dbSize
The current size of the data container.
Definition: DataBuffer.h:379
DataBuffer(const DataBuffer &db)
DataBuffer(string *str)
bool dbPad
A boolean, that if true, maintains 4 and 8 byte alignment in support of double alignment compilation.
Definition: DataBuffer.h:383
string generateMD5HashKey()
string * dbData
A string object is used to contain the actual data.
Definition: DataBuffer.h:373
int dbDataPos
The current iterator position in the data container (dbData).
Definition: DataBuffer.h:376
void writeToFile(fstream &ofs)
bool dbOwnStr
A boolean, that if true, indicates the storage string for this DataBuffer is owned and therefore dele...
Definition: DataBuffer.h:395
void readFromFile(ifstream &ifs, int64 filePos, int num_bytes)
bool dbReverse
A boolean, that if true, reverses the byte order of all 2, 4, and 8 byte intrinsics (shorts,...
Definition: DataBuffer.h:390
void readFromFile(ifstream &ifs, int num_bytes)
static void reverseBO4Array(int n, char *a)
virtual ~DataBuffer()
static void reverseBO8Array(int n, char *a)
void readFromFile(fstream &ifs, int num_bytes)
const string & readMD5HashKey()
void readFromFile(fstream &ifs, int64 filePos, int num_bytes)
DataBuffer & operator=(const DataBuffer &db)
void writeToFile(ofstream &ofs)
string generateDataBufMD5HashKey()
static void reverseBOArray(int n, char *a, int s)
static const int ALLOCATION_REQ_SIZE
Default allocation requirement size for a data buffer.
Definition: DataBuffer.h:359
DataBuffer(bool doPad)
static void reverseBO2Array(int n, char *a)
static int getAllocationReqSize()
Definition: Brents.h:54