00001 /********************************************************************** 00002 * $Id: cpl_path_cpp-source.html,v 1.8 2001/07/05 13:24:08 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.8 2001/07/05 13:24:08 warmerda 00030 * *** empty log message *** 00030 * 00031 * Revision 1.7 2001/05/12 19:20:55 warmerda 00032 * Fixed documentation of CPLGetExtension(). 00033 * 00034 * Revision 1.6 2001/03/16 22:15:08 warmerda 00035 * added CPLResetExtension 00036 * 00037 * Revision 1.5 2001/02/24 01:53:57 warmerda 00038 * Added CPLFormCIFilename() 00039 * 00040 * Revision 1.4 2001/01/19 21:18:25 warmerda 00041 * expanded tabs 00042 * 00043 * Revision 1.3 2000/01/26 17:53:36 warmerda 00044 * Fixed CPLGetExtension() for filenames with no extension. 00045 * 00046 * Revision 1.2 2000/01/24 19:32:59 warmerda 00047 * Fixed CPLGetExtension() to not include the dot. 00048 * 00049 * Revision 1.1 1999/10/14 19:23:39 warmerda 00050 * New 00051 * 00052 **********************************************************************/ 00053 00054 #include "cpl_conv.h" 00055 #include "cpl_string.h" 00056 00057 00058 static char szStaticResult[1024]; /* should be size of larged possible 00059 filename */ 00060 00061 /************************************************************************/ 00062 /* CPLFindFilenameStart() */ 00063 /************************************************************************/ 00064 00065 static int CPLFindFilenameStart( const char * pszFilename ) 00066 00067 { 00068 int iFileStart; 00069 00070 for( iFileStart = strlen(pszFilename); 00071 iFileStart > 0 00072 && pszFilename[iFileStart-1] != '/' 00073 && pszFilename[iFileStart-1] != '\\'; 00074 iFileStart-- ) {} 00075 00076 return iFileStart; 00077 } 00078 00079 /************************************************************************/ 00080 /* CPLGetPath() */ 00081 /************************************************************************/ 00082 00104 const char *CPLGetPath( const char *pszFilename ) 00105 00106 { 00107 int iFileStart = CPLFindFilenameStart(pszFilename); 00108 00109 if( iFileStart == 0 ) 00110 { 00111 strcpy( szStaticResult, "" ); 00112 return szStaticResult; 00113 } 00114 00115 strncpy( szStaticResult, pszFilename, iFileStart ); 00116 szStaticResult[iFileStart] = '\0'; 00117 00118 if( iFileStart > 1 00119 && (szStaticResult[iFileStart-1] == '/' 00120 || szStaticResult[iFileStart-1] == '\\') ) 00121 szStaticResult[iFileStart-1] = '\0'; 00122 00123 return szStaticResult; 00124 } 00125 00126 /************************************************************************/ 00127 /* CPLGetFilename() */ 00128 /************************************************************************/ 00129 00150 const char *CPLGetFilename( const char *pszFullFilename ) 00151 00152 { 00153 int iFileStart = CPLFindFilenameStart( pszFullFilename ); 00154 00155 strcpy( szStaticResult, pszFullFilename + iFileStart ); 00156 00157 return szStaticResult; 00158 } 00159 00160 /************************************************************************/ 00161 /* CPLGetBasename() */ 00162 /************************************************************************/ 00163 00184 const char *CPLGetBasename( const char *pszFullFilename ) 00185 00186 { 00187 int iFileStart = CPLFindFilenameStart( pszFullFilename ); 00188 int iExtStart, nLength; 00189 00190 for( iExtStart = strlen(pszFullFilename); 00191 iExtStart > iFileStart && pszFullFilename[iExtStart] != '.'; 00192 iExtStart-- ) {} 00193 00194 if( iExtStart == iFileStart ) 00195 iExtStart = strlen(pszFullFilename); 00196 00197 nLength = iExtStart - iFileStart; 00198 00199 strncpy( szStaticResult, pszFullFilename + iFileStart, nLength ); 00200 szStaticResult[nLength] = '\0'; 00201 00202 return szStaticResult; 00203 } 00204 00205 00206 /************************************************************************/ 00207 /* CPLGetExtension() */ 00208 /************************************************************************/ 00209 00229 const char *CPLGetExtension( const char *pszFullFilename ) 00230 00231 { 00232 int iFileStart = CPLFindFilenameStart( pszFullFilename ); 00233 int iExtStart; 00234 00235 for( iExtStart = strlen(pszFullFilename); 00236 iExtStart > iFileStart && pszFullFilename[iExtStart] != '.'; 00237 iExtStart-- ) {} 00238 00239 if( iExtStart == iFileStart ) 00240 iExtStart = strlen(pszFullFilename)-1; 00241 00242 strcpy( szStaticResult, pszFullFilename+iExtStart+1 ); 00243 00244 return szStaticResult; 00245 } 00246 00247 /************************************************************************/ 00248 /* CPLResetExtension() */ 00249 /************************************************************************/ 00250 00262 const char *CPLResetExtension( const char *pszPath, const char *pszExt ) 00263 00264 { 00265 int i; 00266 00267 /* -------------------------------------------------------------------- */ 00268 /* First, try and strip off any existing extension. */ 00269 /* -------------------------------------------------------------------- */ 00270 strcpy( szStaticResult, pszPath ); 00271 for( i = strlen(szStaticResult)-1; i > 0; i-- ) 00272 { 00273 if( szStaticResult[i] == '.' ) 00274 { 00275 szStaticResult[i] = '\0'; 00276 break; 00277 } 00278 00279 if( szStaticResult[i] == '/' || szStaticResult[i] == '\\' 00280 || szStaticResult[i] == ':' ) 00281 break; 00282 } 00283 00284 /* -------------------------------------------------------------------- */ 00285 /* Append the new extension. */ 00286 /* -------------------------------------------------------------------- */ 00287 strcat( szStaticResult, "." ); 00288 strcat( szStaticResult, pszExt ); 00289 00290 return szStaticResult; 00291 } 00292 00293 /************************************************************************/ 00294 /* CPLFormFilename() */ 00295 /************************************************************************/ 00296 00325 const char *CPLFormFilename( const char * pszPath, 00326 const char * pszBasename, 00327 const char * pszExtension ) 00328 00329 { 00330 const char *pszAddedPathSep = ""; 00331 const char *pszAddedExtSep = ""; 00332 00333 if( pszPath == NULL ) 00334 pszPath = ""; 00335 else if( strlen(pszPath) > 0 00336 && pszPath[strlen(pszPath)-1] != '/' 00337 && pszPath[strlen(pszPath)-1] != '\\' ) 00338 #ifdef WIN32 00339 pszAddedPathSep = "\\"; 00340 #else 00341 pszAddedPathSep = "/"; 00342 #endif 00343 00344 if( pszExtension == NULL ) 00345 pszExtension = ""; 00346 else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) 00347 pszAddedExtSep = "."; 00348 00349 sprintf( szStaticResult, "%s%s%s%s%s", 00350 pszPath, pszAddedPathSep, 00351 pszBasename, 00352 pszAddedExtSep, pszExtension ); 00353 00354 return szStaticResult; 00355 } 00356 00357 /************************************************************************/ 00358 /* CPLFormCIFilename() */ 00359 /************************************************************************/ 00360 00385 const char *CPLFormCIFilename( const char * pszPath, 00386 const char * pszBasename, 00387 const char * pszExtension ) 00388 00389 { 00390 #ifdef WIN32 00391 return CPLFormFilename( pszPath, pszBasename, pszExtension ); 00392 #else 00393 const char *pszAddedExtSep = ""; 00394 char *pszFilename; 00395 const char *pszFullPath; 00396 int nLen = strlen(pszBasename)+2, i; 00397 FILE *fp; 00398 00399 if( pszExtension != NULL ) 00400 nLen += strlen(pszExtension); 00401 00402 pszFilename = (char *) CPLMalloc(nLen); 00403 00404 if( pszExtension == NULL ) 00405 pszExtension = ""; 00406 else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) 00407 pszAddedExtSep = "."; 00408 00409 sprintf( pszFilename, "%s%s%s", 00410 pszBasename, pszAddedExtSep, pszExtension ); 00411 00412 pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); 00413 fp = VSIFOpen( pszFullPath, "r" ); 00414 if( fp == NULL ) 00415 { 00416 for( i = 0; pszFilename[i] != '\0'; i++ ) 00417 { 00418 if( pszFilename[i] >= 'a' && pszFilename[i] <= 'z' ) 00419 pszFilename[i] = pszFilename[i] + 'A' - 'a'; 00420 } 00421 00422 pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); 00423 fp = VSIFOpen( pszFullPath, "r" ); 00424 } 00425 00426 if( fp == NULL ) 00427 { 00428 for( i = 0; pszFilename[i] != '\0'; i++ ) 00429 { 00430 if( pszFilename[i] >= 'A' && pszFilename[i] <= 'Z' ) 00431 pszFilename[i] = pszFilename[i] + 'a' - 'A'; 00432 } 00433 00434 pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); 00435 fp = VSIFOpen( pszFullPath, "r" ); 00436 } 00437 00438 if( fp != NULL ) 00439 VSIFClose( fp ); 00440 else 00441 pszFullPath = CPLFormFilename( pszPath, pszBasename, pszExtension ); 00442 00443 CPLFree( pszFilename ); 00444 00445 return pszFullPath; 00446 #endif 00447 }