Changeset 5811:28f6eed531d7 in roaraudio for libroar/env.c


Ignore:
Timestamp:
01/05/13 01:29:03 (11 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

improved roar_env_render_path_r() and added support for it to roar-config

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/env.c

    r5754 r5811  
    121121} 
    122122 
     123static char * __get_path(const char ** inpath) { 
     124 char buf_path[64]; 
     125 size_t i; 
     126 
     127 (*inpath)++; 
     128 
     129 for (i = 0; i < sizeof(buf_path); i++) { 
     130  if ( (*inpath)[i] == '/' || (*inpath)[i] == 0 ) { 
     131   break; 
     132  } else { 
     133   buf_path[i] = (*inpath)[i]; 
     134  } 
     135 } 
     136 
     137 if ( i == sizeof(buf_path) ) { 
     138  roar_err_set(ROAR_ERROR_NOSPC); 
     139  return NULL; 
     140 } 
     141 
     142 buf_path[i] = 0; 
     143 
     144 (*inpath) += i; 
     145 
     146 return roar_libroar_get_path(buf_path, 0, NULL, NULL); 
     147} 
     148 
    123149int roar_env_render_path_r(char * out, size_t len, const char * inpath) { 
    124150 size_t inlen; 
     
    126152 size_t homelen = 0; 
    127153 const char * home; 
    128  enum { UNKNOWN, COPY, HOME } mode = UNKNOWN; 
     154 const char * prefix = NULL; 
     155 char * prefix_path = NULL; 
     156 enum { UNKNOWN, COPY, HOME, PREFIX } mode = UNKNOWN; 
    129157 
    130158 if ( len == 0 && inpath == NULL ) 
     
    159187  needlen = inlen + homelen - 1; 
    160188  mode    = HOME; 
    161  } 
    162  
    163  if ( len < (needlen + 1) ) 
    164   return -1; 
     189  prefix  = home; 
     190  inpath++; 
     191 } else if ( inpath[0] == '$' ) { 
     192  prefix_path = __get_path(&inpath); 
     193  if ( prefix_path == NULL ) 
     194   return -1; 
     195  needlen = roar_mm_strlen(inpath) + roar_mm_strlen(prefix_path) + 1; 
     196  mode    = PREFIX; 
     197  prefix  = prefix_path; 
     198 } 
     199 
     200 if ( len < (needlen + 1) ) { 
     201  if ( prefix_path != NULL ) 
     202   roar_mm_free(prefix_path); 
     203  roar_err_set(ROAR_ERROR_NOSPC); 
     204  return -1; 
     205 } 
    165206 
    166207 switch (mode) { 
     
    170211   break; 
    171212  case HOME: 
    172     roar_mm_strlcpy(out, home, len); 
    173     roar_mm_strlcat(out, inpath+1, len); // strip only ~, so we have the / if home is not /-terminated. 
     213  case PREFIX: 
     214    roar_mm_strlcpy(out, prefix, len); 
     215    roar_mm_strlcat(out, inpath, len); // strip only ~, so we have the / if home is not /-terminated. 
    174216   break; 
    175217  default: 
     218    if ( prefix_path != NULL ) 
     219     roar_mm_free(prefix_path); 
     220    roar_err_set(ROAR_ERROR_NOTSUP); 
    176221    return -1; 
    177222   break; 
    178223 } 
    179224 
     225 if ( prefix_path != NULL ) 
     226  roar_mm_free(prefix_path); 
    180227 return 0; 
    181228} 
Note: See TracChangeset for help on using the changeset viewer.