EMAN2
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
EMAN::GatanDM4::TagData Class Reference

#include <dm4io.h>

Collaboration diagram for EMAN::GatanDM4::TagData:
Collaboration graph
[legend]

Public Types

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_tag_data (bool nodata=false, int image_index=0, int num_images=1)
 

Private Member Functions

size_t typesize () const
 
size_t typesize (int type) const
 
int read_any (bool nodata=false, int image_index=0, int num_images=1)
 
vector< int > read_array_types ()
 
int read_array_data (vector< int >item_types, bool nodata=false, int image_index=0, int num_images=1)
 
vector< int > read_struct_types ()
 
string read_native (bool is_value_stored)
 
string read_string (int size)
 

Private Attributes

FILE * in
 
TagTabletagtable
 
string name
 
long long tag_type
 

Detailed Description

Definition at line 96 of file dm4io.h.

Member Enumeration Documentation

◆ Type

Enumerator
UNKNOWN 
SHORT 
INT 
USHORT 
UINT 
FLOAT 
DOUBLE 
BOOLEAN 
CHAR 
OCTET 
OCTEU 
OCTEV 
STRUCT 
STRING 
ARRAY 

Definition at line 99 of file dm4io.h.

100 {
101 UNKNOWN = 0,
102 SHORT = 2,
103 INT = 3,
104 USHORT = 4,
105 UINT = 5,
106 FLOAT = 6,
107 DOUBLE = 7,
108 BOOLEAN = 8,
109 CHAR = 9,
110 OCTET = 10,
111 OCTEU = 11,
112 OCTEV = 12,
113 STRUCT = 15,
114 STRING = 18,
115 ARRAY = 20
116 };

Constructor & Destructor Documentation

◆ TagData()

TagData::TagData ( FILE *  data_file,
TagTable tagtable,
const string &  tagname 
)

Definition at line 177 of file dm4io.cpp.

178 : in(data_file), tagtable(table), name(tagname), tag_type(UNKNOWN)
179{
180}
long long tag_type
Definition: dm4io.h:138
TagTable * tagtable
Definition: dm4io.h:136

◆ ~TagData()

TagData::~TagData ( )

Definition at line 182 of file dm4io.cpp.

183{
184}

Member Function Documentation

◆ read_any()

int TagData::read_any ( bool  nodata = false,
int  image_index = 0,
int  num_images = 1 
)
private

Definition at line 437 of file dm4io.cpp.

438{
439 int err = 0;
440
441 size_t nr;
442 nr = fread(&tag_type, sizeof(tag_type), 1, in); nr++;
443
445 LOGVAR("TagData::read_any tag type = '%s'\n", GatanDM4::to_str((Type) tag_type));
446
447
448 if (tag_type == ARRAY) {
449 vector < int >item_types = read_array_types();
450 err = read_array_data(item_types, nodata, image_index, num_images);
451 }
452 else if (tag_type == STRUCT) {
453 vector < int >field_types = read_struct_types();
454
455 for (unsigned int i = 0; i < field_types.size(); i++) {
456
457 tag_type = static_cast < Type > (field_types[i]);
458
459 string val = read_native(false);
460
461 char int_str[32];
462 sprintf(int_str, " #%d", i);
463 string fieldname = name + string(int_str);
464 tagtable->add(fieldname, val);
465 }
466 }
467 else if (tag_type == STRING) {
468
469 int str_sz = 0;
470 size_t nr;
471 nr = fread(&str_sz, sizeof(str_sz), 1, in); nr++;
473
474 char *val = new char[str_sz + 1];
475 nr = fread(val, str_sz, 1, in); nr++;
476 val[str_sz] = '\0';
477 string val_str = string(val);
478
479 if (val) {
480 delete [] val;
481 val = NULL;
482 }
483
484 tagtable->add(name, val_str);
485 }
486 else {
487 read_native(true);
488 }
489
490 return err;
491}
static void become_big_endian(T *data, size_t n=1)
convert data from host byte order to big endian order.
Definition: byteorder.h:112
vector< int > read_array_types()
Definition: dm4io.cpp:265
int read_array_data(vector< int >item_types, bool nodata=false, int image_index=0, int num_images=1)
Definition: dm4io.cpp:327
string read_native(bool is_value_stored)
Definition: dm4io.cpp:186
vector< int > read_struct_types()
Definition: dm4io.cpp:402
void add(const string &name, const string &value)
Definition: dm4io.cpp:61
#define LOGVAR
Definition: log.h:57
const char * to_str(GatanDM4::TagData::Type type)
Definition: dm4io.cpp:1083

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_tag_data().

◆ read_array_data()

int TagData::read_array_data ( vector< int >  item_types,
bool  nodata = false,
int  image_index = 0,
int  num_images = 1 
)
private

Definition at line 327 of file dm4io.cpp.

328{
329 ENTERFUNC;
330 if (item_types.size() == 0) {
331 LOGERR("DM4 item types cannot be empty");
332 return 1;
333 }
334
335 int err = 0;
336 long long array_size = 0;
337
338 size_t nr;
339 nr = fread(&array_size, sizeof(array_size), 1, in); nr++;
340 ByteOrder::become_big_endian(&array_size);
341
342 LOGVAR("array size = %lld\n", array_size);
343
344 size_t item_size = 0;
345 for (size_t i = 0; i < item_types.size(); i++) {
346 item_size += typesize(item_types[i]);
347 }
348
349 LOGVAR("%s array item size = %lld\n", name.c_str(), item_size);
350
351 size_t buf_size = item_size * array_size;
352
353 if (item_types.size() == 1 && item_types[0] == USHORT && nodata) {
354 string val = read_string(array_size);
355 tagtable->add(name, val);
356 LOGVAR("value: %s", val.c_str());
357 }
358 else if (!nodata && name == "Data") {
359 int num_found = tagtable->get_num_images_found();
360 num_found++;
361 tagtable->set_num_images_found(num_found);
362
363 char * data;
364
365 if (image_index < 0 || buf_size % num_images != 0 || num_found == 1) {
366 data = new char[buf_size];
367 nr = fread(data, item_size, array_size, in); nr++;
368 }
369 else {
370 size_t image_size = buf_size / num_images;
371
372 data = new char[image_size];
373 portable_fseek(in, image_index * image_size, SEEK_CUR);
374 nr = fread(data, image_size, 1, in); nr++;
375 portable_fseek(in, (num_images - image_index - 1) * image_size, SEEK_CUR);
376 array_size = array_size / num_images;
377 }
378
379 if (item_size == sizeof(short)) {
380 tagtable->become_host_endian((short *) data, array_size);
381 }
382 else if (item_size == sizeof(int)) {
383 tagtable->become_host_endian((int *) data, array_size);
384 }
385 else if (item_size == sizeof(double)) {
386 tagtable->become_host_endian((double *) data, array_size);
387 }
388 else {
389 LOGERR("cannot handle this type of DM4 image data");
390 return 1;
391 }
392
393 tagtable->add_data(data);
394 }
395 else {
396 portable_fseek(in, buf_size, SEEK_CUR);
397 }
398 EXITFUNC;
399 return err;
400}
string read_string(int size)
Definition: dm4io.cpp:294
size_t typesize() const
Definition: dm4io.cpp:534
int get_num_images_found() const
Definition: dm4io.cpp:165
void become_host_endian(T *data, int n=1)
Definition: dm4io.h:65
void set_num_images_found(int num_found)
Definition: dm4io.cpp:170
void add_data(char *data)
Definition: dm4io.cpp:85
#define LOGERR
Definition: log.h:51
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
int portable_fseek(FILE *fp, off_t offset, int whence)

References EMAN::GatanDM4::TagTable::add(), EMAN::GatanDM4::TagTable::add_data(), EMAN::ByteOrder::become_big_endian(), EMAN::GatanDM4::TagTable::become_host_endian(), ENTERFUNC, EXITFUNC, EMAN::GatanDM4::TagTable::get_num_images_found(), in, LOGERR, LOGVAR, name, portable_fseek(), read_string(), EMAN::GatanDM4::TagTable::set_num_images_found(), tagtable, typesize(), and USHORT.

Referenced by read_any().

◆ read_array_types()

vector< int > TagData::read_array_types ( )
private

Definition at line 265 of file dm4io.cpp.

266{
267 LOGVAR("TagData::read_array_types()");
268
269 long long array_type = 0;
270 size_t nr;
271 nr = fread(&array_type, sizeof(array_type), 1, in); nr++;
272
273 ByteOrder::become_big_endian(&array_type);
274
275 LOGVAR("array data type = '%s'", GatanDM4::to_str((Type) array_type));
276
277 vector < int >item_types;
278
279 if (array_type == STRUCT) {
280 item_types = read_struct_types();
281 }
282 else if (array_type == ARRAY) {
283 item_types = read_array_types();
284 LOGERR("DM4: don't know how to handle this array type");
285 }
286 else {
287 item_types.push_back(array_type);
288 }
289
290 return item_types;
291}

References ARRAY, EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, read_array_types(), read_struct_types(), STRUCT, and EMAN::GatanDM4::to_str().

Referenced by read_any(), and read_array_types().

◆ read_native()

string TagData::read_native ( bool  is_value_stored)
private

Definition at line 186 of file dm4io.cpp.

187{
188 size_t sz = typesize();
189 size_t nr;
190 char val_str[32];
191
192 if (tag_type == SHORT) {
193 short val = 0;
194 nr = fread(&val, sz, 1, in); nr++;
196 sprintf(val_str, "%d", val);
197 }
198 else if (tag_type == USHORT) {
199 unsigned short val = 0;
200 nr = fread(&val, sz, 1, in); nr++;
202 sprintf(val_str, "%d", val);
203 }
204 else if (tag_type == INT) {
205 int val = 0;
206 nr = fread(&val, sz, 1, in); nr++;
208 sprintf(val_str, "%d", val);
209 }
210 else if (tag_type == CHAR || tag_type == OCTET) {
211 char val = 0;
212 nr = fread(&val, sz, 1, in); nr++;
213 sprintf(val_str, "%d", val);
214 }
215 else if (tag_type == BOOLEAN) {
216 bool val = false;
217 nr = fread(&val, sz, 1, in); nr++;
219 sprintf(val_str, "%d", val);
220 }
221 else if (tag_type == UINT) {
222 unsigned int val = 0;
223 nr = fread(&val, sz, 1, in); nr++;
225 sprintf(val_str, "%u", (int) val);
226 }
227 else if (tag_type == FLOAT) {
228 float val = 0;
229 nr = fread(&val, sz, 1, in); nr++;
231 sprintf(val_str, "%f", val);
232 }
233 else if (tag_type == DOUBLE) {
234 double val = 0;
235 nr = fread(&val, sz, 1, in); nr++;
237 sprintf(val_str, "%10e", val);
238 }
239 else if (tag_type == OCTEU) {
240 long long val = 0;
241 nr = fread(&val, sz, 1, in); nr++;
243 sprintf(val_str, "%lld", val);
244 }
245 else if (tag_type == OCTEV) {
246 unsigned long long val = 0;
247 nr = fread(&val, sz, 1, in); nr++;
249 sprintf(val_str, "%lld", val);
250 }
251 else {
252 LOGERR("invalid tag type: '%lld'", tag_type);
253 exit(1);
254 }
255
256 if (is_value_stored) {
257 tagtable->add(name, val_str);
258 }
259
260 LOGVAR("value = '%s'", val_str);
261
262 return string(val_str);
263}

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().

◆ read_string()

string TagData::read_string ( int  size)
private

Definition at line 294 of file dm4io.cpp.

295{
296 if (size <= 0) {
297 return string("");
298 }
299
300 unsigned short *buf = new unsigned short[size];
301 char *str = new char[size + 1];
302
303 size_t nr;
304 nr = fread(buf, size * sizeof(unsigned short), 1, in); nr++;
305 tagtable->become_host_endian < unsigned short >(buf, size);
306
307 for (int i = 0; i < size; i++) {
308 str[i] = static_cast < char >(buf[i]);
309 }
310
311 str[size] = '\0';
312 string str1 = string(str);
313
314 if (str) {
315 delete [] str;
316 str = NULL;
317 }
318
319 if (buf) {
320 delete [] buf;
321 buf = NULL;
322 }
323
324 return str1;
325}

References EMAN::GatanDM4::TagTable::become_host_endian(), in, and tagtable.

Referenced by read_array_data().

◆ read_struct_types()

vector< int > TagData::read_struct_types ( )
private

Definition at line 402 of file dm4io.cpp.

403{
404 LOGVAR("TagData::read_struct_types()");
405
406 long long namelength = 0;
407 long long nfields = 0;
408
409 size_t nr;
410 nr = fread(&namelength, sizeof(namelength), 1, in); nr++;
411 ByteOrder::become_big_endian(&namelength);
412
413 nr = fread(&nfields, sizeof(nfields), 1, in); nr++;
415
416 LOGVAR("namelength = %lld\n", namelength);
417 LOGVAR("num fields = %lld\n", nfields);
418
419 vector < int >field_types;
420
421 for (unsigned int i = 0; i < nfields; i++) {
422 nr = fread(&namelength, sizeof(namelength), 1, in); nr++;
423 ByteOrder::become_big_endian(&namelength);
424
425 long long field_type = 0;
426 nr = fread(&field_type, sizeof(field_type), 1, in); nr++;
427 ByteOrder::become_big_endian(&field_type);
428
429 LOGVAR("%dth namelength = %lld, type = '%s'",
430 i, namelength, GatanDM4::to_str((Type) field_type));
431 field_types.push_back(field_type);
432 }
433
434 return field_types;
435}

References EMAN::ByteOrder::become_big_endian(), in, LOGVAR, and EMAN::GatanDM4::to_str().

Referenced by read_any(), and read_array_types().

◆ read_tag_data()

int TagData::read_tag_data ( bool  nodata = false,
int  image_index = 0,
int  num_images = 1 
)

Definition at line 493 of file dm4io.cpp.

494{
495 LOGVAR("TagData::read_tag_data()");
496 int err = 0;
497
498 const char *DATA_TYPE_MARK = "%%%%";
499 const size_t mark_sz = strlen(DATA_TYPE_MARK);
500 char *mark = new char[mark_sz + 1];
501
502 long long interval;
503
504 size_t nr;
505 nr = fread(&interval, sizeof(interval), 1, in); nr++;
506
508
509 nr = fread(mark, mark_sz, 1, in); nr++;
510 mark[mark_sz] = '\0';
511
512 if (strcmp(mark, DATA_TYPE_MARK) != 0) {
513 LOGERR("data type label has been changed from '%s' to '%s'",
514 DATA_TYPE_MARK, mark);
515 return 1;
516 }
517
518 if (mark) {
519 delete [] mark;
520 mark = NULL;
521 }
522
523 long long encoded_types_size = 0;
524 nr = fread(&encoded_types_size, sizeof(long long), 1, in); nr++;
525 ByteOrder::become_big_endian(&encoded_types_size);
526
527 LOGVAR("encoded types size = %lld\n", encoded_types_size);
528
529 err = read_any(nodata, image_index, num_images);
530
531 return err;
532}
int read_any(bool nodata=false, int image_index=0, int num_images=1)
Definition: dm4io.cpp:437

References EMAN::ByteOrder::become_big_endian(), in, LOGERR, LOGVAR, and read_any().

Referenced by EMAN::GatanDM4::TagEntry::read_tag_entry().

◆ typesize() [1/2]

size_t TagData::typesize ( ) const
private

Definition at line 534 of file dm4io.cpp.

535{
536 return typesize((int) tag_type);
537}

References tag_type, and typesize().

Referenced by read_array_data(), read_native(), and typesize().

◆ typesize() [2/2]

size_t TagData::typesize ( int  type) const
private

Definition at line 539 of file dm4io.cpp.

540{
541 size_t size = 0;
542 Type type = static_cast < Type > (t);
543
544
545 switch (type) {
546 case SHORT:
547 size = sizeof(short);
548 break;
549 case USHORT:
550 size = sizeof(unsigned short);
551 break;
552 case INT:
553 size = sizeof(int);
554 break;
555 case UINT:
556 size = sizeof(unsigned int);
557 break;
558 case FLOAT:
559 size = sizeof(float);
560 break;
561 case DOUBLE:
562 size = sizeof(double);
563 break;
564 case BOOLEAN:
565 size = sizeof(bool);
566 break;
567 case CHAR:
568 case OCTET:
569 size = sizeof(char);
570 break;
571 case OCTEU:
572 size = sizeof(double);
573 break;
574 case OCTEV:
575 size = sizeof(double);
576 break;
577 default:
578 LOGERR("no such type: '%d'\n", type);
579 break;
580 }
581
582 return size;
583}

References BOOLEAN, CHAR, DOUBLE, FLOAT, INT, LOGERR, OCTET, OCTEU, OCTEV, SHORT, UINT, and USHORT.

Member Data Documentation

◆ in

FILE* EMAN::GatanDM4::TagData::in
private

◆ name

string EMAN::GatanDM4::TagData::name
private

Definition at line 137 of file dm4io.h.

Referenced by read_any(), read_array_data(), and read_native().

◆ tag_type

long long EMAN::GatanDM4::TagData::tag_type
private

Definition at line 138 of file dm4io.h.

Referenced by read_any(), read_native(), and typesize().

◆ tagtable

TagTable* EMAN::GatanDM4::TagData::tagtable
private

Definition at line 136 of file dm4io.h.

Referenced by read_any(), read_array_data(), read_native(), and read_string().


The documentation for this class was generated from the following files: