From 781fb842420ffda1bb639918ec771dc93c187a21 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Wed, 14 Aug 2002 22:43:30 +0000 Subject: changed to std string in expandFilename --- src/StringUtil.cc | 23 +++++++++++++---------- src/StringUtil.hh | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/StringUtil.cc b/src/StringUtil.cc index 9c32e6b..1a9e7f1 100644 --- a/src/StringUtil.cc +++ b/src/StringUtil.cc @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: StringUtil.cc,v 1.10 2002/05/17 16:31:34 fluxgen Exp $ +// $Id: StringUtil.cc,v 1.11 2002/08/14 22:43:30 fluxgen Exp $ #include "StringUtil.hh" @@ -66,18 +66,21 @@ const char *strcasestr(const char *str, const char *ptn) { //------------- expandFilename ---------------------- // if ~ then expand it to home of user // returns expanded filename -// (note: the function creates new memory for the string) //--------------------------------------------------- -char *expandFilename(const char *filename) { +string expandFilename(const std::string &filename) { - auto_ptr retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]); - if (filename[0]=='~') { - strcpy(retval.get(), getenv("HOME")); - strcat(retval.get(), &filename[1]); - } else - return StringUtil::strdup(filename); //return unmodified value + string retval; + size_t pos = filename.find_first_not_of(" \t"); + if (pos != std::string::npos && filename[pos] == '~') { + retval = getenv("HOME"); + if (pos != filename.size()) { + // copy from the character after '~' + retval += static_cast(filename.c_str() + pos + 1); + } + } else + return filename; //return unmodified value - return StringUtil::strdup(retval.get()); //return modified value + return retval; } //------------- getStringBetween ----------- diff --git a/src/StringUtil.hh b/src/StringUtil.hh index cd3e0c0..f2f4944 100644 --- a/src/StringUtil.hh +++ b/src/StringUtil.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: StringUtil.hh,v 1.8 2002/04/12 15:06:07 fluxgen Exp $ +//$Id: StringUtil.hh,v 1.9 2002/08/14 22:43:30 fluxgen Exp $ #ifndef STRINGUTIL_HH #define STRINGUTIL_HH @@ -34,7 +34,7 @@ char *strdup(const char *); //Similar to `strstr' but this function ignores the case of both strings const char *strcasestr(const char *str, const char *ptn); -char *expandFilename(const char *filename); +std::string expandFilename(const std::string &filename); int getStringBetween(std::string& out, const char *instr, const char first, const char last, const char *ok_chars=" \t\n"); -- cgit v0.11.2