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

📄 ddffielddefn.cpp

📁 开源的电子海图程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** * $Id: ddffielddefn.cpp,v 1.1.1.1 2004/12/29 07:54:52 jay-be-em Exp $ * * Project:  ISO 8211 Access * Purpose:  Implements the DDFFieldDefn class. * 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. ****************************************************************************** * * $Log: ddffielddefn.cpp,v $ * Revision 1.1.1.1  2004/12/29 07:54:52  jay-be-em * Initial import * * Revision 1.21  2004/02/18 14:10:07  warmerda * doc fixups * * Revision 1.20  2004/01/06 18:59:18  warmerda * make enum identifiers more unique * * Revision 1.19  2004/01/06 18:53:41  warmerda * made data_type_code and data_struct_code global for HP C++ builds * * Revision 1.18  2003/12/15 20:24:58  warmerda * expand tabs * * Revision 1.17  2003/09/17 21:11:34  warmerda * fixed handling of the field terminator on write * * Revision 1.16  2003/09/15 20:45:47  warmerda * initialize nFixedWidth * * Revision 1.15  2003/09/05 19:13:02  warmerda * added repeating support when creating fields * * Revision 1.14  2003/09/03 20:36:26  warmerda * added subfield writing support * * Revision 1.13  2003/07/18 20:45:30  warmerda * be careful to avoid pszDest buffer overrun * * Revision 1.12  2003/07/03 15:38:46  warmerda * some write capabilities added * * Revision 1.11  2003/05/22 19:44:26  warmerda * Fixed another bug like the last. * * Revision 1.10  2003/05/22 19:14:51  warmerda * Fixed possible problem with writing one byte past end of * pszDest in ExpandFormat() as reported by Ben Discoe. * * Revision 1.9  2003/02/06 03:21:04  warmerda * Modified ExpandFormat() to dynamically allocate the target buffer. It was * overrunning the 400 character szDest buffer on some files, such as * the data/sdts/gainsville/BEDRCATD.DDF dataset. * * Revision 1.8  2001/07/18 04:51:57  warmerda * added CPL_CVSID * * Revision 1.7  2001/06/22 19:22:16  warmerda * Made some oddidies in field definitions non-fatal. * * Revision 1.6  2000/11/30 20:33:18  warmerda * make having more formats than data items a warning, not an error * * Revision 1.5  2000/06/16 18:05:02  warmerda * expanded tabs * * Revision 1.4  2000/01/31 18:03:38  warmerda * completely rewrote format expansion to make more general * * Revision 1.3  1999/11/18 19:03:04  warmerda * expanded tabs * * Revision 1.2  1999/09/20 19:29:16  warmerda * make forgiving of UNIT/FIELD terminator mixup in Tiger SDTS files * * Revision 1.1  1999/04/27 18:45:05  warmerda * New * */#include "iso8211.h"#include "cpl_string.h"#include <ctype.h>CPL_CVSID("$Id: ddffielddefn.cpp,v 1.1.1.1 2004/12/29 07:54:52 jay-be-em Exp $");/************************************************************************//*                            DDFFieldDefn()                            *//************************************************************************/DDFFieldDefn::DDFFieldDefn(){    poModule = NULL;    pszTag = NULL;    _fieldName = NULL;    _arrayDescr = NULL;    _formatControls = NULL;    nSubfieldCount = 0;    papoSubfields = NULL;    bRepeatingSubfields = FALSE;    nFixedWidth = 0;}/************************************************************************//*                           ~DDFFieldDefn()                            *//************************************************************************/DDFFieldDefn::~DDFFieldDefn(){    int   i;    CPLFree( pszTag );    CPLFree( _fieldName );    CPLFree( _arrayDescr );    CPLFree( _formatControls );    for( i = 0; i < nSubfieldCount; i++ )        delete papoSubfields[i];    CPLFree( papoSubfields );}/************************************************************************//*                            AddSubfield()                             *//************************************************************************/void DDFFieldDefn::AddSubfield( const char *pszName,                                 const char *pszFormat ){    DDFSubfieldDefn *poSFDefn = new DDFSubfieldDefn;    poSFDefn->SetName( pszName );    poSFDefn->SetFormat( pszFormat );    AddSubfield( poSFDefn );}/************************************************************************//*                            AddSubfield()                             *//************************************************************************/void DDFFieldDefn::AddSubfield( DDFSubfieldDefn *poNewSFDefn,                                int bDontAddToFormat ){    nSubfieldCount++;    papoSubfields = (DDFSubfieldDefn ** )        CPLRealloc( papoSubfields, sizeof(void*) * nSubfieldCount );    papoSubfields[nSubfieldCount-1] = poNewSFDefn;        if( bDontAddToFormat )        return;/* -------------------------------------------------------------------- *//*      Add this format to the format list.  We don't bother            *//*      aggregating formats here.                                       *//* -------------------------------------------------------------------- */    if( _formatControls == NULL || strlen(_formatControls) == 0 )    {        CPLFree( _formatControls );        _formatControls = CPLStrdup( "()" );    }        int nOldLen = strlen(_formatControls);        char *pszNewFormatControls = (char *)         CPLMalloc(nOldLen+3+strlen(poNewSFDefn->GetFormat()));        strcpy( pszNewFormatControls, _formatControls );    pszNewFormatControls[nOldLen-1] = '\0';    if( pszNewFormatControls[nOldLen-2] != '(' )        strcat( pszNewFormatControls, "," );        strcat( pszNewFormatControls, poNewSFDefn->GetFormat() );    strcat( pszNewFormatControls, ")" );        CPLFree( _formatControls );    _formatControls = pszNewFormatControls;/* -------------------------------------------------------------------- *//*      Add the subfield name to the list.                              *//* -------------------------------------------------------------------- */    if( _arrayDescr == NULL )        _arrayDescr = CPLStrdup("");    _arrayDescr = (char *)         CPLRealloc(_arrayDescr,                    strlen(_arrayDescr)+strlen(poNewSFDefn->GetName())+2);    if( strlen(_arrayDescr) > 0 )        strcat( _arrayDescr, "!" );    strcat( _arrayDescr, poNewSFDefn->GetName() );}/************************************************************************//*                               Create()                               *//*                                                                      *//*      Initialize a new field defn from application input, instead     *//*      of from an existing file.                                       *//************************************************************************/int DDFFieldDefn::Create( const char *pszTag, const char *pszFieldName,                           const char *pszDescription,                           DDF_data_struct_code eDataStructCode,                          DDF_data_type_code   eDataTypeCode,                          const char *pszFormat ){    CPLAssert( this->pszTag == NULL );    poModule = NULL;    this->pszTag = CPLStrdup( pszTag );    _fieldName = CPLStrdup( pszFieldName );    _arrayDescr = CPLStrdup( pszDescription );    _formatControls = CPLStrdup( "" );        _data_struct_code = eDataStructCode;    _data_type_code = eDataTypeCode;    if( pszFormat != NULL )        _formatControls = CPLStrdup( pszFormat );    if( pszDescription != NULL && *pszDescription == '*' )        bRepeatingSubfields = TRUE;    return TRUE;}/************************************************************************//*                          GenerateDDREntry()                          *//************************************************************************/int DDFFieldDefn::GenerateDDREntry( char **ppachData,                                     int *pnLength ){    *pnLength = 9 + strlen(_fieldName) + 1         + strlen(_arrayDescr) + 1        + strlen(_formatControls) + 1;    if( strlen(_formatControls) == 0 )        *pnLength -= 1;    if( ppachData == NULL )        return TRUE;    *ppachData = (char *) CPLMalloc( *pnLength+1 );        if( _data_struct_code == dsc_elementary )        (*ppachData)[0] = '0';    else if( _data_struct_code == dsc_vector )        (*ppachData)[0] = '1';    else if( _data_struct_code == dsc_array )        (*ppachData)[0] = '2';    else if( _data_struct_code == dsc_concatenated )        (*ppachData)[0] = '3';        if( _data_type_code == dtc_char_string )        (*ppachData)[1] = '0';    else if( _data_type_code == dtc_implicit_point )        (*ppachData)[1] = '1';    else if( _data_type_code == dtc_explicit_point )        (*ppachData)[1] = '2';    else if( _data_type_code == dtc_explicit_point_scaled )        (*ppachData)[1] = '3';    else if( _data_type_code == dtc_char_bit_string )        (*ppachData)[1] = '4';    else if( _data_type_code == dtc_bit_string )        (*ppachData)[1] = '5';    else if( _data_type_code == dtc_mixed_data_type )        (*ppachData)[1] = '6';    (*ppachData)[2] = '0';    (*ppachData)[3] = '0';    (*ppachData)[4] = ';';    (*ppachData)[5] = '&';    (*ppachData)[6] = ' ';    (*ppachData)[7] = ' ';    (*ppachData)[8] = ' ';    sprintf( *ppachData + 9, "%s%c%s",              _fieldName, DDF_UNIT_TERMINATOR, _arrayDescr );    if( strlen(_formatControls) > 0 )        sprintf( *ppachData + strlen(*ppachData), "%c%s",                 DDF_UNIT_TERMINATOR, _formatControls );    sprintf( *ppachData + strlen(*ppachData), "%c", DDF_FIELD_TERMINATOR );    return TRUE;}/************************************************************************//*                             Initialize()                             *//*                                                                      *//*      Initialize the field definition from the information in the     *//*      DDR record.  This is called by DDFModule::Open().               *//************************************************************************/int DDFFieldDefn::Initialize( DDFModule * poModuleIn,                              const char * pszTagIn,                               int nFieldEntrySize,                              const char * pachFieldArea ){    int         iFDOffset = poModuleIn->GetFieldControlLength();    int         nCharsConsumed;    poModule = poModuleIn;        pszTag = CPLStrdup( pszTagIn );/* -------------------------------------------------------------------- *//*      Set the data struct and type codes.                             *//* -------------------------------------------------------------------- */    switch( pachFieldArea[0] )    {      case '0':        _data_struct_code = dsc_elementary;        break;      case '1':        _data_struct_code = dsc_vector;        break;      case '2':        _data_struct_code = dsc_array;        break;      case '3':        _data_struct_code = dsc_concatenated;        break;      default:        CPLError( CE_Failure, CPLE_AppDefined,                   "Unrecognised data_struct_code value %c.\n"                  "Field %s initialization incorrect.\n",                  pachFieldArea[0], pszTag );        _data_struct_code = dsc_elementary;    }    switch( pachFieldArea[1] )    {      case '0':        _data_type_code = dtc_char_string;        break;              case '1':        _data_type_code = dtc_implicit_point;        break;              case '2':        _data_type_code = dtc_explicit_point;        break;              case '3':        _data_type_code = dtc_explicit_point_scaled;        break;              case '4':        _data_type_code = dtc_char_bit_string;        break;              case '5':        _data_type_code = dtc_bit_string;        break;              case '6':        _data_type_code = dtc_mixed_data_type;        break;      default:        CPLError( CE_Failure, CPLE_AppDefined,                   "Unrecognised data_type_code value %c.\n"                  "Field %s initialization incorrect.\n",                  pachFieldArea[1], pszTag );        _data_type_code = dtc_char_string;    }    /* -------------------------------------------------------------------- *//*      Capture the field name, description (sub field names), and      *//*      format statements.                                              *//* -------------------------------------------------------------------- */    _fieldName =        DDFFetchVariable( pachFieldArea + iFDOffset,                          nFieldEntrySize - iFDOffset,                          DDF_UNIT_TERMINATOR, DDF_FIELD_TERMINATOR,                          &nCharsConsumed );    iFDOffset += nCharsConsumed;        _arrayDescr =        DDFFetchVariable( pachFieldArea + iFDOffset,                          nFieldEntrySize - iFDOffset,                          DDF_UNIT_TERMINATOR, DDF_FIELD_TERMINATOR,                           &nCharsConsumed );    iFDOffset += nCharsConsumed;        _formatControls =        DDFFetchVariable( pachFieldArea + iFDOffset,                          nFieldEntrySize - iFDOffset,                          DDF_UNIT_TERMINATOR, DDF_FIELD_TERMINATOR,                           &nCharsConsumed );    /* -------------------------------------------------------------------- *//*      Parse the subfield info.                                        *//* -------------------------------------------------------------------- */    if( _data_struct_code != dsc_elementary )    {        if( !BuildSubfields() )            return FALSE;        if( !ApplyFormats() )            return FALSE;    }        return TRUE;}/************************************************************************//*                                Dump()                                *//************************************************************************//** * Write out field definition info to debugging file. * * A variety of information about this field definition, and all it's * subfields is written to the give debugging file handle. * * @param fp The standard io file handle to write to.  ie. stderr */void DDFFieldDefn::Dump( FILE * fp ){    const char  *pszValue = "";        fprintf( fp, "  DDFFieldDefn:\n" );    fprintf( fp, "      Tag = `%s'\n", pszTag );    fprintf( fp, "      _fieldName = `%s'\n", _fieldName );    fprintf( fp, "      _arrayDescr = `%s'\n", _arrayDescr );    fprintf( fp, "      _formatControls = `%s'\n", _formatControls );

⌨️ 快捷键说明

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