Changeset 797
- Timestamp:
- 20/07/06 20:07:47 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/applications/NXtranslate/binary/BinaryRetriever.cpp
r796 r797 14 14 using std::endl; 15 15 16 typedef uint32_t data_t; 17 16 static const string INT8("INT8"); 17 static const string INT16("INT16"); 18 static const string INT32("INT32"); 19 static const string UINT8("UINT8"); 20 static const string UINT16("UINT16"); 21 static const string UINT32("UINT32"); 22 static const string FLOAT32("FLOAT32"); 23 static const string FLOAT64("FLOAT64"); 24 static const string BYTE("BYTE"); 25 26 static const int DEFAULT_TYPE=NX_UINT32; 18 27 19 28 /** … … 80 89 81 90 /** 91 * This function turns the type as a string into an integer for use 92 * with NeXus. 93 */ 94 static int getDataType(const string &str_type){ 95 if(str_type.empty()){ 96 return DEFAULT_TYPE; 97 }else if(str_type==INT8){ 98 return NX_INT8; 99 }else if(str_type==INT16){ 100 return NX_INT16; 101 }else if(str_type==INT32){ 102 return NX_INT32; 103 }else if(str_type==UINT8){ 104 return NX_UINT8; 105 }else if(str_type==UINT16){ 106 return NX_UINT16; 107 }else if(str_type==UINT32){ 108 return NX_UINT32; 109 }else if(str_type==FLOAT32){ 110 return NX_FLOAT32; 111 }else if(str_type==FLOAT64){ 112 return NX_FLOAT64; 113 }else if(str_type==BYTE){ 114 return NX_CHAR; 115 }else{ 116 throw invalid_argument("Invalid type: "+str_type); 117 } 118 } 119 120 /** 121 * This function turns the type as a string into a platform dependent 122 * size. 123 */ 124 static size_t getDataTypeSize(const int type){ 125 if(type==NX_INT8){ 126 return sizeof(int8_t); 127 }else if(type==NX_INT16){ 128 return sizeof(int16_t); 129 }else if(type==NX_INT32){ 130 return sizeof(int32_t); 131 }else if(type==NX_UINT8){ 132 return sizeof(uint8_t); 133 }else if(type==NX_UINT16){ 134 return sizeof(uint16_t); 135 }else if(type==NX_UINT32){ 136 return sizeof(uint32_t); 137 }else if(type==NX_FLOAT32){ 138 return sizeof(float); 139 }else if(type==NX_FLOAT64){ 140 return sizeof(double); 141 }else if(type==NX_CHAR){ 142 return sizeof(char); 143 }else{ 144 throw invalid_argument("This statement should never be reached"); 145 } 146 } 147 148 /** 82 149 * This is the method for retrieving data from a file. The string must 83 150 * be of the form "type:offset,delta,number". … … 95 162 } 96 163 164 // break the location string into a type and sizing information 165 int type; 166 string sizing; 167 { 168 vector<string> temp=string_util::split(location,":"); 169 cout << "SIZE=" << temp.size() << endl; 170 if(temp.size()==1){ 171 type=getDataType(""); 172 sizing=location; 173 }else if(temp.size()==2){ 174 type=getDataType(temp[0]); 175 sizing=temp[1]; 176 }else{ 177 throw invalid_argument("can only specify one type in location string"); 178 } 179 cout << "\"" << type << "\" \"" << sizing << "\"" << endl; 180 } 181 97 182 // break the location string into three parts: file_size,data_start,data_size 98 183 string file_size_str; … … 100 185 string size_str; 101 186 { 102 vector<string> temp=string_util::split( location,"][");187 vector<string> temp=string_util::split(sizing,"]["); 103 188 if(temp.size()!=3) 104 189 { … … 163 248 // allocate the space for the result 164 249 void *data; 165 if(NXmalloc(&data,rank,dims,NX_UINT32)!=NX_OK) 250 cout << "TYPE=" << type << endl; 251 if(NXmalloc(&data,rank,dims,type)!=NX_OK) 166 252 { 167 253 throw runtime_error("NXmalloc failed"); … … 169 255 170 256 // prepare data buffer 171 size_t data_size= sizeof(data_t);257 size_t data_size=getDataTypeSize(type); 172 258 /* 173 259 size_t num_items=1; … … 204 290 size_t num_items=*(size.rbegin()); 205 291 size_t buffer_size=num_items*data_size; 206 data_t data_buffer[num_items];292 char data_buffer[buffer_size]; 207 293 208 294 // push through the file grabbing the proper bits 209 295 scalar_position=data_size*calculate_position(file_size,pos); 210 296 data_file.seekg(scalar_position,std::ios::beg); 211 data_file.read( reinterpret_cast<char *>(data_buffer),buffer_size);297 data_file.read(data_buffer,buffer_size); 212 298 213 299 // copy into final array 214 memcpy((static_cast< data_t *>(data))+data_index,data_buffer,buffer_size);300 memcpy((static_cast<char *>(data))+data_index*data_size,data_buffer,buffer_size); 215 301 data_index+=num_items; 216 302 … … 222 308 data_file.read(reinterpret_cast<char *>(data_buffer),buffer_size); 223 309 // copy into final array 224 memcpy((static_cast< data_t *>(data))+data_index,data_buffer,buffer_size);310 memcpy((static_cast<char *>(data))+data_index*data_size,data_buffer,buffer_size); 225 311 data_index+=num_items; 226 312 }
Note: See TracChangeset
for help on using the changeset viewer.
