00001 /****************************************************************************** 00002 * $Id: cpl_findfile_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $ 00003 * 00004 * Project: CPL - Common Portability Library 00005 * Purpose: Generic data file location finder, with application hooking. 00006 * Author: Frank Warmerdam, warmerda@home.com 00007 * 00008 ****************************************************************************** 00009 * Copyright (c) 2000, Frank Warmerdam 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a 00012 * copy of this software and associated documentation files (the "Software"), 00013 * to deal in the Software without restriction, including without limitation 00014 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00015 * and/or sell copies of the Software, and to permit persons to whom the 00016 * Software is furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included 00019 * in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00022 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00024 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00026 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00027 * DEALINGS IN THE SOFTWARE. 00028 ****************************************************************************** 00029 * 00030 * $Log: cpl_findfile_cpp-source.html,v $ 00030 * Revision 1.10 2002/04/16 13:11:47 warmerda 00030 * updated 00030 * 00031 * Revision 1.3 2001/07/18 04:00:49 warmerda 00032 * added CPL_CVSID 00033 * 00034 * Revision 1.2 2001/01/19 21:16:41 warmerda 00035 * expanded tabs 00036 * 00037 * Revision 1.1 2000/08/29 21:06:25 warmerda 00038 * New 00039 * 00040 */ 00041 00042 #include "cpl_conv.h" 00043 #include "cpl_string.h" 00044 00045 CPL_CVSID("$Id: cpl_findfile_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $"); 00046 00047 static int bFinderInitialized = FALSE; 00048 static int nFileFinders = 0; 00049 static CPLFileFinder *papfnFinders = NULL; 00050 static char **papszFinderLocations = NULL; 00051 00052 /************************************************************************/ 00053 /* CPLFinderInit() */ 00054 /************************************************************************/ 00055 00056 static void CPLFinderInit() 00057 00058 { 00059 if( !bFinderInitialized ) 00060 { 00061 bFinderInitialized = TRUE; 00062 CPLPushFileFinder( CPLDefaultFindFile ); 00063 CPLPushFinderLocation( "/usr/local/share" ); 00064 CPLPushFinderLocation( "." ); 00065 } 00066 } 00067 00068 /************************************************************************/ 00069 /* CPLDefaultFileFind() */ 00070 /************************************************************************/ 00071 00072 const char *CPLDefaultFindFile( const char *pszClass, 00073 const char *pszBasename ) 00074 00075 { 00076 int i, nLocations = CSLCount( papszFinderLocations ); 00077 00078 (void) pszClass; 00079 00080 for( i = nLocations-1; nLocations >= 0; nLocations-- ) 00081 { 00082 const char *pszResult; 00083 VSIStatBuf sStat; 00084 00085 pszResult = CPLFormFilename( papszFinderLocations[i], pszBasename, 00086 NULL ); 00087 00088 if( VSIStat( pszResult, &sStat ) == 0 ) 00089 return pszResult; 00090 } 00091 00092 return NULL; 00093 } 00094 00095 /************************************************************************/ 00096 /* CPLFindFile() */ 00097 /************************************************************************/ 00098 00099 const char *CPLFindFile( const char *pszClass, const char *pszBasename ) 00100 00101 { 00102 int i; 00103 00104 CPLFinderInit(); 00105 00106 for( i = nFileFinders-1; i >= 0; i-- ) 00107 { 00108 const char * pszResult; 00109 00110 pszResult = (papfnFinders[i])( pszClass, pszBasename ); 00111 if( pszResult != NULL ) 00112 return pszResult; 00113 } 00114 00115 return NULL; 00116 } 00117 00118 /************************************************************************/ 00119 /* CPLPushFileFinder() */ 00120 /************************************************************************/ 00121 00122 void CPLPushFileFinder( CPLFileFinder pfnFinder ) 00123 00124 { 00125 CPLFinderInit(); 00126 00127 papfnFinders = (CPLFileFinder *) 00128 CPLRealloc(papfnFinders, sizeof(void*) * ++nFileFinders); 00129 papfnFinders[nFileFinders-1] = pfnFinder; 00130 } 00131 00132 /************************************************************************/ 00133 /* CPLPopFileFinder() */ 00134 /************************************************************************/ 00135 00136 CPLFileFinder CPLPopFileFinder() 00137 00138 { 00139 CPLFinderInit(); 00140 00141 if( nFileFinders == 0 ) 00142 return NULL; 00143 00144 return papfnFinders[--nFileFinders]; 00145 } 00146 00147 /************************************************************************/ 00148 /* CPLPushFinderLocation() */ 00149 /************************************************************************/ 00150 00151 void CPLPushFinderLocation( const char *pszLocation ) 00152 00153 { 00154 CPLFinderInit(); 00155 00156 papszFinderLocations = CSLAddString( papszFinderLocations, 00157 pszLocation ); 00158 } 00159 00160 00161 /************************************************************************/ 00162 /* CPLPushFinderLocation() */ 00163 /************************************************************************/ 00164 00165 void CPLPopFinderLocation() 00166 00167 { 00168 int nCount; 00169 00170 CPLFinderInit(); 00171 00172 nCount = CSLCount(papszFinderLocations); 00173 if( nCount == 0 ) 00174 return; 00175 00176 CPLFree( papszFinderLocations[nCount-1] ); 00177 papszFinderLocations[nCount-1] = NULL; 00178 } 00179 00180