-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.cpp
136 lines (116 loc) · 3.6 KB
/
utilities.cpp
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
#include "utilities.h"
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
namespace pip2cmake
{
namespace utilities
{
const std::string WHITESPACE = " \n\r\t\f\v";
std::string ltrim(const std::string& s)
{
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string& s)
{
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string& s)
{
return rtrim(ltrim(s));
}
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
{
return false;
}
str.replace(start_pos, from.length(), to);
return true;
}
std::vector<std::string> getValueList(std::string value)
{
if(value.empty())
{
return std::vector<std::string>();
}
value = utilities::trim(value);
std::replace( value.begin(), value.end(), ',', ' ');
while(replace(value, " ", " "));
auto list = utilities::split(value,' ');
#if defined(DEBUG)
std::cout << "KeyValue List: [" << value << "]" << std::endl;
for(auto &item : list)
{
std::cout << "Value: [" << item << "]" << std::endl;
item = utilities::trim(item);
}
#endif
return list;
}
std::string normalize_path(std::string &path)
{
std::string sep = (path.find("\\") != std::string::npos) ? "\\" : "/";
std::stack<std::string> st;
std::string dir;
std::string res = sep;
size_t len_path = path.length();
for (size_t i = 0; i < len_path; i++)
{
dir.clear();
while (path[i] == sep[0])
i++;
while (i < len_path && path[i] != sep[0]) {
dir.push_back(path[i]);
i++;
}
if (dir.compare("..") == 0)
{
if (!st.empty())
st.pop();
}
else if (dir.compare(".") == 0)
continue;
else if (dir.length() != 0)
st.push(dir);
}
std::stack<std::string> st1;
while (!st.empty())
{
st1.push(st.top());
st.pop();
}
while (!st1.empty())
{
std::string temp = st1.top();
if (st1.size() != 1)
{
res.append(temp + sep);
}
else
{
res.append(temp);
}
st1.pop();
}
return res;
}
}
}