- Timestamp:
- Mar 7, 2006, 1:48:05 PM (19 years ago)
- Location:
- trunk/src/mesh
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mesh/grid1d.cc
r271 r281 4 4 5 5 #include "grid1d.h" 6 #include "byte_order.h"7 6 8 7 … … 68 67 } 69 68 70 // serialize object 71 // 1 byte: tag indicating if data is uuencoded 72 // 1 byte: tag indicating compression algorithm (N: NONE, Z: zlib) 73 // 4 bytes: number of points in array 74 // rest: data array (x x x...) 75 // 76 // TODO - handling Endianess 69 // 70 // serialize Grid1d object 71 // 16 bytes: Header (including RV identifier, version, object type id) 72 // e.g., RV-A-FIELD, RV-A-MESH3D 73 // 4 bytes: N: number of bytes in object name (to follow) 74 // N bytes: object name (e.g., "output.grid(g1d))" 75 // 4 bytes: number of points in array 76 // rest: data array (x x x...) 77 77 // 78 78 char* 79 RpGrid1d::serialize(int& nb, RP_ENCODE_ALG encodeFlag, 80 RP_COMPRESSION compressFlag) 81 { 82 int npts = m_data.size(); 83 int nbytes = npts*sizeof(DataValType) + sizeof(int) + 2; 79 RpGrid1d::serialize(int& nb) 80 { 84 81 char* buf; 82 int nbytes = numBytes(); 85 83 86 84 // total length = tagEncode + tagCompress + num + array data … … 91 89 } 92 90 93 serialize(buf, nbytes, encodeFlag, compressFlag); 91 doSerialize(buf, nbytes); 92 94 93 nb = nbytes; 95 94 … … 97 96 } 98 97 98 // 99 // serialize object data in Little Endian byte order 100 // 99 101 RP_ERROR 100 RpGrid1d::serialize(char* buf, int nbytes, RP_ENCODE_ALG encodeFlag, 101 RP_COMPRESSION compressFlag) 102 { 103 int npts = m_data.size(); 104 105 if (buf == NULL || (unsigned)nbytes < (npts*sizeof(DataValType)+sizeof(int)+2)) { 102 RpGrid1d::doSerialize(char* buf, int nbytes) 103 { 104 // check buffer 105 if (buf == NULL || nbytes < numBytes()) { 106 106 RpAppendErr("RpGrid1d::serialize: invalid buffer"); 107 107 RpPrintErr(); 108 108 return RP_ERR_INVALID_ARRAY; 109 109 } 110 111 char* ptr = buf; 112 ptr[0] = 'N'; // init to no-encoding 113 switch(encodeFlag) { 114 case RP_UUENCODE: 115 ptr[0] = 'U'; 116 break; 117 case RP_NO_ENCODING: 118 default: 119 break; 120 } 121 122 ptr[1] = 'N'; // init to no compression 123 switch(compressFlag) { 124 case RP_ZLIB: 125 ptr[1] = 'Z'; 126 break; 127 case RP_NO_COMPRESSION: 128 default: 129 break; 130 } 131 ptr += 2; // advance pointer 132 133 // TODO encode, compression 134 // compress() 135 136 // write to stream buffer 137 //memcpy((void*)ptr, (void*)&npts, sizeof(int)); 110 111 char * ptr = buf; 112 113 // prepare header 114 // fill in blanks if version string is shorter than HEADER_SIZE 115 116 int len = strlen(Grid1d_current_version); 117 std::string header(' ', HEADER_SIZE); 118 header = Grid1d_current_version; 119 header.append(HEADER_SIZE-len, ' '); 120 121 // copy header 122 const char* cptr = header.c_str(); 123 ByteOrder<char>::OrderCopyArray(cptr, (char*)ptr, HEADER_SIZE); 124 ptr += HEADER_SIZE; 125 126 // copy total number of bytes: nbytes 127 int* iptr = (int*)ptr; 128 ByteOrder<int>::OrderCopy(&nbytes, iptr); 129 ptr += sizeof(int); 130 131 // copy length of name 132 len = m_name.size(); 133 iptr = (int*)ptr; 134 ByteOrder<int>::OrderCopy(&len, iptr); 135 ptr += sizeof(int); 136 137 // copy name as chars 138 cptr = m_name.c_str(); 139 ByteOrder<char>::OrderCopyArray(cptr, (char*)ptr, len); 140 ptr += len; 141 142 // copy int (number of points) 143 int npts = m_data.size(); 138 144 139 145 // copy int to byte stream in LE byte order 140 i nt* iptr = (int*)ptr;146 iptr = (int*)ptr; 141 147 ByteOrder<int>::OrderCopy(&npts, iptr); 142 148 ptr += sizeof(int); 143 149 144 // copy data to byte stream in LE byte order 145 //memcpy((void*)ptr, (void*)&(m_data[0]), npts*sizeof(DataValType)); 150 // copy data 146 151 147 152 DataValType* dptr = (DataValType*)ptr; … … 154 159 RpGrid1d::deserialize(const char* buf) 155 160 { 156 int npts;157 158 161 if (buf == NULL) { 159 162 RpAppendErr("RpGrid1d::deserialize: null buf pointer"); … … 162 165 } 163 166 164 // TODO: handle encoding, decompression 165 166 buf += 2; // skip 1st 2 bytes 167 int * iptr = (int*)buf; 167 char* ptr = (char*)buf; 168 169 // read header 170 char header[HEADER_SIZE]; 171 ByteOrder<char>::OrderCopyArray(ptr, header, HEADER_SIZE); 172 filterTrailingBlanks(header, HEADER_SIZE); 173 174 ptr += HEADER_SIZE; 175 176 // read total number of bytes 177 int* iptr = (int*)ptr; 178 int nbytes; 179 ByteOrder<int>::OrderCopy(iptr, &nbytes); 180 ptr += sizeof(int); 181 182 if (!strcmp(header, Grid1d_current_version) ) 183 return doDeserialize(ptr); 184 185 // deal with older versions 186 return RP_FAILURE; 187 } 188 189 // 190 // parse out the buffer, 191 // stripped off the version and total #bytes already. 192 // 193 RP_ERROR 194 RpGrid1d::doDeserialize(const char* buf) 195 { 196 char* ptr = (char*)buf; 197 int num; 198 199 // copy length of name 200 201 ByteOrder<int>::OrderCopy((int*)ptr, &num); 202 203 ptr += sizeof(int); 204 205 // copy name as chars 206 char* cstr = new char[num]; ; 207 ByteOrder<char>::OrderCopyArray(ptr, cstr, num); 208 filterTrailingBlanks(cstr, num); 209 m_name.assign(cstr); 210 211 delete cstr; 212 213 ptr += num; 168 214 169 215 // read number of points 170 //memcpy((void*)&npts, (void*)buf, sizeof(int)); 171 172 // copy int in buf to nptrs 173 // use ByteOrder::Copy to swap bytes if on a big endian machine 216 int* iptr = (int*)ptr; 217 int npts; 174 218 ByteOrder<int>::OrderCopy(iptr, &npts); 175 buf+= sizeof(int);219 ptr += sizeof(int); 176 220 177 221 // set the array to be the right size … … 179 223 m_data.resize(npts); 180 224 181 // straight copy182 //memcpy((void*)&(m_data[0]), (void*)buf, npts*sizeof(DataValType));183 184 225 // copy points array - use ByteOrder copy 185 DataValType* dptr = (DataValType*) buf;226 DataValType* dptr = (DataValType*)ptr; 186 227 ByteOrder<DataValType>::OrderCopyArray(dptr, &(m_data[0]), npts); 187 228 … … 189 230 } 190 231 232 // 233 // return pointer to data points 234 // 191 235 DataValType* 192 RpGrid1d::data() 236 RpGrid1d::getData() 237 { 238 return &m_data[0]; 239 } 240 241 // 242 // return pointer to a copy of data points in consecutive memory locations 243 // 244 DataValType* 245 RpGrid1d::getDataCopy() 193 246 { 194 247 int npts = numPoints(); … … 207 260 } 208 261 262 // 263 // mashalling object into xml string 264 // 209 265 void 210 266 RpGrid1d::xmlString(std::string& textString) … … 226 282 } 227 283 284 // 285 // print the xml string from the object 286 // 228 287 void 229 288 RpGrid1d::print() … … 233 292 xmlString(str); 234 293 294 printf("object name: %s", m_name.c_str()); 235 295 printf("%s", str.c_str()); 236 296 } 237 297 238 239 // TODO 240 //int RpGrid1d::xmlPut() { }; 241 //int RpGrid1d::xmlGet() { }; 242 298 // 299 // set object name from a charactor string 300 // 301 void 302 RpGrid1d::objectName(const char* str) 303 { 304 m_name = str; 305 } 306 307 // 308 // get object name 309 // 310 const char* RpGrid1d::objectName() 311 { 312 return m_name.c_str(); 313 } 314 315 // 316 // get object type 317 // 318 const char* RpGrid1d::objectType() 319 { 320 return RpObjectTypes[GRID1D]; 321 } 322 323 // 324 // return total number of bytes when mashalling out as bytes 325 // 326 int RpGrid1d::numBytes() 327 { 328 int nbytes = HEADER_SIZE 329 + sizeof(int) // total #bytes 330 + sizeof(int) // #bytes in name 331 + m_name.size() 332 + sizeof(int) // #points in grid 333 + m_data.size() * sizeof(DataValType); 334 335 return nbytes; 336 } 337 -
trunk/src/mesh/grid1d.h
r279 r281 7 7 8 8 #include <vector> 9 #include <string> 9 10 #include "serializable.h" 11 #include "byte_order.h" 10 12 #include "util.h" 11 12 #define GRID_1D_VERSION "RV-Grid1d-A" 13 #include "obj_types.h" 13 14 14 15 typedef double DataValType; … … 22 23 RpGrid1d(const char* buf); // instantiate with byte stream 23 24 25 virtual void objectName(const char* str); 26 virtual const char* objectName(); 27 virtual const char* objectType(); 28 29 // return number of bytes needed for serialization 30 virtual int numBytes(); 31 32 // return number of points in grid 33 virtual int size() { return numPoints(); }; 34 35 24 36 // add all points to grid 25 37 RP_ERROR addAllPoints(DataValType* val, int nitems); … … 29 41 30 42 // return number of points in grid 31 int numPoints() { return m_data.size(); };32 33 int size() { return numPoints(); };34 35 // return number of bytes needed for serialization36 int numBytes();37 38 // return array of doubles - user must free memory39 // when not needed anymore40 DataValType* data();41 43 42 44 // max num points that can be stored 43 45 int capacity() { return m_data.capacity(); } 44 46 47 45 48 // change the size of the grid after grid is constructed 46 49 void resize(int npoints) { m_data.resize(npoints); } 47 50 51 // return pointer to array of points 52 DataValType* getData(); 53 54 // return copy of data array - caller must free memory when 55 // not done 56 DataValType* getDataCopy(); 57 48 58 // serialize data 49 // If no input parameters are provided, byte stream will be binary50 // without compression.59 // returns pointer to buffer 60 // number of bytes (nbytes) set 51 61 // 52 char * serialize(int& nbytes, RP_ENCODE_ALG eflag=RP_NO_ENCODING,53 RP_COMPRESSION cflag=RP_NO_COMPRESSION);62 virtual char * serialize(int& nbytes); 63 RP_ERROR doSerialize(char * buf, int nbytes); 54 64 55 RP_ERROR serialize(char * buf, int nbytes, 56 RP_ENCODE_ALG eflag=RP_NO_ENCODING, 57 RP_COMPRESSION cflag=RP_NO_COMPRESSION); 58 65 // deserialize data 59 66 RP_ERROR deserialize(const char* buf); 67 RP_ERROR doDeserialize(const char* buf); 60 68 61 69 void xmlString(std::string& str); … … 65 73 virtual ~RpGrid1d() { }; 66 74 67 // TODO 68 //virtual int xmlPut() { }; 69 //virtual int xmlGet() { }; 75 protected: 76 int numPoints() { return m_data.size(); }; 70 77 71 78 private: 79 std::string m_name; // object name 72 80 vector<DataValType> m_data; // array of doubles 73 81 };
Note: See TracChangeset
for help on using the changeset viewer.