SDTS_AL
iso8211.h
1 /******************************************************************************
2  * $Id: iso8211.h 25841 2013-04-02 21:30:03Z rouault $
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 long 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 
80 class CPL_ODLL DDFModule
81 {
82  public:
83  DDFModule();
84  ~DDFModule();
85 
86  int Open( const char * pszFilename, int bFailQuietly = FALSE );
87  int Create( const char *pszFilename );
88  void Close();
89 
90  int Initialize( char chInterchangeLevel = '3',
91  char chLeaderIden = 'L',
92  char chCodeExtensionIndicator = 'E',
93  char chVersionNumber = '1',
94  char chAppIndicator = ' ',
95  const char *pszExtendedCharSet = " ! ",
96  int nSizeFieldLength = 3,
97  int nSizeFieldPos = 4,
98  int nSizeFieldTag = 4 );
99 
100  void Dump( FILE * fp );
101 
102  DDFRecord *ReadRecord( void );
103  void Rewind( long nOffset = -1 );
104 
105  DDFFieldDefn *FindFieldDefn( const char * );
106 
109  int GetFieldCount() { return nFieldDefnCount; }
110  DDFFieldDefn *GetField(int);
111  void AddField( DDFFieldDefn *poNewFDefn );
112 
113  // This is really just for internal use.
114  int GetFieldControlLength() { return _fieldControlLength; }
115  void AddCloneRecord( DDFRecord * );
116  void RemoveCloneRecord( DDFRecord * );
117 
118  // This is just for DDFRecord.
119  VSILFILE *GetFP() { return fpDDF; }
120  int GetSizeFieldTag() const { return (int)_sizeFieldTag; }
121 
122  private:
123  VSILFILE *fpDDF;
124  int bReadOnly;
125  long nFirstRecordOffset;
126 
127  char _interchangeLevel;
128  char _inlineCodeExtensionIndicator;
129  char _versionNumber;
130  char _appIndicator;
131  int _fieldControlLength;
132  char _extendedCharSet[4];
133 
134  long _recLength;
135  char _leaderIden;
136  long _fieldAreaStart;
137  long _sizeFieldLength;
138  long _sizeFieldPos;
139  long _sizeFieldTag;
140 
141  // One DirEntry per field.
142  int nFieldDefnCount;
143  DDFFieldDefn **papoFieldDefns;
144 
145  DDFRecord *poRecord;
146 
147  int nCloneCount;
148  int nMaxCloneCount;
149  DDFRecord **papoClones;
150 };
151 
152 /************************************************************************/
153 /* DDFFieldDefn */
154 /************************************************************************/
155 
156  typedef enum { dsc_elementary, dsc_vector, dsc_array, dsc_concatenated } DDF_data_struct_code;
157  typedef enum { dtc_char_string,
158  dtc_implicit_point,
159  dtc_explicit_point,
160  dtc_explicit_point_scaled,
161  dtc_char_bit_string,
162  dtc_bit_string,
163  dtc_mixed_data_type } DDF_data_type_code;
164 
172 class CPL_ODLL DDFFieldDefn
173 {
174  public:
175  DDFFieldDefn();
176  ~DDFFieldDefn();
177 
178  int Create( const char *pszTag, const char *pszFieldName,
179  const char *pszDescription,
180  DDF_data_struct_code eDataStructCode,
181  DDF_data_type_code eDataTypeCode,
182  const char *pszFormat = NULL );
183  void AddSubfield( DDFSubfieldDefn *poNewSFDefn,
184  int bDontAddToFormat = FALSE );
185  void AddSubfield( const char *pszName, const char *pszFormat );
186  int GenerateDDREntry( char **ppachData, int *pnLength );
187 
188  int Initialize( DDFModule * poModule, const char *pszTag,
189  int nSize, const char * pachRecord );
190 
191  void Dump( FILE * fp );
192 
196  const char *GetName() { return pszTag; }
197 
201  const char *GetDescription() { return _fieldName; }
202 
204  int GetSubfieldCount() { return nSubfieldCount; }
205 
206  DDFSubfieldDefn *GetSubfield( int i );
207  DDFSubfieldDefn *FindSubfieldDefn( const char * );
208 
216  int GetFixedWidth() { return nFixedWidth; }
217 
223  int IsRepeating() { return bRepeatingSubfields; }
224 
225  static char *ExpandFormat( const char * );
226 
228  void SetRepeatingFlag( int n ) { bRepeatingSubfields = n; }
229 
230  char *GetDefaultValue( int *pnSize );
231 
232  const char *GetArrayDescr() const { return _arrayDescr; }
233  const char *GetFormatControls() const { return _formatControls; }
234  DDF_data_struct_code GetDataStructCode() const { return _data_struct_code; }
235  DDF_data_type_code GetDataTypeCode() const { return _data_type_code; }
236 
237  private:
238 
239  static char *ExtractSubstring( const char * );
240 
241  DDFModule * poModule;
242  char * pszTag;
243 
244  char * _fieldName;
245  char * _arrayDescr;
246  char * _formatControls;
247 
248  int bRepeatingSubfields;
249  int nFixedWidth; // zero if variable.
250 
251  int BuildSubfields();
252  int ApplyFormats();
253 
254  DDF_data_struct_code _data_struct_code;
255 
256  DDF_data_type_code _data_type_code;
257 
258  int nSubfieldCount;
259  DDFSubfieldDefn **papoSubfields;
260 };
261 
262 /************************************************************************/
263 /* DDFSubfieldDefn */
264 /* */
265 /* Information from the DDR record for one subfield of a */
266 /* particular field. */
267 /************************************************************************/
268 
276 class CPL_ODLL DDFSubfieldDefn
277 {
278 public:
279 
280  DDFSubfieldDefn();
281  ~DDFSubfieldDefn();
282 
283  void SetName( const char * pszName );
284 
286  const char *GetName() { return pszName; }
287 
289  const char *GetFormat() { return pszFormatString; }
290  int SetFormat( const char * pszFormat );
291 
300  DDFDataType GetType() { return eType; }
301 
302  double ExtractFloatData( const char *pachData, int nMaxBytes,
303  int * pnConsumedBytes );
304  int ExtractIntData( const char *pachData, int nMaxBytes,
305  int * pnConsumedBytes );
306  const char *ExtractStringData( const char *pachData, int nMaxBytes,
307  int * pnConsumedBytes );
308  int GetDataLength( const char *, int, int * );
309  void DumpData( const char *pachData, int nMaxBytes, FILE * fp );
310 
311  int FormatStringValue( char *pachData, int nBytesAvailable,
312  int *pnBytesUsed, const char *pszValue,
313  int nValueLength = -1 );
314 
315  int FormatIntValue( char *pachData, int nBytesAvailable,
316  int *pnBytesUsed, int nNewValue );
317 
318  int FormatFloatValue( char *pachData, int nBytesAvailable,
319  int *pnBytesUsed, double dfNewValue );
320 
322  int GetWidth() { return nFormatWidth; } // zero for variable.
323 
324  int GetDefaultValue( char *pachData, int nBytesAvailable,
325  int *pnBytesUsed );
326 
327  void Dump( FILE * fp );
328 
333 typedef enum {
334  NotBinary=0,
335  UInt=1,
336  SInt=2,
337  FPReal=3,
338  FloatReal=4,
339  FloatComplex=5
340 } DDFBinaryFormat;
341 
342  DDFBinaryFormat GetBinaryFormat(void) const { return eBinaryFormat; }
343 
344 
345 private:
346 
347  char *pszName; // a.k.a. subfield mnemonic
348  char *pszFormatString;
349 
350  DDFDataType eType;
351  DDFBinaryFormat eBinaryFormat;
352 
353 /* -------------------------------------------------------------------- */
354 /* bIsVariable determines whether we using the */
355 /* chFormatDelimeter (TRUE), or the fixed width (FALSE). */
356 /* -------------------------------------------------------------------- */
357  int bIsVariable;
358 
359  char chFormatDelimeter;
360  int nFormatWidth;
361 
362 /* -------------------------------------------------------------------- */
363 /* Fetched string cache. This is where we hold the values */
364 /* returned from ExtractStringData(). */
365 /* -------------------------------------------------------------------- */
366  int nMaxBufChars;
367  char *pachBuffer;
368 };
369 
370 /************************************************************************/
371 /* DDFRecord */
372 /* */
373 /* Class that contains one DR record from a file. We read into */
374 /* the same record object repeatedly to ensure that repeated */
375 /* leaders can be easily preserved. */
376 /************************************************************************/
377 
383 class CPL_ODLL DDFRecord
384 {
385  public:
386  DDFRecord( DDFModule * );
387  ~DDFRecord();
388 
389  DDFRecord *Clone();
390  DDFRecord *CloneOn( DDFModule * );
391 
392  void Dump( FILE * );
393 
395  int GetFieldCount() { return nFieldCount; }
396 
397  DDFField *FindField( const char *, int = 0 );
398  DDFField *GetField( int );
399 
400  int GetIntSubfield( const char *, int, const char *, int,
401  int * = NULL );
402  double GetFloatSubfield( const char *, int, const char *, int,
403  int * = NULL );
404  const char *GetStringSubfield( const char *, int, const char *, int,
405  int * = NULL );
406 
407  int SetIntSubfield( const char *pszField, int iFieldIndex,
408  const char *pszSubfield, int iSubfieldIndex,
409  int nValue );
410  int SetStringSubfield( const char *pszField, int iFieldIndex,
411  const char *pszSubfield, int iSubfieldIndex,
412  const char *pszValue, int nValueLength=-1 );
413  int SetFloatSubfield( const char *pszField, int iFieldIndex,
414  const char *pszSubfield, int iSubfieldIndex,
415  double dfNewValue );
416 
418  int GetDataSize() { return nDataSize; }
419 
425  const char *GetData() { return pachData; }
426 
431  DDFModule * GetModule() { return poModule; }
432 
433  int ResizeField( DDFField *poField, int nNewDataSize );
434  int DeleteField( DDFField *poField );
435  DDFField* AddField( DDFFieldDefn * );
436 
437  int CreateDefaultFieldInstance( DDFField *poField, int iIndexWithinField );
438 
439  int SetFieldRaw( DDFField *poField, int iIndexWithinField,
440  const char *pachRawData, int nRawDataSize );
441  int UpdateFieldRaw( DDFField *poField, int iIndexWithinField,
442  int nStartOffset, int nOldSize,
443  const char *pachRawData, int nRawDataSize );
444 
445  int Write();
446 
447  // This is really just for the DDFModule class.
448  int Read();
449  void Clear();
450  int ResetDirectory();
451 
452  private:
453 
454  int ReadHeader();
455 
456  DDFModule *poModule;
457 
458  int nReuseHeader;
459 
460  int nFieldOffset; // field data area, not dir entries.
461 
462  int _sizeFieldTag;
463  int _sizeFieldPos;
464  int _sizeFieldLength;
465 
466  int nDataSize; // Whole record except leader with header
467  char *pachData;
468 
469  int nFieldCount;
470  DDFField *paoFields;
471 
472  int bIsClone;
473 };
474 
475 /************************************************************************/
476 /* DDFField */
477 /* */
478 /* This object represents one field in a DDFRecord. */
479 /************************************************************************/
480 
490 class CPL_ODLL DDFField
491 {
492  public:
493  void Initialize( DDFFieldDefn *, const char *pszData,
494  int nSize );
495 
496  void Dump( FILE * fp );
497 
498  const char *GetSubfieldData( DDFSubfieldDefn *,
499  int * = NULL, int = 0 );
500 
501  const char *GetInstanceData( int nInstance, int *pnSize );
502 
507  const char *GetData() { return pachData; }
508 
510  int GetDataSize() { return nDataSize; }
511 
512  int GetRepeatCount();
513 
515  DDFFieldDefn *GetFieldDefn() { return poDefn; }
516 
517  private:
518  DDFFieldDefn *poDefn;
519 
520  int nDataSize;
521 
522  const char *pachData;
523 };
524 
525 
526 #endif /* ndef _ISO8211_H_INCLUDED */
int GetSubfieldCount()
Definition: iso8211.h:204
DDFModule * GetModule()
Definition: iso8211.h:431
Definition: iso8211.h:490
DDFDataType GetType()
Definition: iso8211.h:300
const char * GetData()
Definition: iso8211.h:507
const char * GetData()
Definition: iso8211.h:425
Definition: iso8211.h:80
DDFFieldDefn * GetFieldDefn()
Definition: iso8211.h:515
void SetRepeatingFlag(int n)
Definition: iso8211.h:228
int GetDataSize()
Definition: iso8211.h:418
const char * GetName()
Definition: iso8211.h:286
Definition: iso8211.h:172
int IsRepeating()
Definition: iso8211.h:223
Definition: iso8211.h:276
int GetDataSize()
Definition: iso8211.h:510
int GetFixedWidth()
Definition: iso8211.h:216
const char * GetFormat()
Definition: iso8211.h:289
int GetWidth()
Definition: iso8211.h:322
const char * GetName()
Definition: iso8211.h:196
int GetFieldCount()
Definition: iso8211.h:395
Definition: iso8211.h:383
const char * GetDescription()
Definition: iso8211.h:201
int GetFieldCount()
Definition: iso8211.h:109