-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcabjiFunctions.cpp
241 lines (214 loc) · 5.51 KB
/
cabjiFunctions.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "cabjiFunctions.h"
bool fnIgnoreLine(const std::string &str)
{
// checks if a string is a comment or a blank line
// return false if its a comment or blank, true if anything else
return str.size() >= 2 && (str[0] == '/' && str[1] == '/') || str[0] == '#' || str == "";
}
std::set<int> fnParsePageSelection(const std::string &pageSelection, std::string *outString)
{
std::set<int> pages;
// Check if pageSelection contains only allowed characters: digits, commas, dashes, and spaces
std::regex validPattern("^[0-9,\\-\\s]*$");
if (!std::regex_match(pageSelection, validPattern))
{
if (outString != nullptr)
{
*outString += "Error: Invalid characters in page selection string.\n";
}
else
{
std::cout << "Error: Invalid characters in page selection string.\n";
}
return pages; // Return empty set if invalid characters are found
}
if (pageSelection.empty())
{
if (outString != nullptr)
{
*outString += "Error: Empty page selection string.\n";
}
else
{
std::cout << "Error: Empty page selection string.\n";
}
return pages; // Return empty set for empty input
}
// Split the input string by commas to get the elements
std::vector<std::string> elements = fnStrSplitToVector(pageSelection, ",");
for (const auto &element : elements)
{
std::string trimmedElement = fnTrim(element);
if (trimmedElement.find('-') != std::string::npos)
{
// Process range
std::vector<std::string> range = fnStrSplitToVector(trimmedElement, "-");
if (range.size() != 2)
{
if (outString != nullptr)
{
*outString += "Error: Invalid range in page selection: " + trimmedElement + "\n";
}
else
{
std::cout << "Error: Invalid range in page selection: " << trimmedElement << "\n";
}
return pages; // Return empty set if range is invalid
}
try
{
int start = std::stoi(range[0]);
int end = std::stoi(range[1]);
if (start > end)
std::swap(start, end); // Ensure start is less than or equal to end
for (int i = start; i <= end; ++i)
{
pages.insert(i);
}
}
catch (const std::invalid_argument &e)
{
if (outString != nullptr)
{
*outString += "Error: Invalid number format in range: " + std::string(e.what()) + "\n";
}
else
{
std::cout << "Error: Invalid number format in range: " << std::string(e.what()) << "\n";
}
return pages;
}
catch (const std::out_of_range &e)
{
if (outString != nullptr)
{
*outString += "Error: Number out of range: " + std::string(e.what()) + "\n";
}
else
{
std::cout << "Error: Number out of range: " << std::string(e.what()) << "\n";
}
return pages;
}
}
else
{
// Process single number
try
{
pages.insert(std::stoi(trimmedElement));
}
catch (const std::invalid_argument &e)
{
if (outString != nullptr)
{
*outString += "Error: Invalid number format: " + std::string(e.what()) + "\n";
}
else
{
std::cout << "Error: Invalid number format: " << std::string(e.what()) << "\n";
}
return pages;
}
catch (const std::out_of_range &e)
{
if (outString != nullptr)
{
*outString += "Error: Number out of range: " + std::string(e.what()) + "\n";
}
else
{
std::cout << "Error: Number out of range: " << std::string(e.what()) << "\n";
}
return pages;
}
}
}
return pages;
}
std::set<int> fnParsePageSelection(const std::string &pageSelection)
{
std::string fakeStr;
return fnParsePageSelection(pageSelection, &fakeStr);
}
void fnStrNormalizeNewLineChars(std::string &s)
{
size_t pos = 0;
// Replace all \r\n with \n
while ((pos = s.find("\r\n", pos)) != std::string::npos)
{
s.replace(pos, 2, "\n");
pos += 1; // Move past the '\n' we just inserted
}
// Replace all remaining \r with \n
pos = 0;
while ((pos = s.find("\r", pos)) != std::string::npos)
{
s.replace(pos, 1, "\n");
pos += 1; // Move past the '\n' we just inserted
}
}
// split a string on a delimiter and return it as a vector
std::vector<std::string> fnStrSplitToVector(const std::string &s, const std::string &delimiter)
{
std::vector<std::string> tokens;
std::string::size_type start = 0;
std::string::size_type end = s.find(delimiter);
while (end != std::string::npos)
{
tokens.push_back(s.substr(start, end - start));
start = end + delimiter.length();
end = s.find(delimiter, start);
}
tokens.push_back(s.substr(start));
return tokens;
}
std::string fnStrStripExcessiveWhitespace(const std::string &input)
{
std::stringstream result;
std::string::const_iterator it = input.begin();
bool inWhitespace = false;
// Skip leading whitespace
while (it != input.end() && std::isspace(*it))
{
++it;
}
while (it != input.end())
{
if (std::isspace(*it))
{
if (!inWhitespace)
{
result << ' ';
inWhitespace = true;
}
}
else
{
result << *it;
inWhitespace = false;
}
++it;
}
std::string output = result.str();
// Remove trailing whitespace
if (!output.empty() && std::isspace(output.back()))
{
output.pop_back();
}
return output;
}
// Function to trim whitespace from both ends of a string
std::string fnTrim(const std::string &str)
{
std::string result = str;
// Trim from the start
result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
// Trim from the end
result.erase(std::find_if(result.rbegin(), result.rend(), [](unsigned char ch)
{ return !std::isspace(ch); })
.base(),
result.end());
return result;
}