#include <dm3io.h>
Collaboration diagram for EMAN::Gatan::TagData:
Public Types | |
UNKNOWN = 0 | |
SHORT = 2 | |
INT = 3 | |
USHORT = 4 | |
UINT = 5 | |
FLOAT = 6 | |
DOUBLE = 7 | |
BOOLEAN = 8 | |
CHAR = 9 | |
OCTET = 10 | |
STRUCT = 15 | |
STRING = 18 | |
ARRAY = 20 | |
enum | Type { UNKNOWN = 0, SHORT = 2, INT = 3, USHORT = 4, UINT = 5, FLOAT = 6, DOUBLE = 7, BOOLEAN = 8, CHAR = 9, OCTET = 10, STRUCT = 15, STRING = 18, ARRAY = 20 } |
Public Member Functions | |
TagData (FILE *data_file, TagTable *tagtable, const string &tagname) | |
~TagData () | |
int | read (bool nodata=false) |
Private Member Functions | |
size_t | typesize () const |
size_t | typesize (int type) const |
int | read_any (bool nodata=false) |
vector< int > | read_array_types () |
int | read_array_data (vector< int >item_types, bool nodata=false) |
vector< int > | read_struct_types () |
string | read_native (bool is_value_stored) |
string | read_string (int size) |
Private Attributes | |
FILE * | in |
TagTable * | tagtable |
string | name |
Type | tag_type |
Definition at line 97 of file dm3io.h.
TagData::TagData | ( | FILE * | data_file, | |
TagTable * | tagtable, | |||
const string & | tagname | |||
) |
int TagData::read | ( | bool | nodata = false |
) |
Definition at line 442 of file dm3io.cpp.
References EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, and read_any().
Referenced by EMAN::Gatan::TagEntry::read().
00443 { 00444 LOGVAR("TagData::read()"); 00445 int err = 0; 00446 00447 const char *DATA_TYPE_MARK = "%%%%"; 00448 const size_t mark_sz = strlen(DATA_TYPE_MARK); 00449 char *mark = new char[mark_sz + 1]; 00450 fread(mark, mark_sz, 1, in); 00451 mark[mark_sz] = '\0'; 00452 00453 if (strcmp(mark, DATA_TYPE_MARK) != 0) { 00454 LOGERR("data type label has been changed from '%s' to '%s'", 00455 DATA_TYPE_MARK, mark); 00456 return 1; 00457 } 00458 00459 if( mark ) 00460 { 00461 delete[]mark; 00462 mark = 0; 00463 } 00464 00465 int encoded_types_size = 0; 00466 fread(&encoded_types_size, sizeof(int), 1, in); 00467 ByteOrder::become_big_endian(&encoded_types_size); 00468 00469 LOGVAR("encoded types size = %d\n", encoded_types_size); 00470 00471 err = read_any(nodata); 00472 00473 return err; 00474 }
int TagData::read_any | ( | bool | nodata = false |
) | [private] |
Definition at line 394 of file dm3io.cpp.
References EMAN::Gatan::TagTable::add(), ARRAY, EMAN::ByteOrder::become_big_endian(), in, LOGVAR, name, read_array_data(), read_array_types(), read_native(), read_struct_types(), STRING, STRUCT, tag_type, tagtable, and EMAN::Gatan::to_str().
Referenced by read().
00395 { 00396 int err = 0; 00397 00398 fread(&tag_type, sizeof(tag_type), 1, in); 00399 ByteOrder::become_big_endian(&tag_type); 00400 LOGVAR("tag type = '%s'\n", Gatan::to_str((Type) tag_type)); 00401 00402 if (tag_type == ARRAY) { 00403 vector < int >item_types = read_array_types(); 00404 err = read_array_data(item_types, nodata); 00405 } 00406 else if (tag_type == STRUCT) { 00407 vector < int >field_types = read_struct_types(); 00408 00409 for (unsigned int i = 0; i < field_types.size(); i++) { 00410 tag_type = static_cast < Type > (field_types[i]); 00411 string val = read_native(false); 00412 char int_str[32]; 00413 sprintf(int_str, " #%d", i); 00414 string fieldname = name + string(int_str); 00415 tagtable->add(fieldname, val); 00416 } 00417 } 00418 else if (tag_type == STRING) { 00419 int str_sz = 0; 00420 fread(&str_sz, sizeof(str_sz), 1, in); 00421 ByteOrder::become_big_endian(&str_sz); 00422 00423 char *val = new char[str_sz + 1]; 00424 fread(val, str_sz, 1, in); 00425 val[str_sz] = '\0'; 00426 string val_str = string(val); 00427 if( val ) 00428 { 00429 delete[]val; 00430 val = 0; 00431 } 00432 00433 tagtable->add(name, val_str); 00434 } 00435 else { 00436 read_native(true); 00437 } 00438 00439 return err; 00440 }
int TagData::read_array_data | ( | vector< int > | item_types, | |
bool | nodata = false | |||
) | [private] |
Definition at line 301 of file dm3io.cpp.
References EMAN::Gatan::TagTable::add(), EMAN::Gatan::TagTable::add_data(), EMAN::ByteOrder::become_big_endian(), EMAN::Gatan::TagTable::become_host_endian(), data, ENTERFUNC, EXITFUNC, in, LOGERR, LOGVAR, name, portable_fseek(), read_string(), tagtable, typesize(), and USHORT.
Referenced by read_any().
00302 { 00303 ENTERFUNC; 00304 if (item_types.size() == 0) { 00305 LOGERR("DM3 item types cannot be empty"); 00306 return 1; 00307 } 00308 00309 int err = 0; 00310 int array_size = 0; 00311 00312 fread(&array_size, sizeof(array_size), 1, in); 00313 ByteOrder::become_big_endian(&array_size); 00314 00315 LOGVAR("array size = %d\n", array_size); 00316 00317 size_t item_size = 0; 00318 for (size_t i = 0; i < item_types.size(); i++) { 00319 item_size += typesize(item_types[i]); 00320 } 00321 00322 LOGVAR("%s array item size = %d\n", name.c_str(), item_size); 00323 00324 size_t buf_size = item_size * array_size; 00325 00326 if (item_types.size() == 1 && item_types[0] == USHORT) { 00327 string val = read_string(array_size); 00328 tagtable->add(name, val); 00329 LOGVAR("value: %s", val.c_str()); 00330 } 00331 else if (!nodata && name == "Data") { 00332 char *data = new char[buf_size]; 00333 fread(data, buf_size, 1, in); 00334 00335 if (item_size == sizeof(short)) { 00336 tagtable->become_host_endian((short *) data, array_size); 00337 } 00338 else if (item_size == sizeof(int)) { 00339 tagtable->become_host_endian((int *) data, array_size); 00340 } 00341 else if (item_size == sizeof(double)) { 00342 tagtable->become_host_endian((double *) data, array_size); 00343 } 00344 else { 00345 LOGERR("cannot handle this type of DM3 image data"); 00346 return 1; 00347 } 00348 00349 tagtable->add_data(data); 00350 } 00351 else { 00352 portable_fseek(in, buf_size, SEEK_CUR); 00353 } 00354 EXITFUNC; 00355 return err; 00356 }
vector< int > TagData::read_array_types | ( | ) | [private] |
Definition at line 238 of file dm3io.cpp.
References ARRAY, EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, read_struct_types(), STRUCT, and EMAN::Gatan::to_str().
Referenced by read_any().
00239 { 00240 LOGVAR("TagData::read_array_types()"); 00241 00242 int array_type = 0; 00243 fread(&array_type, sizeof(array_type), 1, in); 00244 00245 ByteOrder::become_big_endian(&array_type); 00246 00247 LOGVAR("array data type = '%s'", Gatan::to_str((Type) array_type)); 00248 00249 vector < int >item_types; 00250 00251 if (array_type == STRUCT) { 00252 item_types = read_struct_types(); 00253 } 00254 else if (array_type == ARRAY) { 00255 item_types = read_array_types(); 00256 LOGERR("DM3: don't know how to handle this array type"); 00257 } 00258 else { 00259 item_types.push_back(array_type); 00260 } 00261 00262 return item_types; 00263 }
string TagData::read_native | ( | bool | is_value_stored | ) | [private] |
Definition at line 171 of file dm3io.cpp.
References EMAN::Gatan::TagTable::add(), EMAN::Gatan::TagTable::become_host_endian(), BOOLEAN, CHAR, DOUBLE, FLOAT, in, INT, LOGERR, LOGVAR, name, OCTET, SHORT, tag_type, tagtable, typesize(), UINT, and USHORT.
Referenced by read_any().
00172 { 00173 size_t sz = typesize(); 00174 char val_str[32]; 00175 00176 if (tag_type == SHORT) { 00177 short val = 0; 00178 fread(&val, sz, 1, in); 00179 tagtable->become_host_endian(&val); 00180 sprintf(val_str, "%d", val); 00181 } 00182 else if (tag_type == USHORT) { 00183 unsigned short val = 0; 00184 fread(&val, sz, 1, in); 00185 tagtable->become_host_endian(&val); 00186 sprintf(val_str, "%d", val); 00187 } 00188 else if (tag_type == INT) { 00189 int val = 0; 00190 fread(&val, sz, 1, in); 00191 tagtable->become_host_endian(&val); 00192 sprintf(val_str, "%d", val); 00193 } 00194 else if (tag_type == CHAR || tag_type == OCTET) { 00195 char val = 0; 00196 fread(&val, sz, 1, in); 00197 sprintf(val_str, "%d", val); 00198 } 00199 else if (tag_type == BOOLEAN) { 00200 bool val = false; 00201 fread(&val, sz, 1, in); 00202 tagtable->become_host_endian(&val); 00203 sprintf(val_str, "%d", val); 00204 } 00205 else if (tag_type == UINT) { 00206 unsigned int val = 0; 00207 fread(&val, sz, 1, in); 00208 tagtable->become_host_endian(&val); 00209 sprintf(val_str, "%d", (int) val); 00210 } 00211 else if (tag_type == FLOAT) { 00212 float val = 0; 00213 fread(&val, sz, 1, in); 00214 tagtable->become_host_endian(&val); 00215 sprintf(val_str, "%f", val); 00216 } 00217 else if (tag_type == DOUBLE) { 00218 double val = 0; 00219 fread(&val, sz, 1, in); 00220 tagtable->become_host_endian(&val); 00221 sprintf(val_str, "%10e", val); 00222 } 00223 else { 00224 LOGERR("invalid tag type: '%d'", tag_type); 00225 exit(1); 00226 } 00227 00228 if (is_value_stored) { 00229 tagtable->add(name, val_str); 00230 } 00231 00232 LOGVAR("value = '%s'", val_str); 00233 00234 return string(val_str); 00235 }
string TagData::read_string | ( | int | size | ) | [private] |
Definition at line 267 of file dm3io.cpp.
References EMAN::Gatan::TagTable::become_host_endian(), in, and tagtable.
Referenced by read_array_data().
00268 { 00269 if (size <= 0) { 00270 return string(""); 00271 } 00272 00273 unsigned short *buf = new unsigned short[size]; 00274 char *str = new char[size + 1]; 00275 00276 fread(buf, size * sizeof(unsigned short), 1, in); 00277 tagtable->become_host_endian < unsigned short >(buf, size); 00278 00279 for (int i = 0; i < size; i++) { 00280 str[i] = static_cast < char >(buf[i]); 00281 } 00282 00283 str[size] = '\0'; 00284 string str1 = string(str); 00285 00286 if( str ) 00287 { 00288 delete[]str; 00289 str = 0; 00290 } 00291 if( buf ) 00292 { 00293 delete[]buf; 00294 buf = 0; 00295 } 00296 00297 return str1; 00298 }
vector< int > TagData::read_struct_types | ( | ) | [private] |
Definition at line 358 of file dm3io.cpp.
References EMAN::ByteOrder::become_big_endian(), in, LOGVAR, and EMAN::Gatan::to_str().
Referenced by read_any(), and read_array_types().
00359 { 00360 LOGVAR("TagData::read_struct_types()"); 00361 00362 unsigned int namelength = 0; 00363 unsigned int nfields = 0; 00364 00365 fread(&namelength, sizeof(namelength), 1, in); 00366 ByteOrder::become_big_endian(&namelength); 00367 00368 fread(&nfields, sizeof(nfields), 1, in); 00369 ByteOrder::become_big_endian(&nfields); 00370 00371 LOGVAR("namelength = %d\n", namelength); 00372 LOGVAR("num fields = %d\n", nfields); 00373 00374 vector < int >field_types; 00375 00376 for (unsigned int i = 0; i < nfields; i++) { 00377 fread(&namelength, sizeof(namelength), 1, in); 00378 ByteOrder::become_big_endian(&namelength); 00379 00380 int field_type = 0; 00381 fread(&field_type, sizeof(field_type), 1, in); 00382 ByteOrder::become_big_endian(&field_type); 00383 00384 LOGVAR("%dth namelength = %d, type = '%s'", 00385 i, namelength, Gatan::to_str((Type) field_type)); 00386 field_types.push_back(field_type); 00387 } 00388 00389 return field_types; 00390 }
size_t TagData::typesize | ( | int | type | ) | const [private] |
Definition at line 482 of file dm3io.cpp.
References BOOLEAN, CHAR, DOUBLE, FLOAT, INT, LOGERR, OCTET, SHORT, UINT, and USHORT.
00483 { 00484 size_t size = 0; 00485 Type type = static_cast < Type > (t); 00486 00487 switch (type) { 00488 case SHORT: 00489 size = sizeof(short); 00490 break; 00491 case USHORT: 00492 size = sizeof(unsigned short); 00493 break; 00494 case INT: 00495 size = sizeof(int); 00496 break; 00497 case UINT: 00498 size = sizeof(unsigned int); 00499 break; 00500 case FLOAT: 00501 size = sizeof(float); 00502 break; 00503 case DOUBLE: 00504 size = sizeof(double); 00505 break; 00506 case BOOLEAN: 00507 size = sizeof(bool); 00508 break; 00509 case CHAR: 00510 case OCTET: 00511 size = sizeof(char); 00512 break; 00513 default: 00514 LOGERR("no such type: '%d'\n", type); 00515 break; 00516 } 00517 00518 return size; 00519 }
size_t TagData::typesize | ( | ) | const [private] |
Definition at line 477 of file dm3io.cpp.
References tag_type.
Referenced by read_array_data(), and read_native().
FILE* EMAN::Gatan::TagData::in [private] |
Definition at line 134 of file dm3io.h.
Referenced by read(), read_any(), read_array_data(), read_array_types(), read_native(), read_string(), and read_struct_types().
string EMAN::Gatan::TagData::name [private] |
Definition at line 136 of file dm3io.h.
Referenced by read_any(), read_array_data(), and read_native().
Type EMAN::Gatan::TagData::tag_type [private] |
TagTable* EMAN::Gatan::TagData::tagtable [private] |
Definition at line 135 of file dm3io.h.
Referenced by read_any(), read_array_data(), read_native(), and read_string().