SDTS_AL
iso8211.h
1 /******************************************************************************
2  * $Id: iso8211.h 33717 2016-03-14 06:29:14Z goatbar $
3  *
4  * Project: ISO 8211 Access
5  * Purpose: Main declarations for ISO 8211.
6  * Author: Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 1999, Frank Warmerdam <warmerdam@pobox.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ****************************************************************************/
29 
30 #ifndef ISO8211_H_INCLUDED
31 #define ISO8211_H_INCLUDED
32 
33 #include "cpl_port.h"
34 #include "cpl_vsi.h"
35 
39 typedef enum {
40  DDFInt,
41  DDFFloat,
42  DDFString,
43  DDFBinaryString
44 } DDFDataType;
45 
46 /************************************************************************/
47 /* These should really be private to the library ... they are */
48 /* mostly conveniences. */
49 /************************************************************************/
50 
51 int CPL_ODLL DDFScanInt( const char *pszString, int nMaxChars );
52 int CPL_ODLL DDFScanVariable( const char * pszString, int nMaxChars, int nDelimChar );
53 char CPL_ODLL *DDFFetchVariable( const char *pszString, int nMaxChars,
54  int nDelimChar1, int nDelimChar2,
55  int *pnConsumedChars );
56 
57 #define DDF_FIELD_TERMINATOR 30
58 #define DDF_UNIT_TERMINATOR 31
59 
60 /************************************************************************/
61 /* Predeclarations */
62 /************************************************************************/
63 
64 class DDFFieldDefn;
65 class DDFSubfieldDefn;
66 class DDFRecord;
67 class DDFField;
68 
69 /************************************************************************/
70 /* DDFModule */
71 /************************************************************************/
72 
79 class CPL_ODLL DDFModule
80 {
81  public:
82  DDFModule();
83  ~DDFModule();
84 
85  int Open( const char * pszFilename, int bFailQuietly = FALSE );
86  int Create( const char *pszFilename );
87  void Close();
88 
89  int Initialize( char chInterchangeLevel = '3',
90  char chLeaderIden = 'L',
91  char chCodeExtensionIndicator = 'E',
92  char chVersionNumber = '1',
93  char chAppIndicator = ' ',
94  const char *pszExtendedCharSet = " ! ",
95  int nSizeFieldLength = 3,
96  int nSizeFieldPos = 4,
97  int nSizeFieldTag = 4 );
98 
99  void Dump( FILE * fp );
100 
101  DDFRecord *ReadRecord( void );
102  void Rewind( long nOffset = -1 );
103 
104  DDFFieldDefn *FindFieldDefn( const char * );
105 
108  int GetFieldCount() { return nFieldDefnCount; }
109  DDFFieldDefn *GetField(int);
110  void AddField( DDFFieldDefn *poNewFDefn );
111 
112  // This is really just for internal use.
113  int GetFieldControlLength() { return _fieldControlLength; }
114  void AddCloneRecord( DDFRecord * );
115  void RemoveCloneRecord( DDFRecord * );
116 
117  // This is just for DDFRecord.
118  VSILFILE *GetFP() { return fpDDF; }
119  int GetSizeFieldTag() const { return (int)_sizeFieldTag; }
120 
121  // Advanced uses for 8211dump/8211createfromxml
122  int GetSizeFieldPos() const { return _sizeFieldPos; }
123  int GetSizeFieldLength() const { return _sizeFieldLength; }
124  char GetInterchangeLevel() const { return _interchangeLevel; }
125  char GetLeaderIden() const { return _leaderIden; }
126  char GetCodeExtensionIndicator() const { return _inlineCodeExtensionIndicator; }
127  char GetVersionNumber() const { return _versionNumber; }
128  char GetAppIndicator() const { return _appIndicator; }
129  const char* GetExtendedCharSet() const { return _extendedCharSet; }
130  void SetFieldControlLength(int nVal) { _fieldControlLength = nVal; }
131 
132 
133  private:
134  VSILFILE *fpDDF;
135  int bReadOnly;
136  long nFirstRecordOffset;
137 
138  char _interchangeLevel;
139  char _inlineCodeExtensionIndicator;
140  char _versionNumber;
141  char _appIndicator;
142  int _fieldControlLength;
143  char _extendedCharSet[4];
144 
145  int _recLength;
146  char _leaderIden;
147  int _fieldAreaStart;
148  int _sizeFieldLength;
149  int _sizeFieldPos;
150  int _sizeFieldTag;
151 
152  // One DirEntry per field.
153  int nFieldDefnCount;
154  DDFFieldDefn **papoFieldDefns;
155 
156  DDFRecord *poRecord;
157 
158  int nCloneCount;
159  int nMaxCloneCount;
160  DDFRecord **papoClones;
161 };
162 
163 /************************************************************************/
164 /* DDFFieldDefn */
165 /************************************************************************/
166 
167  typedef enum { dsc_elementary, dsc_vector, dsc_array, dsc_concatenated } DDF_data_struct_code;
168  typedef enum { dtc_char_string,
169  dtc_implicit_point,
170  dtc_explicit_point,
171  dtc_explicit_point_scaled,
172  dtc_char_bit_string,
173  dtc_bit_string,
174  dtc_mixed_data_type } DDF_data_type_code;
175 
183 class CPL_ODLL DDFFieldDefn
184 {
185  public:
186  DDFFieldDefn();
187  ~DDFFieldDefn();
188 
189  int Create( const char *pszTag, const char *pszFieldName,
190  const char *pszDescription,
191  DDF_data_struct_code eDataStructCode,
192  DDF_data_type_code eDataTypeCode,
193  const char *pszFormat = NULL );
194  void AddSubfield( DDFSubfieldDefn *poNewSFDefn,
195  int bDontAddToFormat = FALSE );
196  void AddSubfield( const char *pszName, const char *pszFormat );
197  int GenerateDDREntry( DDFModule * poModule, char **ppachData, int *pnLength );
198 
199  int Initialize( DDFModule * poModule, const char *pszTag,
200  int nSize, const char * pachRecord );
201 
202  void Dump( FILE * fp );
203 
207  const char *GetName() { return pszTag; }
208 
212  const char *GetDescription() { return _fieldName; }
213 
215  int GetSubfieldCount() { return nSubfieldCount; }
216 
217  DDFSubfieldDefn *GetSubfield( int i );
218  DDFSubfieldDefn *FindSubfieldDefn( const char * );
219 
227  int GetFixedWidth() { return nFixedWidth; }
228 
234  int IsRepeating() { return bRepeatingSubfields; }
235 
236  static char *ExpandFormat( const char * );
237 
239  void SetRepeatingFlag( int n ) { bRepeatingSubfields = n; }
240 
241  char *GetDefaultValue( int *pnSize );
242 
243  const char *GetArrayDescr() const { return _arrayDescr; }
244  const char *GetFormatControls() const { return _formatControls; }
245  DDF_data_struct_code GetDataStructCode() const { return _data_struct_code; }
246  DDF_data_type_code GetDataTypeCode() const { return _data_type_code; }
247 
248  void SetFormatControls(const char* pszVal);
249 
250  private:
251 
252  static char *ExtractSubstring( const char * );
253 
254  DDFModule * poModule;
255  char * pszTag;
256 
257  char * _fieldName;
258  char * _arrayDescr;
259  char * _formatControls;
260 
261  int bRepeatingSubfields;
262  int nFixedWidth; // zero if variable.
263 
264  int BuildSubfields();
265  int ApplyFormats();
266 
267  DDF_data_struct_code _data_struct_code;
268 
269  DDF_data_type_code _data_type_code;
270 
271  int nSubfieldCount;
272  DDFSubfieldDefn **papoSubfields;
273 };
274 
275 /************************************************************************/
276 /* DDFSubfieldDefn */
277 /* */
278 /* Information from the DDR record for one subfield of a */
279 /* particular field. */
280 /************************************************************************/
281 
289 class CPL_ODLL DDFSubfieldDefn
290 {
291 public:
292 
293  DDFSubfieldDefn();
294  ~DDFSubfieldDefn();
295 
296  void SetName( const char * pszName );
297 
299  const char *GetName() { return pszName; }
300 
302  const char *GetFormat() { return pszFormatString; }
303  int SetFormat( const char * pszFormat );
304 
313  DDFDataType GetType() { return eType; }
314 
315  double ExtractFloatData( const char *pachData, int nMaxBytes,
316  int * pnConsumedBytes );
317  int ExtractIntData( const char *pachData, int nMaxBytes,
318  int * pnConsumedBytes );
319  const char *ExtractStringData( const char *pachData, int nMaxBytes,
320  int * pnConsumedBytes );
321  int GetDataLength( const char *, int, int * );
322  void DumpData( const char *pachData, int nMaxBytes, FILE * fp );
323 
324  int FormatStringValue( char *pachData, int nBytesAvailable,
325  int *pnBytesUsed, const char *pszValue,
326  int nValueLength = -1 );
327 
328  int FormatIntValue( char *pachData, int nBytesAvailable,
329  int *pnBytesUsed, int nNewValue );
330 
331  int FormatFloatValue( char *pachData, int nBytesAvailable,
332  int *pnBytesUsed, double dfNewValue );
333 
335  int GetWidth() { return nFormatWidth; } // zero for variable.
336 
337  int GetDefaultValue( char *pachData, int nBytesAvailable,
338  int *pnBytesUsed );
339 
340  void Dump( FILE * fp );
341 
346 typedef enum {
347  NotBinary=0,
348  UInt=1,
349  SInt=2,
350  FPReal=3,
351  FloatReal=4,
352  FloatComplex=5
354 
355  DDFBinaryFormat GetBinaryFormat(void) const { return eBinaryFormat; }
356 
357 
358 private:
359 
360  char *pszName; // a.k.a. subfield mnemonic
361  char *pszFormatString;
362 
363  DDFDataType eType;
364  DDFBinaryFormat eBinaryFormat;
365 
366 /* -------------------------------------------------------------------- */
367 /* bIsVariable determines whether we using the */
368 /* chFormatDelimiter (TRUE), or the fixed width (FALSE). */
369 /* -------------------------------------------------------------------- */
370  int bIsVariable;
371 
372  char chFormatDelimiter;
373  int nFormatWidth;
374 
375 /* -------------------------------------------------------------------- */
376 /* Fetched string cache. This is where we hold the values */
377 /* returned from ExtractStringData(). */
378 /* -------------------------------------------------------------------- */
379  int nMaxBufChars;
380  char *pachBuffer;
381 };
382 
383 /************************************************************************/
384 /* DDFRecord */
385 /* */
386 /* Class that contains one DR record from a file. We read into */
387 /* the same record object repeatedly to ensure that repeated */
388 /* leaders can be easily preserved. */
389 /************************************************************************/
390 
396 class CPL_ODLL DDFRecord
397 {
398  public:
399  DDFRecord( DDFModule * );
400  ~DDFRecord();
401 
402  DDFRecord *Clone();
403  DDFRecord *CloneOn( DDFModule * );
404 
405  void Dump( FILE * );
406 
408  int GetFieldCount() { return nFieldCount; }
409 
410  DDFField *FindField( const char *, int = 0 );
411  DDFField *GetField( int );
412 
413  int GetIntSubfield( const char *, int, const char *, int,
414  int * = NULL );
415  double GetFloatSubfield( const char *, int, const char *, int,
416  int * = NULL );
417  const char *GetStringSubfield( const char *, int, const char *, int,
418  int * = NULL );
419 
420  int SetIntSubfield( const char *pszField, int iFieldIndex,
421  const char *pszSubfield, int iSubfieldIndex,
422  int nValue );
423  int SetStringSubfield( const char *pszField, int iFieldIndex,
424  const char *pszSubfield, int iSubfieldIndex,
425  const char *pszValue, int nValueLength=-1 );
426  int SetFloatSubfield( const char *pszField, int iFieldIndex,
427  const char *pszSubfield, int iSubfieldIndex,
428  double dfNewValue );
429 
431  int GetDataSize() { return nDataSize; }
432 
438  const char *GetData() { return pachData; }
439 
444  DDFModule * GetModule() { return poModule; }
445 
446  int ResizeField( DDFField *poField, int nNewDataSize );
447  int DeleteField( DDFField *poField );
448  DDFField* AddField( DDFFieldDefn * );
449 
450  int CreateDefaultFieldInstance( DDFField *poField, int iIndexWithinField );
451 
452  int SetFieldRaw( DDFField *poField, int iIndexWithinField,
453  const char *pachRawData, int nRawDataSize );
454  int UpdateFieldRaw( DDFField *poField, int iIndexWithinField,
455  int nStartOffset, int nOldSize,
456  const char *pachRawData, int nRawDataSize );
457 
458  int Write();
459 
460  // Advanced uses for 8211dump/8211createfromxml
461  int GetReuseHeader() const { return nReuseHeader; }
462  int GetSizeFieldTag() const { return _sizeFieldTag; }
463  int GetSizeFieldPos() const { return _sizeFieldPos; }
464  int GetSizeFieldLength() const { return _sizeFieldLength; }
465  //void SetReuseHeader(int bFlag) { nReuseHeader = bFlag; }
466  void SetSizeFieldTag(int nVal) { _sizeFieldTag = nVal; }
467  void SetSizeFieldPos(int nVal) { _sizeFieldPos = nVal; }
468  void SetSizeFieldLength(int nVal) { _sizeFieldLength = nVal; }
469 
470  // This is really just for the DDFModule class.
471  int Read();
472  void Clear();
473  int ResetDirectory();
474  void RemoveIsCloneFlag() { bIsClone = FALSE; }
475 
476  private:
477 
478  int ReadHeader();
479 
480  DDFModule *poModule;
481 
482  int nReuseHeader;
483 
484  int nFieldOffset; // field data area, not dir entries.
485 
486  int _sizeFieldTag;
487  int _sizeFieldPos;
488  int _sizeFieldLength;
489 
490  int nDataSize; // Whole record except leader with header
491  char *pachData;
492 
493  int nFieldCount;
494  DDFField *paoFields;
495 
496  int bIsClone;
497 };
498 
499 /************************************************************************/
500 /* DDFField */
501 /* */
502 /* This object represents one field in a DDFRecord. */
503 /************************************************************************/
504 
514 class CPL_ODLL DDFField
515 {
516  public:
517  void Initialize( DDFFieldDefn *, const char *pszData,
518  int nSize );
519 
520  void Dump( FILE * fp );
521 
522  const char *GetSubfieldData( DDFSubfieldDefn *,
523  int * = NULL, int = 0 );
524 
525  const char *GetInstanceData( int nInstance, int *pnSize );
526 
531  const char *GetData() { return pachData; }
532 
534  int GetDataSize() { return nDataSize; }
535 
536  int GetRepeatCount();
537 
539  DDFFieldDefn *GetFieldDefn() { return poDefn; }
540 
541  private:
542  DDFFieldDefn *poDefn;
543 
544  int nDataSize;
545 
546  const char *pachData;
547 };
548 
549 
550 #endif /* ndef ISO8211_H_INCLUDED */
int GetSubfieldCount()
Definition: iso8211.h:215
DDFModule * GetModule()
Definition: iso8211.h:444
Definition: iso8211.h:514
DDFDataType GetType()
Definition: iso8211.h:313
const char * GetData()
Definition: iso8211.h:531
const char * GetData()
Definition: iso8211.h:438
Definition: iso8211.h:79
DDFFieldDefn * GetFieldDefn()
Definition: iso8211.h:539
DDFBinaryFormat
Definition: iso8211.h:346
void SetRepeatingFlag(int n)
Definition: iso8211.h:239
int GetDataSize()
Definition: iso8211.h:431
const char * GetName()
Definition: iso8211.h:299
Definition: iso8211.h:183
int IsRepeating()
Definition: iso8211.h:234
Definition: iso8211.h:289
int GetDataSize()
Definition: iso8211.h:534
int GetFixedWidth()
Definition: iso8211.h:227
const char * GetFormat()
Definition: iso8211.h:302
int GetWidth()
Definition: iso8211.h:335
const char * GetName()
Definition: iso8211.h:207
int GetFieldCount()
Definition: iso8211.h:408
Definition: iso8211.h:396
const char * GetDescription()
Definition: iso8211.h:212
int GetFieldCount()
Definition: iso8211.h:108