-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathutils.cpp
208 lines (172 loc) · 5.51 KB
/
utils.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright (c) 2003-2018, Adrian Rossiter
Antiprism - http://www.antiprism.com
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/* \file utils.cpp
\brief utility routines for maths operations, text operations,
I/O conversions, etc
*/
#include "utils.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
using std::string;
using std::vector;
/// Whitespace characters
const char WHITESPACE[] = " \t\r\n\f\v";
Status read_double(const char *str, double *f)
{
bool to_sqrt;
char buff;
if (sscanf(str, " sqrt%lf %c", f, &buff) == 1)
to_sqrt = true;
else if (sscanf(str, " %lf %c", f, &buff) == 1)
to_sqrt = false;
else
return Status::error("not a number");
if (isinf(*f))
return Status::error("number too large\n");
if (isnan(*f))
return Status::error("not a number\n");
if (to_sqrt)
*f = sqrt(*f);
return Status::ok();
}
Status read_int(const char *str, int *i)
{
char buff;
if (sscanf(str, " %d %c", i, &buff) != 1)
return Status::error("not an integer");
if (*i == INT_MAX)
return Status::error("integer too large\n");
return Status::ok();
}
Status read_int_list(vector<char *> &vals, vector<int> &nums, bool is_index)
{
nums.clear();
int num;
for (size_t i = 0; i < vals.size(); i++) {
if (!read_int(vals[i], &num))
return Status::error(msg_str("'%s' is not an integer", vals[i]));
if (is_index && num < 0)
return Status::error(msg_str("'%s' is not a positive integer", vals[i]));
nums.push_back(num);
}
return Status::ok();
}
Status read_int_list(char *str, vector<int> &nums, bool is_index, int len,
const char *sep)
{
nums.clear();
int vec_idx;
char *v_str = strtok(str, sep);
int i = 0;
while (v_str) {
i++;
if (!read_int(v_str, &vec_idx))
return Status::error(msg_str("'%s' is not an integer", v_str));
if (is_index && vec_idx < 0)
return Status::error(msg_str("'%s' is not a positive integer", v_str));
if (len && i > len)
return Status::error(msg_str("more than %d integers given", len));
nums.push_back(vec_idx);
v_str = strtok(0, sep);
}
return Status::ok();
}
Status read_double_list(vector<char *> &vals, vector<double> &nums)
{
nums.clear();
double num;
for (size_t i = 0; i < vals.size(); i++) {
Status stat = read_double(vals[i], &num);
if (stat.is_error())
return Status::error(msg_str("%s: '%s'", stat.c_msg(), vals[i]));
nums.push_back(num);
}
return Status::ok();
}
Status read_double_list(char *str, vector<double> &nums, int len,
const char *sep)
{
nums.clear();
double num;
char *num_str = strtok(str, sep);
int i = 0;
while (num_str) {
i++;
Status stat = read_double(num_str, &num);
if (stat.is_error())
return Status::error(msg_str("%s: '%s'", stat.c_msg(), num_str));
if (len && i > len)
return Status::error(msg_str("more than %d numbers given", len));
nums.push_back(num);
num_str = strtok(0, sep);
}
return Status::ok();
}
int split_line(char *line, vector<char *> &parts, const char *delims,
bool strict)
{
parts.clear();
if (!delims)
delims = WHITESPACE;
if (strict) {
char *cur = line;
parts.push_back(cur); // always an entry, even if null
while (*(cur += strcspn(cur, delims))) { // quit at end of string
*cur = '\0'; // terminate part
cur++; // start of next part
parts.push_back(cur); // add even if final null
}
/*while(*cur) { // quit at end of string
cur += strcspn(cur, delims); // next delimiter
if(*cur) { // part ended with delimiter
*cur = '\0'; // terminate part
cur++; // start of next part
parts.push_back(cur); // add even if final null
}
}*/
}
else {
char *val;
if (!(val = strtok(line, delims)))
return 0;
parts.push_back(val);
while ((val = strtok(0, delims)))
parts.push_back(val);
}
return parts.size();
}
string msg_str(const char *fmt, ...)
{
const int MSG_SZ = 256;
char message[MSG_SZ];
va_list args;
va_start(args, fmt);
vsnprintf(message, MSG_SZ - 1, fmt, args);
return message;
}