📄 cpl_path.cpp
字号:
*/const char *CPLGetExtension( const char *pszFullFilename ){ int iFileStart = CPLFindFilenameStart( pszFullFilename ); int iExtStart; for( iExtStart = strlen(pszFullFilename); iExtStart > iFileStart && pszFullFilename[iExtStart] != '.'; iExtStart-- ) {} if( iExtStart == iFileStart ) iExtStart = strlen(pszFullFilename)-1; strncpy( szStaticResult, pszFullFilename+iExtStart+1, CPL_PATH_BUF_SIZE ); szStaticResult[CPL_PATH_BUF_SIZE - 1] = '\0'; return szStaticResult;}/************************************************************************//* CPLResetExtension() *//************************************************************************//** * Replace the extension with the provided one. * * @param pszPath the input path, this string is not altered. * @param pszExt the new extension to apply to the given path. * * @return an altered filename with the new extension. Do not * modify or free the returned string. The string may be destroyed by the * next CPL call. */const char *CPLResetExtension( const char *pszPath, const char *pszExt ){ int i;/* -------------------------------------------------------------------- *//* First, try and strip off any existing extension. *//* -------------------------------------------------------------------- */ strncpy( szStaticResult, pszPath, CPL_PATH_BUF_SIZE ); szStaticResult[CPL_PATH_BUF_SIZE - 1] = '\0'; for( i = strlen(szStaticResult) - 1; i > 0; i-- ) { if( szStaticResult[i] == '.' ) { szStaticResult[i] = '\0'; break; } if( szStaticResult[i] == '/' || szStaticResult[i] == '\\' || szStaticResult[i] == ':' ) break; }/* -------------------------------------------------------------------- *//* Append the new extension. *//* -------------------------------------------------------------------- */ CPLAssert( strlen(pszExt) + 2 < CPL_PATH_BUF_SIZE ); strcat( szStaticResult, "." ); strcat( szStaticResult, pszExt ); return szStaticResult;}/************************************************************************//* CPLFormFilename() *//************************************************************************//** * Build a full file path from a passed path, file basename and extension. * * The path, and extension are optional. The basename may in fact contain * an extension if desired. * * <pre> * CPLFormFilename("abc/xyz","def", ".dat" ) == "abc/xyz/def.dat" * CPLFormFilename(NULL,"def", NULL ) == "def" * CPLFormFilename(NULL,"abc/def.dat", NULL ) == "abc/def.dat" * CPLFormFilename("/abc/xyz/","def.dat", NULL ) == "/abc/xyz/def.dat" * </pre> * * @param pszPath directory path to the directory containing the file. This * may be relative or absolute, and may have a trailing path separator or * not. May be NULL. * * @param pszBasename file basename. May optionally have path and/or * extension. May not be NULL. * * @param pszExtension file extension, optionally including the period. May * be NULL. * * @return a fully formed filename in an internal static string. Do not * modify or free the returned string. The string may be destroyed by the * next CPL call. */const char *CPLFormFilename( const char * pszPath, const char * pszBasename, const char * pszExtension ){ const char *pszAddedPathSep = ""; const char *pszAddedExtSep = ""; if( pszPath == NULL ) pszPath = ""; else if( strlen(pszPath) > 0 && pszPath[strlen(pszPath)-1] != '/' && pszPath[strlen(pszPath)-1] != '\\' ) pszAddedPathSep = SEP_STRING; if( pszExtension == NULL ) pszExtension = ""; else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) pszAddedExtSep = "."; CPLAssert( strlen(pszPath) + strlen(pszAddedPathSep) + strlen(pszBasename) + strlen(pszAddedExtSep) + strlen(pszExtension) + 1 < CPL_PATH_BUF_SIZE ); strncpy( szStaticResult, pszPath, CPL_PATH_BUF_SIZE ); strncat( szStaticResult, pszAddedPathSep, CPL_PATH_BUF_SIZE); strncat( szStaticResult, pszBasename, CPL_PATH_BUF_SIZE); strncat( szStaticResult, pszAddedExtSep, CPL_PATH_BUF_SIZE); strncat( szStaticResult, pszExtension, CPL_PATH_BUF_SIZE); szStaticResult[CPL_PATH_BUF_SIZE - 1] = '\0'; return szStaticResult;}/************************************************************************//* CPLFormCIFilename() *//************************************************************************//** * Case insensitive file searching, returing full path. * * This function tries to return the path to a file regardless of * whether the file exactly matches the basename, and extension case, or * is all upper case, or all lower case. The path is treated as case * sensitive. This function is equivelent to CPLFormFilename() on * case insensitive file systems (like Windows). * * @param pszPath directory path to the directory containing the file. This * may be relative or absolute, and may have a trailing path separator or * not. May be NULL. * * @param pszBasename file basename. May optionally have path and/or * extension. May not be NULL. * * @param pszExtension file extension, optionally including the period. May * be NULL. * * @return a fully formed filename in an internal static string. Do not * modify or free the returned string. The string may be destroyed by the * next CPL call. */const char *CPLFormCIFilename( const char * pszPath, const char * pszBasename, const char * pszExtension ){#ifdef WIN32 return CPLFormFilename( pszPath, pszBasename, pszExtension );#else const char *pszAddedExtSep = ""; char *pszFilename; const char *pszFullPath; int nLen = strlen(pszBasename)+2, i; FILE *fp; if( pszExtension != NULL ) nLen += strlen(pszExtension); pszFilename = (char *) CPLMalloc(nLen); if( pszExtension == NULL ) pszExtension = ""; else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) pszAddedExtSep = "."; sprintf( pszFilename, "%s%s%s", pszBasename, pszAddedExtSep, pszExtension ); pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); fp = VSIFOpen( pszFullPath, "r" ); if( fp == NULL ) { for( i = 0; pszFilename[i] != '\0'; i++ ) { if( pszFilename[i] >= 'a' && pszFilename[i] <= 'z' ) pszFilename[i] = pszFilename[i] + 'A' - 'a'; } pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); fp = VSIFOpen( pszFullPath, "r" ); } if( fp == NULL ) { for( i = 0; pszFilename[i] != '\0'; i++ ) { if( pszFilename[i] >= 'A' && pszFilename[i] <= 'Z' ) pszFilename[i] = pszFilename[i] + 'a' - 'A'; } pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL ); fp = VSIFOpen( pszFullPath, "r" ); } if( fp != NULL ) VSIFClose( fp ); else pszFullPath = CPLFormFilename( pszPath, pszBasename, pszExtension ); CPLFree( pszFilename ); return pszFullPath;#endif}/************************************************************************//* CPLProjectRelativeFilename() *//************************************************************************//** * Find a file relative to a project file. * * Given the path to a "project" directory, and a path to a secondary file * referenced from that project, build a path to the secondary file * that the current application can use. If the secondary path is already * absolute, rather than relative, then it will be returned unaltered. * * Examples: * <pre> * CPLProjectRelativeFilename("abc/def","tmp/abc.gif") == "abc/def/tmp/abc.gif" * CPLProjectRelativeFilename("abc/def","/tmp/abc.gif") == "/tmp/abc.gif" * CPLProjectRelativeFilename("/xy", "abc.gif") == "/xy/abc.gif" * CPLProjectRelativeFilename("/abc/def","../abc.gif") == "/abc/def/../abc.gif" * CPLProjectRelativeFilename("C:\WIN","abc.gif") == "C:\WIN\abc.gif" * </pre> * * @param pszProjectDir the directory relative to which the secondary files * path should be interpreted. * @param pszSecondaryFilename the filename (potentially with path) that * is to be interpreted relative to the project directory. * * @return a composed path to the secondary file. The returned string is * internal and should not be altered, freed, or depending on past the next * CPL call. */const char *CPLProjectRelativeFilename( const char *pszProjectDir, const char *pszSecondaryFilename ){ if( !CPLIsFilenameRelative( pszSecondaryFilename ) ) return pszSecondaryFilename; if( pszProjectDir == NULL || strlen(pszProjectDir) == 0 ) return pszSecondaryFilename; strncpy( szStaticResult, pszProjectDir, CPL_PATH_BUF_SIZE ); szStaticResult[CPL_PATH_BUF_SIZE - 1] = '\0'; if( pszProjectDir[strlen(pszProjectDir)-1] != '/' && pszProjectDir[strlen(pszProjectDir)-1] != '\\' ) { CPLAssert( strlen(SEP_STRING) + 1 < CPL_PATH_BUF_SIZE ); strcat( szStaticResult, SEP_STRING ); } CPLAssert( strlen(pszSecondaryFilename) + 1 < CPL_PATH_BUF_SIZE ); strcat( szStaticResult, pszSecondaryFilename ); return szStaticResult;}/************************************************************************//* CPLIsFilenameRelative() *//************************************************************************//** * Is filename relative or absolute? * * The test is filesystem convention agnostic. That is it will test for * Unix style and windows style path conventions regardless of the actual * system in use. * * @param pszFilename the filename with path to test. * * @return TRUE if the filename is relative or FALSE if it is absolute. */int CPLIsFilenameRelative( const char *pszFilename ){ if( (strlen(pszFilename) > 2 && strncmp(pszFilename+1,":\\",2) == 0) || pszFilename[0] == '\\' || pszFilename[0] == '/' ) return FALSE; else return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -