00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00030 00030 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040
00041
00042 #include "cpl_conv.h"
00043 #include "cpl_string.h"
00044
00045
00046 static char szStaticResult[1024]; 00047
00048
00049
00050
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
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
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
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
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
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 }