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