GeoTessCPP: rstt/GeoTessCPP/include/IFStreamBinary.h Source File

GeoTessCPP 2.2.3 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 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 IFSTREAMBINARY_OBJECT_H
39 #define IFSTREAMBINARY_OBJECT_H
40
41 // **** _SYSTEM INCLUDES_ ******************************************************
42
43 #include <iostream>
44 #include <string>
45 #include <cstring>
46 #include <fstream>
47 #include <vector>
48 #include <sstream>
49
50 // use standard library objects
51 using namespace std;
52
53 // **** _LOCAL INCLUDES_ *******************************************************
54
55 #include "CPPUtils.h"
56 #include "GeoTessUtils.h"
57 #include "GeoTessException.h"
58
59 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
60
61 namespace geotess {
62
63 // **** _FORWARD REFERENCES_ ***************************************************
64
65 // **** _CLASS DEFINITION_ *****************************************************
66
81 class GEOTESS_EXP_IMP IFStreamBinary
82 {
83 protected:
84
88 string* bData;
89
93 int bDataPos;
94
98 int bSize;
99
104 bool bAlign;
105
113 bool bReverse;
114
120 bool bOwnStr;
121
122 string bFileName;
123
124 int bMemIncr;
125
130 void align2Byte()
131 {
132 int sm = bDataPos % CPPUtils::SSHT;
133 if (sm && bAlign) bDataPos += CPPUtils::SSHT - sm;
134 }
135
140 void align4Byte()
141 {
142 int sm = bDataPos % CPPUtils::SINT;
143 if (sm && bAlign) bDataPos += CPPUtils::SINT - sm;
144 }
145
150 void align8Byte()
151 {
152 int sm = bDataPos % CPPUtils::SDBL;
153 if (sm && bAlign) bDataPos += CPPUtils::SDBL - sm;
154 }
155
160 void checkBufferSize(int sincr)
161 {
162 if (bDataPos + sincr > (int)bData->size())
163 {
164 if (sincr + bData->size() > bData->capacity())
165 bData->reserve(bData->capacity() + bMemIncr);
166 bData->append(bDataPos + sincr - bData->size(), ' ');
167 }
168 }
169
170 /*
171 * Reverses each s-byte element of array a containing n elements
172 * (s*n bytes). The element size s must be 2, 4, or 8.
173 */
174 static void reverseBOArray(int n, char* a, int s);
175
176 /*
177 * Reverses the byte order of each element of a where a is an array of n
178 * 2, 4, or 8 byte elements. Used to convert between big-and
179 * little-endian formats.
180 */
181 static void reverseBO2Array(int n, char* a);
182 static void reverseBO4Array(int n, char* a);
183 static void reverseBO8Array(int n, char* a);
184
185 /*
186 * Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used
187 * to convert between big-and little-endian formats.
188 */
189 static void reverseBO2(char* d);
190 static void reverseBO4(char* d);
191 static void reverseBO8(char* d);
192
193 public:
194
198 IFStreamBinary();
199
203 IFStreamBinary(bool align);
204
209 IFStreamBinary(const string& filename);
210
215 IFStreamBinary(const string& filename, int num_bytes);
216
221 IFStreamBinary(string* str);
222
226 IFStreamBinary(const IFStreamBinary& db);
227
231 virtual ~IFStreamBinary();
232
236 IFStreamBinary& operator=(const IFStreamBinary& db);
237
241 void dumpBuffer();
242
246 static bool exists(const string& filename);
247
251 void writeToFile(const string& filename);
252
256 void writeToFile(ofstream& ofs);
257
261 void readFromFile(const string& filename);
262
266 void readFromFile(const string& filename, int num_bytes);
267
272 // void readFromFile(ifstream& ifs, streampos file_pos,
273 // int num_bytes);
274
278 void readFromFile(ifstream& ifs, int num_bytes);
279
283 const string& getFileName() const { return bFileName; }
284
288 void clear()
289 {
290 (*bData) = "";
291 bDataPos = 0;
292 bSize = 0;
293 }
294
299 void setMemoryCapacityIncrement(int mci)
300 {
301 if (mci > 0) bMemIncr = mci;
302 }
303
312 string readString();
313 void readString(string& s);
314 void readCharArray(string& s, int num_chars);
315 void readCharArray(char* array, int num_chars);
316 void readType(string& s);
317 void readTypeArray(string& array, int num_chars);
318
326 bool readBool();
327 bool readBool(int pos);
328 void readBoolArray(bool* array, int num_bools);
329 void readType(bool& b);
330 void readTypeArray(bool* array, int num_bools);
331
339 byte readByte();
340 byte readByte(int pos);
341 void readByteArray(byte* array, int num_bs);
342 void readType(byte& b);
343 void readTypeArray(byte* array, int num_bs);
344
354 short readShort();
355 short readShortNC();
356 short readShort(int pos);
357 void readShortArray(short* array, int num_shorts);
358 void readType(short& s);
359 void readTypeArray(short* array, int num_shorts);
360
370 int readInt();
371 int readIntNC();
372 int readInt(int pos);
373 void readIntArray(int* array, int num_ints);
374 void readType(int& i);
375 void readTypeArray(int* array, int num_ints);
376
386 LONG_INT readLong();
387 LONG_INT readLongNC();
388 LONG_INT readLong(int pos);
389 void readLongArray(LONG_INT* array, int num_longs);
390 void readType(LONG_INT& l);
391 void readTypeArray(LONG_INT* array, int num_longs);
392
402 float readFloat();
403 float readFloatNC();
404 float readFloat(int pos);
405 void readFloatArray(float* array, int num_floats);
406 void readType(float& f);
407 void readTypeArray(float* array, int num_floats);
408
418 double readDouble();
419 double readDoubleNC();
420 double readDouble(int pos);
421 void readDoubleArray(double* array, int num_doubles);
422 void readType(double& d);
423 void readTypeArray(double* array, int num_doubles);
424
433 void writeString(const string& str);
434 void writeString(const char* char_string);
435 void writeCharArray(const char* array, int num_chars);
436 void writeType(const string& str);
437 void writeType(const char* char_string);
438
449 void writeBool(bool b);
450 void writeBoolNC(bool b);
451 void writeBool(bool b, int pos);
452 void writeBoolArray(const bool* array, int num_bools);
453 void writeType(bool b);
454 void writeTypeArray(const bool* array, int num_bools);
455
466 void writeByte(byte b);
467 void writeByteNC(byte b);
468 void writeByte(byte b, int pos);
469 void writeByteArray(const byte* array, int num_bytes);
470 void writeType(byte b);
471 void writeTypeArray(const byte* array, int num_bytes);
472
484 void writeShort(short i);
485 void writeShortNC(short i);
486 void writeShort(short i, int pos);
487 void writeShortArray(const short* array, int num_ints);
488 void writeType(short i);
489 void writeTypeArray(const short* array, int num_ints);
490
502 void writeInt(int i);
503 void writeIntNC(int i);
504 void writeInt(int i, int pos);
505 void writeIntArray(const int* array, int num_ints);
506 void writeType(int i);
507 void writeTypeArray(const int* array, int num_ints);
508
520 void writeLong(LONG_INT l);
521 void writeLongNC(LONG_INT l);
522 void writeLong(LONG_INT l, int pos);
523 void writeLongArray(const LONG_INT* array, int num_longs);
524 void writeType(LONG_INT l);
525 void writeTypeArray(const LONG_INT* array, int num_longs);
526
538 void writeFloat(float f);
539 void writeFloatNC(float f);
540 void writeFloat(float f, int pos);
541 void writeFloatArray(const float* array, int num_floats);
542 void writeType(float f);
543 void writeTypeArray(const float* array, int num_floats);
544
556 void writeDouble(double d);
557 void writeDoubleNC(double d);
558 void writeDouble(double d, int pos);
559 void writeDoubleArray(const double* array, int num_doubles);
560 void writeType(double d);
561 void writeTypeArray(const double* array, int num_doubles);
562
563 /*
564 * Return class name.
565 */
566 static string className()
567 {
568 return "IFStreamBinary";
569 }
570
571 /*
572 * Return class size.
573 */
574 static int classSize()
575 {
576 return (int) sizeof(IFStreamBinary);
577 }
578
583 void reSize(int spc)
584 {
585 bData->resize(spc);
586 }
587
591 void reserve(int sze)
592 {
593 bData->reserve(sze);
594 }
595
599 int size()
600 {
601 if (bDataPos > bSize) bSize = bDataPos;
602 return bSize;
603 }
604
608 void resetPos()
609 {
610 if (bDataPos > bSize) bSize = bDataPos;
611 bDataPos = 0;
612 }
613
617 void setPosToEnd()
618 {
619 if (bDataPos > bSize) bSize = bDataPos;
620 bDataPos = bSize;
621 }
622
626 int getPos() const
627 {
628 return bDataPos;
629 }
630
634 void incrementPos(int increment = 1)
635 {
636 bDataPos += increment;
637 }
638
642 void decrementPos(int decrement = 1)
643 {
644 bDataPos -= decrement;
645 }
646
650 const string& getData() const
651 {
652 return *bData;
653 }
654
658 int getCapacity() const
659 {
660 return (int)bData->capacity();
661 }
662
667 char* getPosPointer()
668 {
669 return getPosPointer(bDataPos);
670 }
671
675 char* getPosPointer(int pos)
676 {
677 char* p = &(*bData)[pos];
678 return p;
679 }
680
684 void setByteOrderReverse(bool bor)
685 {
686 bReverse = bor;
687 }
688
692 void byteOrderReverseOn()
693 {
694 bReverse = true;
695 }
696
700 void byteOrderReverseOff()
701 {
702 bReverse = false;
703 }
704
709 bool isByteOrderReversalOn() const
710 {
711 return bReverse;
712 }
713
717 void setBoundaryAlignment(bool align)
718 {
719 bAlign = align;
720 }
721
725 void boundaryAlignmentOn()
726 {
727 bAlign = true;
728 }
729
733 void boundaryAlignmentOff()
734 {
735 bAlign = false;
736 }
737
742 bool isBoundaryAlignmentOn() const
743 {
744 return bAlign;
745 }
746
747 }; // end class IFStreamBinary
748
749 //************* Read Strings **************************************************
750
754 inline string IFStreamBinary::readString()
755 {
756 string s;
757 readString(s);
758 return s;
759 }
760
764 inline void IFStreamBinary::readString(string& s)
765 {
766 int len;
767 // check for zero added by sballar 2013-01-04
768 len = readInt();
769 if (len == 0)
770 s="";
771 else
772 {
773 // read the data
774 s = bData->substr(bDataPos, len);
775 bDataPos += len;
776 }
777 }
778
782 inline void IFStreamBinary::readCharArray(string& s, int num_chars)
783 {
784 s.resize(num_chars);
785 readCharArray(&s[0], num_chars);
786 }
787
791 inline void IFStreamBinary::readCharArray(char* array, int num_chars)
792 {
793 // read the data
794
795 memcpy(array, &(*bData)[bDataPos], num_chars);
796 bDataPos += num_chars;
797 }
798
802 inline void IFStreamBinary::readType(string& s)
803 {
804 readString(s);
805 }
806
810 inline void IFStreamBinary::readTypeArray(string& s, int num_chars)
811 {
812 readCharArray(s, num_chars);
813 }
814
815 //************* Read Bools ****************************************************
816
820 inline bool IFStreamBinary::readBool()
821 {
822 // read the data
823
824 int pos = bDataPos;
825 bDataPos += CPPUtils::SBOL;
826 return readBool(pos);
827 }
828
833 inline bool IFStreamBinary::readBool(int pos)
834 {
835 return *((bool*) &(*bData)[pos]);
836 }
837
841 inline void IFStreamBinary::readBoolArray(bool* array, int num_bools)
842 {
843 // get bytes and perform alignment
844
845 int num_bytes = num_bools * CPPUtils::SBOL;
846
847 // copy from buffer into array ...
848 // update buffer position and exit
849
850 memcpy(array, &(*bData)[bDataPos], num_bytes);
851 bDataPos += num_bytes;
852 }
853
857 inline void IFStreamBinary::readType(bool& b)
858 {
859 b = readBool();
860 }
861
865 inline void IFStreamBinary::readTypeArray(bool* array, int num_bools)
866 {
867 readBoolArray(array, num_bools);
868 }
869
870 //************* Read Bytes ****************************************************
871
875 inline byte IFStreamBinary::readByte()
876 {
877 // read the data
878
879 int pos = bDataPos;
880 bDataPos += CPPUtils::SBYT;
881 return readByte(pos);
882 }
883
888 inline byte IFStreamBinary::readByte(int pos)
889 {
890 return *((byte*) &(*bData)[pos]);
891 }
892
896 inline void IFStreamBinary::readByteArray(byte* array, int num_bytes)
897 {
898 // get bytes and perform alignment
899
900 num_bytes *= CPPUtils::SBYT;
901
902 // copy from buffer into array ...
903 // update buffer position and exit
904
905 memcpy(array, &(*bData)[bDataPos], num_bytes);
906 bDataPos += num_bytes;
907 }
908
912 inline void IFStreamBinary::readType(byte& b)
913 {
914 b = readByte();
915 }
916
920 inline void IFStreamBinary::readTypeArray(byte* array, int num_bytes)
921 {
922 readByteArray(array, num_bytes);
923 }
924
925 //************* Read Shorts ***************************************************
926
930 inline short IFStreamBinary::readShort()
931 {
932 align2Byte();
933 return readShortNC();
934 }
935
939 inline short IFStreamBinary::readShortNC()
940 {
941 // read the data
942
943 int pos = bDataPos;
944 bDataPos += CPPUtils::SSHT;
945 return readShort(pos);
946 }
947
952 inline short IFStreamBinary::readShort(int pos)
953 {
954 // read the data ... use memcpy if alignment is off
955
956 short s;
957 if (bAlign)
958 s = *((short*) &(*bData)[pos]);
959 else
960 memcpy(&s, &(*bData)[pos], 2);
961
962 if (bReverse) reverseBO2((char*) &s);
963 return s;
964 }
965
969 inline void IFStreamBinary::readShortArray(short* array, int num_shorts)
970 {
971 // get bytes and perform alignment
972
973 int num_bytes = num_shorts * CPPUtils::SSHT;
974 align2Byte();
975
976 // copy from buffer into array ... reverse data if necessary ...
977 // update buffer position and exit
978
979 memcpy(array, &(*bData)[bDataPos], num_bytes);
980 if (bReverse) reverseBO2Array(num_shorts, (char*) array);
981 bDataPos += num_bytes;
982 }
983
987 inline void IFStreamBinary::readType(short& s)
988 {
989 s = readShort();
990 }
991
995 inline void IFStreamBinary::readTypeArray(short* array, int num_shorts)
996 {
997 readShortArray(array, num_shorts);
998 }
999
1000 //************* Read Ints *****************************************************
1001
1005 inline int IFStreamBinary::readInt()
1006 {
1007 align4Byte();
1008 return readIntNC();
1009 }
1010
1014 inline int IFStreamBinary::readIntNC()
1015 {
1016 // read the data
1017
1018 int pos = bDataPos;
1019 bDataPos += CPPUtils::SINT;
1020 return readInt(pos);
1021 }
1022
1027 inline int IFStreamBinary::readInt(int pos)
1028 {
1029 // read the data ... use memcpy if alignment is off
1030
1031 int i;
1032 if (bAlign)
1033 i = *((int*) &(*bData)[pos]);
1034 else
1035 memcpy(&i, &(*bData)[pos], 4);
1036
1037 if (bReverse) reverseBO4((char*) &i);
1038 return i;
1039 }
1040
1044 inline void IFStreamBinary::readIntArray(int* array, int num_ints)
1045 {
1046 // get bytes and perform alignment
1047
1048 int num_bytes = num_ints * CPPUtils::SINT;
1049 align4Byte();
1050
1051 // copy from buffer into array ... reverse data if necessary ...
1052 // update buffer position and exit
1053
1054 memcpy(array, &(*bData)[bDataPos], num_bytes);
1055 if (bReverse) reverseBO4Array(num_ints, (char*) array);
1056 bDataPos += num_bytes;
1057 }
1058
1062 inline void IFStreamBinary::readType(int& i)
1063 {
1064 i = readInt();
1065 }
1066
1070 inline void IFStreamBinary::readTypeArray(int* array, int num_ints)
1071 {
1072 readIntArray(array, num_ints);
1073 }
1074
1075 //************* Read Longs ****************************************************
1076
1080 inline LONG_INT IFStreamBinary::readLong()
1081 {
1082 align8Byte();
1083 return readLongNC();
1084 }
1085
1089 inline LONG_INT IFStreamBinary::readLongNC()
1090 {
1091 // read the data
1092
1093 int pos = bDataPos;
1094 bDataPos += CPPUtils::SLNG;
1095 return readLong(pos);
1096 }
1097
1102 inline LONG_INT IFStreamBinary::readLong(int pos)
1103 {
1104 // read the data ... use memcpy if alignment is off
1105
1106 LONG_INT l;
1107 if (bAlign)
1108 l = *((LONG_INT*) &(*bData)[pos]);
1109 else
1110 memcpy(&l, &(*bData)[pos], 8);
1111
1112 if (bReverse) reverseBO8((char*) &l);
1113 return l;
1114 }
1115
1119 inline void IFStreamBinary::readLongArray(LONG_INT* array, int num_longs)
1120 {
1121 // get bytes and perform alignment
1122
1123 int num_bytes = num_longs * CPPUtils::SLNG;
1124 align8Byte();
1125
1126 // copy from buffer into array ... reverse data if necessary ...
1127 // update buffer position and exit
1128
1129 memcpy(array, &(*bData)[bDataPos], num_bytes);
1130 if (bReverse) reverseBO8Array(num_longs, (char*) array);
1131 bDataPos += num_bytes;
1132 }
1133
1137 inline void IFStreamBinary::readType(LONG_INT& l)
1138 {
1139 l = readLong();
1140 }
1141
1145 inline void IFStreamBinary::readTypeArray(LONG_INT* array, int num_longs)
1146 {
1147 readLongArray(array, num_longs);
1148 }
1149
1150 //************* Read Floats ***************************************************
1151
1155 inline float IFStreamBinary::readFloat()
1156 {
1157 align4Byte();
1158 return readFloatNC();
1159 }
1160
1164 inline float IFStreamBinary::readFloatNC()
1165 {
1166 // read the data
1167
1168 int pos = bDataPos;
1169 bDataPos += CPPUtils::SFLT;
1170 return readFloat(pos);
1171 }
1172
1177 inline float IFStreamBinary::readFloat(int pos)
1178 {
1179 // read the data ... use memcpy if alignment is off
1180
1181 float f;
1182 if (bAlign)
1183 f = *((float*) &(*bData)[pos]);
1184 else
1185 memcpy(&f, &(*bData)[pos], 4);
1186
1187 if (bReverse) reverseBO4((char*) &f);
1188 return f;
1189 }
1190
1194 inline void IFStreamBinary::readFloatArray(float* array, int num_floats)
1195 {
1196 // get bytes and perform alignment
1197
1198 int num_bytes = num_floats * CPPUtils::SFLT;
1199 align4Byte();
1200
1201 // copy from buffer into array ... reverse data if necessary ...
1202 // update buffer position and exit
1203
1204 memcpy(array, &(*bData)[bDataPos], num_bytes);
1205 if (bReverse) reverseBO4Array(num_floats, (char*) array);
1206 bDataPos += num_bytes;
1207 }
1208
1212 inline void IFStreamBinary::readType(float& f)
1213 {
1214 f = readFloat();
1215 }
1216
1220 inline void IFStreamBinary::readTypeArray(float* array, int num_floats)
1221 {
1222 readFloatArray(array, num_floats);
1223 }
1224
1225 //************* Read Doubles **************************************************
1226
1230 inline double IFStreamBinary::readDouble()
1231 {
1232 align8Byte();
1233 return readDoubleNC();
1234 }
1235
1239 inline double IFStreamBinary::readDoubleNC()
1240 {
1241 // read the data
1242
1243 int pos = bDataPos;
1244 bDataPos += CPPUtils::SDBL;
1245 return readDouble(pos);
1246 }
1247
1252 inline double IFStreamBinary::readDouble(int pos)
1253 {
1254 // read the data ... use memcpy if alignment is off
1255
1256 double d;
1257 if (bAlign)
1258 d = *((double*) &(*bData)[pos]);
1259 else
1260 memcpy(&d, &(*bData)[pos], 8);
1261
1262 if (bReverse) reverseBO8((char*) &d);
1263 return d;
1264 }
1265
1269 inline void IFStreamBinary::readDoubleArray(double* array, int num_doubles)
1270 {
1271 // get bytes and perform alignment
1272
1273 int num_bytes = num_doubles * CPPUtils::SDBL;
1274 align8Byte();
1275
1276 // copy from buffer into array ... reverse data if necessary ...
1277 // update buffer position and exit
1278
1279 memcpy(array, &(*bData)[bDataPos], num_bytes);
1280 if (bReverse) reverseBO8Array(num_doubles, (char*) array);
1281 bDataPos += num_bytes;
1282 }
1283
1287 inline void IFStreamBinary::readType(double& d)
1288 {
1289 d = readDouble();
1290 }
1291
1295 inline void IFStreamBinary::readTypeArray(double* array, int num_doubles)
1296 {
1297 readDoubleArray(array, num_doubles);
1298 }
1299
1300 //************* Write Strings *************************************************
1301
1308 inline void IFStreamBinary::writeString(const string& str)
1309 {
1310 // get total size, align, and check capacity
1311
1312 int sz = (int) str.size() + CPPUtils::SINT;
1313 align4Byte();
1314 checkBufferSize(sz);
1315
1316 // write the data
1317
1318 writeIntNC((int) str.size());
1319 memcpy(&(*bData)[bDataPos], &str[0], str.size());
1320 bDataPos += (int) str.size();
1321 }
1322
1328 inline void IFStreamBinary::writeString(const char* char_string)
1329 {
1330 string tmp_string = char_string;
1331 writeString(tmp_string);
1332 }
1333
1337 inline void IFStreamBinary::writeCharArray(const char* array, int num_chars)
1338 {
1339 checkBufferSize(num_chars);
1340
1341 // write data
1342
1343 memcpy(&(*bData)[bDataPos], array, num_chars);
1344 bDataPos += num_chars;
1345 }
1346
1353 inline void IFStreamBinary::writeType(const string& str)
1354 {
1355 writeString(str);
1356 }
1357
1363 inline void IFStreamBinary::writeType(const char* char_string)
1364 {
1365 writeString(char_string);
1366 }
1367
1368 //************* Write Bools ***************************************************
1369
1375 inline void IFStreamBinary::writeBool(bool b)
1376 {
1377 checkBufferSize(CPPUtils::SBOL);
1378
1379 writeBoolNC(b);
1380 }
1381
1387 inline void IFStreamBinary::writeBoolNC(bool b)
1388 {
1389 writeBool(b, bDataPos);
1390 bDataPos += CPPUtils::SBOL;
1391 }
1392
1398 inline void IFStreamBinary::writeBool(bool b, int pos)
1399 {
1400 *((bool*) &(*bData)[pos]) = b;
1401 }
1402
1406 inline void IFStreamBinary::writeBoolArray(const bool* array, int num_bools)
1407 {
1408 // get number of bytes to be written and check for alignment and buffer
1409 // size
1410
1411 num_bools *= CPPUtils::SBOL;
1412 checkBufferSize(num_bools);
1413
1414 // copy array into data buffer. Reverse data buffer entries if necessary
1415 // and increment the buffer output pointer.
1416
1417 memcpy(&(*bData)[bDataPos], array, num_bools);
1418 bDataPos += num_bools;
1419 }
1420
1426 inline void IFStreamBinary::writeType(bool b)
1427 {
1428 writeBool(b);
1429 }
1430
1434 inline void IFStreamBinary::writeTypeArray(const bool* array, int num_bools)
1435 {
1436 writeBoolArray(array, num_bools);
1437 }
1438
1439 //************* Write Bytes ***************************************************
1440
1446 inline void IFStreamBinary::writeByte(byte b)
1447 {
1448 checkBufferSize(CPPUtils::SBYT);
1449
1450 writeByteNC(b);
1451 }
1452
1458 inline void IFStreamBinary::writeByteNC(byte b)
1459 {
1460 writeByte(b, bDataPos);
1461 bDataPos += CPPUtils::SBYT;
1462 }
1463
1469 inline void IFStreamBinary::writeByte(byte b, int pos)
1470 {
1471 *((byte*) &(*bData)[pos]) = b;
1472 }
1473
1477 inline void IFStreamBinary::writeByteArray(const byte* array, int num_bytes)
1478 {
1479 // get number of bytes to be written and check for alignment and buffer
1480 // size
1481
1482 num_bytes *= CPPUtils::SBYT;
1483 checkBufferSize(num_bytes);
1484
1485 // copy array into data buffer. Reverse data buffer entries if necessary
1486 // and increment the buffer output pointer.
1487
1488 memcpy(&(*bData)[bDataPos], array, num_bytes);
1489 bDataPos += num_bytes;
1490 }
1491
1497 inline void IFStreamBinary::writeType(byte b)
1498 {
1499 writeByte(b);
1500 }
1501
1505 inline void IFStreamBinary::writeTypeArray(const byte* array, int num_bytes)
1506 {
1507 writeByteArray(array, num_bytes);
1508 }
1509
1510 //************* Write Shorts **************************************************
1511
1517 inline void IFStreamBinary::writeShort(short s)
1518 {
1519 align2Byte();
1520 checkBufferSize(CPPUtils::SSHT);
1521
1522 writeShortNC(s);
1523 }
1524
1530 inline void IFStreamBinary::writeShortNC(short s)
1531 {
1532 writeShort(s, bDataPos);
1533 bDataPos += CPPUtils::SSHT;
1534 }
1535
1541 inline void IFStreamBinary::writeShort(short s, int pos)
1542 {
1543 if (bReverse) reverseBO2((char*) &s);
1544
1545 if (bAlign)
1546 *((short*) &(*bData)[pos]) = s;
1547 else
1548 memcpy(&(*bData)[pos], &s, CPPUtils::SSHT);
1549 }
1550
1554 inline void IFStreamBinary::writeShortArray(const short* array, int num_shorts)
1555 {
1556 // get number of bytes to be written and check for alignment and buffer
1557 // size
1558
1559 int num_bytes = num_shorts * CPPUtils::SSHT;
1560 align2Byte();
1561 checkBufferSize(num_bytes);
1562
1563 // copy array into data buffer. Reverse data buffer entries if necessary
1564 // and increment the buffer output pointer.
1565
1566 memcpy(&(*bData)[bDataPos], array, num_bytes);
1567 if (bReverse) reverseBO2Array(num_shorts, &(*bData)[bDataPos]);
1568 bDataPos += num_bytes;
1569 }
1570
1576 inline void IFStreamBinary::writeType(short s)
1577 {
1578 writeShort(s);
1579 }
1580
1584 inline void IFStreamBinary::writeTypeArray(const short* array, int num_shorts)
1585 {
1586 writeShortArray(array, num_shorts);
1587 }
1588
1589 //************* Write Ints ****************************************************
1590
1596 inline void IFStreamBinary::writeInt(int i)
1597 {
1598 align4Byte();
1599 checkBufferSize(CPPUtils::SINT);
1600
1601 writeIntNC(i);
1602 }
1603
1609 inline void IFStreamBinary::writeIntNC(int i)
1610 {
1611 writeInt(i, bDataPos);
1612 bDataPos += CPPUtils::SINT;
1613 }
1614
1620 inline void IFStreamBinary::writeInt(int i, int pos)
1621 {
1622 if (bReverse) reverseBO4((char*) &i);
1623
1624 if (bAlign)
1625 *((int*) &(*bData)[pos]) = i;
1626 else
1627 memcpy(&(*bData)[pos], &i, CPPUtils::SINT);
1628 }
1629
1633 inline void IFStreamBinary::writeIntArray(const int* array, int num_ints)
1634 {
1635 // get number of bytes to be written and check for alignment and buffer
1636 // size
1637
1638 int num_bytes = num_ints * CPPUtils::SINT;
1639 align4Byte();
1640 checkBufferSize(num_bytes);
1641
1642 // copy array into data buffer. Reverse data buffer entries if necessary
1643 // and increment the buffer output pointer.
1644
1645 memcpy(&(*bData)[bDataPos], array, num_bytes);
1646 if (bReverse) reverseBO4Array(num_ints, &(*bData)[bDataPos]);
1647 bDataPos += num_bytes;
1648 }
1649
1655 inline void IFStreamBinary::writeType(int i)
1656 {
1657 writeInt(i);
1658 }
1659
1663 inline void IFStreamBinary::writeTypeArray(const int* array, int num_ints)
1664 {
1665 writeIntArray(array, num_ints);
1666 }
1667
1668 //************* Write Longs ***************************************************
1669
1675 inline void IFStreamBinary::writeLong(LONG_INT l)
1676 {
1677 align8Byte();
1678 checkBufferSize(CPPUtils::SLNG);
1679
1680 writeLongNC(l);
1681 }
1682
1688 inline void IFStreamBinary::writeLongNC(LONG_INT l)
1689 {
1690 writeLong(l, bDataPos);
1691 bDataPos += CPPUtils::SLNG;
1692 }
1693
1699 inline void IFStreamBinary::writeLong(LONG_INT l, int pos)
1700 {
1701 if (bReverse) reverseBO8((char*) &l);
1702
1703 if (bAlign)
1704 *((LONG_INT*) &(*bData)[pos]) = l;
1705 else
1706 memcpy(&(*bData)[pos], &l, CPPUtils::SLNG);
1707 }
1708
1712 inline void IFStreamBinary::writeLongArray(const LONG_INT* array, int num_longs)
1713 {
1714 // get number of bytes to be written and check for alignment and buffer
1715 // size
1716
1717 int num_bytes = num_longs * CPPUtils::SLNG;
1718 align8Byte();
1719 checkBufferSize(num_bytes);
1720
1721 // copy array into data buffer. Reverse data buffer entries if necessary
1722 // and increment the buffer output pointer.
1723
1724 memcpy(&(*bData)[bDataPos], array, num_bytes);
1725 if (bReverse) reverseBO8Array(num_longs, &(*bData)[bDataPos]);
1726 bDataPos += num_bytes;
1727 }
1728
1734 inline void IFStreamBinary::writeType(LONG_INT l)
1735 {
1736 writeLong(l);
1737 }
1738
1742 inline void IFStreamBinary::writeTypeArray(const LONG_INT* array, int num_longs)
1743 {
1744 writeLongArray(array, num_longs);
1745 }
1746
1747 //************* Write Floats **************************************************
1748
1754 inline void IFStreamBinary::writeFloat(float f)
1755 {
1756 align4Byte();
1757 checkBufferSize(CPPUtils::SFLT);
1758
1759 writeFloatNC(f);
1760 }
1761
1767 inline void IFStreamBinary::writeFloatNC(float f)
1768 {
1769 writeFloat(f, bDataPos);
1770 bDataPos += CPPUtils::SFLT;
1771 }
1772
1778 inline void IFStreamBinary::writeFloat(float f, int pos)
1779 {
1780 if (bReverse) reverseBO4((char*) &f);
1781
1782 if (bAlign)
1783 *((float*) &(*bData)[pos]) = f;
1784 else
1785 memcpy(&(*bData)[pos], &f, CPPUtils::SFLT);
1786 }
1787
1791 inline void IFStreamBinary::writeFloatArray(const float* array, int num_floats)
1792 {
1793 // get number of bytes to be written and check for alignment and buffer
1794 // size
1795
1796 int num_bytes = num_floats * CPPUtils::SFLT;
1797 align4Byte();
1798 checkBufferSize(num_bytes);
1799
1800 // copy array into data buffer. Reverse data buffer entries if necessary
1801 // and increment the buffer output pointer.
1802
1803 memcpy(&(*bData)[bDataPos], array, num_bytes);
1804 if (bReverse) reverseBO4Array(num_floats, &(*bData)[bDataPos]);
1805 bDataPos += num_bytes;
1806 }
1807
1813 inline void IFStreamBinary::writeType(float f)
1814 {
1815 writeFloat(f);
1816 }
1817
1821 inline void IFStreamBinary::writeTypeArray(const float* array, int num_floats)
1822 {
1823 writeFloatArray(array, num_floats);
1824 }
1825
1826 //************* Write Doubles *************************************************
1827
1833 inline void IFStreamBinary::writeDouble(double d)
1834 {
1835 align8Byte();
1836 checkBufferSize(CPPUtils::SDBL);
1837
1838 writeDoubleNC(d);
1839 }
1840
1846 inline void IFStreamBinary::writeDoubleNC(double d)
1847 {
1848 writeDouble(d, bDataPos);
1849 bDataPos += CPPUtils::SDBL;
1850 }
1851
1857 inline void IFStreamBinary::writeDouble(double d, int pos)
1858 {
1859 if (bReverse) reverseBO8((char*) &d);
1860
1861 if (bAlign)
1862 *((double*) &(*bData)[pos]) = d;
1863 else
1864 memcpy(&(*bData)[pos], &d, CPPUtils::SDBL);
1865 }
1866
1870 inline void IFStreamBinary::writeDoubleArray(const double* array, int num_doubles)
1871 {
1872 // get number of bytes to be written and check for alignment and buffer
1873 // size
1874
1875 int num_bytes = num_doubles * CPPUtils::SDBL;
1876 align8Byte();
1877 checkBufferSize(num_bytes);
1878
1879 // copy array into data buffer. Reverse data buffer entries if necessary
1880 // and increment the buffer output pointer.
1881
1882 memcpy(&(*bData)[bDataPos], array, num_bytes);
1883 if (bReverse) reverseBO8Array(num_doubles, &(*bData)[bDataPos]);
1884 bDataPos += num_bytes;
1885 }
1886
1892 inline void IFStreamBinary::writeType(double d)
1893 {
1894 writeDouble(d);
1895 }
1896
1900 inline void IFStreamBinary::writeTypeArray(const double* array, int num_doubles)
1901 {
1902 writeDoubleArray(array, num_doubles);
1903 }
1904
1905 //************* Byte reversal functions ***************************************
1906
1910 inline void IFStreamBinary::reverseBO2(char* d)
1911 {
1912 char tmp = d[0];
1913 d[0] = d[1];
1914 d[1] = tmp;
1915 }
1916
1920 inline void IFStreamBinary::reverseBO4(char* d)
1921 {
1922 char tmp = d[0];
1923 d[0] = d[3];
1924 d[3] = tmp;
1925
1926 tmp = d[1];
1927 d[1] = d[2];
1928 d[2] = tmp;
1929 }
1930
1934 inline void IFStreamBinary::reverseBO8(char* d)
1935 {
1936 char tmp = d[0];
1937 d[0] = d[7];
1938 d[7] = tmp;
1939
1940 tmp = d[1];
1941 d[1] = d[6];
1942 d[6] = tmp;
1943
1944 tmp = d[2];
1945 d[2] = d[5];
1946 d[5] = tmp;
1947
1948 tmp = d[3];
1949 d[3] = d[4];
1950 d[4] = tmp;
1951 }
1952
1953 } // end namespace geotess
1954
1955 #endif // IFSTREAMBINARY_OBJECT_H
geotess::IFStreamBinary::reserve
void reserve(int sze)
Definition: IFStreamBinary.h:591
geotess::IFStreamBinary::boundaryAlignmentOff
void boundaryAlignmentOff()
Definition: IFStreamBinary.h:733
geotess
Definition: ArrayReuse.h:57
GeoTessException.h
geotess::IFStreamBinary::getPosPointer
char * getPosPointer()
Definition: IFStreamBinary.h:667
geotess::IFStreamBinary::isBoundaryAlignmentOn
bool isBoundaryAlignmentOn() const
Definition: IFStreamBinary.h:742
geotess::IFStreamBinary::bReverse
bool bReverse
Definition: IFStreamBinary.h:113
geotess::IFStreamBinary::classSize
static int classSize()
Definition: IFStreamBinary.h:574
geotess::IFStreamBinary::exists
static bool exists(const string &filename)
geotess::IFStreamBinary::align8Byte
void align8Byte()
Definition: IFStreamBinary.h:150
geotess::IFStreamBinary::getFileName
const string & getFileName() const
Definition: IFStreamBinary.h:283
geotess::IFStreamBinary::bOwnStr
bool bOwnStr
Definition: IFStreamBinary.h:120
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary(bool align)
geotess::IFStreamBinary::reverseBO2Array
static void reverseBO2Array(int n, char *a)
geotess::IFStreamBinary::writeToFile
void writeToFile(const string &filename)
geotess::IFStreamBinary::boundaryAlignmentOn
void boundaryAlignmentOn()
Definition: IFStreamBinary.h:725
geotess::IFStreamBinary::bFileName
string bFileName
Definition: IFStreamBinary.h:122
geotess::IFStreamBinary::reverseBOArray
static void reverseBOArray(int n, char *a, int s)
geotess::IFStreamBinary::size
int size()
Definition: IFStreamBinary.h:599
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary(string *str)
geotess::IFStreamBinary::getData
const string & getData() const
Definition: IFStreamBinary.h:650
geotess::IFStreamBinary::dumpBuffer
void dumpBuffer()
geotess::IFStreamBinary::isByteOrderReversalOn
bool isByteOrderReversalOn() const
Definition: IFStreamBinary.h:709
geotess::IFStreamBinary::reverseBO8Array
static void reverseBO8Array(int n, char *a)
geotess::IFStreamBinary::decrementPos
void decrementPos(int decrement=1)
Definition: IFStreamBinary.h:642
geotess::IFStreamBinary::bMemIncr
int bMemIncr
Definition: IFStreamBinary.h:124
GeoTessUtils.h
geotess::IFStreamBinary::byteOrderReverseOff
void byteOrderReverseOff()
Definition: IFStreamBinary.h:700
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary(const string &filename)
geotess::IFStreamBinary::readFromFile
void readFromFile(const string &filename)
LONG_INT
#define LONG_INT
Definition: CPPGlobals.h:113
geotess::IFStreamBinary::setByteOrderReverse
void setByteOrderReverse(bool bor)
Definition: IFStreamBinary.h:684
geotess::IFStreamBinary::className
static string className()
Definition: IFStreamBinary.h:566
geotess::IFStreamBinary::readFromFile
void readFromFile(const string &filename, int num_bytes)
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary()
geotess::IFStreamBinary::reSize
void reSize(int spc)
Definition: IFStreamBinary.h:583
geotess::IFStreamBinary::align2Byte
void align2Byte()
Definition: IFStreamBinary.h:130
geotess::IFStreamBinary::resetPos
void resetPos()
Definition: IFStreamBinary.h:608
geotess::IFStreamBinary::incrementPos
void incrementPos(int increment=1)
Definition: IFStreamBinary.h:634
geotess::IFStreamBinary::setPosToEnd
void setPosToEnd()
Definition: IFStreamBinary.h:617
geotess::IFStreamBinary::reverseBO4Array
static void reverseBO4Array(int n, char *a)
geotess::IFStreamBinary::getCapacity
int getCapacity() const
Definition: IFStreamBinary.h:658
geotess::IFStreamBinary::bData
string * bData
Definition: IFStreamBinary.h:88
geotess::IFStreamBinary::setMemoryCapacityIncrement
void setMemoryCapacityIncrement(int mci)
Definition: IFStreamBinary.h:299
GEOTESS_EXP_IMP
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:73
geotess::IFStreamBinary::operator=
IFStreamBinary & operator=(const IFStreamBinary &db)
geotess::IFStreamBinary::getPosPointer
char * getPosPointer(int pos)
Definition: IFStreamBinary.h:675
geotess::IFStreamBinary::byteOrderReverseOn
void byteOrderReverseOn()
Definition: IFStreamBinary.h:692
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary(const IFStreamBinary &db)
geotess::IFStreamBinary::getPos
int getPos() const
Definition: IFStreamBinary.h:626
geotess::IFStreamBinary
Opens a file for binary read and write access.
Definition: IFStreamBinary.h:82
geotess::IFStreamBinary::bSize
int bSize
Definition: IFStreamBinary.h:98
geotess::IFStreamBinary::bDataPos
int bDataPos
Definition: IFStreamBinary.h:93
geotess::IFStreamBinary::checkBufferSize
void checkBufferSize(int sincr)
Definition: IFStreamBinary.h:160
geotess::IFStreamBinary::readFromFile
void readFromFile(ifstream &ifs, int num_bytes)
geotess::IFStreamBinary::writeToFile
void writeToFile(ofstream &ofs)
geotess::IFStreamBinary::clear
void clear()
Definition: IFStreamBinary.h:288
geotess::IFStreamBinary::setBoundaryAlignment
void setBoundaryAlignment(bool align)
Definition: IFStreamBinary.h:717
geotess::IFStreamBinary::align4Byte
void align4Byte()
Definition: IFStreamBinary.h:140
geotess::IFStreamBinary::~IFStreamBinary
virtual ~IFStreamBinary()
geotess::IFStreamBinary::IFStreamBinary
IFStreamBinary(const string &filename, int num_bytes)
geotess::IFStreamBinary::bAlign
bool bAlign
Definition: IFStreamBinary.h:104
CPPUtils.h