#include <dm4io.h>
Collaboration diagram for EMAN::GatanDM4::TagData:
Public Types | |
UNKNOWN = 0 | |
SHORT = 2 | |
INT = 3 | |
USHORT = 4 | |
UINT = 5 | |
FLOAT = 6 | |
DOUBLE = 7 | |
BOOLEAN = 8 | |
CHAR = 9 | |
OCTET = 10 | |
OCTEU = 11 | |
OCTEV = 12 | |
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, OCTEU = 11, OCTEV = 12, 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 |
long | tag_type |
Definition at line 94 of file dm4io.h.
UNKNOWN | |
SHORT | |
INT | |
USHORT | |
UINT | |
FLOAT | |
DOUBLE | |
BOOLEAN | |
CHAR | |
OCTET | |
OCTEU | |
OCTEV | |
STRUCT | |
STRING | |
ARRAY |
Definition at line 97 of file dm4io.h.
00098 { 00099 UNKNOWN = 0, 00100 SHORT = 2, 00101 INT = 3, 00102 USHORT = 4, 00103 UINT = 5, 00104 FLOAT = 6, 00105 DOUBLE = 7, 00106 BOOLEAN = 8, 00107 CHAR = 9, 00108 OCTET = 10, 00109 OCTEU = 11, 00110 OCTEV = 12, 00111 STRUCT = 15, 00112 STRING = 18, 00113 ARRAY = 20 00114 };
TagData::TagData | ( | FILE * | data_file, | |
TagTable * | tagtable, | |||
const string & | tagname | |||
) |
int TagData::read | ( | bool | nodata = false |
) |
Definition at line 456 of file dm4io.cpp.
References EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, and read_any().
Referenced by EMAN::GatanDM4::TagEntry::read().
00457 { 00458 LOGVAR("TagData::read()"); 00459 int err = 0; 00460 00461 const char *DATA_TYPE_MARK = "%%%%"; 00462 const size_t mark_sz = strlen(DATA_TYPE_MARK); 00463 char *mark = new char[mark_sz + 1]; 00464 00465 long interval; 00466 00467 fread(&interval, sizeof(interval), 1, in); 00468 00469 ByteOrder::become_big_endian(&interval); 00470 00471 fread(mark, mark_sz, 1, in); 00472 mark[mark_sz] = '\0'; 00473 00474 if (strcmp(mark, DATA_TYPE_MARK) != 0) { 00475 LOGERR("data type label has been changed from '%s' to '%s'", 00476 DATA_TYPE_MARK, mark); 00477 return 1; 00478 } 00479 00480 if( mark ) 00481 { 00482 delete[]mark; 00483 mark = 0; 00484 } 00485 00486 long encoded_types_size = 0; 00487 fread(&encoded_types_size, sizeof(long), 1, in); 00488 ByteOrder::become_big_endian(&encoded_types_size); 00489 00490 LOGVAR("encoded types size = %d\n", encoded_types_size); 00491 00492 err = read_any(nodata); 00493 00494 return err; 00495 }
int TagData::read_any | ( | bool | nodata = false |
) | [private] |
Definition at line 402 of file dm4io.cpp.
References EMAN::GatanDM4::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::GatanDM4::to_str().
Referenced by read().
00403 { 00404 int err = 0; 00405 00406 fread(&tag_type, sizeof(tag_type), 1, in); 00407 00408 ByteOrder::become_big_endian(&tag_type); 00409 LOGVAR("TagData::read_any tag type = '%s'\n", GatanDM4::to_str((Type) tag_type)); 00410 00411 00412 if (tag_type == ARRAY) { 00413 vector < int >item_types = read_array_types(); 00414 err = read_array_data(item_types, nodata); 00415 } 00416 else if (tag_type == STRUCT) { 00417 vector < int >field_types = read_struct_types(); 00418 00419 for (unsigned int i = 0; i < field_types.size(); i++) { 00420 00421 tag_type = static_cast < Type > (field_types[i]); 00422 00423 string val = read_native(false); 00424 00425 char int_str[32]; 00426 sprintf(int_str, " #%d", i); 00427 string fieldname = name + string(int_str); 00428 tagtable->add(fieldname, val); 00429 } 00430 } 00431 else if (tag_type == STRING) { 00432 00433 int str_sz = 0; 00434 fread(&str_sz, sizeof(str_sz), 1, in); 00435 ByteOrder::become_big_endian(&str_sz); 00436 00437 char *val = new char[str_sz + 1]; 00438 fread(val, str_sz, 1, in); 00439 val[str_sz] = '\0'; 00440 string val_str = string(val); 00441 if( val ) 00442 { 00443 delete[]val; 00444 val = 0; 00445 } 00446 00447 tagtable->add(name, val_str); 00448 } 00449 else { 00450 read_native(true); 00451 } 00452 00453 return err; 00454 }
int TagData::read_array_data | ( | vector< int > | item_types, | |
bool | nodata = false | |||
) | [private] |
Definition at line 311 of file dm4io.cpp.
References EMAN::GatanDM4::TagTable::add(), EMAN::GatanDM4::TagTable::add_data(), EMAN::ByteOrder::become_big_endian(), EMAN::GatanDM4::TagTable::become_host_endian(), data, ENTERFUNC, EXITFUNC, in, LOGERR, LOGVAR, name, portable_fseek(), read_string(), tagtable, typesize(), and USHORT.
Referenced by read_any().
00312 { 00313 ENTERFUNC; 00314 if (item_types.size() == 0) { 00315 LOGERR("DM4 item types cannot be empty"); 00316 return 1; 00317 } 00318 00319 int err = 0; 00320 long array_size = 0; 00321 00322 fread(&array_size, sizeof(array_size), 1, in); 00323 ByteOrder::become_big_endian(&array_size); 00324 00325 LOGVAR("array size = %d\n", array_size); 00326 00327 size_t item_size = 0; 00328 for (size_t i = 0; i < item_types.size(); i++) { 00329 item_size += typesize(item_types[i]); 00330 } 00331 00332 LOGVAR("%s array item size = %d\n", name.c_str(), item_size); 00333 00334 size_t buf_size = item_size * array_size; 00335 00336 if (item_types.size() == 1 && item_types[0] == USHORT && nodata) { 00337 string val = read_string(array_size); 00338 tagtable->add(name, val); 00339 LOGVAR("value: %s", val.c_str()); 00340 } 00341 else if (!nodata && name == "Data") { 00342 char *data = new char[buf_size]; 00343 fread(data, buf_size, 1, in); 00344 00345 if (item_size == sizeof(short)) { 00346 tagtable->become_host_endian((short *) data, array_size); 00347 } 00348 else if (item_size == sizeof(int)) { 00349 tagtable->become_host_endian((int *) data, array_size); 00350 } 00351 else if (item_size == sizeof(double)) { 00352 tagtable->become_host_endian((double *) data, array_size); 00353 } 00354 else { 00355 LOGERR("cannot handle this type of DM4 image data"); 00356 return 1; 00357 } 00358 00359 tagtable->add_data(data); 00360 } 00361 else { 00362 portable_fseek(in, buf_size, SEEK_CUR); 00363 } 00364 EXITFUNC; 00365 return err; 00366 }
vector< int > TagData::read_array_types | ( | ) | [private] |
Definition at line 250 of file dm4io.cpp.
References ARRAY, EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, read_struct_types(), STRUCT, and EMAN::GatanDM4::to_str().
Referenced by read_any().
00251 { 00252 LOGVAR("TagData::read_array_types()"); 00253 00254 long array_type = 0; 00255 fread(&array_type, sizeof(array_type), 1, in); 00256 00257 ByteOrder::become_big_endian(&array_type); 00258 00259 LOGVAR("array data type = '%s'", GatanDM4::to_str((Type) array_type)); 00260 00261 vector < int >item_types; 00262 00263 if (array_type == STRUCT) { 00264 item_types = read_struct_types(); 00265 } 00266 else if (array_type == ARRAY) { 00267 item_types = read_array_types(); 00268 LOGERR("DM4: don't know how to handle this array type"); 00269 } 00270 else { 00271 item_types.push_back(array_type); 00272 } 00273 00274 return item_types; 00275 }
string TagData::read_native | ( | bool | is_value_stored | ) | [private] |
Definition at line 172 of file dm4io.cpp.
References EMAN::GatanDM4::TagTable::add(), EMAN::GatanDM4::TagTable::become_host_endian(), BOOLEAN, CHAR, DOUBLE, FLOAT, in, INT, LOGERR, LOGVAR, name, OCTET, OCTEU, OCTEV, SHORT, tag_type, tagtable, typesize(), UINT, and USHORT.
Referenced by read_any().
00173 { 00174 size_t sz = typesize(); 00175 char val_str[32]; 00176 00177 if (tag_type == SHORT) { 00178 short val = 0; 00179 fread(&val, sz, 1, in); 00180 tagtable->become_host_endian(&val); 00181 sprintf(val_str, "%d", val); 00182 } 00183 else if (tag_type == USHORT) { 00184 unsigned short val = 0; 00185 fread(&val, sz, 1, in); 00186 tagtable->become_host_endian(&val); 00187 sprintf(val_str, "%d", val); 00188 } 00189 else if (tag_type == INT) { 00190 int val = 0; 00191 fread(&val, sz, 1, in); 00192 tagtable->become_host_endian(&val); 00193 sprintf(val_str, "%d", val); 00194 } 00195 else if (tag_type == CHAR || tag_type == OCTET) { 00196 char val = 0; 00197 fread(&val, sz, 1, in); 00198 sprintf(val_str, "%d", val); 00199 } 00200 else if (tag_type == BOOLEAN) { 00201 bool val = false; 00202 fread(&val, sz, 1, in); 00203 tagtable->become_host_endian(&val); 00204 sprintf(val_str, "%d", val); 00205 } 00206 else if (tag_type == UINT) { 00207 unsigned int val = 0; 00208 fread(&val, sz, 1, in); 00209 tagtable->become_host_endian(&val); 00210 sprintf(val_str, "%d", (int) val); 00211 } 00212 else if (tag_type == FLOAT) { 00213 float val = 0; 00214 fread(&val, sz, 1, in); 00215 tagtable->become_host_endian(&val); 00216 sprintf(val_str, "%f", val); 00217 } 00218 else if (tag_type == DOUBLE) { 00219 double val = 0; 00220 fread(&val, sz, 1, in); 00221 tagtable->become_host_endian(&val); 00222 sprintf(val_str, "%10e", val); 00223 } 00224 else if (tag_type == OCTEU) { 00225 double val = 0; 00226 fread(&val, sz, 1, in); 00227 tagtable->become_host_endian(&val); 00228 sprintf(val_str, "%10e", val); 00229 } 00230 else if (tag_type == OCTEV) { 00231 double val = 0; 00232 fread(&val, sz, 1, in); 00233 tagtable->become_host_endian(&val); 00234 sprintf(val_str, "%10e", val); 00235 } 00236 else { 00237 LOGERR("invalid tag type: '%d'", tag_type); 00238 exit(1); 00239 } 00240 00241 if (is_value_stored) { 00242 tagtable->add(name, val_str); 00243 } 00244 00245 LOGVAR("value = '%s'", val_str); 00246 00247 return string(val_str); 00248 }
string TagData::read_string | ( | int | size | ) | [private] |
Definition at line 278 of file dm4io.cpp.
References EMAN::GatanDM4::TagTable::become_host_endian(), in, and tagtable.
Referenced by read_array_data().
00279 { 00280 if (size <= 0) { 00281 return string(""); 00282 } 00283 00284 unsigned short *buf = new unsigned short[size]; 00285 char *str = new char[size + 1]; 00286 00287 fread(buf, size * sizeof(unsigned short), 1, in); 00288 tagtable->become_host_endian < unsigned short >(buf, size); 00289 00290 for (int i = 0; i < size; i++) { 00291 str[i] = static_cast < char >(buf[i]); 00292 } 00293 00294 str[size] = '\0'; 00295 string str1 = string(str); 00296 00297 if( str ) 00298 { 00299 delete[]str; 00300 str = 0; 00301 } 00302 if( buf ) 00303 { 00304 delete[]buf; 00305 buf = 0; 00306 } 00307 00308 return str1; 00309 }
vector< int > TagData::read_struct_types | ( | ) | [private] |
Definition at line 368 of file dm4io.cpp.
References EMAN::ByteOrder::become_big_endian(), in, LOGVAR, and EMAN::GatanDM4::to_str().
Referenced by read_any(), and read_array_types().
00369 { 00370 LOGVAR("TagData::read_struct_types()"); 00371 00372 long namelength = 0; 00373 long nfields = 0; 00374 00375 fread(&namelength, sizeof(namelength), 1, in); 00376 ByteOrder::become_big_endian(&namelength); 00377 00378 fread(&nfields, sizeof(nfields), 1, in); 00379 ByteOrder::become_big_endian(&nfields); 00380 00381 LOGVAR("namelength = %d\n", namelength); 00382 LOGVAR("num fields = %d\n", nfields); 00383 00384 vector < int >field_types; 00385 00386 for (unsigned int i = 0; i < nfields; i++) { 00387 fread(&namelength, sizeof(namelength), 1, in); 00388 ByteOrder::become_big_endian(&namelength); 00389 00390 long field_type = 0; 00391 fread(&field_type, sizeof(field_type), 1, in); 00392 ByteOrder::become_big_endian(&field_type); 00393 00394 LOGVAR("%dth namelength = %d, type = '%s'", 00395 i, namelength, GatanDM4::to_str((Type) field_type)); 00396 field_types.push_back(field_type); 00397 } 00398 00399 return field_types; 00400 }
size_t TagData::typesize | ( | int | type | ) | const [private] |
Definition at line 502 of file dm4io.cpp.
References BOOLEAN, CHAR, DOUBLE, FLOAT, INT, LOGERR, OCTET, OCTEU, OCTEV, SHORT, UINT, and USHORT.
00503 { 00504 size_t size = 0; 00505 Type type = static_cast < Type > (t); 00506 00507 00508 switch (type) { 00509 case SHORT: 00510 size = sizeof(short); 00511 break; 00512 case USHORT: 00513 size = sizeof(unsigned short); 00514 break; 00515 case INT: 00516 size = sizeof(int); 00517 break; 00518 case UINT: 00519 size = sizeof(unsigned int); 00520 break; 00521 case FLOAT: 00522 size = sizeof(float); 00523 break; 00524 case DOUBLE: 00525 size = sizeof(double); 00526 break; 00527 case BOOLEAN: 00528 size = sizeof(bool); 00529 break; 00530 case CHAR: 00531 case OCTET: 00532 size = sizeof(char); 00533 break; 00534 case OCTEU: 00535 size = sizeof(double); 00536 break; 00537 case OCTEV: 00538 size = sizeof(double); 00539 break; 00540 default: 00541 LOGERR("no such type: '%d'\n", type); 00542 break; 00543 } 00544 00545 return size; 00546 }
size_t TagData::typesize | ( | ) | const [private] |
Definition at line 497 of file dm4io.cpp.
References tag_type.
Referenced by read_array_data(), and read_native().
FILE* EMAN::GatanDM4::TagData::in [private] |
Definition at line 133 of file dm4io.h.
Referenced by read(), read_any(), read_array_data(), read_array_types(), read_native(), read_string(), and read_struct_types().
string EMAN::GatanDM4::TagData::name [private] |
Definition at line 135 of file dm4io.h.
Referenced by read_any(), read_array_data(), and read_native().
long EMAN::GatanDM4::TagData::tag_type [private] |
TagTable* EMAN::GatanDM4::TagData::tagtable [private] |
Definition at line 134 of file dm4io.h.
Referenced by read_any(), read_array_data(), read_native(), and read_string().