From 3d1facf2c2020ce19a7c69632bcd64deec7b6bd0 Mon Sep 17 00:00:00 2001 From: "kevin.larke" Date: Mon, 20 Apr 2020 15:01:21 -0400 Subject: [PATCH] cwFileSys.h/cpp : Added expandPath(). --- cwFileSys.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ cwFileSys.h | 5 ++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 4fc4ea3..dffe1a1 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -9,6 +9,7 @@ #include // basename() dirname() #include #include // opendir()/readdir() +#include #endif namespace cw @@ -203,6 +204,66 @@ char* cw::filesys::makeFn( const char* dir, const char* fn, const char* ext, .. 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 ) diff --git a/cwFileSys.h b/cwFileSys.h index 3ce3548..3f57792 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -20,11 +20,14 @@ namespace cw // 'dirPrefixStr' and the file name. // 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* makeFn( const char* dir, const char* fn, const char* ext, ... ); + char* expandPath( const char* dir ); + + // Parse a path into its parts: // // Return record used by pathParts()