?? avc_e00parse.c
字號:
/********************************************************************** * $Id: avc_e00parse.c,v 1.18 2006/06/27 18:06:34 dmorissette Exp $ * * Name: avc_e00parse.c * Project: Arc/Info vector coverage (AVC) E00->BIN conversion library * Language: ANSI C * Purpose: Functions to parse ASCII E00 lines and fill binary structures. * Author: Daniel Morissette, dmorissette@dmsolutions.ca * ********************************************************************** * Copyright (c) 1999-2005, Daniel Morissette * * 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: avc_e00parse.c,v $ * Revision 1.18 2006/06/27 18:06:34 dmorissette * Applied patch for EOP processing from James F. (bug 1497) * * Revision 1.17 2006/06/19 14:35:47 dmorissette * New patch from James F. for E00 read support in OGR (bug 1497) * * Revision 1.16 2006/06/16 11:48:11 daniel * New functions to read E00 files directly as opposed to translating to * binary coverage. Used in the implementation of E00 read support in OGR. * Contributed by James E. Flemer. (bug 1497) * * Revision 1.15 2006/03/02 22:46:26 daniel * Accept empty subclass names for TX6/TX7 sections (bug 1261) * * Revision 1.14 2005/06/03 03:49:58 daniel * Update email address, website url, and copyright dates * * Revision 1.13 2002/08/27 15:43:02 daniel * Small typo in type 40 fix (forgot to commit to CVS on 2002-08-05) * * Revision 1.12 2002/08/05 20:20:17 daniel * Fixed parsing type 40 fields to properly detect negative exp. (bug 1272) * * Revision 1.11 2001/11/25 21:15:23 daniel * Added hack (AVC_MAP_TYPE40_TO_DOUBLE) to map type 40 fields bigger than 8 * digits to double precision as we generate E00 output (bug599) * * Revision 1.10 2001/11/25 19:45:32 daniel * Fixed reading of type 40 when not in exponent format (bug599) * * Revision 1.9 2001/07/12 20:59:34 daniel * Properly handle PAL entries with 0 arcs * * Revision 1.8 2000/09/22 19:45:20 daniel * Switch to MIT-style license * * Revision 1.7 2000/03/16 03:48:00 daniel * Accept 0-length text strings in TX6/TX7 objects * * Revision 1.6 2000/02/03 07:21:40 daniel * TXT/TX6 with string longer than 80 chars: split string in 80 chars chunks * * Revision 1.5 1999/12/05 03:40:13 daniel * Fixed signed/unsigned mismatch compile warning * * Revision 1.4 1999/11/23 05:27:58 daniel * Added AVCE00Str2Int() to extract integer values in E00 lines * * Revision 1.3 1999/08/23 18:20:49 daniel * Fixed support for attribute fields type 40 * * Revision 1.2 1999/05/17 16:20:48 daniel * Added RXP + TXT/TX6/TX7 write support + some simple problems fixed * * Revision 1.1 1999/05/11 02:34:46 daniel * Initial revision * **********************************************************************/#include "avc.h"#include <ctype.h> /* toupper() *//********************************************************************** * AVCE00Str2Int() * * Convert a portion of a string to an integer value. * The difference between this function and atoi() is that this version * takes only the specified number of characters... so it can handle the * case of 2 numbers that are part of the same string but are not separated * by a space. **********************************************************************/int AVCE00Str2Int(const char *pszStr, int numChars){ int nValue = 0; if (pszStr && numChars >= (int)strlen(pszStr)) return atoi(pszStr); else if (pszStr) { char cNextDigit; char *pszTmp; /* Get rid of const */ pszTmp = (char*)pszStr; cNextDigit = pszTmp[numChars]; pszTmp[numChars] = '\0'; nValue = atoi(pszTmp); pszTmp[numChars] = cNextDigit; } return nValue;}/********************************************************************** * AVCE00ParseInfoAlloc() * * Allocate and initialize a new AVCE00ParseInfo structure. * * AVCE00ParseStartSection() will have to be called at least once * to specify the type of objects to parse. * * The structure will eventually have to be freed with AVCE00ParseInfoFree(). **********************************************************************/AVCE00ParseInfo *AVCE00ParseInfoAlloc(){ AVCE00ParseInfo *psInfo; psInfo = (AVCE00ParseInfo*)CPLCalloc(1,sizeof(AVCE00ParseInfo)); psInfo->eFileType = AVCFileUnknown; psInfo->eSuperSectionType = AVCFileUnknown; /* Allocate output buffer. * 2k should be enough... the biggest thing we'll need to store * in it will be 1 complete INFO table record. */ psInfo->nBufSize = 2048; psInfo->pszBuf = (char *)CPLMalloc(psInfo->nBufSize*sizeof(char)); /* Set a default precision, but this value will be set on a section * by section basis inside AVCE00ParseStartSection() */ psInfo->nPrecision = AVC_SINGLE_PREC; return psInfo;}/********************************************************************** * _AVCE00ParseDestroyCurObject() * * Release mem. associated with the psInfo->cur.* object we are * currently using. **********************************************************************/void _AVCE00ParseDestroyCurObject(AVCE00ParseInfo *psInfo){ if (psInfo->eFileType == AVCFileUnknown) return; if (psInfo->eFileType == AVCFileARC) { CPLFree(psInfo->cur.psArc->pasVertices); CPLFree(psInfo->cur.psArc); } else if (psInfo->eFileType == AVCFilePAL || psInfo->eFileType == AVCFileRPL ) { CPLFree(psInfo->cur.psPal->pasArcs); CPLFree(psInfo->cur.psPal); } else if (psInfo->eFileType == AVCFileCNT) { CPLFree(psInfo->cur.psCnt->panLabelIds); CPLFree(psInfo->cur.psCnt); } else if (psInfo->eFileType == AVCFileLAB) { CPLFree(psInfo->cur.psLab); } else if (psInfo->eFileType == AVCFileTOL) { CPLFree(psInfo->cur.psTol); } else if (psInfo->eFileType == AVCFilePRJ) { CSLDestroy(psInfo->cur.papszPrj); } else if (psInfo->eFileType == AVCFileTXT || psInfo->eFileType == AVCFileTX6) { CPLFree(psInfo->cur.psTxt->pasVertices); CPLFree(psInfo->cur.psTxt->pszText); CPLFree(psInfo->cur.psTxt); } else if (psInfo->eFileType == AVCFileRXP) { CPLFree(psInfo->cur.psRxp); } else if (psInfo->eFileType == AVCFileTABLE) { _AVCDestroyTableFields(psInfo->hdr.psTableDef, psInfo->cur.pasFields); _AVCDestroyTableDef(psInfo->hdr.psTableDef); psInfo->bTableHdrComplete = FALSE; } else { CPLError(CE_Failure, CPLE_NotSupported, "_AVCE00ParseDestroyCurObject(): Unsupported file type!"); } psInfo->eFileType = AVCFileUnknown; psInfo->cur.psArc = NULL;}/********************************************************************** * AVCE00ParseInfoFree() * * Free any memory associated with a AVCE00ParseInfo structure. **********************************************************************/void AVCE00ParseInfoFree(AVCE00ParseInfo *psInfo){ if (psInfo) { CPLFree(psInfo->pszSectionHdrLine); psInfo->pszSectionHdrLine = NULL; CPLFree(psInfo->pszBuf); _AVCE00ParseDestroyCurObject(psInfo); } CPLFree(psInfo);}/********************************************************************** * AVCE00ParseReset() * * Reset the fields in a AVCE00ParseInfo structure so that further calls * to the API will be ready to process a new object. **********************************************************************/void AVCE00ParseReset(AVCE00ParseInfo *psInfo){ psInfo->iCurItem = psInfo->numItems = 0; psInfo->bForceEndOfSection = FALSE;}/********************************************************************** * AVCE00ParseSuperSectionHeader() * * Check if pszLine is a valid "supersection" header line, if it is one * then store the supersection type in the ParseInfo structure. * * What I call a "supersection" is a section that contains several * files, such as the TX6/TX7, RPL, RXP, ... and also the IFO (TABLEs). * * The ParseInfo structure won't be ready to read objects until * a call to AVCE00ParseSectionHeader() (see below) succesfully * recognizes the beginning of a subsection of this type. * * Returns the new supersection type, or AVCFileUnknown if the line is * not recognized. **********************************************************************/AVCFileType AVCE00ParseSuperSectionHeader(AVCE00ParseInfo *psInfo, const char *pszLine){ /*----------------------------------------------------------------- * If we're already inside a supersection or a section, then * return AVCFileUnknown right away. *----------------------------------------------------------------*/ if (psInfo == NULL || psInfo->eSuperSectionType != AVCFileUnknown || psInfo->eFileType != AVCFileUnknown ) { return AVCFileUnknown; } /*----------------------------------------------------------------- * Check if pszLine is a valid supersection header line. *----------------------------------------------------------------*/ if (EQUALN(pszLine, "RPL ", 5)) psInfo->eSuperSectionType = AVCFileRPL; else if (EQUALN(pszLine, "TX6 ", 5) || EQUALN(pszLine, "TX7 ", 5)) psInfo->eSuperSectionType = AVCFileTX6; else if (EQUALN(pszLine, "RXP ", 5)) psInfo->eSuperSectionType = AVCFileRXP; else if (EQUALN(pszLine, "IFO ", 5)) psInfo->eSuperSectionType = AVCFileTABLE; else return AVCFileUnknown; /*----------------------------------------------------------------- * Record the start of the supersection (for faster seeking) *----------------------------------------------------------------*/ psInfo->nStartLineNum = psInfo->nCurLineNum; /*----------------------------------------------------------------- * OK, we have a valid new section header. Set the precision and * get ready to read objects from it. *----------------------------------------------------------------*/ if (atoi(pszLine+4) == 2) psInfo->nPrecision = AVC_SINGLE_PREC; else if (atoi(pszLine+4) == 3) psInfo->nPrecision = AVC_DOUBLE_PREC; else { CPLError(CE_Failure, CPLE_AppDefined, "Parse Error: Invalid section header line (\"%s\")!", pszLine); psInfo->eSuperSectionType = AVCFileUnknown; /* psInfo->nStartLineNum = -1; */ } return psInfo->eSuperSectionType;}/********************************************************************** * AVCE00ParseSuperSectionEnd() * * Check if pszLine marks the end of a supersection, and if it is the * case, then reset the supersection flag in the ParseInfo. * * Supersections always end with the line "JABBERWOCKY", except for * the IFO section. **********************************************************************/GBool AVCE00ParseSuperSectionEnd(AVCE00ParseInfo *psInfo, const char *pszLine ){ if (psInfo->eFileType == AVCFileUnknown && psInfo->eSuperSectionType != AVCFileUnknown && (EQUALN(pszLine, "JABBERWOCKY", 11) || (psInfo->eSuperSectionType == AVCFileTABLE && EQUALN(pszLine, "EOI", 3) ) ) ) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -