GeoTessCPP: include/IFStreamBinary.h Source File

GeoTessCPP 2.0.0 Software to facilitate storage and retrieval of 3D information about the Earth.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Friends Macros
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 <fstream>
44 #include <vector>
45 #include <sstream>
46
47 // use standard library objects
48 using namespace std;
49
50 // **** _LOCAL INCLUDES_ *******************************************************
51
52 #include "CPPUtils.h"
53 #include "GeoTessUtils.h"
54 #include "GeoTessException.h"
55
56 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
57
58 namespace geotess {
59
60 // **** _FORWARD REFERENCES_ ***************************************************
61
62 // **** _CLASS DEFINITION_ *****************************************************
63
78 class GEOTESS_EXP_IMP IFStreamBinary
79 {
80 protected:
81
85 string* bData;
86
90 int bDataPos;
91
95 int bSize;
96
101 bool bAlign;
102
110 bool bReverse;
111
117 bool bOwnStr;
118
119 string bFileName;
120
121 int bMemIncr;
122
127 void align2Byte()
128 {
129 int sm = bDataPos % CPPUtils::SSHT;
130 if (sm && bAlign) bDataPos += CPPUtils::SSHT - sm;
131 }
132
137 void align4Byte()
138 {
139 int sm = bDataPos % CPPUtils::SINT;
140 if (sm && bAlign) bDataPos += CPPUtils::SINT - sm;
141 }
142
147 void align8Byte()
148 {
149 int sm = bDataPos % CPPUtils::SDBL;
150 if (sm && bAlign) bDataPos += CPPUtils::SDBL - sm;
151 }
152
157 void checkBufferSize(int sincr)
158 {
159 if (bDataPos + sincr > (int) bData->size())
160 {
161 if (sincr + bData->size() > bData->capacity())
162 bData->reserve(bData->capacity() + bMemIncr);
163 bData->append(bDataPos + sincr - bData->size(), ' ');
164 }
165 }
166
167 /*
168 * Reverses each s-byte element of array a containing n elements
169 * (s*n bytes). The element size s must be 2, 4, or 8.
170 */
171 static void reverseBOArray(int n, char* a, int s);
172
173 /*
174 * Reverses the byte order of each element of a where a is an array of n
175 * 2, 4, or 8 byte elements. Used to convert between big-and
176 * little-endian formats.
177 */
178 static void reverseBO2Array(int n, char* a);
179 static void reverseBO4Array(int n, char* a);
180 static void reverseBO8Array(int n, char* a);
181
182 /*
183 * Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used
184 * to convert between big-and little-endian formats.
185 */
186 static void reverseBO2(char* d);
187 static void reverseBO4(char* d);
188 static void reverseBO8(char* d);
189
190 public:
191
195 IFStreamBinary();
196
200 IFStreamBinary(bool align);
201
206 IFStreamBinary(const string& filename);
207
212 IFStreamBinary(const string& filename, int num_bytes);
213
218 IFStreamBinary(string* str);
219
223 IFStreamBinary(const IFStreamBinary& db);
224
228 virtual ~IFStreamBinary();
229
233 IFStreamBinary& operator=(const IFStreamBinary& db);
234
238 void dumpBuffer();
239
243 static bool exists(const string& filename);
244
248 void writeToFile(const string& filename);
249
253 void writeToFile(ofstream& ofs);
254
258 void readFromFile(const string& filename);
259
263 void readFromFile(const string& filename, int num_bytes);
264
269 // void readFromFile(ifstream& ifs, streampos file_pos,
270 // int num_bytes);
271
275 void readFromFile(ifstream& ifs, int num_bytes);
276
280 const string& getFileName() const { return bFileName; }
281
285 void clear()
286 {
287 (*bData) = "";
288 bDataPos = 0;
289 bSize = 0;
290 }
291
296 void setMemoryCapacityIncrement(int mci)
297 {
298 if (mci > 0) bMemIncr = mci;
299 }
300
309 string readString();
310 void readString(string& s);
311 void readCharArray(string& s, int num_chars);
312 void readCharArray(char* array, int num_chars);
313 void readType(string& s);
314 void readTypeArray(string& array, int num_chars);
315
323 bool readBool();
324 bool readBool(int pos);
325 void readBoolArray(bool* array, int num_bools);
326 void readType(bool& b);
327 void readTypeArray(bool* array, int num_bools);
328
336 byte readByte();
337 byte readByte(int pos);
338 void readByteArray(byte* array, int num_bs);
339 void readType(byte& b);
340 void readTypeArray(byte* array, int num_bs);
341
351 short readShort();
352 short readShortNC();
353 short readShort(int pos);
354 void readShortArray(short* array, int num_shorts);
355 void readType(short& s);
356 void readTypeArray(short* array, int num_shorts);
357
367 int readInt();
368 int readIntNC();
369 int readInt(int pos);
370 void readIntArray(int* array, int num_ints);
371 void readType(int& i);
372 void readTypeArray(int* array, int num_ints);
373
383 LONG_INT readLong();
384 LONG_INT readLongNC();
385 LONG_INT readLong(int pos);
386 void readLongArray(LONG_INT* array, int num_longs);
387 void readType(LONG_INT& l);
388 void readTypeArray(LONG_INT* array, int num_longs);
389
399 float readFloat();
400 float readFloatNC();
401 float readFloat(int pos);
402 void readFloatArray(float* array, int num_floats);
403 void readType(float& f);
404 void readTypeArray(float* array, int num_floats);
405
415 double readDouble();
416 double readDoubleNC();
417 double readDouble(int pos);
418 void readDoubleArray(double* array, int num_doubles);
419 void readType(double& d);
420 void readTypeArray(double* array, int num_doubles);
421
430 void writeString(const string& str);
431 void writeString(const char* char_string);
432 void writeCharArray(const char* array, int num_chars);
433 void writeType(const string& str);
434 void writeType(const char* char_string);
435
446 void writeBool(bool b);
447 void writeBoolNC(bool b);
448 void writeBool(bool b, int pos);
449 void writeBoolArray(const bool* array, int num_bools);
450 void writeType(bool b);
451 void writeTypeArray(const bool* array, int num_bools);
452
463 void writeByte(byte b);
464 void writeByteNC(byte b);
465 void writeByte(byte b, int pos);
466 void writeByteArray(const byte* array, int num_bytes);
467 void writeType(byte b);
468 void writeTypeArray(const byte* array, int num_bytes);
469
481 void writeShort(short i);
482 void writeShortNC(short i);
483 void writeShort(short i, int pos);
484 void writeShortArray(const short* array, int num_ints);
485 void writeType(short i);
486 void writeTypeArray(const short* array, int num_ints);
487
499 void writeInt(int i);
500 void writeIntNC(int i);
501 void writeInt(int i, int pos);
502 void writeIntArray(const int* array, int num_ints);
503 void writeType(int i);
504 void writeTypeArray(const int* array, int num_ints);
505
517 void writeLong(LONG_INT l);
518 void writeLongNC(LONG_INT l);
519 void writeLong(LONG_INT l, int pos);
520 void writeLongArray(const LONG_INT* array, int num_longs);
521 void writeType(LONG_INT l);
522 void writeTypeArray(const LONG_INT* array, int num_longs);
523
535 void writeFloat(float f);
536 void writeFloatNC(float f);
537 void writeFloat(float f, int pos);
538 void writeFloatArray(const float* array, int num_floats);
539 void writeType(float f);
540 void writeTypeArray(const float* array, int num_floats);
541
553 void writeDouble(double d);
554 void writeDoubleNC(double d);
555 void writeDouble(double d, int pos);
556 void writeDoubleArray(const double* array, int num_doubles);
557 void writeType(double d);
558 void writeTypeArray(const double* array, int num_doubles);
559
560 /*
561 * Return class name.
562 */
563 static string className()
564 { return "IFStreamBinary"; }
565
566 /*
567 * Return class size.
568 */
569 static int classSize()
570 { return (int) sizeof(IFStreamBinary); }
571
576 void reSize(int spc)
577 { bData->resize(spc); }
578
582 void reserve(int sze)
583 { bData->reserve(sze); }
584
588 int size()
589 {
590 if (bDataPos > bSize) bSize = bDataPos;
591 return bSize;
592 }
593
597 void resetPos()
598 {
599 if (bDataPos > bSize) bSize = bDataPos;
600 bDataPos = 0;
601 }
602
606 void setPosToEnd()
607 {
608 if (bDataPos > bSize) bSize = bDataPos;
609 bDataPos = bSize;
610 }
611
615 int getPos() const
616 { return bDataPos; }
617
621 void incrementPos(int increment = 1)
622 { bDataPos += increment; }
623
627 void decrementPos(int decrement = 1)
628 { bDataPos -= decrement; }
629
633 const string& getData() const
634 { return *bData; }
635
639 int getCapacity() const
640 { return (int) bData->capacity(); }
641
646 char* getPosPointer()
647 { return getPosPointer(bDataPos); }
648
652 char* getPosPointer(int pos)
653 {
654 char* p = &(*bData)[pos];
655 return p;
656 }
657
661 void setByteOrderReverse(bool bor)
662 { bReverse = bor; }
663
667 void byteOrderReverseOn()
668 { bReverse = true; }
669
673 void byteOrderReverseOff()
674 { bReverse = false; }
675
680 bool isByteOrderReversalOn() const
681 { return bReverse; }
682
686 void setBoundaryAlignment(bool align)
687 { bAlign = align; }
688
692 void boundaryAlignmentOn()
693 { bAlign = true; }
694
698 void boundaryAlignmentOff()
699 { bAlign = false; }
700
705 bool isBoundaryAlignmentOn() const
706 { return bAlign; }
707
708 }; // end class IFStreamBinary
709
710 //************* Read Strings **************************************************
711
715 inline string IFStreamBinary::readString()
716 {
717 string s;
718 readString(s);
719 return s;
720 }
721
725 inline void IFStreamBinary::readString(string& s)
726 {
727 int len;
728 // check for zero added by sballar 2013-01-04
729 len = readInt();
730 if (len == 0)
731 s="";
732 else
733 {
734 // read the data
735 s = bData->substr(bDataPos, len);
736 bDataPos += len;
737 }
738 }
739
743 inline void IFStreamBinary::readCharArray(string& s, int num_chars)
744 {
745 s.resize(num_chars);
746 readCharArray(&s[0], num_chars);
747 }
748
752 inline void IFStreamBinary::readCharArray(char* array, int num_chars)
753 {
754 // read the data
755
756 memcpy(array, &(*bData)[bDataPos], num_chars);
757 bDataPos += num_chars;
758 }
759
763 inline void IFStreamBinary::readType(string& s)
764 {
765 readString(s);
766 }
767
771 inline void IFStreamBinary::readTypeArray(string& s, int num_chars)
772 {
773 readCharArray(s, num_chars);
774 }
775
776 //************* Read Bools ****************************************************
777
781 inline bool IFStreamBinary::readBool()
782 {
783 // read the data
784
785 int pos = bDataPos;
786 bDataPos += CPPUtils::SBOL;
787 return readBool(pos);
788 }
789
794 inline bool IFStreamBinary::readBool(int pos)
795 {
796 return *((bool*) &(*bData)[pos]);
797 }
798
802 inline void IFStreamBinary::readBoolArray(bool* array, int num_bools)
803 {
804 // get bytes and perform alignment
805
806 int num_bytes = num_bools * CPPUtils::SBOL;
807
808 // copy from buffer into array ...
809 // update buffer position and exit
810
811 memcpy(array, &(*bData)[bDataPos], num_bytes);
812 bDataPos += num_bytes;
813 }
814
818 inline void IFStreamBinary::readType(bool& b)
819 {
820 b = readBool();
821 }
822
826 inline void IFStreamBinary::readTypeArray(bool* array, int num_bools)
827 {
828 readBoolArray(array, num_bools);
829 }
830
831 //************* Read Bytes ****************************************************
832
836 inline byte IFStreamBinary::readByte()
837 {
838 // read the data
839
840 int pos = bDataPos;
841 bDataPos += CPPUtils::SBYT;
842 return readByte(pos);
843 }
844
849 inline byte IFStreamBinary::readByte(int pos)
850 {
851 return *((byte*) &(*bData)[pos]);
852 }
853
857 inline void IFStreamBinary::readByteArray(byte* array, int num_bytes)
858 {
859 // get bytes and perform alignment
860
861 num_bytes *= CPPUtils::SBYT;
862
863 // copy from buffer into array ...
864 // update buffer position and exit
865
866 memcpy(array, &(*bData)[bDataPos], num_bytes);
867 bDataPos += num_bytes;
868 }
869
873 inline void IFStreamBinary::readType(byte& b)
874 {
875 b = readByte();
876 }
877
881 inline void IFStreamBinary::readTypeArray(byte* array, int num_bytes)
882 {
883 readByteArray(array, num_bytes);
884 }
885
886 //************* Read Shorts ***************************************************
887
891 inline short IFStreamBinary::readShort()
892 {
893 align2Byte();
894 return readShortNC();
895 }
896
900 inline short IFStreamBinary::readShortNC()
901 {
902 // read the data
903
904 int pos = bDataPos;
905 bDataPos += CPPUtils::SSHT;
906 return readShort(pos);
907 }
908
913 inline short IFStreamBinary::readShort(int pos)
914 {
915 // read the data ... use memcpy if alignment is off
916
917 short s;
918 if (bAlign)
919 s = *((short*) &(*bData)[pos]);
920 else
921 memcpy(&s, &(*bData)[pos], 2);
922
923 if (bReverse) reverseBO2((char*) &s);
924 return s;
925 }
926
930 inline void IFStreamBinary::readShortArray(short* array, int num_shorts)
931 {
932 // get bytes and perform alignment
933
934 int num_bytes = num_shorts * CPPUtils::SSHT;
935 align2Byte();
936
937 // copy from buffer into array ... reverse data if necessary ...
938 // update buffer position and exit
939
940 memcpy(array, &(*bData)[bDataPos], num_bytes);
941 if (bReverse) reverseBO2Array(num_shorts, (char*) array);
942 bDataPos += num_bytes;
943 }
944
948 inline void IFStreamBinary::readType(short& s)
949 {
950 s = readShort();
951 }
952
956 inline void IFStreamBinary::readTypeArray(short* array, int num_shorts)
957 {
958 readShortArray(array, num_shorts);
959 }
960
961 //************* Read Ints *****************************************************
962
966 inline int IFStreamBinary::readInt()
967 {
968 align4Byte();
969 return readIntNC();
970 }
971
975 inline int IFStreamBinary::readIntNC()
976 {
977 // read the data
978
979 int pos = bDataPos;
980 bDataPos += CPPUtils::SINT;
981 return readInt(pos);
982 }
983
988 inline int IFStreamBinary::readInt(int pos)
989 {
990 // read the data ... use memcpy if alignment is off
991
992 int i;
993 if (bAlign)
994 i = *((int*) &(*bData)[pos]);
995 else
996 memcpy(&i, &(*bData)[pos], 4);
997
998 if (bReverse) reverseBO4((char*) &i);
999 return i;
1000 }
1001
1005 inline void IFStreamBinary::readIntArray(int* array, int num_ints)
1006 {
1007 // get bytes and perform alignment
1008
1009 int num_bytes = num_ints * CPPUtils::SINT;
1010 align4Byte();
1011
1012 // copy from buffer into array ... reverse data if necessary ...
1013 // update buffer position and exit
1014
1015 memcpy(array, &(*bData)[bDataPos], num_bytes);
1016 if (bReverse) reverseBO4Array(num_ints, (char*) array);
1017 bDataPos += num_bytes;
1018 }
1019
1023 inline void IFStreamBinary::readType(int& i)
1024 {
1025 i = readInt();
1026 }
1027
1031 inline void IFStreamBinary::readTypeArray(int* array, int num_ints)
1032 {
1033 readIntArray(array, num_ints);
1034 }
1035
1036 //************* Read Longs ****************************************************
1037
1041 inline LONG_INT IFStreamBinary::readLong()
1042 {
1043 align8Byte();
1044 return readLongNC();
1045 }
1046
1050 inline LONG_INT IFStreamBinary::readLongNC()
1051 {
1052 // read the data
1053
1054 int pos = bDataPos;
1055 bDataPos += CPPUtils::SLNG;
1056 return readLong(pos);
1057 }
1058
1063 inline LONG_INT IFStreamBinary::readLong(int pos)
1064 {
1065 // read the data ... use memcpy if alignment is off
1066
1067 LONG_INT l;
1068 if (bAlign)
1069 l = *((LONG_INT*) &(*bData)[pos]);
1070 else
1071 memcpy(&l, &(*bData)[pos], 8);
1072
1073 if (bReverse) reverseBO8((char*) &l);
1074 return l;
1075 }
1076
1080 inline void IFStreamBinary::readLongArray(LONG_INT* array, int num_longs)
1081 {
1082 // get bytes and perform alignment
1083
1084 int num_bytes = num_longs * CPPUtils::SLNG;
1085 align8Byte();
1086
1087 // copy from buffer into array ... reverse data if necessary ...
1088 // update buffer position and exit
1089
1090 memcpy(array, &(*bData)[bDataPos], num_bytes);
1091 if (bReverse) reverseBO8Array(num_longs, (char*) array);
1092 bDataPos += num_bytes;
1093 }
1094
1098 inline void IFStreamBinary::readType(LONG_INT& l)
1099 {
1100 l = readLong();
1101 }
1102
1106 inline void IFStreamBinary::readTypeArray(LONG_INT* array, int num_longs)
1107 {
1108 readLongArray(array, num_longs);
1109 }
1110
1111 //************* Read Floats ***************************************************
1112
1116 inline float IFStreamBinary::readFloat()
1117 {
1118 align4Byte();
1119 return readFloatNC();
1120 }
1121
1125 inline float IFStreamBinary::readFloatNC()
1126 {
1127 // read the data
1128
1129 int pos = bDataPos;
1130 bDataPos += CPPUtils::SFLT;
1131 return readFloat(pos);
1132 }
1133
1138 inline float IFStreamBinary::readFloat(int pos)
1139 {
1140 // read the data ... use memcpy if alignment is off
1141
1142 float f;
1143 if (bAlign)
1144 f = *((float*) &(*bData)[pos]);
1145 else
1146 memcpy(&f, &(*bData)[pos], 4);
1147
1148 if (bReverse) reverseBO4((char*) &f);
1149 return f;
1150 }
1151
1155 inline void IFStreamBinary::readFloatArray(float* array, int num_floats)
1156 {
1157 // get bytes and perform alignment
1158
1159 int num_bytes = num_floats * CPPUtils::SFLT;
1160 align4Byte();
1161
1162 // copy from buffer into array ... reverse data if necessary ...
1163 // update buffer position and exit
1164
1165 memcpy(array, &(*bData)[bDataPos], num_bytes);
1166 if (bReverse) reverseBO4Array(num_floats, (char*) array);
1167 bDataPos += num_bytes;
1168 }
1169
1173 inline void IFStreamBinary::readType(float& f)
1174 {
1175 f = readFloat();
1176 }
1177
1181 inline void IFStreamBinary::readTypeArray(float* array, int num_floats)
1182 {
1183 readFloatArray(array, num_floats);
1184 }
1185
1186 //************* Read Doubles **************************************************
1187
1191 inline double IFStreamBinary::readDouble()
1192 {
1193 align8Byte();
1194 return readDoubleNC();
1195 }
1196
1200 inline double IFStreamBinary::readDoubleNC()
1201 {
1202 // read the data
1203
1204 int pos = bDataPos;
1205 bDataPos += CPPUtils::SDBL;
1206 return readDouble(pos);
1207 }
1208
1213 inline double IFStreamBinary::readDouble(int pos)
1214 {
1215 // read the data ... use memcpy if alignment is off
1216
1217 double d;
1218 if (bAlign)
1219 d = *((double*) &(*bData)[pos]);
1220 else
1221 memcpy(&d, &(*bData)[pos], 8);
1222
1223 if (bReverse) reverseBO8((char*) &d);
1224 return d;
1225 }
1226
1230 inline void IFStreamBinary::readDoubleArray(double* array, int num_doubles)
1231 {
1232 // get bytes and perform alignment
1233
1234 int num_bytes = num_doubles * CPPUtils::SDBL;
1235 align8Byte();
1236
1237 // copy from buffer into array ... reverse data if necessary ...
1238 // update buffer position and exit
1239
1240 memcpy(array, &(*bData)[bDataPos], num_bytes);
1241 if (bReverse) reverseBO8Array(num_doubles, (char*) array);
1242 bDataPos += num_bytes;
1243 }
1244
1248 inline void IFStreamBinary::readType(double& d)
1249 {
1250 d = readDouble();
1251 }
1252
1256 inline void IFStreamBinary::readTypeArray(double* array, int num_doubles)
1257 {
1258 readDoubleArray(array, num_doubles);
1259 }
1260
1261 //************* Write Strings *************************************************
1262
1269 inline void IFStreamBinary::writeString(const string& str)
1270 {
1271 // get total size, align, and check capacity
1272
1273 int sz = (int) str.size() + CPPUtils::SINT;
1274 align4Byte();
1275 checkBufferSize(sz);
1276
1277 // write the data
1278
1279 writeIntNC((int) str.size());
1280 memcpy(&(*bData)[bDataPos], &str[0], str.size());
1281 bDataPos += (int) str.size();
1282 }
1283
1289 inline void IFStreamBinary::writeString(const char* char_string)
1290 {
1291 string tmp_string = char_string;
1292 writeString(tmp_string);
1293 }
1294
1298 inline void IFStreamBinary::writeCharArray(const char* array, int num_chars)
1299 {
1300 checkBufferSize(num_chars);
1301
1302 // write data
1303
1304 memcpy(&(*bData)[bDataPos], array, num_chars);
1305 bDataPos += num_chars;
1306 }
1307
1314 inline void IFStreamBinary::writeType(const string& str)
1315 {
1316 writeString(str);
1317 }
1318
1324 inline void IFStreamBinary::writeType(const char* char_string)
1325 {
1326 writeString(char_string);
1327 }
1328
1329 //************* Write Bools ***************************************************
1330
1336 inline void IFStreamBinary::writeBool(bool b)
1337 {
1338 checkBufferSize(CPPUtils::SBOL);
1339
1340 writeBoolNC(b);
1341 }
1342
1348 inline void IFStreamBinary::writeBoolNC(bool b)
1349 {
1350 writeBool(b, bDataPos);
1351 bDataPos += CPPUtils::SBOL;
1352 }
1353
1359 inline void IFStreamBinary::writeBool(bool b, int pos)
1360 {
1361 *((bool*) &(*bData)[pos]) = b;
1362 }
1363
1367 inline void IFStreamBinary::writeBoolArray(const bool* array, int num_bools)
1368 {
1369 // get number of bytes to be written and check for alignment and buffer
1370 // size
1371
1372 num_bools *= CPPUtils::SBOL;
1373 checkBufferSize(num_bools);
1374
1375 // copy array into data buffer. Reverse data buffer entries if necessary
1376 // and increment the buffer output pointer.
1377
1378 memcpy(&(*bData)[bDataPos], array, num_bools);
1379 bDataPos += num_bools;
1380 }
1381
1387 inline void IFStreamBinary::writeType(bool b)
1388 {
1389 writeBool(b);
1390 }
1391
1395 inline void IFStreamBinary::writeTypeArray(const bool* array, int num_bools)
1396 {
1397 writeBoolArray(array, num_bools);
1398 }
1399
1400 //************* Write Bytes ***************************************************
1401
1407 inline void IFStreamBinary::writeByte(byte b)
1408 {
1409 checkBufferSize(CPPUtils::SBYT);
1410
1411 writeByteNC(b);
1412 }
1413
1419 inline void IFStreamBinary::writeByteNC(byte b)
1420 {
1421 writeByte(b, bDataPos);
1422 bDataPos += CPPUtils::SBYT;
1423 }
1424
1430 inline void IFStreamBinary::writeByte(byte b, int pos)
1431 {
1432 *((byte*) &(*bData)[pos]) = b;
1433 }
1434
1438 inline void IFStreamBinary::writeByteArray(const byte* array, int num_bytes)
1439 {
1440 // get number of bytes to be written and check for alignment and buffer
1441 // size
1442
1443 num_bytes *= CPPUtils::SBYT;
1444 checkBufferSize(num_bytes);
1445
1446 // copy array into data buffer. Reverse data buffer entries if necessary
1447 // and increment the buffer output pointer.
1448
1449 memcpy(&(*bData)[bDataPos], array, num_bytes);
1450 bDataPos += num_bytes;
1451 }
1452
1458 inline void IFStreamBinary::writeType(byte b)
1459 {
1460 writeByte(b);
1461 }
1462
1466 inline void IFStreamBinary::writeTypeArray(const byte* array, int num_bytes)
1467 {
1468 writeByteArray(array, num_bytes);
1469 }
1470
1471 //************* Write Shorts **************************************************
1472
1478 inline void IFStreamBinary::writeShort(short s)
1479 {
1480 align2Byte();
1481 checkBufferSize(CPPUtils::SSHT);
1482
1483 writeShortNC(s);
1484 }
1485
1491 inline void IFStreamBinary::writeShortNC(short s)
1492 {
1493 writeShort(s, bDataPos);
1494 bDataPos += CPPUtils::SSHT;
1495 }
1496
1502 inline void IFStreamBinary::writeShort(short s, int pos)
1503 {
1504 if (bReverse) reverseBO2((char*) &s);
1505
1506 if (bAlign)
1507 *((short*) &(*bData)[pos]) = s;
1508 else
1509 memcpy(&(*bData)[pos], &s, CPPUtils::SSHT);
1510 }
1511
1515 inline void IFStreamBinary::writeShortArray(const short* array, int num_shorts)
1516 {
1517 // get number of bytes to be written and check for alignment and buffer
1518 // size
1519
1520 int num_bytes = num_shorts * CPPUtils::SSHT;
1521 align2Byte();
1522 checkBufferSize(num_bytes);
1523
1524 // copy array into data buffer. Reverse data buffer entries if necessary
1525 // and increment the buffer output pointer.
1526
1527 memcpy(&(*bData)[bDataPos], array, num_bytes);
1528 if (bReverse) reverseBO2Array(num_shorts, &(*bData)[bDataPos]);
1529 bDataPos += num_bytes;
1530 }
1531
1537 inline void IFStreamBinary::writeType(short s)
1538 {
1539 writeShort(s);
1540 }
1541
1545 inline void IFStreamBinary::writeTypeArray(const short* array, int num_shorts)
1546 {
1547 writeShortArray(array, num_shorts);
1548 }
1549
1550 //************* Write Ints ****************************************************
1551
1557 inline void IFStreamBinary::writeInt(int i)
1558 {
1559 align4Byte();
1560 checkBufferSize(CPPUtils::SINT);
1561
1562 writeIntNC(i);
1563 }
1564
1570 inline void IFStreamBinary::writeIntNC(int i)
1571 {
1572 writeInt(i, bDataPos);
1573 bDataPos += CPPUtils::SINT;
1574 }
1575
1581 inline void IFStreamBinary::writeInt(int i, int pos)
1582 {
1583 if (bReverse) reverseBO4((char*) &i);
1584
1585 if (bAlign)
1586 *((int*) &(*bData)[pos]) = i;
1587 else
1588 memcpy(&(*bData)[pos], &i, CPPUtils::SINT);
1589 }
1590
1594 inline void IFStreamBinary::writeIntArray(const int* array, int num_ints)
1595 {
1596 // get number of bytes to be written and check for alignment and buffer
1597 // size
1598
1599 int num_bytes = num_ints * CPPUtils::SINT;
1600 align4Byte();
1601 checkBufferSize(num_bytes);
1602
1603 // copy array into data buffer. Reverse data buffer entries if necessary
1604 // and increment the buffer output pointer.
1605
1606 memcpy(&(*bData)[bDataPos], array, num_bytes);
1607 if (bReverse) reverseBO4Array(num_ints, &(*bData)[bDataPos]);
1608 bDataPos += num_bytes;
1609 }
1610
1616 inline void IFStreamBinary::writeType(int i)
1617 {
1618 writeInt(i);
1619 }
1620
1624 inline void IFStreamBinary::writeTypeArray(const int* array, int num_ints)
1625 {
1626 writeIntArray(array, num_ints);
1627 }
1628
1629 //************* Write Longs ***************************************************
1630
1636 inline void IFStreamBinary::writeLong(LONG_INT l)
1637 {
1638 align8Byte();
1639 checkBufferSize(CPPUtils::SLNG);
1640
1641 writeLongNC(l);
1642 }
1643
1649 inline void IFStreamBinary::writeLongNC(LONG_INT l)
1650 {
1651 writeLong(l, bDataPos);
1652 bDataPos += CPPUtils::SLNG;
1653 }
1654
1660 inline void IFStreamBinary::writeLong(LONG_INT l, int pos)
1661 {
1662 if (bReverse) reverseBO8((char*) &l);
1663
1664 if (bAlign)
1665 *((LONG_INT*) &(*bData)[pos]) = l;
1666 else
1667 memcpy(&(*bData)[pos], &l, CPPUtils::SLNG);
1668 }
1669
1673 inline void IFStreamBinary::writeLongArray(const LONG_INT* array, int num_longs)
1674 {
1675 // get number of bytes to be written and check for alignment and buffer
1676 // size
1677
1678 int num_bytes = num_longs * CPPUtils::SLNG;
1679 align8Byte();
1680 checkBufferSize(num_bytes);
1681
1682 // copy array into data buffer. Reverse data buffer entries if necessary
1683 // and increment the buffer output pointer.
1684
1685 memcpy(&(*bData)[bDataPos], array, num_bytes);
1686 if (bReverse) reverseBO8Array(num_longs, &(*bData)[bDataPos]);
1687 bDataPos += num_bytes;
1688 }
1689
1695 inline void IFStreamBinary::writeType(LONG_INT l)
1696 {
1697 writeLong(l);
1698 }
1699
1703 inline void IFStreamBinary::writeTypeArray(const LONG_INT* array, int num_longs)
1704 {
1705 writeLongArray(array, num_longs);
1706 }
1707
1708 //************* Write Floats **************************************************
1709
1715 inline void IFStreamBinary::writeFloat(float f)
1716 {
1717 align4Byte();
1718 checkBufferSize(CPPUtils::SFLT);
1719
1720 writeFloatNC(f);
1721 }
1722
1728 inline void IFStreamBinary::writeFloatNC(float f)
1729 {
1730 writeFloat(f, bDataPos);
1731 bDataPos += CPPUtils::SFLT;
1732 }
1733
1739 inline void IFStreamBinary::writeFloat(float f, int pos)
1740 {
1741 if (bReverse) reverseBO4((char*) &f);
1742
1743 if (bAlign)
1744 *((float*) &(*bData)[pos]) = f;
1745 else
1746 memcpy(&(*bData)[pos], &f, CPPUtils::SFLT);
1747 }
1748
1752 inline void IFStreamBinary::writeFloatArray(const float* array, int num_floats)
1753 {
1754 // get number of bytes to be written and check for alignment and buffer
1755 // size
1756
1757 int num_bytes = num_floats * CPPUtils::SFLT;
1758 align4Byte();
1759 checkBufferSize(num_bytes);
1760
1761 // copy array into data buffer. Reverse data buffer entries if necessary
1762 // and increment the buffer output pointer.
1763
1764 memcpy(&(*bData)[bDataPos], array, num_bytes);
1765 if (bReverse) reverseBO4Array(num_floats, &(*bData)[bDataPos]);
1766 bDataPos += num_bytes;
1767 }
1768
1774 inline void IFStreamBinary::writeType(float f)
1775 {
1776 writeFloat(f);
1777 }
1778
1782 inline void IFStreamBinary::writeTypeArray(const float* array, int num_floats)
1783 {
1784 writeFloatArray(array, num_floats);
1785 }
1786
1787 //************* Write Doubles *************************************************
1788
1794 inline void IFStreamBinary::writeDouble(double d)
1795 {
1796 align8Byte();
1797 checkBufferSize(CPPUtils::SDBL);
1798
1799 writeDoubleNC(d);
1800 }
1801
1807 inline void IFStreamBinary::writeDoubleNC(double d)
1808 {
1809 writeDouble(d, bDataPos);
1810 bDataPos += CPPUtils::SDBL;
1811 }
1812
1818 inline void IFStreamBinary::writeDouble(double d, int pos)
1819 {
1820 if (bReverse) reverseBO8((char*) &d);
1821
1822 if (bAlign)
1823 *((double*) &(*bData)[pos]) = d;
1824 else
1825 memcpy(&(*bData)[pos], &d, CPPUtils::SDBL);
1826 }
1827
1831 inline void IFStreamBinary::writeDoubleArray(const double* array, int num_doubles)
1832 {
1833 // get number of bytes to be written and check for alignment and buffer
1834 // size
1835
1836 int num_bytes = num_doubles * CPPUtils::SDBL;
1837 align8Byte();
1838 checkBufferSize(num_bytes);
1839
1840 // copy array into data buffer. Reverse data buffer entries if necessary
1841 // and increment the buffer output pointer.
1842
1843 memcpy(&(*bData)[bDataPos], array, num_bytes);
1844 if (bReverse) reverseBO8Array(num_doubles, &(*bData)[bDataPos]);
1845 bDataPos += num_bytes;
1846 }
1847
1853 inline void IFStreamBinary::writeType(double d)
1854 {
1855 writeDouble(d);
1856 }
1857
1861 inline void IFStreamBinary::writeTypeArray(const double* array, int num_doubles)
1862 {
1863 writeDoubleArray(array, num_doubles);
1864 }
1865
1866 //************* Byte reversal functions ***************************************
1867
1871 inline void IFStreamBinary::reverseBO2(char* d)
1872 {
1873 char tmp = d[0];
1874 d[0] = d[1];
1875 d[1] = tmp;
1876 }
1877
1881 inline void IFStreamBinary::reverseBO4(char* d)
1882 {
1883 char tmp = d[0];
1884 d[0] = d[3];
1885 d[3] = tmp;
1886
1887 tmp = d[1];
1888 d[1] = d[2];
1889 d[2] = tmp;
1890 }
1891
1895 inline void IFStreamBinary::reverseBO8(char* d)
1896 {
1897 char tmp = d[0];
1898 d[0] = d[7];
1899 d[7] = tmp;
1900
1901 tmp = d[1];
1902 d[1] = d[6];
1903 d[6] = tmp;
1904
1905 tmp = d[2];
1906 d[2] = d[5];
1907 d[5] = tmp;
1908
1909 tmp = d[3];
1910 d[3] = d[4];
1911 d[4] = tmp;
1912 }
1913
1914 } // end namespace geotess
1915
1916 #endif // IFSTREAMBINARY_OBJECT_H