1 #include <cstring>
2 #include <cerrno>
3 #include <fstream>
4 #include <iomanip>
5 #include <sstream>
6 #include <zlib.h>
7
8 #include "exception.h"
9 #include "helpers.h"
10
11 std::string
get_string_from_int(int value)12 Helpers::get_string_from_int (int value)
13 {
14 std::stringstream ss;
15 ss << value;
16 return ss.str();
17 }
18
19 /* ---------------------------------------------------------------- */
20
21 std::string
get_string_from_uint(unsigned int value)22 Helpers::get_string_from_uint (unsigned int value)
23 {
24 std::stringstream ss;
25 ss << value;
26 return ss.str();
27 }
28
29 /* ---------------------------------------------------------------- */
30
31 std::string
get_string_from_sizet(std::size_t value)32 Helpers::get_string_from_sizet (std::size_t value)
33 {
34 std::stringstream ss;
35 ss << value;
36 return ss.str();
37 }
38
39 /* ---------------------------------------------------------------- */
40
41 std::string
get_string_from_float(float value,int digits)42 Helpers::get_string_from_float (float value, int digits)
43 {
44 std::stringstream ss;
45 ss << std::fixed << std::setprecision(digits) << value;
46 return ss.str();
47
48 }
49
50 /* ---------------------------------------------------------------- */
51
52 std::string
get_string_from_double(double value,int digits)53 Helpers::get_string_from_double (double value, int digits)
54 {
55 std::stringstream ss;
56 ss << std::fixed << std::setprecision(digits) << value;
57 return ss.str();
58
59 }
60
61 /* ---------------------------------------------------------------- */
62
63 int
get_int_from_string(std::string const & value)64 Helpers::get_int_from_string (std::string const& value)
65 {
66 std::stringstream ss(value);
67 int ret;
68 ss >> ret;
69 return ret;
70 }
71
72 /* ---------------------------------------------------------------- */
73
74 unsigned int
get_uint_from_string(std::string const & value)75 Helpers::get_uint_from_string (std::string const& value)
76 {
77 std::stringstream ss(value);
78 unsigned int ret;
79 ss >> ret;
80 return ret;
81 }
82
83 /* ---------------------------------------------------------------- */
84
85 double
get_double_from_string(std::string const & value)86 Helpers::get_double_from_string (std::string const& value)
87 {
88 std::stringstream ss(value);
89 double ret;
90 ss >> ret;
91 return ret;
92 }
93
94 /* ---------------------------------------------------------------- */
95
96 float
get_float_from_string(std::string const & value)97 Helpers::get_float_from_string (std::string const& value)
98 {
99 std::stringstream ss(value);
100 float ret;
101 ss >> ret;
102 return ret;
103 }
104
105 /* ---------------------------------------------------------------- */
106
107 std::string
get_roman_from_int(int value)108 Helpers::get_roman_from_int (int value)
109 {
110 switch (value)
111 {
112 case 0:
113 return "";
114 case 1:
115 return "I";
116 case 2:
117 return "II";
118 case 3:
119 return "III";
120 case 4:
121 return "IV";
122 case 5:
123 return "V";
124 default:
125 return "?";
126 }
127 }
128
129 /* ---------------------------------------------------------------- */
130
131 std::string
get_dotted_str_from_int(int value)132 Helpers::get_dotted_str_from_int (int value)
133 {
134 std::stringstream ss;
135 ss << value;
136 return Helpers::get_dotted_str_from_str(ss.str());
137 }
138
139 /* ---------------------------------------------------------------- */
140
141 std::string
get_dotted_str_from_uint(unsigned int value)142 Helpers::get_dotted_str_from_uint (unsigned int value)
143 {
144 std::stringstream ss;
145 ss << value;
146 return Helpers::get_dotted_str_from_str(ss.str());
147 }
148
149 /* ---------------------------------------------------------------- */
150
151 std::string
get_dotted_str_from_str(std::string const & str)152 Helpers::get_dotted_str_from_str (std::string const& str)
153 {
154 std::string ret;
155
156 int cnt = 0;
157 for (int i = (int)str.size() - 1; i >= 0; --i)
158 {
159 if (cnt % 3 == 0 && cnt > 0)
160 ret.insert(ret.begin(), 1, ',');
161 ret.insert(ret.begin(), 1, str[i]);
162 cnt += 1;
163 }
164 return ret;
165 }
166
167 /* ---------------------------------------------------------------- */
168
169 std::string
get_dotted_isk(std::string const & isk_string)170 Helpers::get_dotted_isk (std::string const& isk_string)
171 {
172 size_t pos = isk_string.find_first_of('.');
173 if (pos == std::string::npos)
174 return isk_string;
175
176 std::string tmp = isk_string.substr(0, pos);
177 tmp = Helpers::get_dotted_str_from_str(tmp);
178 tmp += isk_string.substr(pos);
179
180 return tmp;
181 }
182
183 /* ---------------------------------------------------------------- */
184
185 std::string
trunc_string(std::string const & str,int len)186 Helpers::trunc_string (std::string const& str, int len)
187 {
188 if ((int)str.size() > len + 3)
189 return str.substr(0, len - 3).append("...");
190
191 return str;
192 }
193
194 /* ---------------------------------------------------------------- */
195
196 StringVector
split_string(std::string const & str,char delim)197 Helpers::split_string (std::string const& str, char delim)
198 {
199 StringVector parts;
200
201 unsigned int last = 0;
202 unsigned int cur = 0;
203 for (; cur < str.size(); ++cur)
204 if (str[cur] == delim)
205 {
206 parts.push_back(str.substr(last, cur - last));
207 last = cur + 1;
208 }
209
210 if (last < str.size())
211 parts.push_back(str.substr(last));
212
213 return parts;
214 }
215
216 /* ---------------------------------------------------------------- */
217
218 StringVector
tokenize_cmd(std::string const & str)219 Helpers::tokenize_cmd (std::string const& str)
220 {
221 std::vector<std::string> result;
222
223 /* Tokenize command. Delimiter is ' ', remove and handle '"' gracefully. */
224 bool in_quote = false;
225 std::string token;
226 for (unsigned int i = 0; i < str.size(); ++i)
227 {
228 char chr = str[i];
229
230 if (chr == ' ' && !in_quote)
231 {
232 result.push_back(token);
233 token.clear();
234 }
235 else if (chr == '"')
236 in_quote = !in_quote;
237 else
238 token += chr;
239 }
240 result.push_back(token);
241
242 return result;
243 }
244
245 /* ---------------------------------------------------------------- */
246
247 char**
create_argv(const std::vector<std::string> & cmd)248 Helpers::create_argv (const std::vector<std::string>& cmd)
249 {
250 char** args = new char*[cmd.size() + 1];
251 for (unsigned int i = 0; i < cmd.size(); ++i)
252 {
253 char* cmd_cstr = new char[cmd[i].size() + 1];
254 ::strcpy(cmd_cstr, cmd[i].c_str());
255 args[i] = cmd_cstr;
256 }
257 args[cmd.size()] = 0;
258 return args;
259 }
260
261 /* ---------------------------------------------------------------- */
262
263 void
delete_argv(char ** argv)264 Helpers::delete_argv (char** argv)
265 {
266 for (std::size_t i = 0; argv[i] != 0; ++i)
267 delete [] argv[i];
268 delete [] argv;
269 }
270
271 /* ---------------------------------------------------------------- */
272
273 void
read_file(std::string const & filename,std::string * data,bool auto_gunzip)274 Helpers::read_file (std::string const& filename, std::string* data,
275 bool auto_gunzip)
276 {
277 if (auto_gunzip == false)
278 {
279 /* Read standard binary file. */
280 std::ifstream in(filename.c_str(), std::ios::binary);
281 if (!in)
282 throw FileException(filename, ::strerror(errno));
283
284 in.seekg(0, std::ios::end);
285 data->resize(in.tellg());
286 in.seekg(0, std::ios::beg);
287 in.read(&data->at(0), data->size());
288 in.close();
289 }
290 else
291 {
292 /* Read file and transparently uncompress if gzipped. */
293 ::gzFile file = ::gzopen(filename.c_str(), "r");
294 if (file == NULL)
295 throw FileException(filename, ::strerror(errno));
296
297 while (true)
298 {
299 unsigned char buffer[1024];
300 int bytes_read = ::gzread(file, buffer, 1024);
301 if (bytes_read > 0)
302 data->append(buffer, buffer + bytes_read);
303 else
304 break;
305 }
306 ::gzclose(file);
307 }
308 }
309
310 /* ---------------------------------------------------------------- */
311
312 void
write_file(std::string const & filename,std::string const & data)313 Helpers::write_file (std::string const& filename, std::string const& data)
314 {
315 std::ofstream out(filename.c_str(), std::ios::binary);
316 if (!out)
317 throw FileException(filename, ::strerror(errno));
318 out.write(data.c_str(), data.size());
319 out.close();
320 }
321