Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

cpl_findfile.cpp

00001 /******************************************************************************
00002  * $Id: cpl_findfile_cpp-source.html,v 1.8 2001/07/05 13:24:08 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.8  2001/07/05 13:24:08  warmerda
00030  * *** empty log message ***
00030  *
00031  * Revision 1.2  2001/01/19 21:16:41  warmerda
00032  * expanded tabs
00033  *
00034  * Revision 1.1  2000/08/29 21:06:25  warmerda
00035  * New
00036  *
00037  */
00038 
00039 #include "cpl_conv.h"
00040 #include "cpl_string.h"
00041 
00042 static int bFinderInitialized = FALSE;
00043 static int nFileFinders = 0;
00044 static CPLFileFinder *papfnFinders = NULL;
00045 static char **papszFinderLocations = NULL;
00046 
00047 /************************************************************************/
00048 /*                           CPLFinderInit()                            */
00049 /************************************************************************/
00050 
00051 static void CPLFinderInit()
00052 
00053 {
00054     if( !bFinderInitialized )
00055     {
00056         bFinderInitialized = TRUE;
00057         CPLPushFileFinder( CPLDefaultFindFile );
00058         CPLPushFinderLocation( "/usr/local/share" );
00059         CPLPushFinderLocation( "." );
00060     }
00061 }
00062 
00063 /************************************************************************/
00064 /*                         CPLDefaultFileFind()                         */
00065 /************************************************************************/
00066 
00067 const char *CPLDefaultFindFile( const char *pszClass, 
00068                                 const char *pszBasename )
00069 
00070 {
00071     int         i, nLocations = CSLCount( papszFinderLocations );
00072 
00073     (void) pszClass;
00074 
00075     for( i = nLocations-1; nLocations >= 0; nLocations-- )
00076     {
00077         const char  *pszResult;
00078         VSIStatBuf  sStat;
00079 
00080         pszResult = CPLFormFilename( papszFinderLocations[i], pszBasename, 
00081                                      NULL );
00082 
00083         if( VSIStat( pszResult, &sStat ) == 0 )
00084             return pszResult;
00085     }
00086     
00087     return NULL;
00088 }
00089 
00090 /************************************************************************/
00091 /*                            CPLFindFile()                             */
00092 /************************************************************************/
00093 
00094 const char *CPLFindFile( const char *pszClass, const char *pszBasename )
00095 
00096 {
00097     int         i;
00098 
00099     CPLFinderInit();
00100 
00101     for( i = nFileFinders-1; i >= 0; i-- )
00102     {
00103         const char * pszResult;
00104 
00105         pszResult = (papfnFinders[i])( pszClass, pszBasename );
00106         if( pszResult != NULL )
00107             return pszResult;
00108     }
00109 
00110     return NULL;
00111 }
00112 
00113 /************************************************************************/
00114 /*                         CPLPushFileFinder()                          */
00115 /************************************************************************/
00116 
00117 void CPLPushFileFinder( CPLFileFinder pfnFinder )
00118 
00119 {
00120     CPLFinderInit();
00121 
00122     papfnFinders = (CPLFileFinder *) 
00123         CPLRealloc(papfnFinders,  sizeof(void*) * ++nFileFinders);
00124     papfnFinders[nFileFinders-1] = pfnFinder;
00125 }
00126 
00127 /************************************************************************/
00128 /*                          CPLPopFileFinder()                          */
00129 /************************************************************************/
00130 
00131 CPLFileFinder CPLPopFileFinder()
00132 
00133 {
00134     CPLFinderInit();
00135 
00136     if( nFileFinders == 0 )
00137         return NULL;
00138 
00139     return papfnFinders[--nFileFinders];
00140 }
00141 
00142 /************************************************************************/
00143 /*                       CPLPushFinderLocation()                        */
00144 /************************************************************************/
00145 
00146 void CPLPushFinderLocation( const char *pszLocation )
00147 
00148 {
00149     CPLFinderInit();
00150 
00151     papszFinderLocations  = CSLAddString( papszFinderLocations, 
00152                                           pszLocation );
00153 }
00154 
00155 
00156 /************************************************************************/
00157 /*                       CPLPushFinderLocation()                        */
00158 /************************************************************************/
00159 
00160 void CPLPopFinderLocation()
00161 
00162 {
00163     int      nCount;
00164 
00165     CPLFinderInit();
00166 
00167     nCount = CSLCount(papszFinderLocations);
00168     if( nCount == 0 )
00169         return;
00170 
00171     CPLFree( papszFinderLocations[nCount-1] );
00172     papszFinderLocations[nCount-1] = NULL;
00173 }
00174 
00175 

Generated at Thu Jul 5 09:16:11 2001 for GDAL by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000