librnr/path.c
char *NewSearchPathCanonicalized(const char *sSearchPath)
{
  char   *sExpPath;     // expanded search path
  char   *sPath;        // expanded path
  char   *sNorm;        // normalized path
  char   *sSep;         // path separator
  char   *sNew;         // canonical search path
  char   *sSave = NULL; // strtok() save pointer
  char   *s;            // working string
  size_t  k, m, n, len; // working sizes

  // expand any environment variables and '~''s listed in search path
  if( (sExpPath = NewSearchPathExpanded(sSearchPath)) == NULL )
  {
    return NULL;
  }

  len   = strlen(sExpPath);
  sNew  = NEWSTR(len);
  n     = 0;
  sSep  = "";
  k     = 0;

  for(sPath=strtok_r(sExpPath, PATH_SEP_STR, &sSave); 
      sPath!=NULL; 
      sPath=strtok_r(NULL, PATH_SEP_STR, &sSave))
  {
    sNorm = NewNormPath(sPath);
    m     = strlen(sNorm);

    // grow new search path
    if( n+k+m >= len )
    {
      len = (n + k + m) * 2;
      s = NEWSTR(len);
      strcpy(s, sNew);
      delete(sNew);
      sNew = s;
    }

    sprintf(sNew+n, "%s%s", sSep, sNorm);
    n += k + m;

    if( *sSep == 0 )
    {
      sSep  = PATH_SEP_STR;
      k     = strlen(sSep);
    }
  }

  delete(sExpPath);

  return sNew;
}
/*!
 * \brief Expands and canonicalizes a search path.
 *
 * The path does not have to exist.
 *
 * A canonical search path has: \n
 * \li any beginning or ending path separators stipped
 * \li any contiguous sequence of path separators replaced with a single 
 *     separator
 * \li any contiguous sequence of directory separators replaced with a single
 *     separator
 * \li any terminating directory separator stripped from each path component
 *
 * \param sSearchPath   Search path of form: path [path_sep path...]
 *
 * \return 
 *  Return allocated canonical search path on success, NULL if search path is
 *  is not specified. If the search path is too long, a truncated version
 *  will be returned.
 */
\brief Expands and canonicalizes a search path.
The path does not have to exist.





\return
Return allocated canonical search path on success, NULL if search path is
is not specified. If the search path is too long, a truncated version
will be returned.
\ brief Expands and canonicalizes a search path . The path does not have to exist . \ return Return allocated canonical search path on success NULL if search path is is not specified . If the search path is too long a truncated version will be returned .
char *NewSearchPathCanonicalized(const char *sSearchPath)
{
  char   *sExpPath;     
  char   *sPath;        
  char   *sNorm;        
  char   *sSep;         
  char   *sNew;         
  char   *sSave = NULL; 
  char   *s;            
  size_t  k, m, n, len; 
  if( (sExpPath = NewSearchPathExpanded(sSearchPath)) == NULL )
  {
    return NULL;
  }
  len   = strlen(sExpPath);
  sNew  = NEWSTR(len);
  n     = 0;
  sSep  = "";
  k     = 0;
  for(sPath=strtok_r(sExpPath, PATH_SEP_STR, &sSave); 
      sPath!=NULL; 
      sPath=strtok_r(NULL, PATH_SEP_STR, &sSave))
  {
    sNorm = NewNormPath(sPath);
    m     = strlen(sNorm);
    if( n+k+m >= len )
    {
      len = (n + k + m) * 2;
      s = NEWSTR(len);
      strcpy(s, sNew);
      delete(sNew);
      sNew = s;
    }
    sprintf(sNew+n, "%s%s", sSep, sNorm);
    n += k + m;
    if( *sSep == 0 )
    {
      sSep  = PATH_SEP_STR;
      k     = strlen(sSep);
    }
  }
  delete(sExpPath);
  return sNew;
}
char * NewSearchPathCanonicalized ( const char * sSearchPath ) { char * sExpPath ; char * sPath ; char * sNorm ; char * sSep ; char * sNew ; char * sSave = NULL ; char * s ; size_t k , m , n , len ; if ( ( sExpPath = NewSearchPathExpanded ( sSearchPath ) ) == NULL ) { return NULL ; } len = strlen ( sExpPath ) ; sNew = NEWSTR ( len ) ; n = 0 ; sSep = " " ; k = 0 ; for ( sPath = strtok_r ( sExpPath , PATH_SEP_STR , & sSave ) ; sPath != NULL ; sPath = strtok_r ( NULL , PATH_SEP_STR , & sSave ) ) { sNorm = NewNormPath ( sPath ) ; m = strlen ( sNorm ) ; if ( n + k + m >= len ) { len = ( n + k + m ) * 2 ; s = NEWSTR ( len ) ; strcpy ( s , sNew ) ; delete ( sNew ) ; sNew = s ; } sprintf ( sNew + n , " " , sSep , sNorm ) ; n += k + m ; if ( * sSep == 0 ) { sSep = PATH_SEP_STR ; k = strlen ( sSep ) ; } } delete ( sExpPath ) ; return sNew ; }
\brief Expands and canonicalizes a search path.
\ brief Expands and canonicalizes a search path .
// expanded search path // expanded path // normalized path // path separator // canonical search path // strtok() save pointer // working string // working sizes // expand any environment variables and '~''s listed in search path // grow new search path