00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _VPR_BUFFER_OBJECT_READER_H
00043 #define _VPR_BUFFER_OBJECT_READER_H
00044
00045 #include <vpr/vprConfig.h>
00046
00047 #include <vector>
00048 #include <boost/static_assert.hpp>
00049 #include <boost/concept_check.hpp>
00050
00051 #include <vpr/IO/ObjectReader.h>
00052 #include <vpr/Util/Assert.h>
00053 #include <vpr/System.h>
00054
00055
00056 namespace vpr
00057 {
00058
00063 class BufferObjectReader : public ObjectReader
00064 {
00065 public:
00066 BufferObjectReader(std::vector<vpr::Uint8>* data, unsigned curPos=0)
00067 {
00068 mIsBinary = true;
00069 mData = data;
00070 mCurHeadPos = curPos;
00071 }
00072
00073 void setCurPos(unsigned val)
00074 { mCurHeadPos = val; }
00075 unsigned getCurPos()
00076 { return mCurHeadPos; }
00077
00079 virtual void resetReading()
00080 { setCurPos(0); }
00081
00082 virtual void pushState()
00083 { mHeadPosStateStack.push_back(mCurHeadPos); }
00084 virtual void popState()
00085 {
00086 unsigned new_head_pos = mHeadPosStateStack.back();
00087 mHeadPosStateStack.pop_back();
00088 setCurPos(new_head_pos);
00089 }
00090
00091
00096 virtual vpr::ReturnStatus beginTag(std::string tagName)
00097 {
00098 boost::ignore_unused_variable_warning(tagName);
00099 return vpr::ReturnStatus::Succeed;
00100 }
00101
00103 virtual vpr::ReturnStatus endTag()
00104 {
00105 return vpr::ReturnStatus::Succeed;
00106 }
00107
00109 virtual vpr::ReturnStatus beginAttribute(std::string attributeName)
00110 {
00111 boost::ignore_unused_variable_warning(attributeName);
00112 return vpr::ReturnStatus::Succeed;
00113 }
00114
00116 virtual vpr::ReturnStatus endAttribute()
00117 {
00118 return vpr::ReturnStatus::Succeed;
00119 }
00121
00122
00123 inline virtual vpr::Uint8 readUint8();
00124 inline virtual vpr::Uint16 readUint16();
00125 inline virtual vpr::Uint32 readUint32();
00126 inline virtual vpr::Uint64 readUint64();
00127 inline virtual float readFloat();
00128 inline virtual double readDouble();
00129 inline virtual std::string readString();
00130 inline virtual bool readBool();
00131
00132
00133 virtual void readUint8(vpr::Uint8& val)
00134 { val = this->readUint8(); }
00135 virtual void readUint16(vpr::Uint16& val)
00136 { val = this->readUint16(); }
00137 virtual void readUint32(vpr::Uint32& val)
00138 { val = this->readUint32(); }
00139 virtual void readUint64(vpr::Uint64& val)
00140 { val = this->readUint64(); }
00141 virtual void readFloat(float& val)
00142 { val = this->readFloat(); }
00143 virtual void readDouble(double& val)
00144 { val = this->readDouble(); }
00145 virtual void readString(std::string& str)
00146 { str = this->readString(); }
00147 virtual void readBool(bool& val)
00148 { val = this->readBool(); }
00149
00150
00151
00152
00153
00154
00155 inline vpr::Uint8* readRaw(unsigned len=1);
00156
00157 public:
00158 std::vector<vpr::Uint8>* mData;
00159 unsigned mCurHeadPos;
00160 std::vector<unsigned> mHeadPosStateStack;
00161 };
00162
00163
00164
00165
00166 vpr::Uint8 BufferObjectReader::readUint8()
00167 {
00168 vpr::Uint8 temp_data;
00169 memcpy(&temp_data, readRaw(1), 1);
00170 return temp_data;
00171 }
00172
00173 vpr::Uint16 BufferObjectReader::readUint16()
00174 {
00175 vpr::Uint16 nw_val;
00176 memcpy(&nw_val, readRaw(2), 2);
00177
00178 return vpr::System::Ntohs(nw_val);
00179 }
00180
00181 vpr::Uint32 BufferObjectReader::readUint32()
00182 {
00183 vpr::Uint32 nw_val;
00184 memcpy(&nw_val, readRaw(4), 4);
00185
00186 return vpr::System::Ntohl(nw_val);
00187 }
00188
00189 vpr::Uint64 BufferObjectReader::readUint64()
00190 {
00191 vpr::Uint64 nw_val;
00192 memcpy(&nw_val, readRaw(8), 8);
00193 vpr::Uint64 h_val = vpr::System::Ntohll(nw_val);
00194
00195 return h_val;
00196 }
00197
00198 float BufferObjectReader::readFloat()
00199 {
00200
00201 BOOST_STATIC_ASSERT(sizeof(float) == 4);
00202
00203 vpr::Uint32 nw_val;
00204 memcpy(&nw_val, readRaw(4), 4);
00205 vpr::Uint32 h_val = vpr::System::Ntohl(nw_val);
00206
00207 return *((float*)&h_val);
00208 }
00209
00210 double BufferObjectReader::readDouble()
00211 {
00212
00213 BOOST_STATIC_ASSERT(sizeof(double) == 8);
00214
00215 vpr::Uint64 nw_val;
00216 memcpy(&nw_val, readRaw(8), 8);
00217 vpr::Uint64 h_val = vpr::System::Ntohll(nw_val);
00218 double d_val = *((double*)&h_val);
00219
00220 return d_val;
00221 }
00222
00223
00224 std::string BufferObjectReader::readString()
00225 {
00226 vpr::Uint16 str_len = readUint16();
00227 std::string ret_val;
00228 char tempChar;
00229 for(unsigned i=0; i<str_len;++i)
00230 {
00231 tempChar = (char)(*readRaw(1));
00232 ret_val += tempChar;
00233 }
00234 return ret_val;
00235 }
00236
00237 bool BufferObjectReader::readBool()
00238 {
00239 return (bool)*(readRaw(1));
00240 }
00241
00242
00243 vpr::Uint8* BufferObjectReader::readRaw(unsigned len)
00244 {
00245 mCurHeadPos += len;
00246 vprASSERT((mCurHeadPos-len) < mData->size());
00247
00248 return &((*mData)[mCurHeadPos-len]);
00249 }
00250
00251 }
00252
00253 #endif