GeoTessCPP  2.2
Software to facilitate storage and retrieval of 3D information about the Earth.
IFStreamBinary.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- 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 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #ifndef IFSTREAMBINARY_OBJECT_H
37 #define IFSTREAMBINARY_OBJECT_H
38 
39 // **** _SYSTEM INCLUDES_ ******************************************************
40 
41 #include <iostream>
42 #include <string>
43 #include <cstring>
44 #include <fstream>
45 #include <vector>
46 #include <sstream>
47 
48 // use standard library objects
49 using namespace std;
50 
51 // **** _LOCAL INCLUDES_ *******************************************************
52 
53 #include "CPPUtils.h"
54 #include "GeoTessUtils.h"
55 #include "GeoTessException.h"
56 
57 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
58 
59 namespace geotess {
60 
61 // **** _FORWARD REFERENCES_ ***************************************************
62 
63 // **** _CLASS DEFINITION_ *****************************************************
64 
80 {
81  protected:
82 
86  string* bData;
87 
91  int bDataPos;
92 
96  int bSize;
97 
102  bool bAlign;
103 
111  bool bReverse;
112 
118  bool bOwnStr;
119 
120  string bFileName;
121 
122  int bMemIncr;
123 
128  void align2Byte()
129  {
130  int sm = bDataPos % CPPUtils::SSHT;
131  if (sm && bAlign) bDataPos += CPPUtils::SSHT - sm;
132  }
133 
138  void align4Byte()
139  {
140  int sm = bDataPos % CPPUtils::SINT;
141  if (sm && bAlign) bDataPos += CPPUtils::SINT - sm;
142  }
143 
148  void align8Byte()
149  {
150  int sm = bDataPos % CPPUtils::SDBL;
151  if (sm && bAlign) bDataPos += CPPUtils::SDBL - sm;
152  }
153 
158  void checkBufferSize(int sincr)
159  {
160  if (bDataPos + sincr > (int) bData->size())
161  {
162  if (sincr + bData->size() > bData->capacity())
163  bData->reserve(bData->capacity() + bMemIncr);
164  bData->append(bDataPos + sincr - bData->size(), ' ');
165  }
166  }
167 
168  /*
169  * Reverses each s-byte element of array a containing n elements
170  * (s*n bytes). The element size s must be 2, 4, or 8.
171  */
172  static void reverseBOArray(int n, char* a, int s);
173 
174  /*
175  * Reverses the byte order of each element of a where a is an array of n
176  * 2, 4, or 8 byte elements. Used to convert between big-and
177  * little-endian formats.
178  */
179  static void reverseBO2Array(int n, char* a);
180  static void reverseBO4Array(int n, char* a);
181  static void reverseBO8Array(int n, char* a);
182 
183  /*
184  * Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used
185  * to convert between big-and little-endian formats.
186  */
187  static void reverseBO2(char* d);
188  static void reverseBO4(char* d);
189  static void reverseBO8(char* d);
190 
191  public:
192 
196  IFStreamBinary();
197 
201  IFStreamBinary(bool align);
202 
207  IFStreamBinary(const string& filename);
208 
213  IFStreamBinary(const string& filename, int num_bytes);
214 
219  IFStreamBinary(string* str);
220 
224  IFStreamBinary(const IFStreamBinary& db);
225 
229  virtual ~IFStreamBinary();
230 
234  IFStreamBinary& operator=(const IFStreamBinary& db);
235 
239  void dumpBuffer();
240 
244  static bool exists(const string& filename);
245 
249  void writeToFile(const string& filename);
250 
254  void writeToFile(ofstream& ofs);
255 
259  void readFromFile(const string& filename);
260 
264  void readFromFile(const string& filename, int num_bytes);
265 
270 // void readFromFile(ifstream& ifs, streampos file_pos,
271 // int num_bytes);
272 
276  void readFromFile(ifstream& ifs, int num_bytes);
277 
281  const string& getFileName() const { return bFileName; }
282 
286  void clear()
287  {
288  (*bData) = "";
289  bDataPos = 0;
290  bSize = 0;
291  }
292 
298  {
299  if (mci > 0) bMemIncr = mci;
300  }
301 
310  string readString();
311  void readString(string& s);
312  void readCharArray(string& s, int num_chars);
313  void readCharArray(char* array, int num_chars);
314  void readType(string& s);
315  void readTypeArray(string& array, int num_chars);
316 
324  bool readBool();
325  bool readBool(int pos);
326  void readBoolArray(bool* array, int num_bools);
327  void readType(bool& b);
328  void readTypeArray(bool* array, int num_bools);
329 
337  byte readByte();
338  byte readByte(int pos);
339  void readByteArray(byte* array, int num_bs);
340  void readType(byte& b);
341  void readTypeArray(byte* array, int num_bs);
342 
352  short readShort();
353  short readShortNC();
354  short readShort(int pos);
355  void readShortArray(short* array, int num_shorts);
356  void readType(short& s);
357  void readTypeArray(short* array, int num_shorts);
358 
368  int readInt();
369  int readIntNC();
370  int readInt(int pos);
371  void readIntArray(int* array, int num_ints);
372  void readType(int& i);
373  void readTypeArray(int* array, int num_ints);
374 
384  LONG_INT readLong();
385  LONG_INT readLongNC();
386  LONG_INT readLong(int pos);
387  void readLongArray(LONG_INT* array, int num_longs);
388  void readType(LONG_INT& l);
389  void readTypeArray(LONG_INT* array, int num_longs);
390 
400  float readFloat();
401  float readFloatNC();
402  float readFloat(int pos);
403  void readFloatArray(float* array, int num_floats);
404  void readType(float& f);
405  void readTypeArray(float* array, int num_floats);
406 
416  double readDouble();
417  double readDoubleNC();
418  double readDouble(int pos);
419  void readDoubleArray(double* array, int num_doubles);
420  void readType(double& d);
421  void readTypeArray(double* array, int num_doubles);
422 
431  void writeString(const string& str);
432  void writeString(const char* char_string);
433  void writeCharArray(const char* array, int num_chars);
434  void writeType(const string& str);
435  void writeType(const char* char_string);
436 
447  void writeBool(bool b);
448  void writeBoolNC(bool b);
449  void writeBool(bool b, int pos);
450  void writeBoolArray(const bool* array, int num_bools);
451  void writeType(bool b);
452  void writeTypeArray(const bool* array, int num_bools);
453 
464  void writeByte(byte b);
465  void writeByteNC(byte b);
466  void writeByte(byte b, int pos);
467  void writeByteArray(const byte* array, int num_bytes);
468  void writeType(byte b);
469  void writeTypeArray(const byte* array, int num_bytes);
470 
482  void writeShort(short i);
483  void writeShortNC(short i);
484  void writeShort(short i, int pos);
485  void writeShortArray(const short* array, int num_ints);
486  void writeType(short i);
487  void writeTypeArray(const short* array, int num_ints);
488 
500  void writeInt(int i);
501  void writeIntNC(int i);
502  void writeInt(int i, int pos);
503  void writeIntArray(const int* array, int num_ints);
504  void writeType(int i);
505  void writeTypeArray(const int* array, int num_ints);
506 
518  void writeLong(LONG_INT l);
519  void writeLongNC(LONG_INT l);
520  void writeLong(LONG_INT l, int pos);
521  void writeLongArray(const LONG_INT* array, int num_longs);
522  void writeType(LONG_INT l);
523  void writeTypeArray(const LONG_INT* array, int num_longs);
524 
536  void writeFloat(float f);
537  void writeFloatNC(float f);
538  void writeFloat(float f, int pos);
539  void writeFloatArray(const float* array, int num_floats);
540  void writeType(float f);
541  void writeTypeArray(const float* array, int num_floats);
542 
554  void writeDouble(double d);
555  void writeDoubleNC(double d);
556  void writeDouble(double d, int pos);
557  void writeDoubleArray(const double* array, int num_doubles);
558  void writeType(double d);
559  void writeTypeArray(const double* array, int num_doubles);
560 
561  /*
562  * Return class name.
563  */
564  static string className()
565  { return "IFStreamBinary"; }
566 
567  /*
568  * Return class size.
569  */
570  static int classSize()
571  { return (int) sizeof(IFStreamBinary); }
572 
577  void reSize(int spc)
578  { bData->resize(spc); }
579 
583  void reserve(int sze)
584  { bData->reserve(sze); }
585 
589  int size()
590  {
591  if (bDataPos > bSize) bSize = bDataPos;
592  return bSize;
593  }
594 
598  void resetPos()
599  {
600  if (bDataPos > bSize) bSize = bDataPos;
601  bDataPos = 0;
602  }
603 
607  void setPosToEnd()
608  {
609  if (bDataPos > bSize) bSize = bDataPos;
610  bDataPos = bSize;
611  }
612 
616  int getPos() const
617  { return bDataPos; }
618 
622  void incrementPos(int increment = 1)
623  { bDataPos += increment; }
624 
628  void decrementPos(int decrement = 1)
629  { bDataPos -= decrement; }
630 
634  const string& getData() const
635  { return *bData; }
636 
640  int getCapacity() const
641  { return (int) bData->capacity(); }
642 
648  { return getPosPointer(bDataPos); }
649 
653  char* getPosPointer(int pos)
654  {
655  char* p = &(*bData)[pos];
656  return p;
657  }
658 
662  void setByteOrderReverse(bool bor)
663  { bReverse = bor; }
664 
669  { bReverse = true; }
670 
675  { bReverse = false; }
676 
682  { return bReverse; }
683 
687  void setBoundaryAlignment(bool align)
688  { bAlign = align; }
689 
694  { bAlign = true; }
695 
700  { bAlign = false; }
701 
707  { return bAlign; }
708 
709 }; // end class IFStreamBinary
710 
711 //************* Read Strings **************************************************
712 
716 inline string IFStreamBinary::readString()
717 {
718  string s;
719  readString(s);
720  return s;
721 }
722 
726 inline void IFStreamBinary::readString(string& s)
727 {
728  int len;
729  // check for zero added by sballar 2013-01-04
730  len = readInt();
731  if (len == 0)
732  s="";
733  else
734  {
735  // read the data
736  s = bData->substr(bDataPos, len);
737  bDataPos += len;
738  }
739 }
740 
744 inline void IFStreamBinary::readCharArray(string& s, int num_chars)
745 {
746  s.resize(num_chars);
747  readCharArray(&s[0], num_chars);
748 }
749 
753 inline void IFStreamBinary::readCharArray(char* array, int num_chars)
754 {
755  // read the data
756 
757  memcpy(array, &(*bData)[bDataPos], num_chars);
758  bDataPos += num_chars;
759 }
760 
764 inline void IFStreamBinary::readType(string& s)
765 {
766  readString(s);
767 }
768 
772 inline void IFStreamBinary::readTypeArray(string& s, int num_chars)
773 {
774  readCharArray(s, num_chars);
775 }
776 
777 //************* Read Bools ****************************************************
778 
782 inline bool IFStreamBinary::readBool()
783 {
784  // read the data
785 
786  int pos = bDataPos;
787  bDataPos += CPPUtils::SBOL;
788  return readBool(pos);
789 }
790 
795 inline bool IFStreamBinary::readBool(int pos)
796 {
797  return *((bool*) &(*bData)[pos]);
798 }
799 
803 inline void IFStreamBinary::readBoolArray(bool* array, int num_bools)
804 {
805  // get bytes and perform alignment
806 
807  int num_bytes = num_bools * CPPUtils::SBOL;
808 
809  // copy from buffer into array ...
810  // update buffer position and exit
811 
812  memcpy(array, &(*bData)[bDataPos], num_bytes);
813  bDataPos += num_bytes;
814 }
815 
819 inline void IFStreamBinary::readType(bool& b)
820 {
821  b = readBool();
822 }
823 
827 inline void IFStreamBinary::readTypeArray(bool* array, int num_bools)
828 {
829  readBoolArray(array, num_bools);
830 }
831 
832 //************* Read Bytes ****************************************************
833 
837 inline byte IFStreamBinary::readByte()
838 {
839  // read the data
840 
841  int pos = bDataPos;
842  bDataPos += CPPUtils::SBYT;
843  return readByte(pos);
844 }
845 
850 inline byte IFStreamBinary::readByte(int pos)
851 {
852  return *((byte*) &(*bData)[pos]);
853 }
854 
858 inline void IFStreamBinary::readByteArray(byte* array, int num_bytes)
859 {
860  // get bytes and perform alignment
861 
862  num_bytes *= CPPUtils::SBYT;
863 
864  // copy from buffer into array ...
865  // update buffer position and exit
866 
867  memcpy(array, &(*bData)[bDataPos], num_bytes);
868  bDataPos += num_bytes;
869 }
870 
874 inline void IFStreamBinary::readType(byte& b)
875 {
876  b = readByte();
877 }
878 
882 inline void IFStreamBinary::readTypeArray(byte* array, int num_bytes)
883 {
884  readByteArray(array, num_bytes);
885 }
886 
887 //************* Read Shorts ***************************************************
888 
892 inline short IFStreamBinary::readShort()
893 {
894  align2Byte();
895  return readShortNC();
896 }
897 
901 inline short IFStreamBinary::readShortNC()
902 {
903  // read the data
904 
905  int pos = bDataPos;
906  bDataPos += CPPUtils::SSHT;
907  return readShort(pos);
908 }
909 
914 inline short IFStreamBinary::readShort(int pos)
915 {
916  // read the data ... use memcpy if alignment is off
917 
918  short s;
919  if (bAlign)
920  s = *((short*) &(*bData)[pos]);
921  else
922  memcpy(&s, &(*bData)[pos], 2);
923 
924  if (bReverse) reverseBO2((char*) &s);
925  return s;
926 }
927 
931 inline void IFStreamBinary::readShortArray(short* array, int num_shorts)
932 {
933  // get bytes and perform alignment
934 
935  int num_bytes = num_shorts * CPPUtils::SSHT;
936  align2Byte();
937 
938  // copy from buffer into array ... reverse data if necessary ...
939  // update buffer position and exit
940 
941  memcpy(array, &(*bData)[bDataPos], num_bytes);
942  if (bReverse) reverseBO2Array(num_shorts, (char*) array);
943  bDataPos += num_bytes;
944 }
945 
949 inline void IFStreamBinary::readType(short& s)
950 {
951  s = readShort();
952 }
953 
957 inline void IFStreamBinary::readTypeArray(short* array, int num_shorts)
958 {
959  readShortArray(array, num_shorts);
960 }
961 
962 //************* Read Ints *****************************************************
963 
967 inline int IFStreamBinary::readInt()
968 {
969  align4Byte();
970  return readIntNC();
971 }
972 
976 inline int IFStreamBinary::readIntNC()
977 {
978  // read the data
979 
980  int pos = bDataPos;
981  bDataPos += CPPUtils::SINT;
982  return readInt(pos);
983 }
984 
989 inline int IFStreamBinary::readInt(int pos)
990 {
991  // read the data ... use memcpy if alignment is off
992 
993  int i;
994  if (bAlign)
995  i = *((int*) &(*bData)[pos]);
996  else
997  memcpy(&i, &(*bData)[pos], 4);
998 
999  if (bReverse) reverseBO4((char*) &i);
1000  return i;
1001 }
1002 
1006 inline void IFStreamBinary::readIntArray(int* array, int num_ints)
1007 {
1008  // get bytes and perform alignment
1009 
1010  int num_bytes = num_ints * CPPUtils::SINT;
1011  align4Byte();
1012 
1013  // copy from buffer into array ... reverse data if necessary ...
1014  // update buffer position and exit
1015 
1016  memcpy(array, &(*bData)[bDataPos], num_bytes);
1017  if (bReverse) reverseBO4Array(num_ints, (char*) array);
1018  bDataPos += num_bytes;
1019 }
1020 
1024 inline void IFStreamBinary::readType(int& i)
1025 {
1026  i = readInt();
1027 }
1028 
1032 inline void IFStreamBinary::readTypeArray(int* array, int num_ints)
1033 {
1034  readIntArray(array, num_ints);
1035 }
1036 
1037 //************* Read Longs ****************************************************
1038 
1042 inline LONG_INT IFStreamBinary::readLong()
1043 {
1044  align8Byte();
1045  return readLongNC();
1046 }
1047 
1051 inline LONG_INT IFStreamBinary::readLongNC()
1052 {
1053  // read the data
1054 
1055  int pos = bDataPos;
1056  bDataPos += CPPUtils::SLNG;
1057  return readLong(pos);
1058 }
1059 
1064 inline LONG_INT IFStreamBinary::readLong(int pos)
1065 {
1066  // read the data ... use memcpy if alignment is off
1067 
1068  LONG_INT l;
1069  if (bAlign)
1070  l = *((LONG_INT*) &(*bData)[pos]);
1071  else
1072  memcpy(&l, &(*bData)[pos], 8);
1073 
1074  if (bReverse) reverseBO8((char*) &l);
1075  return l;
1076 }
1077 
1081 inline void IFStreamBinary::readLongArray(LONG_INT* array, int num_longs)
1082 {
1083  // get bytes and perform alignment
1084 
1085  int num_bytes = num_longs * CPPUtils::SLNG;
1086  align8Byte();
1087 
1088  // copy from buffer into array ... reverse data if necessary ...
1089  // update buffer position and exit
1090 
1091  memcpy(array, &(*bData)[bDataPos], num_bytes);
1092  if (bReverse) reverseBO8Array(num_longs, (char*) array);
1093  bDataPos += num_bytes;
1094 }
1095 
1099 inline void IFStreamBinary::readType(LONG_INT& l)
1100 {
1101  l = readLong();
1102 }
1103 
1107 inline void IFStreamBinary::readTypeArray(LONG_INT* array, int num_longs)
1108 {
1109  readLongArray(array, num_longs);
1110 }
1111 
1112 //************* Read Floats ***************************************************
1113 
1117 inline float IFStreamBinary::readFloat()
1118 {
1119  align4Byte();
1120  return readFloatNC();
1121 }
1122 
1126 inline float IFStreamBinary::readFloatNC()
1127 {
1128  // read the data
1129 
1130  int pos = bDataPos;
1131  bDataPos += CPPUtils::SFLT;
1132  return readFloat(pos);
1133 }
1134 
1139 inline float IFStreamBinary::readFloat(int pos)
1140 {
1141  // read the data ... use memcpy if alignment is off
1142 
1143  float f;
1144  if (bAlign)
1145  f = *((float*) &(*bData)[pos]);
1146  else
1147  memcpy(&f, &(*bData)[pos], 4);
1148 
1149  if (bReverse) reverseBO4((char*) &f);
1150  return f;
1151 }
1152 
1156 inline void IFStreamBinary::readFloatArray(float* array, int num_floats)
1157 {
1158  // get bytes and perform alignment
1159 
1160  int num_bytes = num_floats * CPPUtils::SFLT;
1161  align4Byte();
1162 
1163  // copy from buffer into array ... reverse data if necessary ...
1164  // update buffer position and exit
1165 
1166  memcpy(array, &(*bData)[bDataPos], num_bytes);
1167  if (bReverse) reverseBO4Array(num_floats, (char*) array);
1168  bDataPos += num_bytes;
1169 }
1170 
1174 inline void IFStreamBinary::readType(float& f)
1175 {
1176  f = readFloat();
1177 }
1178 
1182 inline void IFStreamBinary::readTypeArray(float* array, int num_floats)
1183 {
1184  readFloatArray(array, num_floats);
1185 }
1186 
1187 //************* Read Doubles **************************************************
1188 
1192 inline double IFStreamBinary::readDouble()
1193 {
1194  align8Byte();
1195  return readDoubleNC();
1196 }
1197 
1201 inline double IFStreamBinary::readDoubleNC()
1202 {
1203  // read the data
1204 
1205  int pos = bDataPos;
1206  bDataPos += CPPUtils::SDBL;
1207  return readDouble(pos);
1208 }
1209 
1214 inline double IFStreamBinary::readDouble(int pos)
1215 {
1216  // read the data ... use memcpy if alignment is off
1217 
1218  double d;
1219  if (bAlign)
1220  d = *((double*) &(*bData)[pos]);
1221  else
1222  memcpy(&d, &(*bData)[pos], 8);
1223 
1224  if (bReverse) reverseBO8((char*) &d);
1225  return d;
1226 }
1227 
1231 inline void IFStreamBinary::readDoubleArray(double* array, int num_doubles)
1232 {
1233  // get bytes and perform alignment
1234 
1235  int num_bytes = num_doubles * CPPUtils::SDBL;
1236  align8Byte();
1237 
1238  // copy from buffer into array ... reverse data if necessary ...
1239  // update buffer position and exit
1240 
1241  memcpy(array, &(*bData)[bDataPos], num_bytes);
1242  if (bReverse) reverseBO8Array(num_doubles, (char*) array);
1243  bDataPos += num_bytes;
1244 }
1245 
1249 inline void IFStreamBinary::readType(double& d)
1250 {
1251  d = readDouble();
1252 }
1253 
1257 inline void IFStreamBinary::readTypeArray(double* array, int num_doubles)
1258 {
1259  readDoubleArray(array, num_doubles);
1260 }
1261 
1262 //************* Write Strings *************************************************
1263 
1270 inline void IFStreamBinary::writeString(const string& str)
1271 {
1272  // get total size, align, and check capacity
1273 
1274  int sz = (int) str.size() + CPPUtils::SINT;
1275  align4Byte();
1276  checkBufferSize(sz);
1277 
1278  // write the data
1279 
1280  writeIntNC((int) str.size());
1281  memcpy(&(*bData)[bDataPos], &str[0], str.size());
1282  bDataPos += (int) str.size();
1283 }
1284 
1290 inline void IFStreamBinary::writeString(const char* char_string)
1291 {
1292  string tmp_string = char_string;
1293  writeString(tmp_string);
1294 }
1295 
1299 inline void IFStreamBinary::writeCharArray(const char* array, int num_chars)
1300 {
1301  checkBufferSize(num_chars);
1302 
1303  // write data
1304 
1305  memcpy(&(*bData)[bDataPos], array, num_chars);
1306  bDataPos += num_chars;
1307 }
1308 
1315 inline void IFStreamBinary::writeType(const string& str)
1316 {
1317  writeString(str);
1318 }
1319 
1325 inline void IFStreamBinary::writeType(const char* char_string)
1326 {
1327  writeString(char_string);
1328 }
1329 
1330 //************* Write Bools ***************************************************
1331 
1337 inline void IFStreamBinary::writeBool(bool b)
1338 {
1339  checkBufferSize(CPPUtils::SBOL);
1340 
1341  writeBoolNC(b);
1342 }
1343 
1349 inline void IFStreamBinary::writeBoolNC(bool b)
1350 {
1351  writeBool(b, bDataPos);
1352  bDataPos += CPPUtils::SBOL;
1353 }
1354 
1360 inline void IFStreamBinary::writeBool(bool b, int pos)
1361 {
1362  *((bool*) &(*bData)[pos]) = b;
1363 }
1364 
1368 inline void IFStreamBinary::writeBoolArray(const bool* array, int num_bools)
1369 {
1370  // get number of bytes to be written and check for alignment and buffer
1371  // size
1372 
1373  num_bools *= CPPUtils::SBOL;
1374  checkBufferSize(num_bools);
1375 
1376  // copy array into data buffer. Reverse data buffer entries if necessary
1377  // and increment the buffer output pointer.
1378 
1379  memcpy(&(*bData)[bDataPos], array, num_bools);
1380  bDataPos += num_bools;
1381 }
1382 
1388 inline void IFStreamBinary::writeType(bool b)
1389 {
1390  writeBool(b);
1391 }
1392 
1396 inline void IFStreamBinary::writeTypeArray(const bool* array, int num_bools)
1397 {
1398  writeBoolArray(array, num_bools);
1399 }
1400 
1401 //************* Write Bytes ***************************************************
1402 
1408 inline void IFStreamBinary::writeByte(byte b)
1409 {
1410  checkBufferSize(CPPUtils::SBYT);
1411 
1412  writeByteNC(b);
1413 }
1414 
1420 inline void IFStreamBinary::writeByteNC(byte b)
1421 {
1422  writeByte(b, bDataPos);
1423  bDataPos += CPPUtils::SBYT;
1424 }
1425 
1431 inline void IFStreamBinary::writeByte(byte b, int pos)
1432 {
1433  *((byte*) &(*bData)[pos]) = b;
1434 }
1435 
1439 inline void IFStreamBinary::writeByteArray(const byte* array, int num_bytes)
1440 {
1441  // get number of bytes to be written and check for alignment and buffer
1442  // size
1443 
1444  num_bytes *= CPPUtils::SBYT;
1445  checkBufferSize(num_bytes);
1446 
1447  // copy array into data buffer. Reverse data buffer entries if necessary
1448  // and increment the buffer output pointer.
1449 
1450  memcpy(&(*bData)[bDataPos], array, num_bytes);
1451  bDataPos += num_bytes;
1452 }
1453 
1459 inline void IFStreamBinary::writeType(byte b)
1460 {
1461  writeByte(b);
1462 }
1463 
1467 inline void IFStreamBinary::writeTypeArray(const byte* array, int num_bytes)
1468 {
1469  writeByteArray(array, num_bytes);
1470 }
1471 
1472 //************* Write Shorts **************************************************
1473 
1479 inline void IFStreamBinary::writeShort(short s)
1480 {
1481  align2Byte();
1482  checkBufferSize(CPPUtils::SSHT);
1483 
1484  writeShortNC(s);
1485 }
1486 
1492 inline void IFStreamBinary::writeShortNC(short s)
1493 {
1494  writeShort(s, bDataPos);
1495  bDataPos += CPPUtils::SSHT;
1496 }
1497 
1503 inline void IFStreamBinary::writeShort(short s, int pos)
1504 {
1505  if (bReverse) reverseBO2((char*) &s);
1506 
1507  if (bAlign)
1508  *((short*) &(*bData)[pos]) = s;
1509  else
1510  memcpy(&(*bData)[pos], &s, CPPUtils::SSHT);
1511 }
1512 
1516 inline void IFStreamBinary::writeShortArray(const short* array, int num_shorts)
1517 {
1518  // get number of bytes to be written and check for alignment and buffer
1519  // size
1520 
1521  int num_bytes = num_shorts * CPPUtils::SSHT;
1522  align2Byte();
1523  checkBufferSize(num_bytes);
1524 
1525  // copy array into data buffer. Reverse data buffer entries if necessary
1526  // and increment the buffer output pointer.
1527 
1528  memcpy(&(*bData)[bDataPos], array, num_bytes);
1529  if (bReverse) reverseBO2Array(num_shorts, &(*bData)[bDataPos]);
1530  bDataPos += num_bytes;
1531 }
1532 
1538 inline void IFStreamBinary::writeType(short s)
1539 {
1540  writeShort(s);
1541 }
1542 
1546 inline void IFStreamBinary::writeTypeArray(const short* array, int num_shorts)
1547 {
1548  writeShortArray(array, num_shorts);
1549 }
1550 
1551 //************* Write Ints ****************************************************
1552 
1558 inline void IFStreamBinary::writeInt(int i)
1559 {
1560  align4Byte();
1561  checkBufferSize(CPPUtils::SINT);
1562 
1563  writeIntNC(i);
1564 }
1565 
1571 inline void IFStreamBinary::writeIntNC(int i)
1572 {
1573  writeInt(i, bDataPos);
1574  bDataPos += CPPUtils::SINT;
1575 }
1576 
1582 inline void IFStreamBinary::writeInt(int i, int pos)
1583 {
1584  if (bReverse) reverseBO4((char*) &i);
1585 
1586  if (bAlign)
1587  *((int*) &(*bData)[pos]) = i;
1588  else
1589  memcpy(&(*bData)[pos], &i, CPPUtils::SINT);
1590 }
1591 
1595 inline void IFStreamBinary::writeIntArray(const int* array, int num_ints)
1596 {
1597  // get number of bytes to be written and check for alignment and buffer
1598  // size
1599 
1600  int num_bytes = num_ints * CPPUtils::SINT;
1601  align4Byte();
1602  checkBufferSize(num_bytes);
1603 
1604  // copy array into data buffer. Reverse data buffer entries if necessary
1605  // and increment the buffer output pointer.
1606 
1607  memcpy(&(*bData)[bDataPos], array, num_bytes);
1608  if (bReverse) reverseBO4Array(num_ints, &(*bData)[bDataPos]);
1609  bDataPos += num_bytes;
1610 }
1611 
1617 inline void IFStreamBinary::writeType(int i)
1618 {
1619  writeInt(i);
1620 }
1621 
1625 inline void IFStreamBinary::writeTypeArray(const int* array, int num_ints)
1626 {
1627  writeIntArray(array, num_ints);
1628 }
1629 
1630 //************* Write Longs ***************************************************
1631 
1637 inline void IFStreamBinary::writeLong(LONG_INT l)
1638 {
1639  align8Byte();
1640  checkBufferSize(CPPUtils::SLNG);
1641 
1642  writeLongNC(l);
1643 }
1644 
1650 inline void IFStreamBinary::writeLongNC(LONG_INT l)
1651 {
1652  writeLong(l, bDataPos);
1653  bDataPos += CPPUtils::SLNG;
1654 }
1655 
1661 inline void IFStreamBinary::writeLong(LONG_INT l, int pos)
1662 {
1663  if (bReverse) reverseBO8((char*) &l);
1664 
1665  if (bAlign)
1666  *((LONG_INT*) &(*bData)[pos]) = l;
1667  else
1668  memcpy(&(*bData)[pos], &l, CPPUtils::SLNG);
1669 }
1670 
1674 inline void IFStreamBinary::writeLongArray(const LONG_INT* array, int num_longs)
1675 {
1676  // get number of bytes to be written and check for alignment and buffer
1677  // size
1678 
1679  int num_bytes = num_longs * CPPUtils::SLNG;
1680  align8Byte();
1681  checkBufferSize(num_bytes);
1682 
1683  // copy array into data buffer. Reverse data buffer entries if necessary
1684  // and increment the buffer output pointer.
1685 
1686  memcpy(&(*bData)[bDataPos], array, num_bytes);
1687  if (bReverse) reverseBO8Array(num_longs, &(*bData)[bDataPos]);
1688  bDataPos += num_bytes;
1689 }
1690 
1696 inline void IFStreamBinary::writeType(LONG_INT l)
1697 {
1698  writeLong(l);
1699 }
1700 
1704 inline void IFStreamBinary::writeTypeArray(const LONG_INT* array, int num_longs)
1705 {
1706  writeLongArray(array, num_longs);
1707 }
1708 
1709 //************* Write Floats **************************************************
1710 
1716 inline void IFStreamBinary::writeFloat(float f)
1717 {
1718  align4Byte();
1719  checkBufferSize(CPPUtils::SFLT);
1720 
1721  writeFloatNC(f);
1722 }
1723 
1729 inline void IFStreamBinary::writeFloatNC(float f)
1730 {
1731  writeFloat(f, bDataPos);
1732  bDataPos += CPPUtils::SFLT;
1733 }
1734 
1740 inline void IFStreamBinary::writeFloat(float f, int pos)
1741 {
1742  if (bReverse) reverseBO4((char*) &f);
1743 
1744  if (bAlign)
1745  *((float*) &(*bData)[pos]) = f;
1746  else
1747  memcpy(&(*bData)[pos], &f, CPPUtils::SFLT);
1748 }
1749 
1753 inline void IFStreamBinary::writeFloatArray(const float* array, int num_floats)
1754 {
1755  // get number of bytes to be written and check for alignment and buffer
1756  // size
1757 
1758  int num_bytes = num_floats * CPPUtils::SFLT;
1759  align4Byte();
1760  checkBufferSize(num_bytes);
1761 
1762  // copy array into data buffer. Reverse data buffer entries if necessary
1763  // and increment the buffer output pointer.
1764 
1765  memcpy(&(*bData)[bDataPos], array, num_bytes);
1766  if (bReverse) reverseBO4Array(num_floats, &(*bData)[bDataPos]);
1767  bDataPos += num_bytes;
1768 }
1769 
1775 inline void IFStreamBinary::writeType(float f)
1776 {
1777  writeFloat(f);
1778 }
1779 
1783 inline void IFStreamBinary::writeTypeArray(const float* array, int num_floats)
1784 {
1785  writeFloatArray(array, num_floats);
1786 }
1787 
1788 //************* Write Doubles *************************************************
1789 
1795 inline void IFStreamBinary::writeDouble(double d)
1796 {
1797  align8Byte();
1798  checkBufferSize(CPPUtils::SDBL);
1799 
1800  writeDoubleNC(d);
1801 }
1802 
1808 inline void IFStreamBinary::writeDoubleNC(double d)
1809 {
1810  writeDouble(d, bDataPos);
1811  bDataPos += CPPUtils::SDBL;
1812 }
1813 
1819 inline void IFStreamBinary::writeDouble(double d, int pos)
1820 {
1821  if (bReverse) reverseBO8((char*) &d);
1822 
1823  if (bAlign)
1824  *((double*) &(*bData)[pos]) = d;
1825  else
1826  memcpy(&(*bData)[pos], &d, CPPUtils::SDBL);
1827 }
1828 
1832 inline void IFStreamBinary::writeDoubleArray(const double* array, int num_doubles)
1833 {
1834  // get number of bytes to be written and check for alignment and buffer
1835  // size
1836 
1837  int num_bytes = num_doubles * CPPUtils::SDBL;
1838  align8Byte();
1839  checkBufferSize(num_bytes);
1840 
1841  // copy array into data buffer. Reverse data buffer entries if necessary
1842  // and increment the buffer output pointer.
1843 
1844  memcpy(&(*bData)[bDataPos], array, num_bytes);
1845  if (bReverse) reverseBO8Array(num_doubles, &(*bData)[bDataPos]);
1846  bDataPos += num_bytes;
1847 }
1848 
1854 inline void IFStreamBinary::writeType(double d)
1855 {
1856  writeDouble(d);
1857 }
1858 
1862 inline void IFStreamBinary::writeTypeArray(const double* array, int num_doubles)
1863 {
1864  writeDoubleArray(array, num_doubles);
1865 }
1866 
1867 //************* Byte reversal functions ***************************************
1868 
1872 inline void IFStreamBinary::reverseBO2(char* d)
1873 {
1874  char tmp = d[0];
1875  d[0] = d[1];
1876  d[1] = tmp;
1877 }
1878 
1882 inline void IFStreamBinary::reverseBO4(char* d)
1883 {
1884  char tmp = d[0];
1885  d[0] = d[3];
1886  d[3] = tmp;
1887 
1888  tmp = d[1];
1889  d[1] = d[2];
1890  d[2] = tmp;
1891 }
1892 
1896 inline void IFStreamBinary::reverseBO8(char* d)
1897 {
1898  char tmp = d[0];
1899  d[0] = d[7];
1900  d[7] = tmp;
1901 
1902  tmp = d[1];
1903  d[1] = d[6];
1904  d[6] = tmp;
1905 
1906  tmp = d[2];
1907  d[2] = d[5];
1908  d[5] = tmp;
1909 
1910  tmp = d[3];
1911  d[3] = d[4];
1912  d[4] = tmp;
1913 }
1914 
1915 } // end namespace geotess
1916 
1917 #endif // IFSTREAMBINARY_OBJECT_H
void clear()
Clears the buffer.
Definition: IFStreamBinary.h:286
void reserve(int sze)
Sets the storage capacity.
Definition: IFStreamBinary.h:583
bool isBoundaryAlignmentOn() const
Returns true if the byte order in reads and writes is reversed for all 2, 4, or 8 byte intrinsincs (s...
Definition: IFStreamBinary.h:706
int getPos() const
Get the current iterator position.
Definition: IFStreamBinary.h:616
bool bOwnStr
A boolean, that if true, indicates the storage string for this IFStreamBinary is owned and therefore ...
Definition: IFStreamBinary.h:118
bool bReverse
A boolean, that if true, reverses the byte order of all 2, 4, and 8 byte intrinsics (shorts...
Definition: IFStreamBinary.h:111
char * getPosPointer()
Return a pointer at the current iterator location in the IFStreamBinary data.
Definition: IFStreamBinary.h:647
void boundaryAlignmentOff()
Turns off intrinsic boundary alignment.
Definition: IFStreamBinary.h:699
Definition: ArrayReuse.h:55
void align8Byte()
Ensure that the buffer position pointer (dbDataPos) is aligned on a 8 byte boundary.
Definition: IFStreamBinary.h:148
int size()
Returns the IFStreamBinary size.
Definition: IFStreamBinary.h:589
string bFileName
Definition: IFStreamBinary.h:120
Opens a file for binary read and write access.
Definition: IFStreamBinary.h:79
void boundaryAlignmentOn()
Turns on intrinsic boundary alignment.
Definition: IFStreamBinary.h:693
static int classSize()
Definition: IFStreamBinary.h:570
void byteOrderReverseOff()
Turns off byte order reversal.
Definition: IFStreamBinary.h:674
int bMemIncr
Definition: IFStreamBinary.h:122
void setByteOrderReverse(bool bor)
Sets the byte order reverse flag to bor.
Definition: IFStreamBinary.h:662
const string & getFileName() const
Return the file name with which this binary reader was opened.
Definition: IFStreamBinary.h:281
void reSize(int spc)
This function allocates spc bytes in the buffer.
Definition: IFStreamBinary.h:577
void decrementPos(int decrement=1)
Decrement the iterator position.
Definition: IFStreamBinary.h:628
void resetPos()
Reset the current iterator position to the beginning of the buffer.
Definition: IFStreamBinary.h:598
const string & getData() const
Return a const reference to 'this' IFStreamBinarys data.
Definition: IFStreamBinary.h:634
#define byte
signed-byte typedef
Definition: CPPGlobals.h:94
static string className()
Definition: IFStreamBinary.h:564
int getCapacity() const
Return the allocated capacity of this IFStreamBinary.
Definition: IFStreamBinary.h:640
bool isByteOrderReversalOn() const
Returns true if the byte order in reads and writes is reversed for all 2, 4, or 8 byte intrinsincs (s...
Definition: IFStreamBinary.h:681
void setPosToEnd()
Set the current iterator position to the end of the buffer.
Definition: IFStreamBinary.h:607
void incrementPos(int increment=1)
Increment the iterator position.
Definition: IFStreamBinary.h:622
#define LONG_INT
Definition: CPPGlobals.h:111
void setMemoryCapacityIncrement(int mci)
Set the amount of memory increase (bytes) for bData everytime it's current capacity is exceeded (defa...
Definition: IFStreamBinary.h:297
string * bData
A string object is used to contain the actual data.
Definition: IFStreamBinary.h:86
char * getPosPointer(int pos)
Return a pointer to position pos in 'this' IFStreamBinarys data.
Definition: IFStreamBinary.h:653
void checkBufferSize(int sincr)
This function checks to make sure that the buffer is large enough to contain sincr more bytes ...
Definition: IFStreamBinary.h:158
int bDataPos
The current iterator position in the data container (dbData).
Definition: IFStreamBinary.h:91
void align4Byte()
Ensure that the buffer position pointer (dbDataPos) is aligned on a 4 byte boundary.
Definition: IFStreamBinary.h:138
void align2Byte()
Ensure that the buffer position pointer (dbDataPos) is aligned on a 4 byte boundary.
Definition: IFStreamBinary.h:128
bool bAlign
A boolean, that if true, maintains 4 and 8 byte alignment in support of double alignment compilation...
Definition: IFStreamBinary.h:102
int bSize
The current size of the data container.
Definition: IFStreamBinary.h:96
void setBoundaryAlignment(bool align)
Sets the intrinsic boundary alignment flag to align.
Definition: IFStreamBinary.h:687
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
void byteOrderReverseOn()
Turns on byte order reversal.
Definition: IFStreamBinary.h:668