GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
include/IFStreamBinary.h
Go to the documentation of this file.
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 <fstream>
00044 #include <vector>
00045 #include <sstream>
00046 
00047 // use standard library objects
00048 using namespace std;
00049 
00050 // **** _LOCAL INCLUDES_ *******************************************************
00051 
00052 #include "CPPUtils.h"
00053 #include "GeoTessUtils.h"
00054 #include "GeoTessException.h"
00055 
00056 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
00057 
00058 namespace geotess {
00059 
00060 // **** _FORWARD REFERENCES_ ***************************************************
00061 
00062 // **** _CLASS DEFINITION_ *****************************************************
00063 
00078 class GEOTESS_EXP_IMP IFStreamBinary
00079 {
00080   protected:
00081 
00085                 string*                                 bData;
00086 
00090                 int                                                     bDataPos;
00091 
00095                 int                                                     bSize;
00096 
00101                 bool                                            bAlign;
00102 
00110                 bool                                            bReverse;
00111 
00117                 bool                                            bOwnStr;
00118 
00119                 string                                  bFileName;
00120 
00121                 int                                                     bMemIncr;
00122 
00127     void                                                align2Byte()
00128                 {
00129                         int sm = bDataPos % CPPUtils::SSHT;
00130                         if (sm && bAlign) bDataPos += CPPUtils::SSHT - sm;
00131                 }
00132 
00137     void                                                align4Byte()
00138                 {
00139                         int sm = bDataPos % CPPUtils::SINT;
00140                         if (sm && bAlign) bDataPos += CPPUtils::SINT - sm;
00141                 }
00142 
00147     void                                                align8Byte()
00148                 {
00149                         int sm = bDataPos % CPPUtils::SDBL;
00150                         if (sm && bAlign) bDataPos += CPPUtils::SDBL - sm;
00151                 }
00152 
00157     void                                                checkBufferSize(int sincr)
00158                 {
00159                         if (bDataPos + sincr > (int) bData->size())
00160                         {
00161                                 if (sincr + bData->size() > bData->capacity())
00162                                         bData->reserve(bData->capacity() + bMemIncr);
00163                                 bData->append(bDataPos + sincr - bData->size(), ' ');
00164                         }
00165                 }
00166 
00167                 /*
00168                  * Reverses each s-byte element of array a containing n elements
00169                  * (s*n bytes). The element size s must be 2, 4, or 8.
00170                  */
00171                 static void                     reverseBOArray(int n, char* a, int s);
00172 
00173                 /*
00174                  * Reverses the byte order of each element of a where a is an array of n
00175                  * 2, 4, or 8 byte elements. Used to convert between big-and
00176                  * little-endian formats.
00177                  */
00178                 static void                     reverseBO2Array(int n, char* a);
00179                 static void                     reverseBO4Array(int n, char* a);
00180                 static void                     reverseBO8Array(int n, char* a);
00181 
00182                 /*
00183                  * Reverses the byte order of d, where d is a 2, 4, or 8 byte array. Used
00184                  * to convert between big-and little-endian formats.
00185                  */
00186                 static void                     reverseBO2(char* d);
00187                 static void                     reverseBO4(char* d);
00188                 static void                     reverseBO8(char* d);
00189 
00190         public:
00191 
00195                                                                                 IFStreamBinary();
00196 
00200                                                                                 IFStreamBinary(bool align);
00201 
00206                                                                                 IFStreamBinary(const string& filename);
00207 
00212                                                                                 IFStreamBinary(const string& filename, int num_bytes);
00213 
00218                                                                                 IFStreamBinary(string* str);
00219 
00223                                                                                 IFStreamBinary(const IFStreamBinary& db);
00224 
00228                 virtual                                 ~IFStreamBinary();
00229 
00233                 IFStreamBinary& operator=(const IFStreamBinary& db);
00234 
00238                 void                                            dumpBuffer();
00239 
00243                 static bool             exists(const string& filename);
00244 
00248                 void                                            writeToFile(const string& filename);
00249 
00253                 void                                            writeToFile(ofstream& ofs);
00254 
00258                 void                                            readFromFile(const string& filename);
00259 
00263                 void                                            readFromFile(const string& filename, int num_bytes);
00264 
00269 //              void                                            readFromFile(ifstream& ifs, streampos file_pos,
00270 //                                                                                                                                      int num_bytes);
00271 
00275                 void                                            readFromFile(ifstream& ifs, int num_bytes);
00276 
00280                 const string&           getFileName() const { return bFileName; }
00281 
00285                 void                                            clear()
00286                 {
00287                         (*bData) = "";
00288                         bDataPos = 0;
00289                         bSize = 0;
00290                 }
00291 
00296                 void                                            setMemoryCapacityIncrement(int mci)
00297                 {
00298                         if (mci > 0) bMemIncr = mci;
00299                 }
00300 
00309                 string                                  readString();
00310                 void                                            readString(string& s);
00311                 void                                            readCharArray(string& s, int num_chars);
00312                 void                                            readCharArray(char* array, int num_chars);
00313                 void                                            readType(string& s);
00314                 void                                            readTypeArray(string& array, int num_chars);
00315 
00323                 bool                                            readBool();
00324                 bool                                            readBool(int pos);
00325                 void                                            readBoolArray(bool* array, int num_bools);
00326                 void                                            readType(bool& b);
00327                 void                                            readTypeArray(bool* array, int num_bools);
00328 
00336                 byte                                            readByte();
00337                 byte                                            readByte(int pos);
00338                 void                                            readByteArray(byte* array, int num_bs);
00339                 void                                            readType(byte& b);
00340                 void                                            readTypeArray(byte* array, int num_bs);
00341 
00351                 short                                           readShort();
00352                 short                                           readShortNC();
00353                 short                                           readShort(int pos);
00354                 void                                            readShortArray(short* array, int num_shorts);
00355                 void                                            readType(short& s);
00356                 void                                            readTypeArray(short* array, int num_shorts);
00357 
00367                 int                                                     readInt();
00368                 int                                                     readIntNC();
00369                 int                                                     readInt(int pos);
00370                 void                                            readIntArray(int* array, int num_ints);
00371                 void                                            readType(int& i);
00372                 void                                            readTypeArray(int* array, int num_ints);
00373 
00383                 LONG_INT                                                readLong();
00384                 LONG_INT                                                readLongNC();
00385                 LONG_INT                                                readLong(int pos);
00386                 void                                            readLongArray(LONG_INT* array, int num_longs);
00387                 void                                            readType(LONG_INT& l);
00388                 void                                            readTypeArray(LONG_INT* array, int num_longs);
00389 
00399                 float                                           readFloat();
00400                 float                                           readFloatNC();
00401                 float                                           readFloat(int pos);
00402                 void                                            readFloatArray(float* array, int num_floats);
00403                 void                                            readType(float& f);
00404                 void                                            readTypeArray(float* array, int num_floats);
00405 
00415                 double                                  readDouble();
00416                 double                                  readDoubleNC();
00417                 double                                  readDouble(int pos);
00418                 void                                            readDoubleArray(double* array, int num_doubles);
00419                 void                                            readType(double& d);
00420                 void                                            readTypeArray(double* array, int num_doubles);
00421 
00430                 void                                            writeString(const string& str);
00431                 void                                            writeString(const char* char_string);
00432                 void                                            writeCharArray(const char* array, int num_chars);
00433                 void            writeType(const string& str);
00434                 void                                            writeType(const char* char_string);
00435 
00446                 void                                            writeBool(bool b);
00447                 void                                            writeBoolNC(bool b);
00448                 void                                            writeBool(bool b, int pos);
00449                 void                                            writeBoolArray(const bool* array, int num_bools);
00450                 void                                            writeType(bool b);
00451                 void                                            writeTypeArray(const bool* array, int num_bools);
00452 
00463                 void                                            writeByte(byte b);
00464                 void                                            writeByteNC(byte b);
00465                 void                                            writeByte(byte b, int pos);
00466                 void                                            writeByteArray(const byte* array, int num_bytes);
00467                 void                                            writeType(byte b);
00468                 void                                            writeTypeArray(const byte* array, int num_bytes);
00469 
00481                 void                                            writeShort(short i);
00482                 void                                            writeShortNC(short i);
00483                 void                                            writeShort(short i, int pos);
00484                 void                                            writeShortArray(const short* array, int num_ints);
00485                 void                                            writeType(short i);
00486                 void                                            writeTypeArray(const short* array, int num_ints);
00487 
00499                 void                                            writeInt(int i);
00500                 void                                            writeIntNC(int i);
00501                 void                                            writeInt(int i, int pos);
00502                 void                                            writeIntArray(const int* array, int num_ints);
00503                 void                                            writeType(int i);
00504                 void                                            writeTypeArray(const int* array, int num_ints);
00505 
00517                 void                                            writeLong(LONG_INT l);
00518                 void                                            writeLongNC(LONG_INT l);
00519                 void                                            writeLong(LONG_INT l, int pos);
00520                 void                                            writeLongArray(const LONG_INT* array, int num_longs);
00521                 void                                            writeType(LONG_INT l);
00522                 void                                            writeTypeArray(const LONG_INT* array, int num_longs);
00523 
00535                 void                                            writeFloat(float f);
00536                 void                                            writeFloatNC(float f);
00537                 void                                            writeFloat(float f, int pos);
00538                 void                                            writeFloatArray(const float* array, int num_floats);
00539                 void                                            writeType(float f);
00540                 void                                            writeTypeArray(const float* array, int num_floats);
00541 
00553                 void                                            writeDouble(double d);
00554                 void                                            writeDoubleNC(double d);
00555                 void                                            writeDouble(double d, int pos);
00556                 void                                            writeDoubleArray(const double* array, int num_doubles);
00557                 void                                            writeType(double d);
00558                 void                                            writeTypeArray(const double* array, int num_doubles);
00559 
00560                 /*
00561                  * Return class name.
00562                  */
00563                 static string           className()
00564                 { return "IFStreamBinary"; }
00565 
00566                 /*
00567                  * Return class size.
00568                  */
00569                 static int                      classSize()
00570                 { return (int) sizeof(IFStreamBinary); }
00571 
00576                 void                                            reSize(int spc)
00577                 { bData->resize(spc); }
00578 
00582                 void                                            reserve(int sze)
00583                 { bData->reserve(sze); }
00584 
00588                 int                                                     size()
00589                 {
00590                         if (bDataPos > bSize) bSize = bDataPos;
00591                         return bSize;
00592                 }
00593 
00597                 void                                            resetPos()
00598                 {
00599                         if (bDataPos > bSize) bSize = bDataPos;
00600                         bDataPos = 0;
00601                 }
00602 
00606                 void                                            setPosToEnd()
00607                 {
00608                         if (bDataPos > bSize) bSize = bDataPos;
00609                         bDataPos = bSize;
00610                 }
00611 
00615                 int                                                     getPos() const
00616                 { return bDataPos; }
00617 
00621                 void                                            incrementPos(int increment = 1)
00622                 { bDataPos += increment; }
00623 
00627                 void                                            decrementPos(int decrement = 1)
00628                 { bDataPos -= decrement; }
00629 
00633                 const string&           getData() const
00634                 {       return *bData; }
00635 
00639                 int                                                     getCapacity() const
00640                 { return (int) bData->capacity(); }
00641 
00646                 char*                                           getPosPointer()
00647                 {       return getPosPointer(bDataPos); }
00648 
00652                 char*                                           getPosPointer(int pos)
00653                 {
00654                         char* p = &(*bData)[pos];
00655                         return p;
00656                 }
00657 
00661                 void                                            setByteOrderReverse(bool bor)
00662                 { bReverse = bor; }
00663 
00667                 void                                            byteOrderReverseOn()
00668                 { bReverse = true; }
00669 
00673                 void                                            byteOrderReverseOff()
00674                 { bReverse = false; }
00675 
00680                 bool                                            isByteOrderReversalOn() const
00681                 { return bReverse; }
00682 
00686                 void                                            setBoundaryAlignment(bool align)
00687                 { bAlign = align; }
00688 
00692                 void                                            boundaryAlignmentOn()
00693                 { bAlign = true; }
00694 
00698                 void                                            boundaryAlignmentOff()
00699                 { bAlign = false; }
00700 
00705                 bool                                            isBoundaryAlignmentOn() const
00706                 { return bAlign; }
00707 
00708 }; // end class IFStreamBinary
00709 
00710 //************* Read Strings **************************************************
00711 
00715 inline string IFStreamBinary::readString()
00716 {
00717   string s;
00718   readString(s);
00719   return s;
00720 }
00721 
00725 inline void IFStreamBinary::readString(string& s)
00726 {
00727   int len;
00728   // check for zero added by sballar 2013-01-04
00729   len = readInt();
00730   if (len == 0)
00731           s="";
00732   else
00733   {
00734           // read the data
00735           s = bData->substr(bDataPos, len);
00736           bDataPos += len;
00737   }
00738 }
00739 
00743 inline void IFStreamBinary::readCharArray(string& s, int num_chars)
00744 {
00745         s.resize(num_chars);
00746         readCharArray(&s[0], num_chars);
00747 }
00748 
00752 inline void IFStreamBinary::readCharArray(char* array, int num_chars)
00753 {
00754   // read the data
00755 
00756   memcpy(array, &(*bData)[bDataPos], num_chars);
00757   bDataPos += num_chars;
00758 }
00759 
00763 inline void IFStreamBinary::readType(string& s)
00764 {
00765         readString(s);
00766 }
00767 
00771 inline void IFStreamBinary::readTypeArray(string& s, int num_chars)
00772 {
00773         readCharArray(s, num_chars);
00774 }
00775 
00776 //************* Read Bools ****************************************************
00777 
00781 inline bool IFStreamBinary::readBool()
00782 {
00783   // read the data
00784 
00785         int pos = bDataPos;
00786         bDataPos += CPPUtils::SBOL;
00787         return readBool(pos);
00788 }
00789 
00794 inline bool IFStreamBinary::readBool(int pos)
00795 {
00796   return *((bool*) &(*bData)[pos]);
00797 }
00798 
00802 inline void IFStreamBinary::readBoolArray(bool* array, int num_bools)
00803 {
00804         // get bytes and perform alignment
00805 
00806   int num_bytes = num_bools * CPPUtils::SBOL;
00807 
00808         // copy from buffer into array ...
00809         // update buffer position and exit
00810 
00811   memcpy(array, &(*bData)[bDataPos], num_bytes);
00812   bDataPos += num_bytes;
00813 }
00814 
00818 inline void IFStreamBinary::readType(bool& b)
00819 {
00820         b = readBool();
00821 }
00822 
00826 inline void IFStreamBinary::readTypeArray(bool* array, int num_bools)
00827 {
00828         readBoolArray(array, num_bools);
00829 }
00830 
00831 //************* Read Bytes ****************************************************
00832 
00836 inline byte IFStreamBinary::readByte()
00837 {
00838   // read the data
00839 
00840         int pos = bDataPos;
00841         bDataPos += CPPUtils::SBYT;
00842         return readByte(pos);
00843 }
00844 
00849 inline byte IFStreamBinary::readByte(int pos)
00850 {
00851   return *((byte*) &(*bData)[pos]);
00852 }
00853 
00857 inline void IFStreamBinary::readByteArray(byte* array, int num_bytes)
00858 {
00859         // get bytes and perform alignment
00860 
00861   num_bytes *= CPPUtils::SBYT;
00862 
00863         // copy from buffer into array ...
00864         // update buffer position and exit
00865 
00866   memcpy(array, &(*bData)[bDataPos], num_bytes);
00867   bDataPos += num_bytes;
00868 }
00869 
00873 inline void IFStreamBinary::readType(byte& b)
00874 {
00875         b = readByte();
00876 }
00877 
00881 inline void IFStreamBinary::readTypeArray(byte* array, int num_bytes)
00882 {
00883         readByteArray(array, num_bytes);
00884 }
00885 
00886 //************* Read Shorts ***************************************************
00887 
00891 inline short IFStreamBinary::readShort()
00892 {
00893   align2Byte();
00894   return readShortNC();
00895 }
00896 
00900 inline short IFStreamBinary::readShortNC()
00901 {
00902   // read the data
00903 
00904         int pos = bDataPos;
00905         bDataPos += CPPUtils::SSHT;
00906         return readShort(pos);
00907 }
00908 
00913 inline short IFStreamBinary::readShort(int pos)
00914 {
00915   // read the data ... use memcpy if alignment is off
00916 
00917         short s;
00918         if (bAlign)
00919     s = *((short*) &(*bData)[pos]);
00920         else
00921           memcpy(&s, &(*bData)[pos], 2);
00922 
00923   if (bReverse) reverseBO2((char*) &s);
00924         return s;
00925 }
00926 
00930 inline void IFStreamBinary::readShortArray(short* array, int num_shorts)
00931 {
00932         // get bytes and perform alignment
00933 
00934   int num_bytes = num_shorts * CPPUtils::SSHT;
00935   align2Byte();
00936 
00937         // copy from buffer into array ... reverse data if necessary ...
00938         // update buffer position and exit
00939 
00940   memcpy(array, &(*bData)[bDataPos], num_bytes);
00941   if (bReverse) reverseBO2Array(num_shorts, (char*) array);
00942   bDataPos += num_bytes;
00943 }
00944 
00948 inline void IFStreamBinary::readType(short& s)
00949 {
00950         s = readShort();
00951 }
00952 
00956 inline void IFStreamBinary::readTypeArray(short* array, int num_shorts)
00957 {
00958         readShortArray(array, num_shorts);
00959 }
00960 
00961 //************* Read Ints *****************************************************
00962 
00966 inline int IFStreamBinary::readInt()
00967 {
00968   align4Byte();
00969   return readIntNC();
00970 }
00971 
00975 inline int IFStreamBinary::readIntNC()
00976 {
00977   // read the data
00978 
00979         int pos = bDataPos;
00980         bDataPos += CPPUtils::SINT;
00981         return readInt(pos);
00982 }
00983 
00988 inline int IFStreamBinary::readInt(int pos)
00989 {
00990   // read the data ... use memcpy if alignment is off
00991 
00992         int i;
00993         if (bAlign)
00994                 i = *((int*) &(*bData)[pos]);
00995         else
00996                 memcpy(&i, &(*bData)[pos], 4);
00997 
00998   if (bReverse) reverseBO4((char*) &i);
00999         return i;
01000 }
01001 
01005 inline void IFStreamBinary::readIntArray(int* array, int num_ints)
01006 {
01007         // get bytes and perform alignment
01008 
01009   int num_bytes = num_ints * CPPUtils::SINT;
01010   align4Byte();
01011 
01012         // copy from buffer into array ... reverse data if necessary ...
01013         // update buffer position and exit
01014 
01015   memcpy(array, &(*bData)[bDataPos], num_bytes);
01016   if (bReverse) reverseBO4Array(num_ints, (char*) array);
01017   bDataPos += num_bytes;
01018 }
01019 
01023 inline void IFStreamBinary::readType(int& i)
01024 {
01025         i = readInt();
01026 }
01027 
01031 inline void IFStreamBinary::readTypeArray(int* array, int num_ints)
01032 {
01033         readIntArray(array, num_ints);
01034 }
01035 
01036 //************* Read Longs ****************************************************
01037 
01041 inline LONG_INT IFStreamBinary::readLong()
01042 {
01043   align8Byte();
01044   return readLongNC();
01045 }
01046 
01050 inline LONG_INT IFStreamBinary::readLongNC()
01051 {
01052   // read the data
01053 
01054         int pos = bDataPos;
01055         bDataPos += CPPUtils::SLNG;
01056         return readLong(pos);
01057 }
01058 
01063 inline LONG_INT IFStreamBinary::readLong(int pos)
01064 {
01065   // read the data ... use memcpy if alignment is off
01066 
01067         LONG_INT l;
01068         if (bAlign)
01069                 l = *((LONG_INT*) &(*bData)[pos]);
01070         else
01071           memcpy(&l, &(*bData)[pos], 8);
01072 
01073   if (bReverse) reverseBO8((char*) &l);
01074         return l;
01075 }
01076 
01080 inline void IFStreamBinary::readLongArray(LONG_INT* array, int num_longs)
01081 {
01082         // get bytes and perform alignment
01083 
01084   int num_bytes = num_longs * CPPUtils::SLNG;
01085   align8Byte();
01086 
01087         // copy from buffer into array ... reverse data if necessary ...
01088         // update buffer position and exit
01089 
01090   memcpy(array, &(*bData)[bDataPos], num_bytes);
01091   if (bReverse) reverseBO8Array(num_longs, (char*) array);
01092   bDataPos += num_bytes;
01093 }
01094 
01098 inline void IFStreamBinary::readType(LONG_INT& l)
01099 {
01100         l = readLong();
01101 }
01102 
01106 inline void IFStreamBinary::readTypeArray(LONG_INT* array, int num_longs)
01107 {
01108         readLongArray(array, num_longs);
01109 }
01110 
01111 //************* Read Floats ***************************************************
01112 
01116 inline float IFStreamBinary::readFloat()
01117 {
01118   align4Byte();
01119   return readFloatNC();
01120 }
01121 
01125 inline float IFStreamBinary::readFloatNC()
01126 {
01127   // read the data
01128 
01129         int pos = bDataPos;
01130         bDataPos += CPPUtils::SFLT;
01131         return readFloat(pos);
01132 }
01133 
01138 inline float IFStreamBinary::readFloat(int pos)
01139 {
01140   // read the data ... use memcpy if alignment is off
01141 
01142         float f;
01143         if (bAlign)
01144                 f = *((float*) &(*bData)[pos]);
01145         else
01146           memcpy(&f, &(*bData)[pos], 4);
01147 
01148   if (bReverse) reverseBO4((char*) &f);
01149         return f;
01150 }
01151 
01155 inline void IFStreamBinary::readFloatArray(float* array, int num_floats)
01156 {
01157         // get bytes and perform alignment
01158 
01159   int num_bytes = num_floats * CPPUtils::SFLT;
01160   align4Byte();
01161 
01162         // copy from buffer into array ... reverse data if necessary ...
01163         // update buffer position and exit
01164 
01165   memcpy(array, &(*bData)[bDataPos], num_bytes);
01166   if (bReverse) reverseBO4Array(num_floats, (char*) array);
01167   bDataPos += num_bytes;
01168 }
01169 
01173 inline void IFStreamBinary::readType(float& f)
01174 {
01175         f = readFloat();
01176 }
01177 
01181 inline void IFStreamBinary::readTypeArray(float* array, int num_floats)
01182 {
01183         readFloatArray(array, num_floats);
01184 }
01185 
01186 //************* Read Doubles **************************************************
01187 
01191 inline double IFStreamBinary::readDouble()
01192 {
01193   align8Byte();
01194   return readDoubleNC();
01195 }
01196 
01200 inline double IFStreamBinary::readDoubleNC()
01201 {
01202   // read the data
01203 
01204         int pos = bDataPos;
01205         bDataPos += CPPUtils::SDBL;
01206         return readDouble(pos);
01207 }
01208 
01213 inline double IFStreamBinary::readDouble(int pos)
01214 {
01215   // read the data ... use memcpy if alignment is off
01216 
01217         double d;
01218         if (bAlign)
01219                 d = *((double*) &(*bData)[pos]);
01220         else
01221           memcpy(&d, &(*bData)[pos], 8);
01222 
01223   if (bReverse) reverseBO8((char*) &d);
01224         return d;
01225 }
01226 
01230 inline void IFStreamBinary::readDoubleArray(double* array, int num_doubles)
01231 {
01232         // get bytes and perform alignment
01233 
01234   int num_bytes = num_doubles * CPPUtils::SDBL;
01235   align8Byte();
01236 
01237         // copy from buffer into array ... reverse data if necessary ...
01238         // update buffer position and exit
01239 
01240   memcpy(array, &(*bData)[bDataPos], num_bytes);
01241   if (bReverse) reverseBO8Array(num_doubles, (char*) array);
01242   bDataPos += num_bytes;
01243 }
01244 
01248 inline void IFStreamBinary::readType(double& d)
01249 {
01250         d = readDouble();
01251 }
01252 
01256 inline void IFStreamBinary::readTypeArray(double* array, int num_doubles)
01257 {
01258         readDoubleArray(array, num_doubles);
01259 }
01260 
01261 //************* Write Strings *************************************************
01262 
01269 inline void IFStreamBinary::writeString(const string& str)
01270 {
01271   // get total size, align, and check capacity
01272 
01273         int sz = (int) str.size() + CPPUtils::SINT;
01274   align4Byte();
01275   checkBufferSize(sz);
01276 
01277   // write the data
01278 
01279         writeIntNC((int) str.size());
01280   memcpy(&(*bData)[bDataPos], &str[0], str.size());
01281   bDataPos += (int) str.size();
01282 }
01283 
01289 inline void IFStreamBinary::writeString(const char* char_string)
01290 {
01291   string tmp_string = char_string;
01292   writeString(tmp_string);
01293 }
01294 
01298 inline void IFStreamBinary::writeCharArray(const char* array, int num_chars)
01299 {
01300   checkBufferSize(num_chars);
01301 
01302   // write data
01303 
01304   memcpy(&(*bData)[bDataPos], array, num_chars);
01305   bDataPos += num_chars;
01306 }
01307 
01314 inline void IFStreamBinary::writeType(const string& str)
01315 {
01316         writeString(str);
01317 }
01318 
01324 inline void IFStreamBinary::writeType(const char* char_string)
01325 {
01326         writeString(char_string);
01327 }
01328 
01329 //************* Write Bools ***************************************************
01330 
01336 inline void IFStreamBinary::writeBool(bool b)
01337 {
01338         checkBufferSize(CPPUtils::SBOL);
01339 
01340   writeBoolNC(b);
01341 }
01342 
01348 inline void IFStreamBinary::writeBoolNC(bool b)
01349 {
01350         writeBool(b, bDataPos);
01351   bDataPos += CPPUtils::SBOL;
01352 }
01353 
01359 inline void IFStreamBinary::writeBool(bool b, int pos)
01360 {
01361   *((bool*) &(*bData)[pos]) = b;
01362 }
01363 
01367 inline void IFStreamBinary::writeBoolArray(const bool* array, int num_bools)
01368 {
01369         // get number of bytes to be written and check for alignment and buffer
01370         // size
01371 
01372   num_bools *= CPPUtils::SBOL;
01373   checkBufferSize(num_bools);
01374 
01375         // copy array into data buffer. Reverse data buffer entries if necessary
01376         // and increment the buffer output pointer.
01377 
01378   memcpy(&(*bData)[bDataPos], array, num_bools);
01379   bDataPos += num_bools;
01380 }
01381 
01387 inline void IFStreamBinary::writeType(bool b)
01388 {
01389         writeBool(b);
01390 }
01391 
01395 inline void IFStreamBinary::writeTypeArray(const bool* array, int num_bools)
01396 {
01397         writeBoolArray(array, num_bools);
01398 }
01399 
01400 //************* Write Bytes ***************************************************
01401 
01407 inline void IFStreamBinary::writeByte(byte b)
01408 {
01409   checkBufferSize(CPPUtils::SBYT);
01410 
01411   writeByteNC(b);
01412 }
01413 
01419 inline void IFStreamBinary::writeByteNC(byte b)
01420 {
01421         writeByte(b, bDataPos);
01422   bDataPos += CPPUtils::SBYT;
01423 }
01424 
01430 inline void IFStreamBinary::writeByte(byte b, int pos)
01431 {
01432   *((byte*) &(*bData)[pos]) = b;
01433 }
01434 
01438 inline void IFStreamBinary::writeByteArray(const byte* array, int num_bytes)
01439 {
01440         // get number of bytes to be written and check for alignment and buffer
01441         // size
01442 
01443   num_bytes *= CPPUtils::SBYT;
01444   checkBufferSize(num_bytes);
01445 
01446         // copy array into data buffer. Reverse data buffer entries if necessary
01447         // and increment the buffer output pointer.
01448 
01449   memcpy(&(*bData)[bDataPos], array, num_bytes);
01450   bDataPos += num_bytes;
01451 }
01452 
01458 inline void IFStreamBinary::writeType(byte b)
01459 {
01460         writeByte(b);
01461 }
01462 
01466 inline void IFStreamBinary::writeTypeArray(const byte* array, int num_bytes)
01467 {
01468         writeByteArray(array, num_bytes);
01469 }
01470 
01471 //************* Write Shorts **************************************************
01472 
01478 inline void IFStreamBinary::writeShort(short s)
01479 {
01480   align2Byte();
01481   checkBufferSize(CPPUtils::SSHT);
01482 
01483   writeShortNC(s);
01484 }
01485 
01491 inline void IFStreamBinary::writeShortNC(short s)
01492 {
01493         writeShort(s, bDataPos);
01494   bDataPos += CPPUtils::SSHT;
01495 }
01496 
01502 inline void IFStreamBinary::writeShort(short s, int pos)
01503 {
01504   if (bReverse) reverseBO2((char*) &s);
01505 
01506   if (bAlign)
01507         *((short*) &(*bData)[pos]) = s;
01508   else
01509     memcpy(&(*bData)[pos], &s, CPPUtils::SSHT);
01510 }
01511 
01515 inline void IFStreamBinary::writeShortArray(const short* array, int num_shorts)
01516 {
01517         // get number of bytes to be written and check for alignment and buffer
01518         // size
01519 
01520   int num_bytes = num_shorts * CPPUtils::SSHT;
01521   align2Byte();
01522   checkBufferSize(num_bytes);
01523 
01524         // copy array into data buffer. Reverse data buffer entries if necessary
01525         // and increment the buffer output pointer.
01526 
01527   memcpy(&(*bData)[bDataPos], array, num_bytes);
01528   if (bReverse) reverseBO2Array(num_shorts, &(*bData)[bDataPos]);
01529   bDataPos += num_bytes;
01530 }
01531 
01537 inline void IFStreamBinary::writeType(short s)
01538 {
01539         writeShort(s);
01540 }
01541 
01545 inline void IFStreamBinary::writeTypeArray(const short* array, int num_shorts)
01546 {
01547         writeShortArray(array, num_shorts);
01548 }
01549 
01550 //************* Write Ints ****************************************************
01551 
01557 inline void IFStreamBinary::writeInt(int i)
01558 {
01559   align4Byte();
01560   checkBufferSize(CPPUtils::SINT);
01561 
01562   writeIntNC(i);
01563 }
01564 
01570 inline void IFStreamBinary::writeIntNC(int i)
01571 {
01572         writeInt(i, bDataPos);
01573   bDataPos += CPPUtils::SINT;
01574 }
01575 
01581 inline void IFStreamBinary::writeInt(int i, int pos)
01582 {
01583   if (bReverse) reverseBO4((char*) &i);
01584 
01585   if (bAlign)
01586         *((int*) &(*bData)[pos]) = i;
01587   else
01588     memcpy(&(*bData)[pos], &i, CPPUtils::SINT);
01589 }
01590 
01594 inline void IFStreamBinary::writeIntArray(const int* array, int num_ints)
01595 {
01596         // get number of bytes to be written and check for alignment and buffer
01597         // size
01598 
01599   int num_bytes = num_ints * CPPUtils::SINT;
01600   align4Byte();
01601   checkBufferSize(num_bytes);
01602 
01603         // copy array into data buffer. Reverse data buffer entries if necessary
01604         // and increment the buffer output pointer.
01605 
01606   memcpy(&(*bData)[bDataPos], array, num_bytes);
01607   if (bReverse) reverseBO4Array(num_ints, &(*bData)[bDataPos]);
01608   bDataPos += num_bytes;
01609 }
01610 
01616 inline void IFStreamBinary::writeType(int i)
01617 {
01618         writeInt(i);
01619 }
01620 
01624 inline void IFStreamBinary::writeTypeArray(const int* array, int num_ints)
01625 {
01626         writeIntArray(array, num_ints);
01627 }
01628 
01629 //************* Write Longs ***************************************************
01630 
01636 inline void IFStreamBinary::writeLong(LONG_INT l)
01637 {
01638   align8Byte();
01639   checkBufferSize(CPPUtils::SLNG);
01640 
01641   writeLongNC(l);
01642 }
01643 
01649 inline void IFStreamBinary::writeLongNC(LONG_INT l)
01650 {
01651         writeLong(l, bDataPos);
01652   bDataPos += CPPUtils::SLNG;
01653 }
01654 
01660 inline void IFStreamBinary::writeLong(LONG_INT l, int pos)
01661 {
01662   if (bReverse) reverseBO8((char*) &l);
01663 
01664   if (bAlign)
01665         *((LONG_INT*) &(*bData)[pos]) = l;
01666   else
01667     memcpy(&(*bData)[pos], &l, CPPUtils::SLNG);
01668 }
01669 
01673 inline void IFStreamBinary::writeLongArray(const LONG_INT* array, int num_longs)
01674 {
01675         // get number of bytes to be written and check for alignment and buffer
01676         // size
01677 
01678   int num_bytes = num_longs * CPPUtils::SLNG;
01679   align8Byte();
01680   checkBufferSize(num_bytes);
01681 
01682         // copy array into data buffer. Reverse data buffer entries if necessary
01683         // and increment the buffer output pointer.
01684 
01685   memcpy(&(*bData)[bDataPos], array, num_bytes);
01686   if (bReverse) reverseBO8Array(num_longs, &(*bData)[bDataPos]);
01687   bDataPos += num_bytes;
01688 }
01689 
01695 inline void IFStreamBinary::writeType(LONG_INT l)
01696 {
01697         writeLong(l);
01698 }
01699 
01703 inline void IFStreamBinary::writeTypeArray(const LONG_INT* array, int num_longs)
01704 {
01705         writeLongArray(array, num_longs);
01706 }
01707 
01708 //************* Write Floats **************************************************
01709 
01715 inline void IFStreamBinary::writeFloat(float f)
01716 {
01717   align4Byte();
01718   checkBufferSize(CPPUtils::SFLT);
01719 
01720   writeFloatNC(f);
01721 }
01722 
01728 inline void IFStreamBinary::writeFloatNC(float f)
01729 {
01730         writeFloat(f, bDataPos);
01731   bDataPos += CPPUtils::SFLT;
01732 }
01733 
01739 inline void IFStreamBinary::writeFloat(float f, int pos)
01740 {
01741   if (bReverse) reverseBO4((char*) &f);
01742 
01743   if (bAlign)
01744         *((float*) &(*bData)[pos]) = f;
01745   else
01746     memcpy(&(*bData)[pos], &f, CPPUtils::SFLT);
01747 }
01748 
01752 inline void IFStreamBinary::writeFloatArray(const float* array, int num_floats)
01753 {
01754         // get number of bytes to be written and check for alignment and buffer
01755         // size
01756 
01757   int num_bytes = num_floats * CPPUtils::SFLT;
01758   align4Byte();
01759   checkBufferSize(num_bytes);
01760 
01761         // copy array into data buffer. Reverse data buffer entries if necessary
01762         // and increment the buffer output pointer.
01763 
01764   memcpy(&(*bData)[bDataPos], array, num_bytes);
01765   if (bReverse) reverseBO4Array(num_floats, &(*bData)[bDataPos]);
01766   bDataPos += num_bytes;
01767 }
01768 
01774 inline void IFStreamBinary::writeType(float f)
01775 {
01776         writeFloat(f);
01777 }
01778 
01782 inline void IFStreamBinary::writeTypeArray(const float* array, int num_floats)
01783 {
01784         writeFloatArray(array, num_floats);
01785 }
01786 
01787 //************* Write Doubles *************************************************
01788 
01794 inline void IFStreamBinary::writeDouble(double d)
01795 {
01796   align8Byte();
01797         checkBufferSize(CPPUtils::SDBL);
01798 
01799   writeDoubleNC(d);
01800 }
01801 
01807 inline void IFStreamBinary::writeDoubleNC(double d)
01808 {
01809         writeDouble(d, bDataPos);
01810   bDataPos += CPPUtils::SDBL;
01811 }
01812 
01818 inline void IFStreamBinary::writeDouble(double d, int pos)
01819 {
01820   if (bReverse) reverseBO8((char*) &d);
01821 
01822   if (bAlign)
01823         *((double*) &(*bData)[pos]) = d;
01824   else
01825     memcpy(&(*bData)[pos], &d, CPPUtils::SDBL);
01826 }
01827 
01831 inline void IFStreamBinary::writeDoubleArray(const double* array, int num_doubles)
01832 {
01833         // get number of bytes to be written and check for alignment and buffer
01834         // size
01835 
01836   int num_bytes = num_doubles * CPPUtils::SDBL;
01837   align8Byte();
01838   checkBufferSize(num_bytes);
01839 
01840         // copy array into data buffer. Reverse data buffer entries if necessary
01841         // and increment the buffer output pointer.
01842 
01843   memcpy(&(*bData)[bDataPos], array, num_bytes);
01844   if (bReverse) reverseBO8Array(num_doubles, &(*bData)[bDataPos]);
01845   bDataPos += num_bytes;
01846 }
01847 
01853 inline void IFStreamBinary::writeType(double d)
01854 {
01855         writeDouble(d);
01856 }
01857 
01861 inline void IFStreamBinary::writeTypeArray(const double* array, int num_doubles)
01862 {
01863         writeDoubleArray(array, num_doubles);
01864 }
01865 
01866 //************* Byte reversal functions ***************************************
01867 
01871 inline void IFStreamBinary::reverseBO2(char* d)
01872 {
01873   char tmp = d[0];
01874   d[0] = d[1];
01875   d[1] = tmp;
01876 }
01877 
01881 inline void IFStreamBinary::reverseBO4(char* d)
01882 {
01883   char tmp = d[0];
01884   d[0] = d[3];
01885   d[3] = tmp;
01886 
01887   tmp = d[1];
01888   d[1] = d[2];
01889   d[2] = tmp;
01890 }
01891 
01895 inline void IFStreamBinary::reverseBO8(char* d)
01896 {
01897   char tmp = d[0];
01898   d[0] = d[7];
01899   d[7] = tmp;
01900 
01901   tmp = d[1];
01902   d[1] = d[6];
01903   d[6] = tmp;
01904 
01905   tmp = d[2];
01906   d[2] = d[5];
01907   d[5] = tmp;
01908 
01909   tmp = d[3];
01910   d[3] = d[4];
01911   d[4] = tmp;
01912 }
01913 
01914 } // end namespace geotess
01915 
01916 #endif  // IFSTREAMBINARY_OBJECT_H