GeoTessCPP
2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
|
00001 //- **************************************************************************** 00002 //- 00003 //- Copyright 2009 Sandia Corporation. Under the terms of Contract 00004 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00005 //- retains certain rights in this software. 00006 //- 00007 //- BSD Open Source License. 00008 //- All rights reserved. 00009 //- 00010 //- Redistribution and use in source and binary forms, with or without 00011 //- modification, are permitted provided that the following conditions are met: 00012 //- 00013 //- * Redistributions of source code must retain the above copyright notice, 00014 //- this list of conditions and the following disclaimer. 00015 //- * Redistributions in binary form must reproduce the above copyright 00016 //- notice, this list of conditions and the following disclaimer in the 00017 //- documentation and/or other materials provided with the distribution. 00018 //- * Neither the name of Sandia National Laboratories nor the names of its 00019 //- contributors may be used to endorse or promote products derived from 00020 //- this software without specific prior written permission. 00021 //- 00022 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00023 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00025 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00026 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00027 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00028 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00029 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00030 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00031 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 //- POSSIBILITY OF SUCH DAMAGE. 00033 //- 00034 //- **************************************************************************** 00035 00036 #ifndef IFSTREAMBINARY_OBJECT_H 00037 #define IFSTREAMBINARY_OBJECT_H 00038 00039 // **** _SYSTEM INCLUDES_ ****************************************************** 00040 00041 #include <iostream> 00042 #include <string> 00043 #include <cstring> 00044 #include <fstream> 00045 #include <vector> 00046 #include <sstream> 00047 00048 // use standard library objects 00049 using namespace std; 00050 00051 // **** _LOCAL INCLUDES_ ******************************************************* 00052 00053 #include "CPPUtils.h" 00054 #include "GeoTessUtils.h" 00055 #include "GeoTessException.h" 00056 00057 // **** _BEGIN GEOTESS NAMESPACE_ ********************************************** 00058 00059 namespace geotess { 00060 00061 // **** _FORWARD REFERENCES_ *************************************************** 00062 00063 // **** _CLASS DEFINITION_ ***************************************************** 00064 00079 class GEOTESS_EXP_IMP IFStreamBinary 00080 { 00081 protected: 00082 00086 string* bData; 00087 00091 int bDataPos; 00092 00096 int bSize; 00097 00102 bool bAlign; 00103 00111 bool bReverse; 00112 00118 bool bOwnStr; 00119 00120 string bFileName; 00121 00122 int bMemIncr; 00123 00128 void align2Byte() 00129 { 00130 int sm = bDataPos % CPPUtils::SSHT; 00131 if (sm && bAlign) bDataPos += CPPUtils::SSHT - sm; 00132 } 00133 00138 void align4Byte() 00139 { 00140 int sm = bDataPos % CPPUtils::SINT; 00141 if (sm && bAlign) bDataPos += CPPUtils::SINT - sm; 00142 } 00143 00148 void align8Byte() 00149 { 00150 int sm = bDataPos % CPPUtils::SDBL; 00151 if (sm && bAlign) bDataPos += CPPUtils::SDBL - sm; 00152 } 00153 00158 void checkBufferSize(int sincr) 00159 { 00160 if (bDataPos + sincr > (int) bData->size()) 00161 { 00162 if (sincr + bData->size() > bData->capacity()) 00163 bData->reserve(bData->capacity() + bMemIncr); 00164 bData->append(bDataPos + sincr - bData->size(), ' '); 00165 } 00166 } 00167 00168 /* 00169 * Reverses each s-byte element of array a containing n elements 00170 * (s*n bytes). The element size s must be 2, 4, or 8. 00171 */ 00172 static void reverseBOArray(int n, char* a, int s); 00173 00174 /* 00175 * Reverses the byte order of each element of a where a is an array of n 00176 * 2, 4, or 8 byte elements. Used to convert between big-and 00177 * little-endian formats. 00178 */ 00179 static void reverseBO2Array(int n, char* a); 00180 static void reverseBO4Array(int n, char* a); 00181 static void reverseBO8Array(int n, char* a); 00182 00183 /* 00184 * Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used 00185 * to convert between big-and little-endian formats. 00186 */ 00187 static void reverseBO2(char* d); 00188 static void reverseBO4(char* d); 00189 static void reverseBO8(char* d); 00190 00191 public: 00192 00196 IFStreamBinary(); 00197 00201 IFStreamBinary(bool align); 00202 00207 IFStreamBinary(const string& filename); 00208 00213 IFStreamBinary(const string& filename, int num_bytes); 00214 00219 IFStreamBinary(string* str); 00220 00224 IFStreamBinary(const IFStreamBinary& db); 00225 00229 virtual ~IFStreamBinary(); 00230 00234 IFStreamBinary& operator=(const IFStreamBinary& db); 00235 00239 void dumpBuffer(); 00240 00244 static bool exists(const string& filename); 00245 00249 void writeToFile(const string& filename); 00250 00254 void writeToFile(ofstream& ofs); 00255 00259 void readFromFile(const string& filename); 00260 00264 void readFromFile(const string& filename, int num_bytes); 00265 00270 // void readFromFile(ifstream& ifs, streampos file_pos, 00271 // int num_bytes); 00272 00276 void readFromFile(ifstream& ifs, int num_bytes); 00277 00281 const string& getFileName() const { return bFileName; } 00282 00286 void clear() 00287 { 00288 (*bData) = ""; 00289 bDataPos = 0; 00290 bSize = 0; 00291 } 00292 00297 void setMemoryCapacityIncrement(int mci) 00298 { 00299 if (mci > 0) bMemIncr = mci; 00300 } 00301 00310 string readString(); 00311 void readString(string& s); 00312 void readCharArray(string& s, int num_chars); 00313 void readCharArray(char* array, int num_chars); 00314 void readType(string& s); 00315 void readTypeArray(string& array, int num_chars); 00316 00324 bool readBool(); 00325 bool readBool(int pos); 00326 void readBoolArray(bool* array, int num_bools); 00327 void readType(bool& b); 00328 void readTypeArray(bool* array, int num_bools); 00329 00337 byte readByte(); 00338 byte readByte(int pos); 00339 void readByteArray(byte* array, int num_bs); 00340 void readType(byte& b); 00341 void readTypeArray(byte* array, int num_bs); 00342 00352 short readShort(); 00353 short readShortNC(); 00354 short readShort(int pos); 00355 void readShortArray(short* array, int num_shorts); 00356 void readType(short& s); 00357 void readTypeArray(short* array, int num_shorts); 00358 00368 int readInt(); 00369 int readIntNC(); 00370 int readInt(int pos); 00371 void readIntArray(int* array, int num_ints); 00372 void readType(int& i); 00373 void readTypeArray(int* array, int num_ints); 00374 00384 LONG_INT readLong(); 00385 LONG_INT readLongNC(); 00386 LONG_INT readLong(int pos); 00387 void readLongArray(LONG_INT* array, int num_longs); 00388 void readType(LONG_INT& l); 00389 void readTypeArray(LONG_INT* array, int num_longs); 00390 00400 float readFloat(); 00401 float readFloatNC(); 00402 float readFloat(int pos); 00403 void readFloatArray(float* array, int num_floats); 00404 void readType(float& f); 00405 void readTypeArray(float* array, int num_floats); 00406 00416 double readDouble(); 00417 double readDoubleNC(); 00418 double readDouble(int pos); 00419 void readDoubleArray(double* array, int num_doubles); 00420 void readType(double& d); 00421 void readTypeArray(double* array, int num_doubles); 00422 00431 void writeString(const string& str); 00432 void writeString(const char* char_string); 00433 void writeCharArray(const char* array, int num_chars); 00434 void writeType(const string& str); 00435 void writeType(const char* char_string); 00436 00447 void writeBool(bool b); 00448 void writeBoolNC(bool b); 00449 void writeBool(bool b, int pos); 00450 void writeBoolArray(const bool* array, int num_bools); 00451 void writeType(bool b); 00452 void writeTypeArray(const bool* array, int num_bools); 00453 00464 void writeByte(byte b); 00465 void writeByteNC(byte b); 00466 void writeByte(byte b, int pos); 00467 void writeByteArray(const byte* array, int num_bytes); 00468 void writeType(byte b); 00469 void writeTypeArray(const byte* array, int num_bytes); 00470 00482 void writeShort(short i); 00483 void writeShortNC(short i); 00484 void writeShort(short i, int pos); 00485 void writeShortArray(const short* array, int num_ints); 00486 void writeType(short i); 00487 void writeTypeArray(const short* array, int num_ints); 00488 00500 void writeInt(int i); 00501 void writeIntNC(int i); 00502 void writeInt(int i, int pos); 00503 void writeIntArray(const int* array, int num_ints); 00504 void writeType(int i); 00505 void writeTypeArray(const int* array, int num_ints); 00506 00518 void writeLong(LONG_INT l); 00519 void writeLongNC(LONG_INT l); 00520 void writeLong(LONG_INT l, int pos); 00521 void writeLongArray(const LONG_INT* array, int num_longs); 00522 void writeType(LONG_INT l); 00523 void writeTypeArray(const LONG_INT* array, int num_longs); 00524 00536 void writeFloat(float f); 00537 void writeFloatNC(float f); 00538 void writeFloat(float f, int pos); 00539 void writeFloatArray(const float* array, int num_floats); 00540 void writeType(float f); 00541 void writeTypeArray(const float* array, int num_floats); 00542 00554 void writeDouble(double d); 00555 void writeDoubleNC(double d); 00556 void writeDouble(double d, int pos); 00557 void writeDoubleArray(const double* array, int num_doubles); 00558 void writeType(double d); 00559 void writeTypeArray(const double* array, int num_doubles); 00560 00561 /* 00562 * Return class name. 00563 */ 00564 static string className() 00565 { return "IFStreamBinary"; } 00566 00567 /* 00568 * Return class size. 00569 */ 00570 static int classSize() 00571 { return (int) sizeof(IFStreamBinary); } 00572 00577 void reSize(int spc) 00578 { bData->resize(spc); } 00579 00583 void reserve(int sze) 00584 { bData->reserve(sze); } 00585 00589 int size() 00590 { 00591 if (bDataPos > bSize) bSize = bDataPos; 00592 return bSize; 00593 } 00594 00598 void resetPos() 00599 { 00600 if (bDataPos > bSize) bSize = bDataPos; 00601 bDataPos = 0; 00602 } 00603 00607 void setPosToEnd() 00608 { 00609 if (bDataPos > bSize) bSize = bDataPos; 00610 bDataPos = bSize; 00611 } 00612 00616 int getPos() const 00617 { return bDataPos; } 00618 00622 void incrementPos(int increment = 1) 00623 { bDataPos += increment; } 00624 00628 void decrementPos(int decrement = 1) 00629 { bDataPos -= decrement; } 00630 00634 const string& getData() const 00635 { return *bData; } 00636 00640 int getCapacity() const 00641 { return (int) bData->capacity(); } 00642 00647 char* getPosPointer() 00648 { return getPosPointer(bDataPos); } 00649 00653 char* getPosPointer(int pos) 00654 { 00655 char* p = &(*bData)[pos]; 00656 return p; 00657 } 00658 00662 void setByteOrderReverse(bool bor) 00663 { bReverse = bor; } 00664 00668 void byteOrderReverseOn() 00669 { bReverse = true; } 00670 00674 void byteOrderReverseOff() 00675 { bReverse = false; } 00676 00681 bool isByteOrderReversalOn() const 00682 { return bReverse; } 00683 00687 void setBoundaryAlignment(bool align) 00688 { bAlign = align; } 00689 00693 void boundaryAlignmentOn() 00694 { bAlign = true; } 00695 00699 void boundaryAlignmentOff() 00700 { bAlign = false; } 00701 00706 bool isBoundaryAlignmentOn() const 00707 { return bAlign; } 00708 00709 }; // end class IFStreamBinary 00710 00711 //************* Read Strings ************************************************** 00712 00716 inline string IFStreamBinary::readString() 00717 { 00718 string s; 00719 readString(s); 00720 return s; 00721 } 00722 00726 inline void IFStreamBinary::readString(string& s) 00727 { 00728 int len; 00729 // check for zero added by sballar 2013-01-04 00730 len = readInt(); 00731 if (len == 0) 00732 s=""; 00733 else 00734 { 00735 // read the data 00736 s = bData->substr(bDataPos, len); 00737 bDataPos += len; 00738 } 00739 } 00740 00744 inline void IFStreamBinary::readCharArray(string& s, int num_chars) 00745 { 00746 s.resize(num_chars); 00747 readCharArray(&s[0], num_chars); 00748 } 00749 00753 inline void IFStreamBinary::readCharArray(char* array, int num_chars) 00754 { 00755 // read the data 00756 00757 memcpy(array, &(*bData)[bDataPos], num_chars); 00758 bDataPos += num_chars; 00759 } 00760 00764 inline void IFStreamBinary::readType(string& s) 00765 { 00766 readString(s); 00767 } 00768 00772 inline void IFStreamBinary::readTypeArray(string& s, int num_chars) 00773 { 00774 readCharArray(s, num_chars); 00775 } 00776 00777 //************* Read Bools **************************************************** 00778 00782 inline bool IFStreamBinary::readBool() 00783 { 00784 // read the data 00785 00786 int pos = bDataPos; 00787 bDataPos += CPPUtils::SBOL; 00788 return readBool(pos); 00789 } 00790 00795 inline bool IFStreamBinary::readBool(int pos) 00796 { 00797 return *((bool*) &(*bData)[pos]); 00798 } 00799 00803 inline void IFStreamBinary::readBoolArray(bool* array, int num_bools) 00804 { 00805 // get bytes and perform alignment 00806 00807 int num_bytes = num_bools * CPPUtils::SBOL; 00808 00809 // copy from buffer into array ... 00810 // update buffer position and exit 00811 00812 memcpy(array, &(*bData)[bDataPos], num_bytes); 00813 bDataPos += num_bytes; 00814 } 00815 00819 inline void IFStreamBinary::readType(bool& b) 00820 { 00821 b = readBool(); 00822 } 00823 00827 inline void IFStreamBinary::readTypeArray(bool* array, int num_bools) 00828 { 00829 readBoolArray(array, num_bools); 00830 } 00831 00832 //************* Read Bytes **************************************************** 00833 00837 inline byte IFStreamBinary::readByte() 00838 { 00839 // read the data 00840 00841 int pos = bDataPos; 00842 bDataPos += CPPUtils::SBYT; 00843 return readByte(pos); 00844 } 00845 00850 inline byte IFStreamBinary::readByte(int pos) 00851 { 00852 return *((byte*) &(*bData)[pos]); 00853 } 00854 00858 inline void IFStreamBinary::readByteArray(byte* array, int num_bytes) 00859 { 00860 // get bytes and perform alignment 00861 00862 num_bytes *= CPPUtils::SBYT; 00863 00864 // copy from buffer into array ... 00865 // update buffer position and exit 00866 00867 memcpy(array, &(*bData)[bDataPos], num_bytes); 00868 bDataPos += num_bytes; 00869 } 00870 00874 inline void IFStreamBinary::readType(byte& b) 00875 { 00876 b = readByte(); 00877 } 00878 00882 inline void IFStreamBinary::readTypeArray(byte* array, int num_bytes) 00883 { 00884 readByteArray(array, num_bytes); 00885 } 00886 00887 //************* Read Shorts *************************************************** 00888 00892 inline short IFStreamBinary::readShort() 00893 { 00894 align2Byte(); 00895 return readShortNC(); 00896 } 00897 00901 inline short IFStreamBinary::readShortNC() 00902 { 00903 // read the data 00904 00905 int pos = bDataPos; 00906 bDataPos += CPPUtils::SSHT; 00907 return readShort(pos); 00908 } 00909 00914 inline short IFStreamBinary::readShort(int pos) 00915 { 00916 // read the data ... use memcpy if alignment is off 00917 00918 short s; 00919 if (bAlign) 00920 s = *((short*) &(*bData)[pos]); 00921 else 00922 memcpy(&s, &(*bData)[pos], 2); 00923 00924 if (bReverse) reverseBO2((char*) &s); 00925 return s; 00926 } 00927 00931 inline void IFStreamBinary::readShortArray(short* array, int num_shorts) 00932 { 00933 // get bytes and perform alignment 00934 00935 int num_bytes = num_shorts * CPPUtils::SSHT; 00936 align2Byte(); 00937 00938 // copy from buffer into array ... reverse data if necessary ... 00939 // update buffer position and exit 00940 00941 memcpy(array, &(*bData)[bDataPos], num_bytes); 00942 if (bReverse) reverseBO2Array(num_shorts, (char*) array); 00943 bDataPos += num_bytes; 00944 } 00945 00949 inline void IFStreamBinary::readType(short& s) 00950 { 00951 s = readShort(); 00952 } 00953 00957 inline void IFStreamBinary::readTypeArray(short* array, int num_shorts) 00958 { 00959 readShortArray(array, num_shorts); 00960 } 00961 00962 //************* Read Ints ***************************************************** 00963 00967 inline int IFStreamBinary::readInt() 00968 { 00969 align4Byte(); 00970 return readIntNC(); 00971 } 00972 00976 inline int IFStreamBinary::readIntNC() 00977 { 00978 // read the data 00979 00980 int pos = bDataPos; 00981 bDataPos += CPPUtils::SINT; 00982 return readInt(pos); 00983 } 00984 00989 inline int IFStreamBinary::readInt(int pos) 00990 { 00991 // read the data ... use memcpy if alignment is off 00992 00993 int i; 00994 if (bAlign) 00995 i = *((int*) &(*bData)[pos]); 00996 else 00997 memcpy(&i, &(*bData)[pos], 4); 00998 00999 if (bReverse) reverseBO4((char*) &i); 01000 return i; 01001 } 01002 01006 inline void IFStreamBinary::readIntArray(int* array, int num_ints) 01007 { 01008 // get bytes and perform alignment 01009 01010 int num_bytes = num_ints * CPPUtils::SINT; 01011 align4Byte(); 01012 01013 // copy from buffer into array ... reverse data if necessary ... 01014 // update buffer position and exit 01015 01016 memcpy(array, &(*bData)[bDataPos], num_bytes); 01017 if (bReverse) reverseBO4Array(num_ints, (char*) array); 01018 bDataPos += num_bytes; 01019 } 01020 01024 inline void IFStreamBinary::readType(int& i) 01025 { 01026 i = readInt(); 01027 } 01028 01032 inline void IFStreamBinary::readTypeArray(int* array, int num_ints) 01033 { 01034 readIntArray(array, num_ints); 01035 } 01036 01037 //************* Read Longs **************************************************** 01038 01042 inline LONG_INT IFStreamBinary::readLong() 01043 { 01044 align8Byte(); 01045 return readLongNC(); 01046 } 01047 01051 inline LONG_INT IFStreamBinary::readLongNC() 01052 { 01053 // read the data 01054 01055 int pos = bDataPos; 01056 bDataPos += CPPUtils::SLNG; 01057 return readLong(pos); 01058 } 01059 01064 inline LONG_INT IFStreamBinary::readLong(int pos) 01065 { 01066 // read the data ... use memcpy if alignment is off 01067 01068 LONG_INT l; 01069 if (bAlign) 01070 l = *((LONG_INT*) &(*bData)[pos]); 01071 else 01072 memcpy(&l, &(*bData)[pos], 8); 01073 01074 if (bReverse) reverseBO8((char*) &l); 01075 return l; 01076 } 01077 01081 inline void IFStreamBinary::readLongArray(LONG_INT* array, int num_longs) 01082 { 01083 // get bytes and perform alignment 01084 01085 int num_bytes = num_longs * CPPUtils::SLNG; 01086 align8Byte(); 01087 01088 // copy from buffer into array ... reverse data if necessary ... 01089 // update buffer position and exit 01090 01091 memcpy(array, &(*bData)[bDataPos], num_bytes); 01092 if (bReverse) reverseBO8Array(num_longs, (char*) array); 01093 bDataPos += num_bytes; 01094 } 01095 01099 inline void IFStreamBinary::readType(LONG_INT& l) 01100 { 01101 l = readLong(); 01102 } 01103 01107 inline void IFStreamBinary::readTypeArray(LONG_INT* array, int num_longs) 01108 { 01109 readLongArray(array, num_longs); 01110 } 01111 01112 //************* Read Floats *************************************************** 01113 01117 inline float IFStreamBinary::readFloat() 01118 { 01119 align4Byte(); 01120 return readFloatNC(); 01121 } 01122 01126 inline float IFStreamBinary::readFloatNC() 01127 { 01128 // read the data 01129 01130 int pos = bDataPos; 01131 bDataPos += CPPUtils::SFLT; 01132 return readFloat(pos); 01133 } 01134 01139 inline float IFStreamBinary::readFloat(int pos) 01140 { 01141 // read the data ... use memcpy if alignment is off 01142 01143 float f; 01144 if (bAlign) 01145 f = *((float*) &(*bData)[pos]); 01146 else 01147 memcpy(&f, &(*bData)[pos], 4); 01148 01149 if (bReverse) reverseBO4((char*) &f); 01150 return f; 01151 } 01152 01156 inline void IFStreamBinary::readFloatArray(float* array, int num_floats) 01157 { 01158 // get bytes and perform alignment 01159 01160 int num_bytes = num_floats * CPPUtils::SFLT; 01161 align4Byte(); 01162 01163 // copy from buffer into array ... reverse data if necessary ... 01164 // update buffer position and exit 01165 01166 memcpy(array, &(*bData)[bDataPos], num_bytes); 01167 if (bReverse) reverseBO4Array(num_floats, (char*) array); 01168 bDataPos += num_bytes; 01169 } 01170 01174 inline void IFStreamBinary::readType(float& f) 01175 { 01176 f = readFloat(); 01177 } 01178 01182 inline void IFStreamBinary::readTypeArray(float* array, int num_floats) 01183 { 01184 readFloatArray(array, num_floats); 01185 } 01186 01187 //************* Read Doubles ************************************************** 01188 01192 inline double IFStreamBinary::readDouble() 01193 { 01194 align8Byte(); 01195 return readDoubleNC(); 01196 } 01197 01201 inline double IFStreamBinary::readDoubleNC() 01202 { 01203 // read the data 01204 01205 int pos = bDataPos; 01206 bDataPos += CPPUtils::SDBL; 01207 return readDouble(pos); 01208 } 01209 01214 inline double IFStreamBinary::readDouble(int pos) 01215 { 01216 // read the data ... use memcpy if alignment is off 01217 01218 double d; 01219 if (bAlign) 01220 d = *((double*) &(*bData)[pos]); 01221 else 01222 memcpy(&d, &(*bData)[pos], 8); 01223 01224 if (bReverse) reverseBO8((char*) &d); 01225 return d; 01226 } 01227 01231 inline void IFStreamBinary::readDoubleArray(double* array, int num_doubles) 01232 { 01233 // get bytes and perform alignment 01234 01235 int num_bytes = num_doubles * CPPUtils::SDBL; 01236 align8Byte(); 01237 01238 // copy from buffer into array ... reverse data if necessary ... 01239 // update buffer position and exit 01240 01241 memcpy(array, &(*bData)[bDataPos], num_bytes); 01242 if (bReverse) reverseBO8Array(num_doubles, (char*) array); 01243 bDataPos += num_bytes; 01244 } 01245 01249 inline void IFStreamBinary::readType(double& d) 01250 { 01251 d = readDouble(); 01252 } 01253 01257 inline void IFStreamBinary::readTypeArray(double* array, int num_doubles) 01258 { 01259 readDoubleArray(array, num_doubles); 01260 } 01261 01262 //************* Write Strings ************************************************* 01263 01270 inline void IFStreamBinary::writeString(const string& str) 01271 { 01272 // get total size, align, and check capacity 01273 01274 int sz = (int) str.size() + CPPUtils::SINT; 01275 align4Byte(); 01276 checkBufferSize(sz); 01277 01278 // write the data 01279 01280 writeIntNC((int) str.size()); 01281 memcpy(&(*bData)[bDataPos], &str[0], str.size()); 01282 bDataPos += (int) str.size(); 01283 } 01284 01290 inline void IFStreamBinary::writeString(const char* char_string) 01291 { 01292 string tmp_string = char_string; 01293 writeString(tmp_string); 01294 } 01295 01299 inline void IFStreamBinary::writeCharArray(const char* array, int num_chars) 01300 { 01301 checkBufferSize(num_chars); 01302 01303 // write data 01304 01305 memcpy(&(*bData)[bDataPos], array, num_chars); 01306 bDataPos += num_chars; 01307 } 01308 01315 inline void IFStreamBinary::writeType(const string& str) 01316 { 01317 writeString(str); 01318 } 01319 01325 inline void IFStreamBinary::writeType(const char* char_string) 01326 { 01327 writeString(char_string); 01328 } 01329 01330 //************* Write Bools *************************************************** 01331 01337 inline void IFStreamBinary::writeBool(bool b) 01338 { 01339 checkBufferSize(CPPUtils::SBOL); 01340 01341 writeBoolNC(b); 01342 } 01343 01349 inline void IFStreamBinary::writeBoolNC(bool b) 01350 { 01351 writeBool(b, bDataPos); 01352 bDataPos += CPPUtils::SBOL; 01353 } 01354 01360 inline void IFStreamBinary::writeBool(bool b, int pos) 01361 { 01362 *((bool*) &(*bData)[pos]) = b; 01363 } 01364 01368 inline void IFStreamBinary::writeBoolArray(const bool* array, int num_bools) 01369 { 01370 // get number of bytes to be written and check for alignment and buffer 01371 // size 01372 01373 num_bools *= CPPUtils::SBOL; 01374 checkBufferSize(num_bools); 01375 01376 // copy array into data buffer. Reverse data buffer entries if necessary 01377 // and increment the buffer output pointer. 01378 01379 memcpy(&(*bData)[bDataPos], array, num_bools); 01380 bDataPos += num_bools; 01381 } 01382 01388 inline void IFStreamBinary::writeType(bool b) 01389 { 01390 writeBool(b); 01391 } 01392 01396 inline void IFStreamBinary::writeTypeArray(const bool* array, int num_bools) 01397 { 01398 writeBoolArray(array, num_bools); 01399 } 01400 01401 //************* Write Bytes *************************************************** 01402 01408 inline void IFStreamBinary::writeByte(byte b) 01409 { 01410 checkBufferSize(CPPUtils::SBYT); 01411 01412 writeByteNC(b); 01413 } 01414 01420 inline void IFStreamBinary::writeByteNC(byte b) 01421 { 01422 writeByte(b, bDataPos); 01423 bDataPos += CPPUtils::SBYT; 01424 } 01425 01431 inline void IFStreamBinary::writeByte(byte b, int pos) 01432 { 01433 *((byte*) &(*bData)[pos]) = b; 01434 } 01435 01439 inline void IFStreamBinary::writeByteArray(const byte* array, int num_bytes) 01440 { 01441 // get number of bytes to be written and check for alignment and buffer 01442 // size 01443 01444 num_bytes *= CPPUtils::SBYT; 01445 checkBufferSize(num_bytes); 01446 01447 // copy array into data buffer. Reverse data buffer entries if necessary 01448 // and increment the buffer output pointer. 01449 01450 memcpy(&(*bData)[bDataPos], array, num_bytes); 01451 bDataPos += num_bytes; 01452 } 01453 01459 inline void IFStreamBinary::writeType(byte b) 01460 { 01461 writeByte(b); 01462 } 01463 01467 inline void IFStreamBinary::writeTypeArray(const byte* array, int num_bytes) 01468 { 01469 writeByteArray(array, num_bytes); 01470 } 01471 01472 //************* Write Shorts ************************************************** 01473 01479 inline void IFStreamBinary::writeShort(short s) 01480 { 01481 align2Byte(); 01482 checkBufferSize(CPPUtils::SSHT); 01483 01484 writeShortNC(s); 01485 } 01486 01492 inline void IFStreamBinary::writeShortNC(short s) 01493 { 01494 writeShort(s, bDataPos); 01495 bDataPos += CPPUtils::SSHT; 01496 } 01497 01503 inline void IFStreamBinary::writeShort(short s, int pos) 01504 { 01505 if (bReverse) reverseBO2((char*) &s); 01506 01507 if (bAlign) 01508 *((short*) &(*bData)[pos]) = s; 01509 else 01510 memcpy(&(*bData)[pos], &s, CPPUtils::SSHT); 01511 } 01512 01516 inline void IFStreamBinary::writeShortArray(const short* array, int num_shorts) 01517 { 01518 // get number of bytes to be written and check for alignment and buffer 01519 // size 01520 01521 int num_bytes = num_shorts * CPPUtils::SSHT; 01522 align2Byte(); 01523 checkBufferSize(num_bytes); 01524 01525 // copy array into data buffer. Reverse data buffer entries if necessary 01526 // and increment the buffer output pointer. 01527 01528 memcpy(&(*bData)[bDataPos], array, num_bytes); 01529 if (bReverse) reverseBO2Array(num_shorts, &(*bData)[bDataPos]); 01530 bDataPos += num_bytes; 01531 } 01532 01538 inline void IFStreamBinary::writeType(short s) 01539 { 01540 writeShort(s); 01541 } 01542 01546 inline void IFStreamBinary::writeTypeArray(const short* array, int num_shorts) 01547 { 01548 writeShortArray(array, num_shorts); 01549 } 01550 01551 //************* Write Ints **************************************************** 01552 01558 inline void IFStreamBinary::writeInt(int i) 01559 { 01560 align4Byte(); 01561 checkBufferSize(CPPUtils::SINT); 01562 01563 writeIntNC(i); 01564 } 01565 01571 inline void IFStreamBinary::writeIntNC(int i) 01572 { 01573 writeInt(i, bDataPos); 01574 bDataPos += CPPUtils::SINT; 01575 } 01576 01582 inline void IFStreamBinary::writeInt(int i, int pos) 01583 { 01584 if (bReverse) reverseBO4((char*) &i); 01585 01586 if (bAlign) 01587 *((int*) &(*bData)[pos]) = i; 01588 else 01589 memcpy(&(*bData)[pos], &i, CPPUtils::SINT); 01590 } 01591 01595 inline void IFStreamBinary::writeIntArray(const int* array, int num_ints) 01596 { 01597 // get number of bytes to be written and check for alignment and buffer 01598 // size 01599 01600 int num_bytes = num_ints * CPPUtils::SINT; 01601 align4Byte(); 01602 checkBufferSize(num_bytes); 01603 01604 // copy array into data buffer. Reverse data buffer entries if necessary 01605 // and increment the buffer output pointer. 01606 01607 memcpy(&(*bData)[bDataPos], array, num_bytes); 01608 if (bReverse) reverseBO4Array(num_ints, &(*bData)[bDataPos]); 01609 bDataPos += num_bytes; 01610 } 01611 01617 inline void IFStreamBinary::writeType(int i) 01618 { 01619 writeInt(i); 01620 } 01621 01625 inline void IFStreamBinary::writeTypeArray(const int* array, int num_ints) 01626 { 01627 writeIntArray(array, num_ints); 01628 } 01629 01630 //************* Write Longs *************************************************** 01631 01637 inline void IFStreamBinary::writeLong(LONG_INT l) 01638 { 01639 align8Byte(); 01640 checkBufferSize(CPPUtils::SLNG); 01641 01642 writeLongNC(l); 01643 } 01644 01650 inline void IFStreamBinary::writeLongNC(LONG_INT l) 01651 { 01652 writeLong(l, bDataPos); 01653 bDataPos += CPPUtils::SLNG; 01654 } 01655 01661 inline void IFStreamBinary::writeLong(LONG_INT l, int pos) 01662 { 01663 if (bReverse) reverseBO8((char*) &l); 01664 01665 if (bAlign) 01666 *((LONG_INT*) &(*bData)[pos]) = l; 01667 else 01668 memcpy(&(*bData)[pos], &l, CPPUtils::SLNG); 01669 } 01670 01674 inline void IFStreamBinary::writeLongArray(const LONG_INT* array, int num_longs) 01675 { 01676 // get number of bytes to be written and check for alignment and buffer 01677 // size 01678 01679 int num_bytes = num_longs * CPPUtils::SLNG; 01680 align8Byte(); 01681 checkBufferSize(num_bytes); 01682 01683 // copy array into data buffer. Reverse data buffer entries if necessary 01684 // and increment the buffer output pointer. 01685 01686 memcpy(&(*bData)[bDataPos], array, num_bytes); 01687 if (bReverse) reverseBO8Array(num_longs, &(*bData)[bDataPos]); 01688 bDataPos += num_bytes; 01689 } 01690 01696 inline void IFStreamBinary::writeType(LONG_INT l) 01697 { 01698 writeLong(l); 01699 } 01700 01704 inline void IFStreamBinary::writeTypeArray(const LONG_INT* array, int num_longs) 01705 { 01706 writeLongArray(array, num_longs); 01707 } 01708 01709 //************* Write Floats ************************************************** 01710 01716 inline void IFStreamBinary::writeFloat(float f) 01717 { 01718 align4Byte(); 01719 checkBufferSize(CPPUtils::SFLT); 01720 01721 writeFloatNC(f); 01722 } 01723 01729 inline void IFStreamBinary::writeFloatNC(float f) 01730 { 01731 writeFloat(f, bDataPos); 01732 bDataPos += CPPUtils::SFLT; 01733 } 01734 01740 inline void IFStreamBinary::writeFloat(float f, int pos) 01741 { 01742 if (bReverse) reverseBO4((char*) &f); 01743 01744 if (bAlign) 01745 *((float*) &(*bData)[pos]) = f; 01746 else 01747 memcpy(&(*bData)[pos], &f, CPPUtils::SFLT); 01748 } 01749 01753 inline void IFStreamBinary::writeFloatArray(const float* array, int num_floats) 01754 { 01755 // get number of bytes to be written and check for alignment and buffer 01756 // size 01757 01758 int num_bytes = num_floats * CPPUtils::SFLT; 01759 align4Byte(); 01760 checkBufferSize(num_bytes); 01761 01762 // copy array into data buffer. Reverse data buffer entries if necessary 01763 // and increment the buffer output pointer. 01764 01765 memcpy(&(*bData)[bDataPos], array, num_bytes); 01766 if (bReverse) reverseBO4Array(num_floats, &(*bData)[bDataPos]); 01767 bDataPos += num_bytes; 01768 } 01769 01775 inline void IFStreamBinary::writeType(float f) 01776 { 01777 writeFloat(f); 01778 } 01779 01783 inline void IFStreamBinary::writeTypeArray(const float* array, int num_floats) 01784 { 01785 writeFloatArray(array, num_floats); 01786 } 01787 01788 //************* Write Doubles ************************************************* 01789 01795 inline void IFStreamBinary::writeDouble(double d) 01796 { 01797 align8Byte(); 01798 checkBufferSize(CPPUtils::SDBL); 01799 01800 writeDoubleNC(d); 01801 } 01802 01808 inline void IFStreamBinary::writeDoubleNC(double d) 01809 { 01810 writeDouble(d, bDataPos); 01811 bDataPos += CPPUtils::SDBL; 01812 } 01813 01819 inline void IFStreamBinary::writeDouble(double d, int pos) 01820 { 01821 if (bReverse) reverseBO8((char*) &d); 01822 01823 if (bAlign) 01824 *((double*) &(*bData)[pos]) = d; 01825 else 01826 memcpy(&(*bData)[pos], &d, CPPUtils::SDBL); 01827 } 01828 01832 inline void IFStreamBinary::writeDoubleArray(const double* array, int num_doubles) 01833 { 01834 // get number of bytes to be written and check for alignment and buffer 01835 // size 01836 01837 int num_bytes = num_doubles * CPPUtils::SDBL; 01838 align8Byte(); 01839 checkBufferSize(num_bytes); 01840 01841 // copy array into data buffer. Reverse data buffer entries if necessary 01842 // and increment the buffer output pointer. 01843 01844 memcpy(&(*bData)[bDataPos], array, num_bytes); 01845 if (bReverse) reverseBO8Array(num_doubles, &(*bData)[bDataPos]); 01846 bDataPos += num_bytes; 01847 } 01848 01854 inline void IFStreamBinary::writeType(double d) 01855 { 01856 writeDouble(d); 01857 } 01858 01862 inline void IFStreamBinary::writeTypeArray(const double* array, int num_doubles) 01863 { 01864 writeDoubleArray(array, num_doubles); 01865 } 01866 01867 //************* Byte reversal functions *************************************** 01868 01872 inline void IFStreamBinary::reverseBO2(char* d) 01873 { 01874 char tmp = d[0]; 01875 d[0] = d[1]; 01876 d[1] = tmp; 01877 } 01878 01882 inline void IFStreamBinary::reverseBO4(char* d) 01883 { 01884 char tmp = d[0]; 01885 d[0] = d[3]; 01886 d[3] = tmp; 01887 01888 tmp = d[1]; 01889 d[1] = d[2]; 01890 d[2] = tmp; 01891 } 01892 01896 inline void IFStreamBinary::reverseBO8(char* d) 01897 { 01898 char tmp = d[0]; 01899 d[0] = d[7]; 01900 d[7] = tmp; 01901 01902 tmp = d[1]; 01903 d[1] = d[6]; 01904 d[6] = tmp; 01905 01906 tmp = d[2]; 01907 d[2] = d[5]; 01908 d[5] = tmp; 01909 01910 tmp = d[3]; 01911 d[3] = d[4]; 01912 d[4] = tmp; 01913 } 01914 01915 } // end namespace geotess 01916 01917 #endif // IFSTREAMBINARY_OBJECT_H