1 /* ****************************************************************************
2  *
3  * Project:  SDTS Translator
4  * Purpose:  Example program dumping data in 8211 data to stdout.
5  * Author:   Frank Warmerdam, warmerda@home.com
6  *
7  ******************************************************************************
8  * Copyright (c) 1999, Frank Warmerdam
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #include <stdio.h>
30 #include "iso8211.h"
31 
32 CPL_CVSID("$Id: timetest.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")
33 
34 static void ViewRecordField( DDFField * poField );
35 static int ViewSubfield( DDFSubfieldDefn *poSFDefn,
36                          const char * pachFieldData,
37                          int nBytesRemaining );
38 
39 /* **********************************************************************/
40 /*                                main()                                */
41 /* **********************************************************************/
42 
main(int nArgc,char ** papszArgv)43 int main( int nArgc, char ** papszArgv )
44 
45 {
46     if( nArgc < 2 )
47     {
48         printf( "Usage: 8211view filename\n" );
49         exit( 1 );
50     }
51 
52     const char  *pszFilename = papszArgv[1];
53     DDFModule oModule;
54 
55     for( int i = 0; i < 40; i++ )
56     {
57 /* -------------------------------------------------------------------- */
58 /*      Open the file.  Note that by default errors are reported to     */
59 /*      stderr, so we don't bother doing it ourselves.                  */
60 /* -------------------------------------------------------------------- */
61         if( !oModule.Open( pszFilename ) )
62         {
63             exit( 1 );
64         }
65 
66 /* -------------------------------------------------------------------- */
67 /*      Loop reading records till there are none left.                  */
68 /* -------------------------------------------------------------------- */
69         DDFRecord *poRecord = nullptr;
70         int nRecordCount = 0;
71         int nFieldCount = 0;
72 
73         while( (poRecord = oModule.ReadRecord()) != nullptr )
74         {
75             /* ------------------------------------------------------------ */
76             /*      Loop over each field in this particular record.         */
77             /* ------------------------------------------------------------ */
78             for( int iField = 0; iField < poRecord->GetFieldCount(); iField++ )
79             {
80                 DDFField *poField = poRecord->GetField( iField );
81 
82                 ViewRecordField( poField );
83 
84                 nFieldCount++;
85             }
86 
87             nRecordCount++;
88         }
89 
90         oModule.Close();
91 
92         printf( "Read %d records, %d fields.\n", nRecordCount, nFieldCount );
93     }
94 }
95 
96 /* **********************************************************************/
97 /*                          ViewRecordField()                           */
98 /*                                                                      */
99 /*      Dump the contents of a field instance in a record.              */
100 /* **********************************************************************/
101 
ViewRecordField(DDFField * poField)102 static void ViewRecordField( DDFField * poField )
103 
104 {
105     DDFFieldDefn *poFieldDefn = poField->GetFieldDefn();
106 
107     // Get pointer to this fields raw data.  We will move through
108     // it consuming data as we report subfield values.
109 
110     const char  *pachFieldData = poField->GetData();
111     int nBytesRemaining = poField->GetDataSize();
112 
113     /* -------------------------------------------------------- */
114     /*      Loop over the repeat count for this fields          */
115     /*      subfields.  The repeat count will almost            */
116     /*      always be one.                                      */
117     /* -------------------------------------------------------- */
118 
119     int nRepeatCount = poField->GetRepeatCount();
120 
121     for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ )
122     {
123 
124         /* -------------------------------------------------------- */
125         /*   Loop over all the subfields of this field, advancing   */
126         /*   the data pointer as we consume data.                   */
127         /* -------------------------------------------------------- */
128         for( int iSF = 0; iSF < poFieldDefn->GetSubfieldCount(); iSF++ )
129         {
130             DDFSubfieldDefn *poSFDefn = poFieldDefn->GetSubfield( iSF );
131             int nBytesConsumed =
132                 ViewSubfield( poSFDefn, pachFieldData, nBytesRemaining );
133 
134             nBytesRemaining -= nBytesConsumed;
135             pachFieldData += nBytesConsumed;
136         }
137     }
138 }
139 
140 /* **********************************************************************/
141 /*                            ViewSubfield()                            */
142 /* **********************************************************************/
143 
ViewSubfield(DDFSubfieldDefn * poSFDefn,const char * pachFieldData,int nBytesRemaining)144 static int ViewSubfield( DDFSubfieldDefn *poSFDefn,
145                          const char * pachFieldData,
146                          int nBytesRemaining )
147 
148 {
149     int         nBytesConsumed = 0;
150 
151     switch( poSFDefn->GetType() )
152     {
153       case DDFInt:
154         poSFDefn->ExtractIntData( pachFieldData, nBytesRemaining,
155                                   &nBytesConsumed );
156         break;
157 
158       case DDFFloat:
159         poSFDefn->ExtractFloatData( pachFieldData, nBytesRemaining,
160                                     &nBytesConsumed );
161         break;
162 
163       case DDFString:
164         poSFDefn->ExtractStringData( pachFieldData, nBytesRemaining,
165                                      &nBytesConsumed );
166         break;
167 
168       default:
169         break;
170     }
171 
172     return nBytesConsumed;
173 }
174