Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: SDTS Translator
4 : * Purpose: Dump 8211 file in verbose form - just a junk program.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1999, Frank Warmerdam
9 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include <stdio.h>
15 : #include "iso8211.h"
16 : #include "cpl_vsi.h"
17 : #include "cpl_string.h"
18 :
19 2 : int main(int nArgc, char **papszArgv)
20 :
21 : {
22 4 : DDFModule oModule;
23 2 : const char *pszFilename = nullptr;
24 2 : bool bFSPTHack = false;
25 2 : bool bXML = false;
26 2 : bool bAllDetails = false;
27 :
28 : /* -------------------------------------------------------------------- */
29 : /* Check arguments. */
30 : /* -------------------------------------------------------------------- */
31 6 : for (int iArg = 1; iArg < nArgc; iArg++)
32 : {
33 4 : if (EQUAL(papszArgv[iArg], "-fspt_repeating"))
34 : {
35 0 : bFSPTHack = true;
36 : }
37 4 : else if (EQUAL(papszArgv[iArg], "-xml"))
38 : {
39 0 : bXML = true;
40 : }
41 4 : else if (EQUAL(papszArgv[iArg], "-xml_all_details"))
42 : {
43 2 : bXML = TRUE;
44 2 : bAllDetails = true;
45 : }
46 : else
47 : {
48 2 : pszFilename = papszArgv[iArg];
49 : }
50 : }
51 :
52 2 : if (pszFilename == nullptr)
53 : {
54 0 : printf("Usage: 8211dump [-xml|-xml_all_details] "
55 : "[-fspt_repeating] filename\n");
56 0 : exit(1);
57 : }
58 :
59 : /* -------------------------------------------------------------------- */
60 : /* Open file. */
61 : /* -------------------------------------------------------------------- */
62 2 : if (!oModule.Open(pszFilename))
63 0 : exit(1);
64 :
65 : /* -------------------------------------------------------------------- */
66 : /* Apply FSPT hack if required. */
67 : /* -------------------------------------------------------------------- */
68 2 : if (bFSPTHack)
69 : {
70 0 : DDFFieldDefn *poFSPT = oModule.FindFieldDefn("FSPT");
71 :
72 0 : if (poFSPT == nullptr)
73 0 : fprintf(stderr,
74 : "unable to find FSPT field to set repeating flag.\n");
75 : else
76 0 : poFSPT->SetRepeatingFlag(TRUE);
77 : }
78 :
79 : /* -------------------------------------------------------------------- */
80 : /* Dump header, and all records. */
81 : /* -------------------------------------------------------------------- */
82 2 : if (bXML)
83 : {
84 2 : printf("<DDFModule");
85 2 : if (bAllDetails)
86 : {
87 2 : printf(" _interchangeLevel=\"%c\"", oModule.GetInterchangeLevel());
88 2 : printf(" _leaderIden=\"%c\"", oModule.GetLeaderIden());
89 2 : printf(" _inlineCodeExtensionIndicator=\"%c\"",
90 2 : oModule.GetCodeExtensionIndicator());
91 2 : printf(" _versionNumber=\"%c\"", oModule.GetVersionNumber());
92 2 : printf(" _appIndicator=\"%c\"", oModule.GetAppIndicator());
93 2 : printf(" _extendedCharSet=\"%c%c%c\"",
94 2 : oModule.GetExtendedCharSet()[0],
95 2 : oModule.GetExtendedCharSet()[1],
96 2 : oModule.GetExtendedCharSet()[2]);
97 2 : printf(" _fieldControlLength=\"%d\"",
98 : oModule.GetFieldControlLength());
99 2 : printf(" _sizeFieldLength=\"%d\"", oModule.GetSizeFieldLength());
100 2 : printf(" _sizeFieldPos=\"%d\"", oModule.GetSizeFieldPos());
101 2 : printf(" _sizeFieldTag=\"%d\"", oModule.GetSizeFieldTag());
102 : }
103 2 : printf(">\n");
104 :
105 2 : int nFieldDefnCount = oModule.GetFieldCount();
106 42 : for (int i = 0; i < nFieldDefnCount; i++)
107 : {
108 40 : DDFFieldDefn *poFieldDefn = oModule.GetField(i);
109 40 : const char *pszDataStructCode = nullptr;
110 40 : switch (poFieldDefn->GetDataStructCode())
111 : {
112 4 : case dsc_elementary:
113 4 : pszDataStructCode = "elementary";
114 4 : break;
115 :
116 20 : case dsc_vector:
117 20 : pszDataStructCode = "vector";
118 20 : break;
119 :
120 16 : case dsc_array:
121 16 : pszDataStructCode = "array";
122 16 : break;
123 :
124 0 : case dsc_concatenated:
125 0 : pszDataStructCode = "concatenated";
126 0 : break;
127 :
128 0 : default:
129 0 : pszDataStructCode = "(unknown)";
130 0 : break;
131 : }
132 :
133 40 : const char *pszDataTypeCode = nullptr;
134 40 : switch (poFieldDefn->GetDataTypeCode())
135 : {
136 2 : case dtc_char_string:
137 2 : pszDataTypeCode = "char_string";
138 2 : break;
139 :
140 0 : case dtc_implicit_point:
141 0 : pszDataTypeCode = "implicit_point";
142 0 : break;
143 :
144 0 : case dtc_explicit_point:
145 0 : pszDataTypeCode = "explicit_point";
146 0 : break;
147 :
148 0 : case dtc_explicit_point_scaled:
149 0 : pszDataTypeCode = "explicit_point_scaled";
150 0 : break;
151 :
152 0 : case dtc_char_bit_string:
153 0 : pszDataTypeCode = "char_bit_string";
154 0 : break;
155 :
156 6 : case dtc_bit_string:
157 6 : pszDataTypeCode = "bit_string";
158 6 : break;
159 :
160 32 : case dtc_mixed_data_type:
161 32 : pszDataTypeCode = "mixed_data_type";
162 32 : break;
163 :
164 0 : default:
165 0 : pszDataTypeCode = "(unknown)";
166 0 : break;
167 : }
168 :
169 40 : printf("<DDFFieldDefn tag=\"%s\" fieldName=\"%s\""
170 : " dataStructCode=\"%s\" dataTypeCode=\"%s\"",
171 : poFieldDefn->GetName(), poFieldDefn->GetDescription(),
172 : pszDataStructCode, pszDataTypeCode);
173 40 : int nSubfieldCount = poFieldDefn->GetSubfieldCount();
174 40 : if (bAllDetails || nSubfieldCount == 0)
175 : {
176 40 : printf(" arrayDescr=\"%s\"", poFieldDefn->GetArrayDescr());
177 40 : printf(" formatControls=\"%s\"",
178 : poFieldDefn->GetFormatControls());
179 : }
180 40 : printf(">\n");
181 218 : for (const auto &poSubFieldDefn : poFieldDefn->GetSubfields())
182 : {
183 178 : printf(" <DDFSubfieldDefn name=\"%s\" format=\"%s\"/>\n",
184 : poSubFieldDefn->GetName(), poSubFieldDefn->GetFormat());
185 : }
186 40 : printf("</DDFFieldDefn>\n");
187 : }
188 :
189 : // DDFRecord *poRecord;
190 142 : for (DDFRecord *poRecord = oModule.ReadRecord(); poRecord != nullptr;
191 140 : poRecord = oModule.ReadRecord())
192 : {
193 140 : printf("<DDFRecord");
194 140 : if (bAllDetails)
195 : {
196 140 : if (poRecord->GetReuseHeader())
197 0 : printf(" reuseHeader=\"1\"");
198 140 : printf(" dataSize=\"%d\"", poRecord->GetDataSize());
199 140 : printf(" _sizeFieldTag=\"%d\"", poRecord->GetSizeFieldTag());
200 140 : printf(" _sizeFieldPos=\"%d\"", poRecord->GetSizeFieldPos());
201 140 : printf(" _sizeFieldLength=\"%d\"",
202 : poRecord->GetSizeFieldLength());
203 : }
204 140 : printf(">\n");
205 140 : int nFieldCount = poRecord->GetFieldCount();
206 706 : for (int iField = 0; iField < nFieldCount; iField++)
207 : {
208 566 : const DDFField *poField = poRecord->GetField(iField);
209 566 : const DDFFieldDefn *poDefn = poField->GetFieldDefn();
210 566 : const char *pszFieldName = poDefn->GetName();
211 566 : printf(" <DDFField name=\"%s\"", pszFieldName);
212 566 : if (poField->GetRepeatCount() > 1)
213 104 : printf(" repeatCount=\"%d\"", poField->GetRepeatCount());
214 566 : int iOffset = 0, nLoopCount;
215 566 : int nRepeatCount = poField->GetRepeatCount();
216 566 : const char *pachData = poField->GetData();
217 566 : int nDataSize = poField->GetDataSize();
218 566 : if (nRepeatCount == 1 && poDefn->GetSubfieldCount() == 0)
219 : {
220 140 : printf(" value=\"0x");
221 420 : for (int i = 0; i < nDataSize - 1; i++)
222 280 : printf("%02X", pachData[i]);
223 140 : printf("\">\n");
224 : }
225 : else
226 426 : printf(">\n");
227 1438 : for (nLoopCount = 0; nLoopCount < nRepeatCount; nLoopCount++)
228 : {
229 3506 : for (const auto &poSubFieldDefn : poDefn->GetSubfields())
230 : {
231 : int nBytesConsumed;
232 2634 : const char *pszSubFieldName = poSubFieldDefn->GetName();
233 2634 : printf(" <DDFSubfield name=\"%s\" ",
234 : pszSubFieldName);
235 2634 : DDFDataType eType = poSubFieldDefn->GetType();
236 2634 : const char *pachSubdata = pachData + iOffset;
237 2634 : int nMaxBytes = nDataSize - iOffset;
238 2634 : if (eType == DDFFloat)
239 2 : printf("type=\"float\">%f",
240 : poSubFieldDefn->ExtractFloatData(
241 : pachSubdata, nMaxBytes, nullptr));
242 2632 : else if (eType == DDFInt)
243 2248 : printf("type=\"integer\">%d",
244 : poSubFieldDefn->ExtractIntData(
245 : pachSubdata, nMaxBytes, nullptr));
246 384 : else if (eType == DDFBinaryString)
247 : {
248 : int nBytes, i;
249 : GByte *pabyBString =
250 270 : (GByte *)poSubFieldDefn->ExtractStringData(
251 : pachSubdata, nMaxBytes, &nBytes);
252 :
253 270 : printf("type=\"binary\">0x");
254 1620 : for (i = 0; i < nBytes; i++)
255 1350 : printf("%02X", pabyBString[i]);
256 : }
257 : else
258 : {
259 : GByte *pabyString =
260 114 : (GByte *)poSubFieldDefn->ExtractStringData(
261 : pachSubdata, nMaxBytes, nullptr);
262 114 : int bBinary = FALSE;
263 : int i;
264 286 : for (i = 0; pabyString[i] != '\0'; i++)
265 : {
266 172 : if (pabyString[i] < 32 || pabyString[i] > 127)
267 : {
268 0 : bBinary = TRUE;
269 0 : break;
270 : }
271 : }
272 114 : if (bBinary)
273 : {
274 0 : printf("type=\"binary\">0x");
275 0 : for (i = 0; pabyString[i] != '\0'; i++)
276 0 : printf("%02X", pabyString[i]);
277 : }
278 : else
279 : {
280 114 : char *pszEscaped = CPLEscapeString(
281 : (const char *)pabyString, -1, CPLES_XML);
282 114 : printf("type=\"string\">%s", pszEscaped);
283 114 : CPLFree(pszEscaped);
284 : }
285 : }
286 2634 : printf("</DDFSubfield>\n");
287 :
288 2634 : poSubFieldDefn->GetDataLength(pachSubdata, nMaxBytes,
289 : &nBytesConsumed);
290 :
291 2634 : iOffset += nBytesConsumed;
292 : }
293 : }
294 566 : printf(" </DDFField>\n");
295 : }
296 140 : printf("</DDFRecord>\n");
297 : }
298 2 : printf("</DDFModule>\n");
299 : }
300 : else
301 : {
302 0 : oModule.Dump(stdout);
303 : long nStartLoc;
304 :
305 0 : nStartLoc = VSIFTellL(oModule.GetFP());
306 0 : for (DDFRecord *poRecord = oModule.ReadRecord(); poRecord != nullptr;
307 0 : poRecord = oModule.ReadRecord())
308 : {
309 0 : printf("File Offset: %ld\n", nStartLoc);
310 0 : poRecord->Dump(stdout);
311 :
312 0 : nStartLoc = VSIFTellL(oModule.GetFP());
313 : }
314 : }
315 :
316 2 : oModule.Close();
317 2 : }
|