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

#include <dm3io.h>

Collaboration diagram for EMAN::Gatan::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 , 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
 
TagTabletagtable
 
string name
 
Type tag_type
 

Detailed Description

Definition at line 93 of file dm3io.h.

Member Enumeration Documentation

◆ Type

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

Definition at line 96 of file dm3io.h.

97 {
98 UNKNOWN = 0,
99 SHORT = 2,
100 INT = 3,
101 USHORT = 4,
102 UINT = 5,
103 FLOAT = 6,
104 DOUBLE = 7,
105 BOOLEAN = 8,
106 CHAR = 9,
107 OCTET = 10,
108 STRUCT = 15,
109 STRING = 18,
110 ARRAY = 20
111 };

Constructor & Destructor Documentation

◆ TagData()

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

Definition at line 157 of file dm3io.cpp.

158 : in(data_file), tagtable(table), name(tagname), tag_type(UNKNOWN)
159{
160}
TagTable * tagtable
Definition: dm3io.h:131

◆ ~TagData()

TagData::~TagData ( )

Definition at line 162 of file dm3io.cpp.

163{
164}

Member Function Documentation

◆ read()

int TagData::read ( bool  nodata = false)

Definition at line 438 of file dm3io.cpp.

439{
440 LOGVAR("TagData::read()");
441 int err = 0;
442
443 const char *DATA_TYPE_MARK = "%%%%";
444 const size_t mark_sz = strlen(DATA_TYPE_MARK);
445 char *mark = new char[mark_sz + 1];
446
447 size_t nr;
448 nr = fread(mark, mark_sz, 1, in); nr++;
449 mark[mark_sz] = '\0';
450
451 if (strcmp(mark, DATA_TYPE_MARK) != 0) {
452 LOGERR("data type label has been changed from '%s' to '%s'",
453 DATA_TYPE_MARK, mark);
454 return 1;
455 }
456
457 if (mark) {
458 delete [] mark;
459 mark = 0;
460 }
461
462 int encoded_types_size = 0;
463 nr = fread(&encoded_types_size, sizeof(int), 1, in); nr++;
464 ByteOrder::become_big_endian(&encoded_types_size);
465
466 LOGVAR("encoded types size = %d\n", encoded_types_size);
467
468 err = read_any(nodata);
469
470 return err;
471}
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
int read_any(bool nodata=false)
Definition: dm3io.cpp:389
#define LOGERR
Definition: log.h:51
#define LOGVAR
Definition: log.h:57

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

Referenced by EMAN::Gatan::TagEntry::read().

◆ read_any()

int TagData::read_any ( bool  nodata = false)
private

Definition at line 389 of file dm3io.cpp.

390{
391 int err = 0;
392
393 size_t nr;
394 nr = fread(&tag_type, sizeof(tag_type), 1, in); nr++;
396 LOGVAR("tag type = '%s'\n", Gatan::to_str((Type) tag_type));
397
398 if (tag_type == ARRAY) {
399 vector < int >item_types = read_array_types();
400 err = read_array_data(item_types, nodata);
401 }
402 else if (tag_type == STRUCT) {
403 vector < int >field_types = read_struct_types();
404
405 for (unsigned int i = 0; i < field_types.size(); i++) {
406 tag_type = static_cast < Type > (field_types[i]);
407 string val = read_native(false);
408 char int_str[32];
409 sprintf(int_str, " #%d", i);
410 string fieldname = name + string(int_str);
411 tagtable->add(fieldname, val);
412 }
413 }
414 else if (tag_type == STRING) {
415 int str_sz = 0;
416 nr = fread(&str_sz, sizeof(str_sz), 1, in); nr++;
418
419 char *val = new char[str_sz + 1];
420 nr = fread(val, str_sz, 1, in); nr++;
421 val[str_sz] = '\0';
422 string val_str = string(val);
423 if( val )
424 {
425 delete[]val;
426 val = 0;
427 }
428
429 tagtable->add(name, val_str);
430 }
431 else {
432 read_native(true);
433 }
434
435 return err;
436}
int read_array_data(vector< int >item_types, bool nodata=false)
Definition: dm3io.cpp:296
vector< int > read_array_types()
Definition: dm3io.cpp:233
string read_native(bool is_value_stored)
Definition: dm3io.cpp:166
vector< int > read_struct_types()
Definition: dm3io.cpp:354
void add(const string &name, const string &value)
Definition: dm3io.cpp:61
const char * to_str(Gatan::TagData::Type type)
Definition: dm3io.cpp:925

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

◆ read_array_data()

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

Definition at line 296 of file dm3io.cpp.

297{
298 ENTERFUNC;
299 if (item_types.size() == 0) {
300 LOGERR("DM3 item types cannot be empty");
301 return 1;
302 }
303
304 int err = 0;
305 int array_size = 0;
306
307 size_t nr;
308 nr = fread(&array_size, sizeof(array_size), 1, in); nr++;
309 ByteOrder::become_big_endian(&array_size);
310
311 LOGVAR("array size = %d\n", array_size);
312
313 size_t item_size = 0;
314 for (size_t i = 0; i < item_types.size(); i++) {
315 item_size += typesize(item_types[i]);
316 }
317
318 LOGVAR("%s array item size = %d\n", name.c_str(), item_size);
319
320 size_t buf_size = item_size * array_size;
321
322 if (item_types.size() == 1 && item_types[0] == USHORT && nodata) {
323 string val = read_string(array_size);
324 tagtable->add(name, val);
325 LOGVAR("value: %s", val.c_str());
326 }
327 else if (!nodata && name == "Data") {
328 char *data = new char[buf_size];
329 nr = fread(data, buf_size, 1, in); nr++;
330
331 if (item_size == sizeof(short)) {
332 tagtable->become_host_endian((short *) data, array_size);
333 }
334 else if (item_size == sizeof(int)) {
335 tagtable->become_host_endian((int *) data, array_size);
336 }
337 else if (item_size == sizeof(double)) {
338 tagtable->become_host_endian((double *) data, array_size);
339 }
340 else {
341 LOGERR("cannot handle this type of DM3 image data");
342 return 1;
343 }
344
345 tagtable->add_data(data);
346 }
347 else {
348 portable_fseek(in, buf_size, SEEK_CUR);
349 }
350 EXITFUNC;
351 return err;
352}
string read_string(int size)
Definition: dm3io.cpp:263
size_t typesize() const
Definition: dm3io.cpp:473
void become_host_endian(T *data, int n=1)
Definition: dm3io.h:65
void add_data(char *data)
Definition: dm3io.cpp:82
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
int portable_fseek(FILE *fp, off_t offset, int whence)

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

Referenced by read_any().

◆ read_array_types()

vector< int > TagData::read_array_types ( )
private

Definition at line 233 of file dm3io.cpp.

234{
235 LOGVAR("TagData::read_array_types()");
236
237 int array_type = 0;
238 size_t nr;
239 nr = fread(&array_type, sizeof(array_type), 1, in); nr++;
240
241 ByteOrder::become_big_endian(&array_type);
242
243 LOGVAR("array data type = '%s'", Gatan::to_str((Type) array_type));
244
245 vector < int >item_types;
246
247 if (array_type == STRUCT) {
248 item_types = read_struct_types();
249 }
250 else if (array_type == ARRAY) {
251 item_types = read_array_types();
252 LOGERR("DM3: don't know how to handle this array type");
253 }
254 else {
255 item_types.push_back(array_type);
256 }
257
258 return item_types;
259}

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

Referenced by read_any(), and read_array_types().

◆ read_native()

string TagData::read_native ( bool  is_value_stored)
private

Definition at line 166 of file dm3io.cpp.

167{
168 size_t sz = typesize();
169 size_t nr;
170 char val_str[32];
171
172 if (tag_type == SHORT) {
173 short val = 0;
174 nr = fread(&val, sz, 1, in); nr++;
176 sprintf(val_str, "%d", val);
177 }
178 else if (tag_type == USHORT) {
179 unsigned short val = 0;
180 nr = fread(&val, sz, 1, in); nr++;
182 sprintf(val_str, "%d", val);
183 }
184 else if (tag_type == INT) {
185 int val = 0;
186 nr = fread(&val, sz, 1, in); nr++;
188 sprintf(val_str, "%d", val);
189 }
190 else if (tag_type == CHAR || tag_type == OCTET) {
191 char val = 0;
192 nr = fread(&val, sz, 1, in); nr++;
193 sprintf(val_str, "%d", val);
194 }
195 else if (tag_type == BOOLEAN) {
196 bool val = false;
197 nr = fread(&val, sz, 1, in); nr++;
199 sprintf(val_str, "%d", val);
200 }
201 else if (tag_type == UINT) {
202 unsigned int val = 0;
203 nr = fread(&val, sz, 1, in); nr++;
205 sprintf(val_str, "%d", (int) val);
206 }
207 else if (tag_type == FLOAT) {
208 float val = 0;
209 nr = fread(&val, sz, 1, in); nr++;
211 sprintf(val_str, "%f", val);
212 }
213 else if (tag_type == DOUBLE) {
214 double val = 0;
215 nr = fread(&val, sz, 1, in); nr++;
217 sprintf(val_str, "%10e", val);
218 }
219 else {
220 LOGERR("invalid tag type: '%d'", tag_type);
221 exit(1);
222 }
223
224 if (is_value_stored) {
225 tagtable->add(name, val_str);
226 }
227
228 LOGVAR("value = '%s'", val_str);
229
230 return string(val_str);
231}

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

◆ read_string()

string TagData::read_string ( int  size)
private

Definition at line 263 of file dm3io.cpp.

264{
265 if (size <= 0) {
266 return string("");
267 }
268
269 unsigned short *buf = new unsigned short[size];
270 char *str = new char[size + 1];
271
272 size_t nr;
273 nr = fread(buf, size * sizeof(unsigned short), 1, in); nr++;
274 tagtable->become_host_endian < unsigned short >(buf, size);
275
276 for (int i = 0; i < size; i++) {
277 str[i] = static_cast < char >(buf[i]);
278 }
279
280 str[size] = '\0';
281 string str1 = string(str);
282
283 if (str) {
284 delete [] str;
285 str = NULL;
286 }
287
288 if (buf) {
289 delete [] buf;
290 buf = NULL;
291 }
292
293 return str1;
294}

References EMAN::Gatan::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 354 of file dm3io.cpp.

355{
356 LOGVAR("TagData::read_struct_types()");
357
358 unsigned int namelength = 0;
359 unsigned int nfields = 0;
360
361 size_t nr;
362 nr = fread(&namelength, sizeof(namelength), 1, in); nr++;
363 ByteOrder::become_big_endian(&namelength);
364
365 nr = fread(&nfields, sizeof(nfields), 1, in); nr++;
367
368 LOGVAR("namelength = %d\n", namelength);
369 LOGVAR("num fields = %d\n", nfields);
370
371 vector < int >field_types;
372
373 for (unsigned int i = 0; i < nfields; i++) {
374 nr = fread(&namelength, sizeof(namelength), 1, in); nr++;
375 ByteOrder::become_big_endian(&namelength);
376
377 int field_type = 0;
378 nr = fread(&field_type, sizeof(field_type), 1, in); nr++;
379 ByteOrder::become_big_endian(&field_type);
380
381 LOGVAR("%dth namelength = %d, type = '%s'",
382 i, namelength, Gatan::to_str((Type) field_type));
383 field_types.push_back(field_type);
384 }
385
386 return field_types;
387}

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

Referenced by read_any(), and read_array_types().

◆ typesize() [1/2]

size_t TagData::typesize ( ) const
private

Definition at line 473 of file dm3io.cpp.

474{
475 return typesize((int) tag_type);
476}

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 478 of file dm3io.cpp.

479{
480 size_t size = 0;
481 Type type = static_cast < Type > (t);
482
483 switch (type) {
484 case SHORT:
485 size = sizeof(short);
486 break;
487 case USHORT:
488 size = sizeof(unsigned short);
489 break;
490 case INT:
491 size = sizeof(int);
492 break;
493 case UINT:
494 size = sizeof(unsigned int);
495 break;
496 case FLOAT:
497 size = sizeof(float);
498 break;
499 case DOUBLE:
500 size = sizeof(double);
501 break;
502 case BOOLEAN:
503 size = sizeof(bool);
504 break;
505 case CHAR:
506 case OCTET:
507 size = sizeof(char);
508 break;
509 default:
510 LOGERR("no such type: '%d'\n", type);
511 break;
512 }
513
514 return size;
515}

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

Member Data Documentation

◆ in

FILE* EMAN::Gatan::TagData::in
private

◆ name

string EMAN::Gatan::TagData::name
private

Definition at line 132 of file dm3io.h.

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

◆ tag_type

Type EMAN::Gatan::TagData::tag_type
private

Definition at line 133 of file dm3io.h.

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

◆ tagtable

TagTable* EMAN::Gatan::TagData::tagtable
private

Definition at line 131 of file dm3io.h.

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


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