summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/putil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/netlist/plib/putil.cpp')
-rw-r--r--src/lib/netlist/plib/putil.cpp153
1 files changed, 42 insertions, 111 deletions
diff --git a/src/lib/netlist/plib/putil.cpp b/src/lib/netlist/plib/putil.cpp
index a9531006904..fb430f89a68 100644
--- a/src/lib/netlist/plib/putil.cpp
+++ b/src/lib/netlist/plib/putil.cpp
@@ -1,153 +1,84 @@
-// license:GPL-2.0+
+// license:BSD-3-Clause
// copyright-holders:Couriersud
#include "putil.h"
-#include "plists.h"
+#include "penum.h"
+#include "pstream.h"
+#include "pstrutil.h"
#include "ptypes.h"
#include <algorithm>
-//#include <cstdlib>
-//#include <cstring>
+// needed for getenv ...
#include <initializer_list>
namespace plib
{
namespace util
{
- const pstring buildpath(std::initializer_list<pstring> list )
+ static constexpr const char PATH_SEP = compile_info::win32::value ? '\\' : '/';
+ static constexpr const char *PATH_SEPS = compile_info::win32::value ? "\\/" :"/";
+
+ pstring basename(const pstring &filename, const pstring &suffix)
{
- pstring ret = "";
- for( const auto &elem : list )
- {
- if (ret == "")
- ret = elem;
- else
- #ifdef _WIN32
- ret = ret + '\\' + elem;
- #else
- ret += ('/' + elem);
- #endif
- }
+ auto p=find_last_of(filename, pstring(PATH_SEPS));
+ pstring ret = (p == pstring::npos) ? filename : filename.substr(p+1);
+ if (!suffix.empty() && endsWith(ret, suffix))
+ return ret.substr(0, ret.length() - suffix.length());
return ret;
}
- const pstring environment(const pstring &var, const pstring &default_val)
+ pstring path(const pstring &filename)
{
- if (std::getenv(var.c_str()) == nullptr)
- return default_val;
- else
- return pstring(std::getenv(var.c_str()));
- }
- } // namespace util
+ auto p=find_last_of(filename, pstring(1, PATH_SEP));
+ if (p == pstring::npos)
+ return "";
+ if (p == 0) // root case
+ return filename.substr(0, 1);
- std::vector<pstring> psplit(const pstring &str, const pstring &onstr, bool ignore_empty)
- {
- std::vector<pstring> ret;
-
- pstring::size_type p = 0;
- pstring::size_type pn = str.find(onstr, p);
-
- while (pn != pstring::npos)
- {
- pstring t = str.substr(p, pn - p);
- if (!ignore_empty || t.length() != 0)
- ret.push_back(t);
- p = pn + onstr.length();
- pn = str.find(onstr, p);
+ return filename.substr(0, p);
}
- if (p < str.length())
+
+ bool exists (const pstring &filename)
{
- pstring t = str.substr(p);
- if (!ignore_empty || t.length() != 0)
- ret.push_back(t);
+ plib::ifstream f(filename);
+ return f.good();
}
- return ret;
- }
- std::vector<std::string> psplit_r(const std::string &stri,
- const std::string &token,
- const std::size_t maxsplit)
- {
- std::string str(stri);
- std::vector<std::string> result;
- std::size_t splits = 0;
-
- while(str.size())
+ pstring build_path(std::initializer_list<pstring> list )
{
- std::size_t index = str.rfind(token);
- bool found = index!=std::string::npos;
- if (found)
- splits++;
- if ((splits <= maxsplit || maxsplit == 0) && found)
- {
- result.push_back(str.substr(index+token.size()));
- str = str.substr(0, index);
- if (str.size()==0)
- result.push_back(str);
- }
- else
+ pstring ret = "";
+ for( const auto &elem : list )
{
- result.push_back(str);
- str = "";
+ if (ret.empty())
+ ret = elem;
+ else
+ ret += (PATH_SEP + elem);
}
+ return ret;
}
- return result;
- }
-
- std::vector<pstring> psplit(const pstring &str, const std::vector<pstring> &onstrl)
- {
- pstring col = "";
- std::vector<pstring> ret;
- auto i = str.begin();
- while (i != str.end())
+ pstring environment(const pstring &var, const pstring &default_val)
{
- auto p = static_cast<std::size_t>(-1);
- for (std::size_t j=0; j < onstrl.size(); j++)
- {
- if (std::equal(onstrl[j].begin(), onstrl[j].end(), i))
- {
- p = j;
- break;
- }
- }
- if (p != static_cast<std::size_t>(-1))
- {
- if (col != "")
- ret.push_back(col);
-
- col = "";
- ret.push_back(onstrl[p]);
- i = std::next(i, static_cast<pstring::difference_type>(onstrl[p].length()));
- }
- else
- {
- pstring::value_type c = *i;
- col += c;
- i++;
- }
+ putf8string varu8(var);
+ return (std::getenv(varu8.c_str()) == nullptr) ? default_val
+ : pstring(std::getenv(varu8.c_str()));
}
- if (col != "")
- ret.push_back(col);
-
- return ret;
- }
-
+ } // namespace util
- int penum_base::from_string_int(const char *str, const char *x)
+ int penum_base::from_string_int(const pstring &str, const pstring &x)
{
int cnt = 0;
- for (auto &s : psplit(str, ",", false))
+ for (auto &s : psplit(str, ',', false))
{
- if (s == x)
+ if (trim(s) == x)
return cnt;
cnt++;
}
return -1;
}
- std::string penum_base::nthstr(int n, const char *str)
+ pstring penum_base::nthstr(std::size_t n, const pstring &str)
{
- return psplit(str, ",", false)[static_cast<std::size_t>(n)];
+ return psplit(str, ',', false)[n];
}
} // namespace plib