📄 ogrfmecacheindex.cpp
字号:
/****************************************************************************** * $Id: ogrfmecacheindex.cpp,v 1.3 2003/02/06 05:02:42 warmerda Exp $ * * Project: FMEObjects Translator * Purpose: Implement the OGRFMECacheIndex class, a mechanism to manage a * persisent index list of cached datasets. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2002 Safe Software Inc. * All Rights Reserved * * This software may not be copied or reproduced, in all or in part, * without the prior written consent of Safe Software Inc. * * The entire risk as to the results and performance of the software, * supporting text and other information contained in this file * (collectively called the "Software") is with the user. Although * Safe Software Incorporated has used considerable efforts in preparing * the Software, Safe Software Incorporated does not warrant the * accuracy or completeness of the Software. In no event will Safe Software * Incorporated be liable for damages, including loss of profits or * consequential damages, arising out of the use of the Software. ****************************************************************************** * * $Log: ogrfmecacheindex.cpp,v $ * Revision 1.3 2003/02/06 05:02:42 warmerda * added rec to clean list * * Revision 1.2 2002/07/11 16:07:27 warmerda * added CreationTime support, test against FMECACHE_MAX_RETENTION * * Revision 1.1 2002/05/24 06:23:57 warmerda * New * * Revision 1.2 2002/05/24 05:14:05 warmerda * removed stuff moved to minixml in cpl * * Revision 1.1 2002/05/24 03:59:00 warmerda * New * */#include "fme2ogr.h"#include "cpl_multiproc.h"#include "cpl_conv.h"#include "cpl_minixml.h"#include "cpl_string.h"#include <time.h>CPL_CVSID("$Id: ogrfmecacheindex.cpp,v 1.3 2003/02/06 05:02:42 warmerda Exp $");/************************************************************************//* OGRFMECacheIndex() *//************************************************************************/OGRFMECacheIndex::OGRFMECacheIndex( const char * pszPathIn ) { psTree = NULL; pszPath = CPLStrdup( pszPathIn ); hLock = NULL;}/************************************************************************//* ~OGRFMECacheIndex() *//************************************************************************/OGRFMECacheIndex::~OGRFMECacheIndex(){ if( psTree != NULL ) { Unlock(); CPLDestroyXMLNode( psTree ); psTree = NULL; } CPLFree( pszPath );}/************************************************************************//* Lock() *//************************************************************************/int OGRFMECacheIndex::Lock(){ if( pszPath == NULL ) return FALSE; hLock = CPLLockFile( pszPath, 5.0 ); return hLock != NULL;}/************************************************************************//* Unlock() *//************************************************************************/int OGRFMECacheIndex::Unlock(){ if( pszPath == NULL || hLock == NULL ) return FALSE; CPLUnlockFile( hLock ); hLock = NULL; return TRUE;}/************************************************************************//* Load() *//************************************************************************/int OGRFMECacheIndex::Load(){/* -------------------------------------------------------------------- *//* Lock the cache index file if not already locked. *//* -------------------------------------------------------------------- */ if( hLock == NULL && !Lock() ) return FALSE; if( psTree != NULL ) { CPLDestroyXMLNode( psTree ); psTree = NULL; }/* -------------------------------------------------------------------- *//* Open the index file. If we don't get it, we assume it is *//* because it doesn't exist, and we create a "stub" tree in *//* memory. *//* -------------------------------------------------------------------- */ FILE *fpIndex; int nLength; char *pszIndexBuffer; fpIndex = VSIFOpen( GetPath(), "rb" ); if( fpIndex == NULL ) { psTree = CPLCreateXMLNode( NULL, CXT_Element, "OGRFMECacheIndex" ); return TRUE; } /* -------------------------------------------------------------------- *//* Load the data from the file. *//* -------------------------------------------------------------------- */ VSIFSeek( fpIndex, 0, SEEK_END ); nLength = VSIFTell( fpIndex ); VSIFSeek( fpIndex, 0, SEEK_SET ); pszIndexBuffer = (char *) CPLMalloc(nLength+1); if( (int) VSIFRead( pszIndexBuffer, 1, nLength, fpIndex ) != nLength ) { CPLError( CE_Failure, CPLE_FileIO, "Read of %d byte index file failed.", nLength ); return FALSE; } VSIFClose( fpIndex );/* -------------------------------------------------------------------- *//* Parse the result into an inmemory XML tree. *//* -------------------------------------------------------------------- */ pszIndexBuffer[nLength] = '\0'; psTree = CPLParseXMLString( pszIndexBuffer ); CPLFree( pszIndexBuffer ); return psTree != NULL;}/************************************************************************//* Save() *//************************************************************************/int OGRFMECacheIndex::Save(){ if( hLock == NULL ) return FALSE;/* -------------------------------------------------------------------- *//* Convert the XML tree into one big character buffer, and *//* write it out. *//* -------------------------------------------------------------------- */ char *pszIndexBuffer = CPLSerializeXMLTree( psTree ); if( pszIndexBuffer == NULL ) return FALSE; FILE *fpIndex = VSIFOpen( GetPath(), "wb" ); if( fpIndex == NULL ) return FALSE; VSIFWrite( pszIndexBuffer, 1, strlen(pszIndexBuffer), fpIndex ); CPLFree( pszIndexBuffer ); VSIFClose( fpIndex ); Unlock(); return TRUE;}/************************************************************************//* FindMatch() *//* *//* Find a DataSource subtree that matches the passed in *//* component values. *//************************************************************************/CPLXMLNode *OGRFMECacheIndex::FindMatch( const char *pszDriver, const char *pszDataset, IFMEStringArray &oUserDirectives ){ CPLXMLNode *psCDS; if( psTree == NULL ) return NULL; for( psCDS = psTree->psChild; psCDS != NULL; psCDS = psCDS->psNext ) { if( !EQUAL(pszDriver,CPLGetXMLValue(psCDS,"Driver","")) ) continue; if( !EQUAL(pszDataset,CPLGetXMLValue(psCDS,"DSName","")) ) continue; CPLXMLNode *psDirective; int bMatch = TRUE; int iDir; psDirective = CPLGetXMLNode( psCDS, "UserDirectives.Directive" ); for( iDir = 0; iDir < (int)oUserDirectives.entries() && bMatch; iDir++ ) { if( psDirective == NULL || psDirective->psChild == NULL ) bMatch = FALSE; else if( !EQUAL(psDirective->psChild->pszValue, oUserDirectives(iDir)) ) bMatch = FALSE; else psDirective = psDirective->psNext;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -