s57classregistrar.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 605 行 · 第 1/2 页

CPP
605
字号
/****************************************************************************** * $Id: s57classregistrar.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project:  S-57 Translator * Purpose:  Implements S57ClassRegistrar class for keeping track of *           information on S57 object classes. * 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 "s57.h"#include "cpl_conv.h"#include "cpl_string.h"CPL_CVSID("$Id: s57classregistrar.cpp 10646 2007-01-18 02:38:10Z warmerdam $");#ifdef S57_BUILTIN_CLASSES#include "s57tables.h"#endif/************************************************************************//*                         S57ClassRegistrar()                          *//************************************************************************/S57ClassRegistrar::S57ClassRegistrar(){    nClasses = 0;    papszClassesInfo = NULL;        iCurrentClass = -1;    papszCurrentFields = NULL;    papszTempResult = NULL;    papszNextLine = NULL;    papapszClassesFields = NULL;    pachAttrClass = NULL;    pachAttrType = NULL;    panAttrIndex = NULL;    papszAttrNames = NULL;    papszAttrAcronym = NULL;    papapszAttrValues = NULL;}/************************************************************************//*                         ~S57ClassRegistrar()                         *//************************************************************************/S57ClassRegistrar::~S57ClassRegistrar(){    int i;    CSLDestroy( papszClassesInfo );    CSLDestroy( papszTempResult );        if( papapszClassesFields != NULL )    {        for( i = 0; i < nClasses; i++ )            CSLDestroy( papapszClassesFields[i] );        CPLFree( papapszClassesFields );    }    if( papszAttrNames )    {        for( i = 0; i < MAX_ATTRIBUTES; i++ )        {            CPLFree( papszAttrNames[i] );            CPLFree( papszAttrAcronym[i] );        }        CPLFree( papszAttrNames );        CPLFree( papszAttrAcronym );    }    CPLFree( pachAttrType );    CPLFree( pachAttrClass );    CPLFree( panAttrIndex );}/************************************************************************//*                              FindFile()                              *//************************************************************************/int S57ClassRegistrar::FindFile( const char *pszTarget,                                  const char *pszDirectory,                                  int bReportErr,                                 FILE **pfp ){    const char *pszFilename;        if( pszDirectory == NULL )    {        pszFilename = CPLFindFile( "s57", pszTarget );        if( pszFilename == NULL )            pszFilename = pszTarget;    }    else    {        pszFilename = CPLFormFilename( pszDirectory, pszTarget, NULL );    }    *pfp = VSIFOpen( pszFilename, "rb" );#ifdef S57_BUILTIN_CLASSES    if( *pfp == NULL )    {        if( EQUAL(pszTarget, "s57objectclasses.csv") )            papszNextLine = gpapszS57Classes;        else            papszNextLine = gpapszS57attributes;    }#else    if( *pfp == NULL )    {        if( bReportErr )            CPLError( CE_Failure, CPLE_OpenFailed,                      "Failed to open %s.\n",                      pszFilename );        return FALSE;    }#endif    return TRUE;}/************************************************************************//*                              ReadLine()                              *//*                                                                      *//*      Read a line from the provided file, or from the "built-in"      *//*      configuration file line list if the file is NULL.               *//************************************************************************/const char *S57ClassRegistrar::ReadLine( FILE * fp ){    if( fp != NULL )        return CPLReadLine( fp );    if( papszNextLine == NULL )        return NULL;    if( *papszNextLine == NULL )    {        papszNextLine = NULL;        return NULL;    }    else        return *(papszNextLine++);}/************************************************************************//*                              LoadInfo()                              *//************************************************************************/int S57ClassRegistrar::LoadInfo( const char * pszDirectory,                                  const char * pszProfile,                                 int bReportErr ){    FILE        *fp;    char        szTargetFile[1024];    if( pszDirectory == NULL )        pszDirectory = CPLGetConfigOption("S57_CSV",NULL);/* ==================================================================== *//*      Read the s57objectclasses file.                                 *//* ==================================================================== */    if( pszProfile == NULL )        pszProfile = CPLGetConfigOption( "S57_PROFILE", "" );        if( EQUAL(pszProfile, "Additional_Military_Layers") )    {       sprintf( szTargetFile, "s57objectclasses_%s.csv", "aml" );    }    else if ( EQUAL(pszProfile, "Inland_Waterways") )    {       sprintf( szTargetFile, "s57objectclasses_%s.csv", "iw" );    }    else if( strlen(pszProfile) > 0 )    {       sprintf( szTargetFile, "s57objectclasses_%s.csv", pszProfile );    }    else    {       strcpy( szTargetFile, "s57objectclasses.csv" );    }    if( !FindFile( szTargetFile, pszDirectory, bReportErr, &fp ) )        return FALSE;/* -------------------------------------------------------------------- *//*      Skip the line defining the column titles.                       *//* -------------------------------------------------------------------- */    const char * pszLine = ReadLine( fp );    if( !EQUAL(pszLine,               "\"Code\",\"ObjectClass\",\"Acronym\",\"Attribute_A\","               "\"Attribute_B\",\"Attribute_C\",\"Class\",\"Primitives\"" ) )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "s57objectclasses columns don't match expected format!\n" );        return FALSE;    }/* -------------------------------------------------------------------- *//*      Read and form string list.                                      *//* -------------------------------------------------------------------- */        CSLDestroy( papszClassesInfo );    papszClassesInfo = (char **) CPLCalloc(sizeof(char *),MAX_CLASSES);    nClasses = 0;    while( nClasses < MAX_CLASSES           && (pszLine = ReadLine(fp)) != NULL )    {        papszClassesInfo[nClasses] = CPLStrdup(pszLine);        if( papszClassesInfo[nClasses] == NULL )            break;        nClasses++;    }    if( nClasses == MAX_CLASSES )        CPLError( CE_Warning, CPLE_AppDefined,                  "MAX_CLASSES exceeded in S57ClassRegistrar::LoadInfo().\n" );/* -------------------------------------------------------------------- *//*      Cleanup, and establish state.                                   *//* -------------------------------------------------------------------- */    if( fp != NULL )        VSIFClose( fp );    iCurrentClass = -1;    if( nClasses == 0 )        return FALSE;/* ==================================================================== *//*      Read the attributes list.                                       *//* ==================================================================== */    if( EQUAL(pszProfile, "Additional_Military_Layers") )    {        sprintf( szTargetFile, "s57attributes_%s.csv", "aml" );    }    else if ( EQUAL(pszProfile, "Inland_Waterways") )    {       sprintf( szTargetFile, "s57attributes_%s.csv", "iw" );    }    else if( strlen(pszProfile) > 0 )    {       sprintf( szTargetFile, "s57attributes_%s.csv", pszProfile );    }    else    {       strcpy( szTargetFile, "s57attributes.csv" );    }           if( !FindFile( szTargetFile, pszDirectory, bReportErr, &fp ) )        return FALSE;/* -------------------------------------------------------------------- *//*      Skip the line defining the column titles.                       *//* -------------------------------------------------------------------- */    pszLine = ReadLine( fp );    if( !EQUAL(pszLine,          "\"Code\",\"Attribute\",\"Acronym\",\"Attributetype\",\"Class\"") )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "s57attributes columns don't match expected format!\n" );        return FALSE;    }    /* -------------------------------------------------------------------- *//*      Prepare arrays for the per-attribute information.               *//* -------------------------------------------------------------------- */    nAttrMax = MAX_ATTRIBUTES-1;    papszAttrNames = (char **) CPLCalloc(sizeof(char *),MAX_ATTRIBUTES);    papszAttrAcronym = (char **) CPLCalloc(sizeof(char *),MAX_ATTRIBUTES);    //papapszAttrValues = (char ***) CPLCalloc(sizeof(char **),MAX_ATTRIBUTES);    pachAttrType = (char *) CPLCalloc(sizeof(char),MAX_ATTRIBUTES);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?