cwFileSys.h/cpp : Added expandPath().

This commit is contained in:
kevin.larke 2020-04-20 15:01:21 -04:00
parent 85d7098061
commit 3d1facf2c2
2 changed files with 65 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include <libgen.h> // basename() dirname() #include <libgen.h> // basename() dirname()
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> // opendir()/readdir() #include <dirent.h> // opendir()/readdir()
#include <wordexp.h>
#endif #endif
namespace cw namespace cw
@ -203,6 +204,66 @@ char* cw::filesys::makeFn( const char* dir, const char* fn, const char* ext, ..
return fnOut; return fnOut;
} }
char* cw::filesys::expandPath( const char* dir )
{
rc_t rc = kOkRC;
int sysRC = 0;
int flags = WRDE_NOCMD;
char* newDir = nullptr;
wordexp_t res;
memset(&res,0,sizeof(res));
if((sysRC = wordexp(dir,&res,flags)) != 0)
{
switch(sysRC)
{
case WRDE_BADCHAR:
rc = cwLogError(kOpFailRC,"Bad character.");
break;
case WRDE_BADVAL:
rc = cwLogError(kOpFailRC,"Bad value.");
break;
case WRDE_CMDSUB:
rc = cwLogError(kOpFailRC,"Command substitution forbidden.");
break;
case WRDE_NOSPACE:
rc = cwLogError(kOpFailRC,"Mem. alloc failed.");
break;
case WRDE_SYNTAX:
rc = cwLogError(kOpFailRC,"Syntax error..");
break;
}
goto errLabel;
}
if( res.we_wordc > 1 )
{
rc = cwLogError(kOpFailRC,"Unexpected word expansion count: %i.", res.we_wordc );
goto errLabel;
}
if( res.we_wordc == 1 )
newDir = mem::duplStr(res.we_wordv[0]);
else
newDir = mem::duplStr(dir);
errLabel:
if( rc != kOkRC )
rc = cwLogError(rc,"Path expansion failed.");
wordfree(&res);
return newDir;
}
cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr )

View File

@ -20,11 +20,14 @@ namespace cw
// 'dirPrefixStr' and the file name. // 'dirPrefixStr' and the file name.
// Terminate var arg's directory list with a nullptr. // Terminate var arg's directory list with a nullptr.
// //
// The returned string must be released by a call to memRelease() or memFree(). // The returned string must be released by a call to mem::release() or mem::free().
char* vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ); char* vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl );
char* makeFn( const char* dir, const char* fn, const char* ext, ... ); char* makeFn( const char* dir, const char* fn, const char* ext, ... );
char* expandPath( const char* dir );
// Parse a path into its parts: // Parse a path into its parts:
// //
// Return record used by pathParts() // Return record used by pathParts()