📄 cpl_path.cpp
字号:
strcat( pszStaticResult, "." );
strcat( pszStaticResult, pszExt );
return pszStaticResult;
}
/************************************************************************/
/* 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 )
{
char *pszStaticResult = CPLGetStaticResult();
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( pszStaticResult, pszPath, CPL_PATH_BUF_SIZE );
strncat( pszStaticResult, pszAddedPathSep, CPL_PATH_BUF_SIZE);
strncat( pszStaticResult, pszBasename, CPL_PATH_BUF_SIZE);
strncat( pszStaticResult, pszAddedExtSep, CPL_PATH_BUF_SIZE);
strncat( pszStaticResult, pszExtension, CPL_PATH_BUF_SIZE);
pszStaticResult[CPL_PATH_BUF_SIZE - 1] = '\0';
return pszStaticResult;
}
/************************************************************************/
/* 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 )
{
#if defined(WIN32) || defined(WIN32CE)
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( islower(pszFilename[i]) )
pszFilename[i] = toupper(pszFilename[i]);
}
pszFullPath = CPLFormFilename( pszPath, pszFilename, NULL );
fp = VSIFOpen( pszFullPath, "r" );
}
if( fp == NULL )
{
for( i = 0; pszFilename[i] != '\0'; i++ )
{
if( isupper(pszFilename[i]) )
pszFilename[i] = tolower(pszFilename[i]);
}
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 )
{
char *pszStaticResult = CPLGetStaticResult();
if( !CPLIsFilenameRelative( pszSecondaryFilename ) )
return pszSecondaryFilename;
if( pszProjectDir == NULL || strlen(pszProjectDir) == 0 )
return pszSecondaryFilename;
strncpy( pszStaticResult, pszProjectDir, CPL_PATH_BUF_SIZE );
pszStaticResult[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( pszStaticResult, SEP_STRING );
}
CPLAssert( strlen(pszSecondaryFilename) + 1 < CPL_PATH_BUF_SIZE );
strcat( pszStaticResult, pszSecondaryFilename );
return pszStaticResult;
}
/************************************************************************/
/* 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
|| strncmp(pszFilename+1,":/",2) == 0))
|| pszFilename[0] == '\\'
|| pszFilename[0] == '/' )
return FALSE;
else
return TRUE;
}
/************************************************************************/
/* CPLExtractRelativePath() */
/************************************************************************/
/**
* Get relative path from directory to target file.
*
* Computes a relative path for pszTarget relative to pszBaseDir.
* Currently this only works if they share a common base path. The returned
* path is normally into the pszTarget string. It should only be considered
* valid as long as pszTarget is valid or till the next call to
* this function, whichever comes first.
*
* @param pszBaseDir the name of the directory relative to which the path
* should be computed. pszBaseDir may be NULL in which case the original
* target is returned without relitivizing.
*
* @param pszTarget the filename to be changed to be relative to pszBaseDir.
*
* @param pbGotRelative Pointer to location in which a flag is placed
* indicating that the returned path is relative to the basename (TRUE) or
* not (FALSE). This pointer may be NULL if flag is not desired.
*
* @return an adjusted path or the original if it could not be made relative
* to the pszBaseFile's path.
**/
const char *CPLExtractRelativePath( const char *pszBaseDir,
const char *pszTarget,
int *pbGotRelative )
{
size_t nBasePathLen;
/* -------------------------------------------------------------------- */
/* If we don't have a basedir, then we can't relativize the path. */
/* -------------------------------------------------------------------- */
if( pszBaseDir == NULL )
{
if( pbGotRelative != NULL )
*pbGotRelative = FALSE;
return pszTarget;
}
nBasePathLen = strlen(pszBaseDir);
/* -------------------------------------------------------------------- */
/* One simple case is where neither file has a path. We return */
/* the original target filename and it is relative. */
/* -------------------------------------------------------------------- */
const char *pszTargetPath = CPLGetPath(pszTarget);
if( (nBasePathLen == 0 || EQUAL(pszBaseDir,"."))
&& (strlen(pszTargetPath) == 0 || EQUAL(pszTargetPath,".")) )
{
if( pbGotRelative != NULL )
*pbGotRelative = TRUE;
return pszTarget;
}
/* -------------------------------------------------------------------- */
/* By this point, if we don't have a base path, we can't have a */
/* meaningful common prefix. */
/* -------------------------------------------------------------------- */
if( nBasePathLen == 0 )
{
if( pbGotRelative != NULL )
*pbGotRelative = FALSE;
return pszTarget;
}
/* -------------------------------------------------------------------- */
/* If we don't have a common path prefix, then we can't get a */
/* relative path. */
/* -------------------------------------------------------------------- */
if( !EQUALN(pszBaseDir,pszTarget,nBasePathLen)
|| (pszTarget[nBasePathLen] != '\\'
&& pszTarget[nBasePathLen] != '/') )
{
if( pbGotRelative != NULL )
*pbGotRelative = FALSE;
return pszTarget;
}
/* -------------------------------------------------------------------- */
/* We have a relative path. Strip it off to get a string to */
/* return. */
/* -------------------------------------------------------------------- */
if( pbGotRelative != NULL )
*pbGotRelative = TRUE;
return pszTarget + nBasePathLen + 1;
}
/************************************************************************/
/* CPLCleanTrailingSlash() */
/************************************************************************/
/**
* Remove trailing forward/backward slash from the path for unix/windows resp.
*
* Returns a string containing the portion of the passed path string with
* trailing slash removed. If there is no path in the passed filename
* an empty string will be returned (not NULL).
*
* <pre>
* CPLCleanTrailingSlash( "abc/def/" ) == "abc/def"
* CPLCleanTrailingSlash( "abc/def" ) == "abc/def"
* CPLCleanTrailingSlash( "c:\abc\def\" ) == "c:\abc\def"
* CPLCleanTrailingSlash( "c:\abc\def" ) == "c:\abc\def"
* CPLCleanTrailingSlash( "abc" ) == "abc"
* </pre>
*
* @param pszPath the path to be cleaned up
*
* @return Path in an internal string which must not be freed. The string
* may be destroyed by the next CPL filename handling call. The returned
* will generally not contain a trailing path separator.
*/
const char *CPLCleanTrailingSlash( const char *pszFilename )
{
char *pszStaticResult = CPLGetStaticResult();
int iPathLength = strlen(pszFilename);
strncpy( pszStaticResult, pszFilename, iPathLength );
pszStaticResult[iPathLength] = '\0';
if( iPathLength > 0
&& (pszStaticResult[iPathLength-1] == '\\'
|| pszStaticResult[iPathLength-1] == '/'))
pszStaticResult[iPathLength-1] = '\0';
return pszStaticResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -