cpl/cpl_path.cpp
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;
}
/**
 * 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. 
 **/
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.
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( pszBaseDir == NULL )
    {
        if( pbGotRelative != NULL )
            *pbGotRelative = FALSE;
        return pszTarget;
    }
    nBasePathLen = strlen(pszBaseDir);
    const char *pszTargetPath = CPLGetPath(pszTarget);
    if( (nBasePathLen == 0 || EQUAL(pszBaseDir,"."))
        && (strlen(pszTargetPath) == 0 || EQUAL(pszTargetPath,".")) )
    {
        if( pbGotRelative != NULL )
            *pbGotRelative = TRUE;
        return pszTarget;
    }
    if( nBasePathLen == 0 )
    {
        if( pbGotRelative != NULL )
            *pbGotRelative = FALSE;
        return pszTarget;
    }
    if( !EQUALN(pszBaseDir,pszTarget,nBasePathLen) 
        || (pszTarget[nBasePathLen] != '\\' 
            && pszTarget[nBasePathLen] != '/') )
    {
        if( pbGotRelative != NULL )
            *pbGotRelative = FALSE;
        return pszTarget;
    }
    if( pbGotRelative != NULL )
        *pbGotRelative = TRUE;
    return pszTarget + nBasePathLen + 1;
}
const char * CPLExtractRelativePath ( const char * pszBaseDir , const char * pszTarget , int * pbGotRelative ) { size_t nBasePathLen ; if ( pszBaseDir == NULL ) { if ( pbGotRelative != NULL ) * pbGotRelative = FALSE ; return pszTarget ; } nBasePathLen = strlen ( pszBaseDir ) ; const char * pszTargetPath = CPLGetPath ( pszTarget ) ; if ( ( nBasePathLen == 0 || EQUAL ( pszBaseDir , " " ) ) && ( strlen ( pszTargetPath ) == 0 || EQUAL ( pszTargetPath , " " ) ) ) { if ( pbGotRelative != NULL ) * pbGotRelative = TRUE ; return pszTarget ; } if ( nBasePathLen == 0 ) { if ( pbGotRelative != NULL ) * pbGotRelative = FALSE ; return pszTarget ; } if ( ! EQUALN ( pszBaseDir , pszTarget , nBasePathLen ) || ( pszTarget [ nBasePathLen ] != ' \\ ' && pszTarget [ nBasePathLen ] != ' ' ) ) { if ( pbGotRelative != NULL ) * pbGotRelative = FALSE ; return pszTarget ; } if ( pbGotRelative != NULL ) * pbGotRelative = TRUE ; return pszTarget + nBasePathLen + 1 ; }
Get relative path from directory to target file.
Get relative path from directory to target file .
/* -------------------------------------------------------------------- */ /*      If we don't have a basedir, then we can't relativize the path.  */ /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /*      One simple case is where neither file has a path.  We return    */ /*      the original target filename and it is relative.                */ /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /*      By this point, if we don't have a base path, we can't have a    */ /*      meaningful common prefix.                                       */ /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /*      If we don't have a common path prefix, then we can't get a      */ /*      relative path.                                                  */ /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /*      We have a relative path.  Strip it off to get a string to       */ /*      return.                                                         */ /* -------------------------------------------------------------------- */