diff --git a/Makefile b/Makefile index 63ad5d1..1e23b7a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LIBNAME := $(shell basename $$(pwd)) MAJOR := 1 -MINOR := 0.0 +MINOR := 1.0 UNAME_S := $(shell uname -s) diff --git a/string_utils.cpp b/string_utils.cpp index 42f04b9..b8a25ee 100644 --- a/string_utils.cpp +++ b/string_utils.cpp @@ -17,38 +17,38 @@ using namespace std; vector sc::split(const string& str, const string& sep) { - vector components; - string::size_type start = 0; - string::size_type pos = str.find(sep); - while (pos != string::npos) { // found separator => add substring to vector - components.push_back(str.substr(start, pos - start)); - start = pos + sep.length(); // next search starts just after found separator - pos = str.find(sep, start); - } - // at end of string => add last component - components.push_back(str.substr(start, str.length() - start)); - return components; + vector components; + string::size_type start = 0; + string::size_type pos = str.find(sep); + while (pos != string::npos) { // found separator => add substring to vector + components.push_back(str.substr(start, pos - start)); + start = pos + sep.length(); // next search starts just after found separator + pos = str.find(sep, start); + } + // at end of string => add last component + components.push_back(str.substr(start, str.length() - start)); + return components; } vector sc::split(const string& str, const regex& sep) { - vector components; - sregex_token_iterator end {}; - for (sregex_token_iterator p {str.begin(), str.end(), sep, -1}; p != end; ++p) { - components.push_back(*p); - } - return components; + vector components; + sregex_token_iterator end {}; + for (sregex_token_iterator p {str.begin(), str.end(), sep, -1}; p != end; ++p) { + components.push_back(*p); + } + return components; } string sc::join(const vector& components, const string& join) { - string result; - for (vector::const_iterator i = components.cbegin(); i != components.cend(); ++i) { - if (i != components.cbegin()) { - result += join; - } - result += *i; - } - return result; + string result; + for (vector::const_iterator i = components.cbegin(); i != components.cend(); ++i) { + if (i != components.cbegin()) { + result += join; + } + result += *i; + } + return result; } string sc::trim(const string& str, const string& del) @@ -78,8 +78,8 @@ string sc::file_get_contents(const string& path) { ifstream file {path}; file.exceptions(ios::failbit | ios::badbit); - file.seekg(0, ios::end); - ios::pos_type file_len {file.tellg()}; + file.seekg(0, ios::end); + ios::pos_type file_len {file.tellg()}; file.seekg(0); vector buf; buf.resize(file_len); @@ -92,7 +92,7 @@ map sc::parse_ini_file(const string& path) map result; string line; ifstream file {path}; - file.exceptions(/*ios::failbit |*/ ios::badbit); // it seems that getline() will set failbit when confronted with eof immediately + file.exceptions(/*ios::failbit |*/ ios::badbit); // it seems that getline() will set failbit when confronted with eof immediately while (getline(file, line)) { if (line[0] == '[') continue; vector parts {split(line, "=")}; @@ -114,27 +114,27 @@ string sc::str_replace(const string& what, const string& replacement, const stri result += target.substr(from, pos - from); result += replacement; from = pos + what.length(); - if (from >= target.length()) break; + if (from >= target.length()) break; } result += target.substr(from); return result; } wstring sc::replace_all(const std::wstring& what, - const std::wstring& replacement, - const std::wstring& target) + const std::wstring& replacement, + const std::wstring& target) { - wstring term {target}; - wstring::size_type pos = wstring::npos; - wstring::size_type from = 0; - do { - pos = term.find(what, from); - if (pos != wstring::npos) { - term.replace(pos, what.length(), replacement); - from = pos + replacement.length(); - } - } while (pos != wstring::npos); - return term; + wstring term {target}; + wstring::size_type pos = wstring::npos; + wstring::size_type from = 0; + do { + pos = term.find(what, from); + if (pos != wstring::npos) { + term.replace(pos, what.length(), replacement); + from = pos + replacement.length(); + } + } while (pos != wstring::npos); + return term; } void sc::create_dir(const std::string &path, int mode) @@ -165,12 +165,12 @@ string sc::dirname(const string& path) { string sc::basename(const string& path, bool remove_extension) { string result {path}; - if (remove_extension) { - auto dot = path.rfind("."); - if (dot != string::npos) { - result = result.substr(0, dot); - } - } + if (remove_extension) { + auto dot = path.rfind("."); + if (dot != string::npos) { + result = result.substr(0, dot); + } + } auto pos = result.rfind("/"); if (pos != string::npos) { return result.substr(pos + 1); @@ -189,20 +189,16 @@ string sc::replace_tilde(const string& path) { } string sc::filename_extension(const string& path) { - auto pos = path.rfind("."); - if (pos != string::npos) { - return path.substr(pos); - } - return ""; + auto pos = path.rfind("."); + if (pos != string::npos) { + return path.substr(pos); + } + return ""; } string sc::tool_path(const string& name) { if (name.find('/') != string::npos) { // name has (at least one) slash - vector buf; - buf.resize(PATH_MAX); - if (::realpath(name.c_str(), buf.data())) { - return string(buf.data()); - } + return sc::real_path(name); } else { // no slash in name => command from PATH? string envpath {::getenv("PATH")}; vector paths {sc::split(envpath, ":")}; @@ -238,7 +234,7 @@ string sc::uppercase(const string& str, const locale& loc) { } bool sc::is_valid_utf8(const string& str) { - // From: http://www.zedwood.com/article/cpp-is-valid-utf8-string-function + // From: http://www.zedwood.com/article/cpp-is-valid-utf8-string-function int c,i,ix,n,j; for (i=0, ix=str.length(); i < ix; i++) { @@ -259,3 +255,12 @@ bool sc::is_valid_utf8(const string& str) { } return true; } + +string sc::real_path(const string& path) { + vector buf; + buf.resize(PATH_MAX); + if (::realpath(path.c_str(), buf.data())) { + return string(buf.data()); + } + return ""; +} diff --git a/string_utils.hpp b/string_utils.hpp index 05e57fb..da24fbd 100644 --- a/string_utils.hpp +++ b/string_utils.hpp @@ -19,27 +19,27 @@ namespace sc { std::vector split (const std::string& str, const std::string& sep); - std::vector split (const std::string& str, - const std::regex& sep); - + std::vector split (const std::string& str, + const std::regex& sep); + std::string join (const std::vector& components, const std::string& join); std::string trim (const std::string& str, const std::string& del = " "); - - std::string str_replace (const std::string& what, + + std::string str_replace (const std::string& what, const std::string& replacement, const std::string& target); - std::wstring replace_all (const std::wstring& what, - const std::wstring& replacement, - const std::wstring& target); + std::wstring replace_all (const std::wstring& what, + const std::wstring& replacement, + const std::wstring& target); + + bool file_exists (const std::string& path); - bool file_exists (const std::string& path); - std::string file_get_contents (const std::string& path); - + std::map parse_ini_file (const std::string& path); void create_dir (const std::string& path, @@ -50,8 +50,8 @@ namespace sc { std::string basename (const std::string& path, bool remove_extension = false); std::string replace_tilde (const std::string& path); - - std::string filename_extension (const std::string& path); + + std::string filename_extension (const std::string& path); std::string tool_path (const std::string& name); @@ -61,8 +61,9 @@ namespace sc { std::string uppercase (const std::string& str, const std::locale& loc); - bool is_valid_utf8 (const std::string& str); + bool is_valid_utf8 (const std::string& str); + std::string real_path (const std::string& path); } #endif // _string_utils_