forked from katiya-cw/iso8211lib-1.4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso8211.h
582 lines (463 loc) · 19.9 KB
/
iso8211.h
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/******************************************************************************
* $Id: iso8211.h,v 1.20 2005/12/08 20:22:24 fwarmerdam Exp $
*
* Project: ISO 8211 Access
* Purpose: Main declarations for ISO 8211.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*
* $Log: iso8211.h,v $
* Revision 1.20 2005/12/08 20:22:24 fwarmerdam
* use CPL_ODLL to optionally export ISO8211lib api
*
* Revision 1.19 2004/01/06 18:59:18 warmerda
* make enum identifiers more unique
*
* Revision 1.18 2004/01/06 18:53:41 warmerda
* made data_type_code and data_struct_code global for HP C++ builds
*
* Revision 1.17 2003/09/03 20:36:26 warmerda
* added subfield writing support
*
* Revision 1.16 2003/08/21 21:21:44 warmerda
* expose the binary format type for a subfield defn
*
* Revision 1.15 2003/07/03 15:38:46 warmerda
* some write capabilities added
*
* Revision 1.14 2001/08/29 17:47:33 warmerda
* added GetInstanceData
*
* Revision 1.13 2001/08/24 19:41:19 warmerda
* fixed cloning problems
*
* Revision 1.12 2001/08/24 16:30:55 warmerda
* added DDFRecord update in place methods for S57 updating
*
* Revision 1.11 2000/09/19 14:08:51 warmerda
* keep and report _extendedCharSet
*
* Revision 1.10 2000/06/16 18:02:08 warmerda
* added SetRepeatingFlag hack support
*
* Revision 1.9 2000/01/31 18:03:39 warmerda
* completely rewrote format expansion to make more general
*
* Revision 1.8 1999/11/18 19:03:04 warmerda
* expanded tabs
*
* Revision 1.7 1999/11/18 19:02:38 warmerda
* added failquietly to open
*
* Revision 1.6 1999/09/20 19:29:30 warmerda
* make forgiving of UNIT/FIELD terminator mixup in Tiger SDTS files
*
* Revision 1.5 1999/08/13 03:26:29 warmerda
* added Rewind()
*
* Revision 1.4 1999/05/07 14:11:22 warmerda
* added subfield value fetches on record, and other odds and ends.
*
* Revision 1.3 1999/05/06 14:23:32 warmerda
* added DDFBinaryString
*
* Revision 1.2 1999/04/27 22:09:50 warmerda
* updated docs
*
* Revision 1.1 1999/04/27 18:45:09 warmerda
* New
*
*/
#ifndef _ISO8211_H_INCLUDED
#define _ISO8211_H_INCLUDED
#include "cpl_port.h"
/**
General data type
*/
typedef enum {
DDFInt,
DDFFloat,
DDFString,
DDFBinaryString
} DDFDataType;
/************************************************************************/
/* These should really be private to the library ... they are */
/* mostly conveniences. */
/************************************************************************/
long CPL_ODLL DDFScanInt( const char *pszString, int nMaxChars );
int CPL_ODLL DDFScanVariable( const char * pszString, int nMaxChars, int nDelimChar );
char CPL_ODLL *DDFFetchVariable( const char *pszString, int nMaxChars,
int nDelimChar1, int nDelimChar2,
int *pnConsumedChars );
#define DDF_FIELD_TERMINATOR 30
#define DDF_UNIT_TERMINATOR 31
/************************************************************************/
/* Predeclarations */
/************************************************************************/
class DDFFieldDefn;
class DDFSubfieldDefn;
class DDFRecord;
class DDFField;
/************************************************************************/
/* DDFModule */
/************************************************************************/
/**
The primary class for reading ISO 8211 files. This class contains all
the information read from the DDR record, and is used to read records
from the file.
*/
class CPL_ODLL DDFModule
{
public:
DDFModule();
~DDFModule();
int Open( const char * pszFilename, int bFailQuietly = FALSE );
int Create( const char *pszFilename );
void Close();
int Initialize( char chInterchangeLevel = '3',
char chLeaderIden = 'L',
char chCodeExtensionIndicator = 'E',
char chVersionNumber = '1',
char chAppIndicator = ' ',
const char *pszExtendedCharSet = " ! ",
int nSizeFieldLength = 3,
int nSizeFieldPos = 4,
int nSizeFieldTag = 4 );
void Dump( FILE * fp );
DDFRecord *ReadRecord( void );
void Rewind( long nOffset = -1 );
DDFFieldDefn *FindFieldDefn( const char * );
/** Fetch the number of defined fields. */
int GetFieldCount() { return nFieldDefnCount; }
DDFFieldDefn *GetField(int);
void AddField( DDFFieldDefn *poNewFDefn );
// This is really just for internal use.
int GetFieldControlLength() { return _fieldControlLength; }
void AddCloneRecord( DDFRecord * );
void RemoveCloneRecord( DDFRecord * );
// This is just for DDFRecord.
FILE *GetFP() { return fpDDF; }
private:
FILE *fpDDF;
int bReadOnly;
long nFirstRecordOffset;
char _interchangeLevel;
char _inlineCodeExtensionIndicator;
char _versionNumber;
char _appIndicator;
int _fieldControlLength;
char _extendedCharSet[4];
long _recLength;
char _leaderIden;
long _fieldAreaStart;
long _sizeFieldLength;
long _sizeFieldPos;
long _sizeFieldTag;
// One DirEntry per field.
int nFieldDefnCount;
DDFFieldDefn **papoFieldDefns;
DDFRecord *poRecord;
int nCloneCount;
int nMaxCloneCount;
DDFRecord **papoClones;
};
/************************************************************************/
/* DDFFieldDefn */
/************************************************************************/
typedef enum { dsc_elementary, dsc_vector, dsc_array, dsc_concatenated } DDF_data_struct_code;
typedef enum { dtc_char_string,
dtc_implicit_point,
dtc_explicit_point,
dtc_explicit_point_scaled,
dtc_char_bit_string,
dtc_bit_string,
dtc_mixed_data_type } DDF_data_type_code;
/**
* Information from the DDR defining one field. Note that just because
* a field is defined for a DDFModule doesn't mean that it actually occurs
* on any records in the module. DDFFieldDefns are normally just significant
* as containers of the DDFSubfieldDefns.
*/
class CPL_ODLL DDFFieldDefn
{
public:
DDFFieldDefn();
~DDFFieldDefn();
int Create( const char *pszTag, const char *pszFieldName,
const char *pszDescription,
DDF_data_struct_code eDataStructCode,
DDF_data_type_code eDataTypeCode,
const char *pszFormat = NULL );
void AddSubfield( DDFSubfieldDefn *poNewSFDefn,
int bDontAddToFormat = FALSE );
void AddSubfield( const char *pszName, const char *pszFormat );
int GenerateDDREntry( char **ppachData, int *pnLength );
int Initialize( DDFModule * poModule, const char *pszTag,
int nSize, const char * pachRecord );
void Dump( FILE * fp );
/** Fetch a pointer to the field name (tag).
* @return this is an internal copy and shouldn't be freed.
*/
const char *GetName() { return pszTag; }
/** Fetch a longer descriptio of this field.
* @return this is an internal copy and shouldn't be freed.
*/
const char *GetDescription() { return _fieldName; }
/** Get the number of subfields. */
int GetSubfieldCount() { return nSubfieldCount; }
DDFSubfieldDefn *GetSubfield( int i );
DDFSubfieldDefn *FindSubfieldDefn( const char * );
/**
* Get the width of this field. This function isn't normally used
* by applications.
*
* @return The width of the field in bytes, or zero if the field is not
* apparently of a fixed width.
*/
int GetFixedWidth() { return nFixedWidth; }
/**
* Fetch repeating flag.
* @see DDFField::GetRepeatCount()
* @return TRUE if the field is marked as repeating.
*/
int IsRepeating() { return bRepeatingSubfields; }
static char *ExpandFormat( const char * );
/** this is just for an S-57 hack for swedish data */
void SetRepeatingFlag( int n ) { bRepeatingSubfields = n; }
char *GetDefaultValue( int *pnSize );
private:
static char *ExtractSubstring( const char * );
DDFModule * poModule;
char * pszTag;
char * _fieldName;
char * _arrayDescr;
char * _formatControls;
int bRepeatingSubfields;
int nFixedWidth; // zero if variable.
int BuildSubfields();
int ApplyFormats();
DDF_data_struct_code _data_struct_code;
DDF_data_type_code _data_type_code;
int nSubfieldCount;
DDFSubfieldDefn **papoSubfields;
};
/************************************************************************/
/* DDFSubfieldDefn */
/* */
/* Information from the DDR record for one subfield of a */
/* particular field. */
/************************************************************************/
/**
* Information from the DDR record describing one subfield of a DDFFieldDefn.
* All subfields of a field will occur in each occurance of that field
* (as a DDFField) in a DDFRecord. Subfield's actually contain formatted
* data (as instances within a record).
*/
class CPL_ODLL DDFSubfieldDefn
{
public:
DDFSubfieldDefn();
~DDFSubfieldDefn();
void SetName( const char * pszName );
/** Get pointer to subfield name. */
const char *GetName() { return pszName; }
/** Get pointer to subfield format string */
const char *GetFormat() { return pszFormatString; }
int SetFormat( const char * pszFormat );
/**
* Get the general type of the subfield. This can be used to
* determine which of ExtractFloatData(), ExtractIntData() or
* ExtractStringData() should be used.
* @return The subfield type. One of DDFInt, DDFFloat, DDFString or
* DDFBinaryString.
*/
DDFDataType GetType() { return eType; }
double ExtractFloatData( const char *pachData, int nMaxBytes,
int * pnConsumedBytes );
int ExtractIntData( const char *pachData, int nMaxBytes,
int * pnConsumedBytes );
const char *ExtractStringData( const char *pachData, int nMaxBytes,
int * pnConsumedBytes );
int GetDataLength( const char *, int, int * );
void DumpData( const char *pachData, int nMaxBytes, FILE * fp );
int FormatStringValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed, const char *pszValue,
int nValueLength = -1 );
int FormatIntValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed, int nNewValue );
int FormatFloatValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed, double dfNewValue );
/** Get the subfield width (zero for variable). */
int GetWidth() { return nFormatWidth; } // zero for variable.
int GetDefaultValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed );
void Dump( FILE * fp );
/**
Binary format: this is the digit immediately following the B or b for
binary formats.
*/
typedef enum {
NotBinary=0,
UInt=1,
SInt=2,
FPReal=3,
FloatReal=4,
FloatComplex=5
} DDFBinaryFormat;
DDFBinaryFormat GetBinaryFormat(void) const { return eBinaryFormat; }
private:
char *pszName; // a.k.a. subfield mnemonic
char *pszFormatString;
DDFDataType eType;
DDFBinaryFormat eBinaryFormat;
/* -------------------------------------------------------------------- */
/* bIsVariable determines whether we using the */
/* chFormatDelimeter (TRUE), or the fixed width (FALSE). */
/* -------------------------------------------------------------------- */
int bIsVariable;
char chFormatDelimeter;
int nFormatWidth;
/* -------------------------------------------------------------------- */
/* Fetched string cache. This is where we hold the values */
/* returned from ExtractStringData(). */
/* -------------------------------------------------------------------- */
int nMaxBufChars;
char *pachBuffer;
};
/************************************************************************/
/* DDFRecord */
/* */
/* Class that contains one DR record from a file. We read into */
/* the same record object repeatedly to ensure that repeated */
/* leaders can be easily preserved. */
/************************************************************************/
/**
* Contains instance data from one data record (DR). The data is contained
* as a list of DDFField instances partitioning the raw data into fields.
*/
class CPL_ODLL DDFRecord
{
public:
DDFRecord( DDFModule * );
~DDFRecord();
DDFRecord *Clone();
DDFRecord *CloneOn( DDFModule * );
void Dump( FILE * );
/** Get the number of DDFFields on this record. */
int GetFieldCount() { return nFieldCount; }
DDFField *FindField( const char *, int = 0 );
DDFField *GetField( int );
int GetIntSubfield( const char *, int, const char *, int,
int * = NULL );
double GetFloatSubfield( const char *, int, const char *, int,
int * = NULL );
const char *GetStringSubfield( const char *, int, const char *, int,
int * = NULL );
int SetIntSubfield( const char *pszField, int iFieldIndex,
const char *pszSubfield, int iSubfieldIndex,
int nValue );
int SetStringSubfield( const char *pszField, int iFieldIndex,
const char *pszSubfield, int iSubfieldIndex,
const char *pszValue, int nValueLength=-1 );
int SetFloatSubfield( const char *pszField, int iFieldIndex,
const char *pszSubfield, int iSubfieldIndex,
double dfNewValue );
/** Fetch size of records raw data (GetData()) in bytes. */
int GetDataSize() { return nDataSize; }
/**
* Fetch the raw data for this record. The returned pointer is effectively
* to the data for the first field of the record, and is of size
* GetDataSize().
*/
const char *GetData() { return pachData; }
/**
* Fetch the DDFModule with which this record is associated.
*/
DDFModule * GetModule() { return poModule; }
int ResizeField( DDFField *poField, int nNewDataSize );
int DeleteField( DDFField *poField );
DDFField* AddField( DDFFieldDefn * );
int CreateDefaultFieldInstance( DDFField *poField, int iIndexWithinField );
int SetFieldRaw( DDFField *poField, int iIndexWithinField,
const char *pachRawData, int nRawDataSize );
int UpdateFieldRaw( DDFField *poField, int iIndexWithinField,
int nStartOffset, int nOldSize,
const char *pachRawData, int nRawDataSize );
int Write();
// This is really just for the DDFModule class.
int Read();
void Clear();
int ResetDirectory();
private:
int ReadHeader();
DDFModule *poModule;
int nReuseHeader;
int nFieldOffset; // field data area, not dir entries.
int _sizeFieldTag;
int _sizeFieldPos;
int _sizeFieldLength;
int nDataSize; // Whole record except leader with header
char *pachData;
int nFieldCount;
DDFField *paoFields;
int bIsClone;
};
/************************************************************************/
/* DDFField */
/* */
/* This object represents one field in a DDFRecord. */
/************************************************************************/
/**
* This object represents one field in a DDFRecord. This
* models an instance of the fields data, rather than it's data definition
* which is handled by the DDFFieldDefn class. Note that a DDFField
* doesn't have DDFSubfield children as you would expect. To extract
* subfield values use GetSubfieldData() to find the right data pointer and
* then use ExtractIntData(), ExtractFloatData() or ExtractStringData().
*/
class CPL_ODLL DDFField
{
public:
void Initialize( DDFFieldDefn *, const char *pszData,
int nSize );
void Dump( FILE * fp );
const char *GetSubfieldData( DDFSubfieldDefn *,
int * = NULL, int = 0 );
const char *GetInstanceData( int nInstance, int *pnSize );
/**
* Return the pointer to the entire data block for this record. This
* is an internal copy, and shouldn't be freed by the application.
*/
const char *GetData() { return pachData; }
/** Return the number of bytes in the data block returned by GetData(). */
int GetDataSize() { return nDataSize; }
int GetRepeatCount();
/** Fetch the corresponding DDFFieldDefn. */
DDFFieldDefn *GetFieldDefn() { return poDefn; }
private:
DDFFieldDefn *poDefn;
int nDataSize;
const char *pachData;
};
#endif /* ndef _ISO8211_H_INCLUDED */