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