File: metaUtils.h

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (462 lines) | stat: -rw-r--r-- 12,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*============================================================================
  MetaIO
  Copyright 2000-2010 Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
/**
 *    MetaUtils (.h and .cpp)
 *
 * Description:
 *    This file provides generic ascii file parsing capabilities.
 *    It assumes that the files consist of a set of fields
 *    Each field is list of variable = value pairs
 *
 * Features:
 *    There can be dependencies between fields, required fields,
 *       and optional fields.
 *    Undefined fields are skipped.
 *    Values must conform to expected types.   There can be default
 *       values for fields.
 *
 * Author:
 *    Stephen R. Aylward
 *
 * Date:
 *    February 22, 2002
 *
 **/
#include "metaTypes.h"

#ifndef ITKMetaIO_METAUTILS_H
#  define ITKMetaIO_METAUTILS_H

#  ifdef _MSC_VER
#    pragma warning(disable : 4251)
#    pragma warning(disable : 4511)
#    pragma warning(disable : 4512)
#    pragma warning(disable : 4702)
#    pragma warning(disable : 4786)
#    pragma warning(disable : 4996)
#  endif

#  include <vector>
#  include <string>
#  include <sstream>
#  include <iostream>
//#include <iomanip>
#  include <typeinfo>
#  include <cstring>

#  if (METAIO_USE_NAMESPACE)
namespace METAIO_NAMESPACE
{
#  endif

extern bool META_DEBUG;

#  define META_DEBUG_PRINT(content)                                                                                  \
      if (META_DEBUG)                                                                                                \
        {                                                                                                            \
        std::cout << content << std::endl;                                                                           \
        }                                                                                                            \
    static_assert(true, "Compiled away assert that syntactically require semicolon at end of macro.")

// Types used for storing the compression table
typedef struct MET_CompressionOffset
{
  std::streamoff uncompressedOffset;
  std::streamoff compressedOffset;
} MET_CompressionOffsetType;

typedef std::vector<MET_CompressionOffsetType> MET_CompressionOffsetListType;

typedef struct MET_CompressionTable
{
  MET_CompressionOffsetListType offsetList;
  z_stream *                    compressedStream;
  char *                        buffer;
  std::streamoff                bufferSize;
} MET_CompressionTableType;

METAIO_EXPORT MET_FieldRecordType *
              MET_GetFieldRecord(const char * _fieldName, std::vector<MET_FieldRecordType *> * _fields);

METAIO_EXPORT
int
MET_GetFieldRecordNumber(const char * _fieldName, std::vector<MET_FieldRecordType *> * _fields);

METAIO_EXPORT
bool
MET_SizeOfType(MET_ValueEnumType _vType, int * s);

// Byte Order
METAIO_EXPORT
bool
MET_SystemByteOrderMSB();

inline unsigned short
MET_ByteOrderSwapShort(unsigned short x)
{
  return x << 8 | x >> 8;
}

inline unsigned int
MET_ByteOrderSwapLong(unsigned int x)
{
  return (((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff));
}

inline void
MET_ByteOrderSwap2(void * x)
{
  char   one_byte;
  char * p = reinterpret_cast<char *>(x);

  one_byte = p[0];
  p[0] = p[1];
  p[1] = one_byte;
}

inline void
MET_ByteOrderSwap4(void * x)
{
  char   one_byte;
  char * p = reinterpret_cast<char *>(x);

  one_byte = p[0];
  p[0] = p[3];
  p[3] = one_byte;

  one_byte = p[1];
  p[1] = p[2];
  p[2] = one_byte;
}

inline void
MET_ByteOrderSwap8(void * x)
{
  char   one_byte;
  char * p = reinterpret_cast<char *>(x);
  one_byte = p[0];
  p[0] = p[7];
  p[7] = one_byte;

  one_byte = p[1];
  p[1] = p[6];
  p[6] = one_byte;

  one_byte = p[2];
  p[2] = p[5];
  p[5] = one_byte;

  one_byte = p[3];
  p[3] = p[4];
  p[4] = one_byte;
}

/** Make sure that all the byte are read and written as LSB */
void
MET_SwapByteIfSystemMSB(void * val, MET_ValueEnumType _type);


// STRINGS AND TYPES
METAIO_EXPORT
bool
MET_StringToWordArray(const char * s, int * n, char *** val);

template <class T>
void
MET_StringToVector(const std::string & s, std::vector<T> & vec, const char separator = ',')
{
  vec.clear();

  std::string::size_type prevPos = 0;
  std::string::size_type pos = s.find(separator, prevPos);
  T                      tVal;
  while (pos != std::string::npos)
  {
    std::stringstream ss;
    std::string       tmpString = s.substr(prevPos, (pos - prevPos));
    ss << tmpString;
    ss >> tVal;
    vec.push_back(tVal);

    prevPos = pos + 1;
    pos = s.find(separator, prevPos);
  }
  std::stringstream ss;
  std::string       tmpString = s.substr(prevPos, (s.size() - prevPos));
  ss << tmpString;
  ss >> tVal;
  vec.push_back(tVal);
}

METAIO_EXPORT
bool
MET_StringToType(const char * _s, MET_ValueEnumType * _vType);

METAIO_EXPORT
bool
MET_TypeToString(MET_ValueEnumType _vType, char * _s);

METAIO_EXPORT
bool
MET_StringToInterpolationType(const char * _str, MET_InterpolationEnumType * _type);

METAIO_EXPORT
bool
MET_InterpolationTypeToString(MET_InterpolationEnumType _type, char * _str);

inline MET_ValueEnumType
MET_GetPixelType(const std::type_info & ptype)
{
  if (ptype == typeid(MET_UCHAR_TYPE))
  {
    return MET_UCHAR;
  }
  else if (ptype == typeid(MET_CHAR_TYPE))
  {
    return MET_CHAR;
  }
  else if (ptype == typeid(MET_USHORT_TYPE))
  {
    return MET_USHORT;
  }
  else if (ptype == typeid(MET_SHORT_TYPE))
  {
    return MET_SHORT;
  }
  else if (ptype == typeid(MET_UINT_TYPE))
  {
    return MET_UINT;
  }
  else if (ptype == typeid(MET_INT_TYPE))
  {
    return MET_INT;
  }
  else if (ptype == typeid(MET_ULONG_TYPE))
  {
    return MET_ULONG;
  }
  else if (ptype == typeid(MET_LONG_TYPE))
  {
    return MET_LONG;
  }
  else if (ptype == typeid(MET_ULONG_LONG_TYPE))
  {
    return MET_ULONG_LONG;
  }
  else if (ptype == typeid(MET_LONG_LONG_TYPE))
  {
    return MET_LONG_LONG;
  }
  else if (ptype == typeid(MET_FLOAT_TYPE))
  {
    return MET_FLOAT;
  }
  else if (ptype == typeid(MET_DOUBLE_TYPE))
  {
    return MET_DOUBLE;
  }
  else
  {
    std::cerr << "MET_GetPixelType: Couldn't convert pixel type : " << ptype.name() << std::endl;
    return MET_NONE;
  }
}

inline MET_ValueEnumType
MET_GetValueEnumType(const std::type_info & ptype)
{
  return MET_GetPixelType(ptype);
}

inline void
MET_StringStripEnd(MET_ASCII_CHAR_TYPE * str)
{
  // note the post-decrement in the condition
  for (size_t j = strlen(str); j-- > 0;)
  {
    if (isprint(str[j]) && !isspace(str[j]))
    {
      break;
    }
    str[j] = '\0';
  }
}


// VALUES
METAIO_EXPORT
bool
MET_ValueToDouble(MET_ValueEnumType _type, const void * _data, std::streamoff _index, double * _value);

// Deprecated. Instead, use the variant below where the _data buffer size is specified.
METAIO_EXPORT
bool
MET_DoubleToValue(double _value, MET_ValueEnumType _type, void * _data, std::streamoff _index);

METAIO_EXPORT
bool
MET_DoubleToValueN(double _value, MET_ValueEnumType _type, void * _data, size_t _dataSize, std::streamoff _index);

// Deprecated. Instead, use the variant below where the _toData buffer size is specified.
METAIO_EXPORT
bool
MET_ValueToValue(MET_ValueEnumType _fromType,
                 const void *      _fromData,
                 std::streamoff    _index,
                 MET_ValueEnumType _toType,
                 void *            _toData,
                 double            _fromMin = 0,
                 double            _fromMax = 0,
                 double            _toMin = 0,
                 double            _toMax = 0);

METAIO_EXPORT
bool
MET_ValueToValueN(MET_ValueEnumType _fromType,
                 const void *      _fromData,
                 std::streamoff    _index,
                 MET_ValueEnumType _toType,
                 void *            _toData,
                 size_t            _toDataSize,
                 double            _fromMin = 0,
                 double            _fromMax = 0,
                 double            _toMin = 0,
                 double            _toMax = 0);

METAIO_EXPORT
unsigned char *
MET_PerformCompression(const unsigned char * source,
                       std::streamoff        sourceSize,
                       std::streamoff *      compressedDataSize,
                       int                   compressionLevel);

METAIO_EXPORT
bool
MET_PerformUncompression(const unsigned char * sourceCompressed,
                         std::streamoff        sourceCompressedSize,
                         unsigned char *       uncompressedData,
                         std::streamoff        uncompressedDataSize);

// Uncompress a stream given an uncompressedSeekPosition
METAIO_EXPORT
std::streamoff
MET_UncompressStream(std::ifstream *            stream,
                     std::streamoff             uncompressedSeekPosition,
                     unsigned char *            uncompressedData,
                     std::streamoff             uncompressedDataSize,
                     std::streamoff             compressedDataSize,
                     MET_CompressionTableType * compressionTable);


// FILES NAMES
METAIO_EXPORT
bool
MET_GetFilePath(const std::string & _fName, std::string & _fPath);

METAIO_EXPORT
bool
MET_GetFileSuffixPtr(const std::string & _fName, int * i);

METAIO_EXPORT
bool
MET_SetFileSuffix(std::string & _fName, const std::string & _suf);

METAIO_EXPORT
bool
MET_InitWriteField(MET_FieldRecordType * _mf, const char * _name, MET_ValueEnumType _type, double _v = 0);

template <class T>
bool
MET_InitWriteField(MET_FieldRecordType * _mf, const char * _name, MET_ValueEnumType _type, size_t _length, T * _v)
{
  strncpy(_mf->name, _name, 254);
  _mf->name[254] = '\0';
  _mf->type = _type;
  _mf->defined = true;
  _mf->length = static_cast<int>(_length);
  _mf->dependsOn = -1;
  _mf->required = false;
  _mf->terminateRead = false;
  if (_type == MET_FLOAT_MATRIX)
  {
    size_t i;
    for (i = 0; i < MET_MAX_NUMBER_OF_FIELD_VALUES && i < _length * _length; i++)
    {
      _mf->value[i] = (double)(_v[i]);
    }
  }
  else if (_type != MET_STRING)
  {
    size_t i;
    for (i = 0; i < MET_MAX_NUMBER_OF_FIELD_VALUES && i < _length; i++)
    {
      _mf->value[i] = (double)(_v[i]);
    }
  }
  else
  {
    strncpy((char *)(_mf->value), (const char *)_v, (sizeof(_mf->value) - 1));
    ((char *)(_mf->value))[(sizeof(_mf->value) - 1)] = '\0';
  }
  return true;
}

METAIO_EXPORT
bool
MET_Write(std::ostream & fp, std::vector<MET_FieldRecordType *> * fields, char _met_SeperatorChar = '=');

METAIO_EXPORT
bool
MET_WriteFieldToFile(std::ostream & _fp, const char * _fieldName, MET_ValueEnumType _pType, size_t _n, const void * _v);

METAIO_EXPORT
bool
MET_WriteFieldToFile(std::ostream & _fp, const char * _fieldName, MET_ValueEnumType _pType, double _v);


METAIO_EXPORT
bool
MET_InitReadField(MET_FieldRecordType * _mf,
                  const char *          _name,
                  MET_ValueEnumType     _type,
                  bool                  _required = true,
                  int                   _dependsOn = -1,
                  size_t                _length = 0);

METAIO_EXPORT
bool
MET_Read(std::istream &                       fp,
         std::vector<MET_FieldRecordType *> * fields,
         char                                 _met_SeperatorChar = '=',
         bool                                 oneLine = false,
         bool                                 display_warnings = true,
         std::vector<MET_FieldRecordType *> * newFields = nullptr);


METAIO_EXPORT
std::string
MET_ReadForm(std::istream & _fp);

METAIO_EXPORT
std::string
MET_ReadType(std::istream & _fp);

METAIO_EXPORT
char *
MET_ReadSubType(std::istream & _fp);

#  if (METAIO_USE_NAMESPACE)
};
#  endif


#endif