1 : /******************************************************************************
2 : * $Id: sdtscatd.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: SDTS Translator
5 : * Purpose: Implementation of SDTS_CATD and SDTS_CATDEntry classes for
6 : * reading CATD files.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 1999, Frank Warmerdam
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "sdts_al.h"
32 :
33 : CPL_CVSID("$Id: sdtscatd.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 :
36 : /************************************************************************/
37 : /* ==================================================================== */
38 : /* SDTS_CATDEntry */
39 : /* */
40 : /* This class is for internal use of the SDTS_CATD class only, */
41 : /* and represents one entry in the directory ... a reference */
42 : /* to another module file. */
43 : /* ==================================================================== */
44 : /************************************************************************/
45 :
46 : class SDTS_CATDEntry
47 :
48 : {
49 : public:
50 : char * pszModule;
51 : char * pszType;
52 : char * pszFile;
53 : char * pszExternalFlag;
54 :
55 : char * pszFullPath;
56 : };
57 :
58 : /************************************************************************/
59 : /* ==================================================================== */
60 : /* SDTS_CATD */
61 : /* ==================================================================== */
62 : /************************************************************************/
63 :
64 : /************************************************************************/
65 : /* SDTS_CATD() */
66 : /************************************************************************/
67 :
68 12 : SDTS_CATD::SDTS_CATD()
69 :
70 : {
71 12 : nEntries = 0;
72 12 : papoEntries = NULL;
73 12 : pszPrefixPath = NULL;
74 12 : }
75 :
76 : /************************************************************************/
77 : /* ~SDTS_CATD() */
78 : /************************************************************************/
79 :
80 12 : SDTS_CATD::~SDTS_CATD()
81 : {
82 : int i;
83 :
84 72 : for( i = 0; i < nEntries; i++ )
85 : {
86 60 : CPLFree( papoEntries[i]->pszModule );
87 60 : CPLFree( papoEntries[i]->pszType );
88 60 : CPLFree( papoEntries[i]->pszFile );
89 60 : CPLFree( papoEntries[i]->pszExternalFlag );
90 60 : CPLFree( papoEntries[i]->pszFullPath );
91 60 : delete papoEntries[i];
92 : }
93 :
94 12 : CPLFree( papoEntries );
95 12 : CPLFree( pszPrefixPath );
96 12 : }
97 :
98 : /************************************************************************/
99 : /* Read() */
100 : /* */
101 : /* Read the named file to initialize this structure. */
102 : /************************************************************************/
103 :
104 12 : int SDTS_CATD::Read( const char * pszFilename )
105 :
106 : {
107 12 : DDFModule oCATDFile;
108 : DDFRecord *poRecord;
109 :
110 : /* -------------------------------------------------------------------- */
111 : /* Open the file. */
112 : /* -------------------------------------------------------------------- */
113 12 : if( !oCATDFile.Open( pszFilename ) )
114 0 : return FALSE;
115 :
116 12 : CPLErrorReset(); // clear any ADRG "unrecognised data_struct_code" errors
117 :
118 : /* -------------------------------------------------------------------- */
119 : /* Does this file have a CATD field? If not, it isn't an SDTS */
120 : /* record and we won't even try reading the first record for */
121 : /* fear it will we a huge honking ADRG data record or something. */
122 : /* -------------------------------------------------------------------- */
123 12 : if( oCATDFile.FindFieldDefn( "CATD" ) == NULL )
124 9 : return FALSE;
125 :
126 : /* -------------------------------------------------------------------- */
127 : /* Strip off the filename, and keep the path prefix. */
128 : /* -------------------------------------------------------------------- */
129 : int i;
130 :
131 3 : pszPrefixPath = CPLStrdup( pszFilename );
132 39 : for( i = strlen(pszPrefixPath)-1; i > 0; i-- )
133 : {
134 39 : if( pszPrefixPath[i] == '\\' || pszPrefixPath[i] == '/' )
135 : {
136 3 : pszPrefixPath[i] = '\0';
137 3 : break;
138 : }
139 : }
140 :
141 3 : if( i <= 0 )
142 : {
143 0 : strcpy( pszPrefixPath, "." );
144 : }
145 :
146 : /* ==================================================================== */
147 : /* Loop reading CATD records, and adding to our list of entries */
148 : /* for each. */
149 : /* ==================================================================== */
150 66 : while( (poRecord = oCATDFile.ReadRecord()) != NULL )
151 : {
152 : /* -------------------------------------------------------------------- */
153 : /* Verify that we have a proper CATD record. */
154 : /* -------------------------------------------------------------------- */
155 60 : if( poRecord->GetStringSubfield( "CATD", 0, "MODN", 0 ) == NULL )
156 0 : continue;
157 :
158 : /* -------------------------------------------------------------------- */
159 : /* Create a new entry, and get the module and file name. */
160 : /* -------------------------------------------------------------------- */
161 60 : SDTS_CATDEntry *poEntry = new SDTS_CATDEntry;
162 :
163 : poEntry->pszModule =
164 60 : CPLStrdup(poRecord->GetStringSubfield( "CATD", 0, "NAME", 0 ));
165 : poEntry->pszFile =
166 60 : CPLStrdup(poRecord->GetStringSubfield( "CATD", 0, "FILE", 0 ));
167 : poEntry->pszExternalFlag =
168 60 : CPLStrdup(poRecord->GetStringSubfield( "CATD", 0, "EXTR", 0 ));
169 : poEntry->pszType =
170 60 : CPLStrdup(poRecord->GetStringSubfield( "CATD", 0, "TYPE", 0 ));
171 :
172 : /* -------------------------------------------------------------------- */
173 : /* Create a full path to the file. */
174 : /* -------------------------------------------------------------------- */
175 : poEntry->pszFullPath =
176 : CPLStrdup(CPLFormCIFilename( pszPrefixPath, poEntry->pszFile,
177 60 : NULL ));
178 :
179 : /* -------------------------------------------------------------------- */
180 : /* Add the entry to the list. */
181 : /* -------------------------------------------------------------------- */
182 : papoEntries = (SDTS_CATDEntry **)
183 60 : CPLRealloc(papoEntries, sizeof(void*) * ++nEntries );
184 60 : papoEntries[nEntries-1] = poEntry;
185 : }
186 :
187 3 : return nEntries > 0;
188 : }
189 :
190 :
191 : /************************************************************************/
192 : /* GetModuleFilePath() */
193 : /************************************************************************/
194 :
195 28 : const char * SDTS_CATD::GetModuleFilePath( const char * pszModule )
196 :
197 : {
198 : int i;
199 :
200 188 : for( i = 0; i < nEntries; i++ )
201 : {
202 188 : if( EQUAL(papoEntries[i]->pszModule,pszModule) )
203 28 : return papoEntries[i]->pszFullPath;
204 : }
205 :
206 0 : return NULL;
207 : }
208 :
209 : /************************************************************************/
210 : /* GetEntryModule() */
211 : /************************************************************************/
212 :
213 34 : const char * SDTS_CATD::GetEntryModule( int iEntry )
214 :
215 : {
216 34 : if( iEntry < 0 || iEntry >= nEntries )
217 0 : return NULL;
218 : else
219 34 : return papoEntries[iEntry]->pszModule;
220 : }
221 :
222 : /************************************************************************/
223 : /* GetEntryTypeDesc() */
224 : /************************************************************************/
225 :
226 : /**
227 : * Fetch the type description of a module in the catalog.
228 : *
229 : * @param iEntry The module index within the CATD catalog. A number from
230 : * zero to GetEntryCount()-1.
231 : *
232 : * @return A pointer to an internal string with the type description for
233 : * this module. This is from the CATD file (subfield TYPE of field CATD),
234 : * and will be something like "Attribute Primary ".
235 : */
236 :
237 0 : const char * SDTS_CATD::GetEntryTypeDesc( int iEntry )
238 :
239 : {
240 0 : if( iEntry < 0 || iEntry >= nEntries )
241 0 : return NULL;
242 : else
243 0 : return papoEntries[iEntry]->pszType;
244 : }
245 :
246 : /************************************************************************/
247 : /* GetEntryType() */
248 : /************************************************************************/
249 :
250 : /**
251 : * Fetch the enumerated type of a module in the catalog.
252 : *
253 : * @param iEntry The module index within the CATD catalog. A number from
254 : * zero to GetEntryCount()-1.
255 : *
256 : * @return A value from the SDTSLayerType enumeration indicating the type of
257 : * the module, and indicating the corresponding type of reader.<p>
258 : *
259 : * <ul>
260 : * <li> SLTPoint: Read with SDTSPointReader, underlying type of
261 : * <tt>Point-Node</tt>.
262 : * <li> SLTLine: Read with SDTSLineReader, underlying type of
263 : * <tt>Line</tt>.
264 : * <li> SLTAttr: Read with SDTSAttrReader, underlying type of
265 : * <tt>Attribute Primary</tt> or <tt>Attribute Secondary</tt>.
266 : * <li> SLTPolygon: Read with SDTSPolygonReader, underlying type of
267 : * <tt>Polygon</tt>.
268 : * </ul>
269 : */
270 :
271 1278 : SDTSLayerType SDTS_CATD::GetEntryType( int iEntry )
272 :
273 : {
274 1278 : if( iEntry < 0 || iEntry >= nEntries )
275 0 : return SLTUnknown;
276 :
277 1278 : else if( EQUALN(papoEntries[iEntry]->pszType,"Attribute Primary",17) )
278 600 : return SLTAttr;
279 :
280 678 : else if( EQUALN(papoEntries[iEntry]->pszType,"Attribute Secondary",17) )
281 0 : return SLTAttr;
282 :
283 1356 : else if( EQUAL(papoEntries[iEntry]->pszType,"Line")
284 678 : || EQUALN(papoEntries[iEntry]->pszType,"Line ",5) )
285 93 : return SLTLine;
286 :
287 585 : else if( EQUALN(papoEntries[iEntry]->pszType,"Point-Node",10) )
288 411 : return SLTPoint;
289 :
290 174 : else if( EQUALN(papoEntries[iEntry]->pszType,"Polygon",7) )
291 118 : return SLTPoly;
292 :
293 56 : else if( EQUALN(papoEntries[iEntry]->pszType,"Cell",4) )
294 6 : return SLTRaster;
295 :
296 : else
297 50 : return SLTUnknown;
298 : }
299 :
300 : /************************************************************************/
301 : /* GetEntryFilePath() */
302 : /************************************************************************/
303 :
304 : /**
305 : * Fetch the full filename of the requested module.
306 : *
307 : * @param iEntry The module index within the CATD catalog. A number from
308 : * zero to GetEntryCount()-1.
309 : *
310 : * @return A pointer to an internal string containing the filename. This
311 : * string should not be altered, or freed by the application.
312 : */
313 :
314 8 : const char * SDTS_CATD::GetEntryFilePath( int iEntry )
315 :
316 : {
317 8 : if( iEntry < 0 || iEntry >= nEntries )
318 0 : return NULL;
319 : else
320 8 : return papoEntries[iEntry]->pszFullPath;
321 : }
|