summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/putil.cpp
blob: 60eab248c9d4a2598d8e359c8ef2d3a1617aa056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// license:GPL-2.0+
// copyright-holders:Couriersud

#include "putil.h"
#include "penum.h"
#include "pstream.h"
#include "pstrutil.h"
#include "ptypes.h"

#include <algorithm>
#include <cstdlib> // needed for getenv ...
#include <initializer_list>

namespace plib
{
	namespace util
	{
		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)
		{
			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;
		}

		pstring path(const pstring &filename)
		{
			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);

			return filename.substr(0, p);
		}

		bool exists (const pstring &filename)
		{
			plib::ifstream f(filename);
			return f.good();
		}

		pstring buildpath(std::initializer_list<pstring> list )
		{
			pstring ret = "";
			for( const auto &elem : list )
			{
				if (ret.empty())
					ret = elem;
				else
					ret += (PATH_SEP + elem);
			}
			return ret;
		}

		pstring environment(const pstring &var, const pstring &default_val)
		{
			return (std::getenv(var.c_str()) == nullptr) ? default_val
				: pstring(std::getenv(var.c_str()));
		}
	} // namespace util

	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);
		}
		if (p < str.length())
		{
			pstring t = str.substr(p);
			if (!ignore_empty || t.length() != 0)
				ret.push_back(t);
		}
		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.empty())
		{
			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.empty())
					result.push_back(str);
			}
			else
			{
				result.push_back(str);
				str = "";
			}
		}
		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())
		{
			auto p = pstring::npos;
			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 != pstring::npos)
			{
				if (!col.empty())
					ret.push_back(col);

				col = "";
				ret.push_back(onstrl[p]);
				i = std::next(i, narrow_cast<pstring::difference_type>(onstrl[p].length()));
			}
			else
			{
				pstring::value_type c = *i;
				col += c;
				i++;
			}
		}
		if (!col.empty())
			ret.push_back(col);

		return ret;
	}


	int penum_base::from_string_int(const pstring &str, const pstring &x)
	{
		int cnt = 0;
		for (auto &s : psplit(str, ",", false))
		{
			if (trim(s) == x)
				return cnt;
			cnt++;
		}
		return -1;
	}

	pstring penum_base::nthstr(std::size_t n, const pstring &str)
	{
		return psplit(str, ",", false)[n];
	}
} // namespace plib