Translate the supplied null terminate string, assumed to be an path relative to the current directory, into an absolute path. Places the result into the supplied string recepticle. Note that if the relative path is not supplied the output value will be the name of the current working directory. Note also that this function DOES NOT TEST if the resulting path exists or that it can be accessed.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | GetAbsPath | (hzString&,const char*,) |
Declared in file: hzDirectory.h
Defined in file : hzDirectory.cpp
Function Logic:
Function body:
hzEcode GetAbsPath (hzString& abs)const char* rel,
{
// Category: Directory
//
// Translate the supplied null terminate string, assumed to be an path relative to the current directory, into an absolute path. Places the result into the
// supplied string recepticle.
//
// Note that if the relative path is not supplied the output value will be the name of the current working directory.
//
// Note also that this function DOES NOT TEST if the resulting path exists or that it can be accessed.
//
// Arguments: 1) Dir The hzString reference to populate
//
// Returns: E_NOTFOUND If the current directory has access issues or has been unlinked
// E_OK If the operation was successful
_hzfunc("GetCurrDir") ;
char* cpDir ; // Buffer for current working directory
// If the supplied relative path is actually absolute, just set absolte = relative
if (rel[0]== CHAR_FWSLASH)
{ abs = rel ; return E_OK ; }
// Look for the home directory sequence
if (rel[0]== CHAR_TILDA && rel[1]== CHAR_FWSLASH)
{
abs = getenv("HOME") ;
abs += "/" ;
abs += (rel + 2);
return E_OK ;
}
// Clear string
abs.Clear() ;
// Get current working dir
cpDir = get_current_dir_name() ;
if (!cpDir)
{
if (errno == EACCES) return hzwarn(E_NOTFOUND, "The current working directory has access issues") ;
if (errno == ENOENT) return hzwarn(E_NOTFOUND, "The current working directory has been unlinked") ;
return hzwarn(E_NOTFOUND, "Unspecified error") ;
}
// If no relative path supplied, use the current directory
if (!rel || !rel[0])
{
abs = cpDir ;
free(cpDir) ;
return E_OK ;
}
char* i ; // Path iterator
char* j ; // Placeholder
uint32_t lev ; // Directory level
uint32_t oset ; // Offset into path
uint32_t step ; // Number of backward steps (../)
if (rel[0]== CHAR_PERIOD && rel[1]== CHAR_PERIOD && rel[2]== CHAR_FWSLASH)
{
for (lev = 0,i = cpDir ; *i ; i++)
{
if (*i == CHAR_FWSLASH)
{ j = i ; lev++ ; }
}
// Count the sequences of ../
for (step = 1,oset = 3; rel[oset] == CHAR_PERIOD && rel[oset+1]== CHAR_PERIOD && rel[oset+2]== CHAR_FWSLASH ; step++, oset += 3);
if (step > lev)
{ free(cpDir) ; return E_BADVALUE ; }
for (; step ; step--)
{
for (j-- ; i != cpDir && *j != CHAR_FWSLASH ; j--) ;
}
*j = 0;
}
abs = cpDir ;
abs += "/" ;
abs += rel ;
free(cpDir) ;
return E_OK ;
}