⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tigerfilebase.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** * $Id: tigerfilebase.cpp,v 1.17 2003/10/15 19:09:23 warmerda Exp $ * * Project:  TIGER/Line Translator * Purpose:  Implements TigerBaseFile class, providing common services to all *           the tiger file readers. * Author:   Frank Warmerdam, warmerda@home.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. ****************************************************************************** * * $Log: tigerfilebase.cpp,v $ * Revision 1.17  2003/10/15 19:09:23  warmerda * fix featuredefn memory leak * * Revision 1.16  2003/10/15 19:04:21  warmerda * ensure primary file gets closed - bug 414 * * Revision 1.15  2003/01/11 15:29:55  warmerda * expanded tabs * * Revision 1.14  2003/01/09 18:27:40  warmerda * added headers/function headers * * Revision 1.13  2003/01/04 23:21:56  mbp * Minor bug fixes and field definition changes.  Cleaned * up and commented code written for TIGER 2002 support. * * Revision 1.12  2002/12/26 00:20:19  mbp * re-organized code to hold TIGER-version details in TigerRecordInfo structs; * first round implementation of TIGER_2002 support * * Revision 1.11  2001/07/24 18:04:43  warmerda * Avoid crash if fp is NULL in establish record length. * * Revision 1.10  2001/07/19 16:05:49  warmerda * clear out tabs * * Revision 1.9  2001/07/19 16:03:11  warmerda * allow VERSION override on write * * Revision 1.8  2001/07/19 13:26:32  warmerda * enable override of existing modules * * Revision 1.7  2001/07/18 04:55:16  warmerda * added CPL_CSVID * * Revision 1.6  2001/07/04 23:25:32  warmerda * first round implementation of writer * * Revision 1.5  2001/07/04 05:40:35  warmerda * upgraded to support FILE, and Tiger2000 schema * * Revision 1.4  2001/01/19 21:15:20  warmerda * expanded tabs * * Revision 1.3  2000/01/13 05:18:11  warmerda * added support for multiple versions * * Revision 1.2  1999/12/22 15:38:15  warmerda * major update * * Revision 1.1  1999/10/07 18:19:21  warmerda * New * */#include "ogr_tiger.h"#include "cpl_conv.h"#include "cpl_error.h"CPL_CVSID("$Id: tigerfilebase.cpp,v 1.17 2003/10/15 19:09:23 warmerda Exp $");/************************************************************************//*                           TigerFileBase()                            *//************************************************************************/TigerFileBase::TigerFileBase(){    pszShortModule = NULL;    pszModule = NULL;    fpPrimary = NULL;    poFeatureDefn = NULL;    nFeatures = 0;    nVersionCode = 0;    nVersion = TIGER_Unknown;}/************************************************************************//*                           ~TigerFileBase()                           *//************************************************************************/TigerFileBase::~TigerFileBase(){    CPLFree( pszModule );    CPLFree( pszShortModule );    if( poFeatureDefn != NULL )    {        delete poFeatureDefn;        poFeatureDefn = NULL;    }    if( fpPrimary != NULL )    {        VSIFClose( fpPrimary );        fpPrimary = NULL;    }}/************************************************************************//*                              OpenFile()                              *//************************************************************************/int TigerFileBase::OpenFile( const char * pszModuleToOpen,                             const char *pszExtension ){    char        *pszFilename;    CPLFree( pszModule );    pszModule = NULL;    CPLFree( pszShortModule );    pszShortModule = NULL;        if( fpPrimary != NULL )    {        VSIFClose( fpPrimary );        fpPrimary = NULL;    }    if( pszModuleToOpen == NULL )        return TRUE;    pszFilename = poDS->BuildFilename( pszModuleToOpen, pszExtension );    fpPrimary = VSIFOpen( pszFilename, "rb" );    CPLFree( pszFilename );    if( fpPrimary != NULL )    {        pszModule = CPLStrdup(pszModuleToOpen);        pszShortModule = CPLStrdup(pszModuleToOpen);        for( int i = 0; pszShortModule[i] != '\0'; i++ )        {            if( pszShortModule[i] == '.' )                pszShortModule[i] = '\0';        }        SetupVersion();        return TRUE;    }    else        return FALSE;}/************************************************************************//*                            SetupVersion()                            *//************************************************************************/void TigerFileBase::SetupVersion(){    char        aszRecordHead[6];    VSIFSeek( fpPrimary, 0, SEEK_SET );    VSIFRead( aszRecordHead, 1, 5, fpPrimary );    aszRecordHead[5] = '\0';    nVersionCode = atoi(aszRecordHead+1);    VSIFSeek( fpPrimary, 0, SEEK_SET );    nVersion = TigerClassifyVersion( nVersionCode );}/************************************************************************//*                       EstablishRecordLength()                        *//************************************************************************/int TigerFileBase::EstablishRecordLength( FILE * fp ){    char        chCurrent;    int         nRecLen = 0;        if( fp == NULL || VSIFSeek( fp, 0, SEEK_SET ) != 0 )        return -1;/* -------------------------------------------------------------------- *//*      Read through to the end of line.                                *//* -------------------------------------------------------------------- */    chCurrent = '\0';    while( VSIFRead( &chCurrent, 1, 1, fp ) == 1           && chCurrent != 10           && chCurrent != 13 )    {        nRecLen++;    }/* -------------------------------------------------------------------- *//*      Is the file zero length?                                        *//* -------------------------------------------------------------------- */    if( nRecLen == 0 )    {        return -1;    }        nRecLen++; /* for the 10 or 13 we encountered *//* -------------------------------------------------------------------- *//*      Read through line terminator characters.  We are trying to      *//*      handle cases of CR, CR/LF and LF/CR gracefully.                 *//* -------------------------------------------------------------------- */    while( VSIFRead( &chCurrent, 1, 1, fp ) == 1           && (chCurrent == 10 || chCurrent == 13 ) )    {        nRecLen++;    }    VSIFSeek( fp, 0, SEEK_SET );    return nRecLen;}/************************************************************************//*                       EstablishFeatureCount()                        *//************************************************************************/void TigerFileBase::EstablishFeatureCount(){    if( fpPrimary == NULL )        return;    nRecordLength = EstablishRecordLength( fpPrimary );    if( nRecordLength == -1 )    {        nRecordLength = 1;        nFeatures = 0;        return;    }/* -------------------------------------------------------------------- *//*      Now we think we know the fixed record length for the file       *//*      (including line terminators).  Get the total file size, and     *//*      divide by this length to get the presumed number of records.    *//* -------------------------------------------------------------------- */    long        nFileSize;        VSIFSeek( fpPrimary, 0, SEEK_END );    nFileSize = VSIFTell( fpPrimary );    if( (nFileSize % nRecordLength) != 0 )    {        CPLError( CE_Warning, CPLE_FileIO,                  "TigerFileBase::EstablishFeatureCount(): "                  "File length %d doesn't divide by record length %d.\n",                  nFileSize, nRecordLength );    }    nFeatures = nFileSize / nRecordLength;}

⌨️ 快捷键说明

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