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

# cpl_path.cpp

    00001 /**********************************************************************
    00002  * $Id: cpl_path_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $
    00003  *
    00004  * Project:  CPL - Common Portability Library
    00005  * Purpose:  Portable filename/path parsing, and forming ala "Glob API".
    00006  * Author:   Frank Warmerdam, warmerda@home.com
    00007  *
    00008  **********************************************************************
    00009  * Copyright (c) 1999, 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 OR
    00022  * 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_path_cpp-source.html,v $
    00030  * Revision 1.10  2002/04/16 13:11:47  warmerda
    00030  * updated
    00030  *
    00031  * Revision 1.9  2001/08/30 21:20:49  warmerda
    00032  * expand tabs
    00033  *
    00034  * Revision 1.8  2001/07/18 04:00:49  warmerda
    00035  * added CPL_CVSID
    00036  *
    00037  * Revision 1.7  2001/05/12 19:20:55  warmerda
    00038  * Fixed documentation of CPLGetExtension().
    00039  *
    00040  * Revision 1.6  2001/03/16 22:15:08  warmerda
    00041  * added CPLResetExtension
    00042  *
    00043  * Revision 1.5  2001/02/24 01:53:57  warmerda
    00044  * Added CPLFormCIFilename()
    00045  *
    00046  * Revision 1.4  2001/01/19 21:18:25  warmerda
    00047  * expanded tabs
    00048  *
    00049  * Revision 1.3  2000/01/26 17:53:36  warmerda
    00050  * Fixed CPLGetExtension() for filenames with no extension.
    00051  *
    00052  * Revision 1.2  2000/01/24 19:32:59  warmerda
    00053  * Fixed CPLGetExtension() to not include the dot.
    00054  *
    00055  * Revision 1.1  1999/10/14 19:23:39  warmerda
    00056  * New
    00057  *
    00058  **********************************************************************/
    00059
    00060 #include "cpl_conv.h"
    00061 #include "cpl_string.h"
    00062
    00063 CPL_CVSID("$Id: cpl_path_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $");
    00064
    00065
    00066 static char     szStaticResult[1024]; /* should be size of larged possible
    00067                                          filename */
    00068
    00069 /************************************************************************/
    00070 /*                        CPLFindFilenameStart()                        */
    00071 /************************************************************************/
    00072
    00073 static int CPLFindFilenameStart( const char * pszFilename )
    00074
    00075 {
    00076     int         iFileStart;
    00077
    00078     for( iFileStart = strlen(pszFilename);
    00079          iFileStart > 0
    00080              && pszFilename[iFileStart-1] != '/'
    00081              && pszFilename[iFileStart-1] != '\\';
    00082          iFileStart-- ) {}
    00083
    00084     return iFileStart;
    00085 }
    00086
    00087 /************************************************************************/
    00088 /*                             CPLGetPath()                             */
    00089 /************************************************************************/
    00090
    00112 const char *CPLGetPath( const char *pszFilename )
    00113
    00114 {
    00115     int         iFileStart = CPLFindFilenameStart(pszFilename);
    00116
    00117     if( iFileStart == 0 )
    00118     {
    00119         strcpy( szStaticResult, "" );
    00120         return szStaticResult;
    00121     }
    00122
    00123     strncpy( szStaticResult, pszFilename, iFileStart );
    00124     szStaticResult[iFileStart] = '\0';
    00125
    00126     if( iFileStart > 1
    00127         && (szStaticResult[iFileStart-1] == '/'
    00128             || szStaticResult[iFileStart-1] == '\\') )
    00129         szStaticResult[iFileStart-1] = '\0';
    00130
    00131     return szStaticResult;
    00132 }
    00133
    00134 /************************************************************************/
    00135 /*                           CPLGetFilename()                           */
    00136 /************************************************************************/
    00137
    00158 const char *CPLGetFilename( const char *pszFullFilename )
    00159
    00160 {
    00161     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00162
    00163     strcpy( szStaticResult, pszFullFilename + iFileStart );
    00164
    00165     return szStaticResult;
    00166 }
    00167
    00168 /************************************************************************/
    00169 /*                           CPLGetBasename()                           */
    00170 /************************************************************************/
    00171
    00192 const char *CPLGetBasename( const char *pszFullFilename )
    00193
    00194 {
    00195     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00196     int iExtStart, nLength;
    00197
    00198     for( iExtStart = strlen(pszFullFilename);
    00199          iExtStart > iFileStart && pszFullFilename[iExtStart] != '.';
    00200          iExtStart-- ) {}
    00201
    00202     if( iExtStart == iFileStart )
    00203         iExtStart = strlen(pszFullFilename);
    00204
    00205     nLength = iExtStart - iFileStart;
    00206
    00207     strncpy( szStaticResult, pszFullFilename + iFileStart, nLength );
    00208     szStaticResult[nLength] = '\0';
    00209
    00210     return szStaticResult;
    00211 }
    00212
    00213
    00214 /************************************************************************/
    00215 /*                           CPLGetExtension()                          */
    00216 /************************************************************************/
    00217
    00237 const char *CPLGetExtension( const char *pszFullFilename )
    00238
    00239 {
    00240     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00241     int iExtStart;
    00242
    00243     for( iExtStart = strlen(pszFullFilename);
    00244          iExtStart > iFileStart && pszFullFilename[iExtStart] != '.';
    00245          iExtStart-- ) {}
    00246
    00247     if( iExtStart == iFileStart )
    00248         iExtStart = strlen(pszFullFilename)-1;
    00249
    00250     strcpy( szStaticResult, pszFullFilename+iExtStart+1 );
    00251
    00252     return szStaticResult;
    00253 }
    00254
    00255 /************************************************************************/
    00256 /*                         CPLResetExtension()                          */
    00257 /************************************************************************/
    00258
    00270 const char *CPLResetExtension( const char *pszPath, const char *pszExt )
    00271
    00272 {
    00273     int         i;
    00274
    00275 /* -------------------------------------------------------------------- */
    00276 /*      First, try and strip off any existing extension.                */
    00277 /* -------------------------------------------------------------------- */
    00278     strcpy( szStaticResult, pszPath );
    00279     for( i = strlen(szStaticResult)-1; i > 0; i-- )
    00280     {
    00281         if( szStaticResult[i] == '.' )
    00282         {
    00283             szStaticResult[i] = '\0';
    00284             break;
    00285         }
    00286
    00287         if( szStaticResult[i] == '/' || szStaticResult[i] == '\\'
    00288             || szStaticResult[i] == ':' )
    00289             break;
    00290     }
    00291
    00292 /* -------------------------------------------------------------------- */
    00293 /*      Append the new extension.                                       */
    00294 /* -------------------------------------------------------------------- */
    00295     strcat( szStaticResult, "." );
    00296     strcat( szStaticResult, pszExt );
    00297
    00298     return szStaticResult;
    00299 }
    00300
    00301 /************************************************************************/
    00302 /*                          CPLFormFilename()                           */
    00303 /************************************************************************/
    00304
    00333 const char *CPLFormFilename( const char * pszPath,
    00334                              const char * pszBasename,
    00335                              const char * pszExtension )
    00336
    00337 {
    00338     const char  *pszAddedPathSep = "";
    00339     const char  *pszAddedExtSep = "";
    00340
    00341     if( pszPath == NULL )
    00342         pszPath = "";
    00343     else if( strlen(pszPath) > 0
    00344              && pszPath[strlen(pszPath)-1] != '/'
    00345              && pszPath[strlen(pszPath)-1] != '\\' )
    00346 #ifdef WIN32
    00347         pszAddedPathSep = "\\";
    00348 #else
    00349         pszAddedPathSep = "/";
    00350 #endif
    00351
    00352     if( pszExtension == NULL )
    00353         pszExtension = "";
    00354     else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 )
    00355         pszAddedExtSep = ".";
    00356
    00357     sprintf( szStaticResult, "%s%s%s%s%s",
    00358              pszPath, pszAddedPathSep,
    00359              pszBasename,
    00360              pszAddedExtSep, pszExtension );
    00361
    00362     return szStaticResult;
    00363 }
    00364
    00365 /************************************************************************/
    00366 /*                          CPLFormCIFilename()                         */
    00367 /************************************************************************/
    00368
    00393 const char *CPLFormCIFilename( const char * pszPath,
    00394                                const char * pszBasename,
    00395                                const char * pszExtension )
    00396
    00397 {
    00398 #ifdef WIN32
    00399     return CPLFormFilename( pszPath, pszBasename, pszExtension );
    00400 #else
    00401     const char  *pszAddedExtSep = "";
    00402     char        *pszFilename;
    00403     const char  *pszFullPath;
    00404     int         nLen = strlen(pszBasename)+2, i;
    00405     FILE        *fp;
    00406
    00407     if( pszExtension != NULL )
    00408         nLen += strlen(pszExtension);
    00409
    00410     pszFilename = (char *) CPLMalloc(nLen);
    00411
    00412     if( pszExtension == NULL )
    00413         pszExtension = "";
    00414     else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 )
    00415         pszAddedExtSep = ".";
    00416
    00417     sprintf( pszFilename, "%s%s%s",
    00418              pszBasename, pszAddedExtSep, pszExtension );
    00419
    00420     pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL );
    00421     fp = VSIFOpen( pszFullPath, "r" );
    00422     if( fp == NULL )
    00423     {
    00424         for( i = 0; pszFilename[i] != '\0'; i++ )
    00425         {
    00426             if( pszFilename[i] >= 'a' && pszFilename[i] <= 'z' )
    00427                 pszFilename[i] = pszFilename[i] + 'A' - 'a';
    00428         }
    00429
    00430         pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL );
    00431         fp = VSIFOpen( pszFullPath, "r" );
    00432     }
    00433
    00434     if( fp == NULL )
    00435     {
    00436         for( i = 0; pszFilename[i] != '\0'; i++ )
    00437         {
    00438             if( pszFilename[i] >= 'A' && pszFilename[i] <= 'Z' )
    00439                 pszFilename[i] = pszFilename[i] + 'a' - 'A';
    00440         }
    00441
    00442         pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL );
    00443         fp = VSIFOpen( pszFullPath, "r" );
    00444     }
    00445
    00446     if( fp != NULL )
    00447         VSIFClose( fp );
    00448     else
    00449         pszFullPath = CPLFormFilename( pszPath, pszBasename, pszExtension );
    00450
    00451     CPLFree( pszFilename );
    00452
    00453     return pszFullPath;
    00454 #endif
    00455 }