-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cc
194 lines (155 loc) · 3.61 KB
/
helper.cc
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2015-2016 SUSE Linux GmbH
*
* Licensed under the GNU General Public License Version 2
* as published by the Free Software Foundation.
*
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* for details.
*
* Author: Joerg Roedel <[email protected]>
*/
#include <iostream>
#include <string>
#include <vector>
#include "helper.h"
static size_t end_of_string(const std::string &line, size_t start)
{
char c = line[start];
size_t len = line.size();
for (start += 1; start < len; start++) {
if (line[start] == '\\')
start += 1;
else if (line[start] == c)
break;
}
start += 1;
if (start >= len)
start = std::string::npos;
return start;
}
static size_t end_of_comment(const std::string &line, size_t start)
{
size_t len = line.size();
for (start += 1; start < len; start++) {
if (start + 1 < len &&
line[start] == '*' && line[start + 1] == '/') {
start += 2;
break;
}
}
if (start >= len)
start = std::string::npos;
return start;
}
std::string trim(const std::string &line)
{
static const char *spaces = " \n\t\r";
size_t pos1, pos2;
pos1 = line.find_first_not_of(spaces);
pos2 = line.find_last_not_of(spaces);
if (pos1 == std::string::npos)
return std::string("");
return line.substr(pos1, pos2-pos1+1);
}
std::string strip_comment(std::string line)
{
size_t pos = 0;
while (true) {
pos = line.find_first_of("#\"'/", pos);
if (pos == std::string::npos)
return line;
if (line[pos] == '#') {
return line.substr(0,pos);
} else if (pos + 1 != std::string::npos &&
line[pos + 1] == '*') {
/* C-Style Comment */
std::string l1, l2;
l1 = line.substr(0, pos);
pos = end_of_comment(line, pos + 1);
if (pos != std::string::npos) {
l2 = line.substr(pos);
pos = l1.size();
}
line = l1 + l2;
} else {
pos = end_of_string(line, pos);
}
}
return line;
}
std::vector<std::string> split_trim(const char *delim, std::string line, unsigned splits)
{
std::string item, delimiters;
std::vector<std::string> items;
unsigned num;
line = trim(line);
delimiters = delim;
delimiters += "\"'";
num = 0;
for (size_t pos = 0; pos != std::string::npos;) {
pos = line.find_first_of(delimiters.c_str(), pos);
if (pos == std::string::npos) {
item = line;
} else if (line[pos] == '"' || line[pos] == '\'') {
pos = end_of_string(line, pos);
if (pos == std::string::npos)
item = line;
else
continue;
} else {
item = line.substr(0, pos);
line = line.substr(pos + 1);
num += 1;
pos = 0;
}
items.push_back(trim(item));
if (splits && num == splits) {
items.push_back(trim(line));
break;
}
}
return items;
}
bool generated_symbol(std::string symbol)
{
/*
* We consider symbols generated by the compiler
* when they contain a '.'
*/
return (symbol.find_first_of(".") != std::string::npos);
}
std::string expand_tab(std::string input)
{
size_t index = 0;
while (true) {
index = input.find('\t', index);
if (index == std::string::npos)
break;
size_t delta = (4 - (index % 4));
const char *d_str;
switch (delta) {
case 0: d_str = ""; break;
case 1: d_str = " "; break;
case 2: d_str = " "; break;
case 3: d_str = " "; break;
case 4: d_str = " "; break;
}
input.replace(index, 1, d_str);
}
return input;
}
std::string base_name(std::string fname)
{
auto pos = fname.find_last_of("/");
if (pos != std::string::npos)
fname = fname.substr(pos + 1);
return fname;
}
std::string base_fn_name(std::string fn_name)
{
auto pos = fn_name.find_first_of(".");
if (pos != std::string::npos)
fn_name = fn_name.substr(0, pos);
return fn_name;
}