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.1 2000/09/25 20:50:11 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.1  2000/09/25 20:50:11  warmerda
    00030  * New
    00030  *
    00031  * Revision 1.3  2000/01/26 17:53:36  warmerda
    00032  * Fixed CPLGetExtension() for filenames with no extension.
    00033  *
    00034  * Revision 1.2  2000/01/24 19:32:59  warmerda
    00035  * Fixed CPLGetExtension() to not include the dot.
    00036  *
    00037  * Revision 1.1  1999/10/14 19:23:39  warmerda
    00038  * New
    00039  *
    00040  **********************************************************************/
    00041
    00042 #include "cpl_conv.h"
    00043 #include "cpl_string.h"
    00044
    00045
    00046 static char     szStaticResult[1024]; /* should be size of larged possible
    00047                                          filename */
    00048
    00049 /************************************************************************/
    00050 /*                        CPLFindFilenameStart()                        */
    00051 /************************************************************************/
    00052
    00053 static int CPLFindFilenameStart( const char * pszFilename )
    00054
    00055 {
    00056     int         iFileStart;
    00057
    00058     for( iFileStart = strlen(pszFilename);
    00059          iFileStart > 0
    00060              && pszFilename[iFileStart-1] != '/'
    00061              && pszFilename[iFileStart-1] != '\\';
    00062          iFileStart-- ) {}
    00063
    00064     return iFileStart;
    00065 }
    00066
    00067 /************************************************************************/
    00068 /*                             CPLGetPath()                             */
    00069 /************************************************************************/
    00070
    00092 const char *CPLGetPath( const char *pszFilename )
    00093
    00094 {
    00095     int         iFileStart = CPLFindFilenameStart(pszFilename);
    00096
    00097     if( iFileStart == 0 )
    00098     {
    00099         strcpy( szStaticResult, "" );
    00100         return szStaticResult;
    00101     }
    00102
    00103     strncpy( szStaticResult, pszFilename, iFileStart );
    00104     szStaticResult[iFileStart] = '\0';
    00105
    00106     if( iFileStart > 1
    00107         && (szStaticResult[iFileStart-1] == '/'
    00108             || szStaticResult[iFileStart-1] == '\\') )
    00109         szStaticResult[iFileStart-1] = '\0';
    00110
    00111     return szStaticResult;
    00112 }
    00113
    00114 /************************************************************************/
    00115 /*                           CPLGetFilename()                           */
    00116 /************************************************************************/
    00117
    00138 const char *CPLGetFilename( const char *pszFullFilename )
    00139
    00140 {
    00141     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00142
    00143     strcpy( szStaticResult, pszFullFilename + iFileStart );
    00144
    00145     return szStaticResult;
    00146 }
    00147
    00148 /************************************************************************/
    00149 /*                           CPLGetBasename()                           */
    00150 /************************************************************************/
    00151
    00172 const char *CPLGetBasename( const char *pszFullFilename )
    00173
    00174 {
    00175     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00176     int iExtStart, nLength;
    00177
    00178     for( iExtStart = strlen(pszFullFilename);
    00179          iExtStart > iFileStart && pszFullFilename[iExtStart] != '.';
    00180          iExtStart-- ) {}
    00181
    00182     if( iExtStart == iFileStart )
    00183         iExtStart = strlen(pszFullFilename);
    00184
    00185     nLength = iExtStart - iFileStart;
    00186
    00187     strncpy( szStaticResult, pszFullFilename + iFileStart, nLength );
    00188     szStaticResult[nLength] = '\0';
    00189
    00190     return szStaticResult;
    00191 }
    00192
    00193
    00194 /************************************************************************/
    00195 /*                           CPLGetExtension()                          */
    00196 /************************************************************************/
    00197
    00217 const char *CPLGetExtension( const char *pszFullFilename )
    00218
    00219 {
    00220     int iFileStart = CPLFindFilenameStart( pszFullFilename );
    00221     int iExtStart;
    00222
    00223     for( iExtStart = strlen(pszFullFilename);
    00224          iExtStart > iFileStart && pszFullFilename[iExtStart] != '.';
    00225          iExtStart-- ) {}
    00226
    00227     if( iExtStart == iFileStart )
    00228         iExtStart = strlen(pszFullFilename)-1;
    00229
    00230     strcpy( szStaticResult, pszFullFilename+iExtStart+1 );
    00231
    00232     return szStaticResult;
    00233 }
    00234
    00235 /************************************************************************/
    00236 /*                          CPLFormFilename()                           */
    00237 /************************************************************************/
    00238
    00267 const char *CPLFormFilename( const char * pszPath,
    00268                              const char * pszBasename,
    00269                              const char * pszExtension )
    00270
    00271 {
    00272     const char  *pszAddedPathSep = "";
    00273     const char  *pszAddedExtSep = "";
    00274
    00275     if( pszPath == NULL )
    00276         pszPath = "";
    00277     else if( strlen(pszPath) > 0
    00278              && pszPath[strlen(pszPath)-1] != '/'
    00279              && pszPath[strlen(pszPath)-1] != '\\' )
    00280 #ifdef WIN32
    00281         pszAddedPathSep = "\\";
    00282 #else
    00283         pszAddedPathSep = "/";
    00284 #endif
    00285
    00286     if( pszExtension == NULL )
    00287         pszExtension = "";
    00288     else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 )
    00289         pszAddedExtSep = ".";
    00290
    00291     sprintf( szStaticResult, "%s%s%s%s%s",
    00292              pszPath, pszAddedPathSep,
    00293              pszBasename,
    00294              pszAddedExtSep, pszExtension );
    00295
    00296     return szStaticResult;
    00297 }