2025-02-09 12:34:07 +01:00
|
|
|
#include "Util.h"
|
2025-02-09 19:58:55 +01:00
|
|
|
#include "Error.h"
|
2025-02-09 12:34:07 +01:00
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
|
|
|
|
2025-02-09 19:58:55 +01:00
|
|
|
using err::Error;
|
2025-02-09 12:34:07 +01:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
string Util::read_string_between_percent(istream& is) {
|
|
|
|
char c;
|
|
|
|
string s = "";
|
|
|
|
bool read = false;
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; ++i) {
|
|
|
|
while (true) {
|
|
|
|
is >> c;
|
|
|
|
if (!is)
|
2025-02-09 19:58:55 +01:00
|
|
|
throw Error(700, "Could not read char!");
|
2025-02-09 12:34:07 +01:00
|
|
|
if (c == '%') {
|
|
|
|
read = !read;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!read)
|
|
|
|
continue;
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int> Util::read_numbers(istream &is) {
|
|
|
|
string until_eol;
|
|
|
|
getline(is, until_eol);
|
|
|
|
|
2025-02-09 19:58:55 +01:00
|
|
|
vector<string> numbers_as_string = split(until_eol);
|
2025-02-09 12:34:07 +01:00
|
|
|
vector<int> nums;
|
|
|
|
|
|
|
|
try {
|
|
|
|
for(string s : numbers_as_string)
|
|
|
|
nums.push_back(stoi(s));
|
|
|
|
} catch (exception& _) {
|
2025-02-09 19:58:55 +01:00
|
|
|
throw Error(700, "Could not convert string to int.");
|
2025-02-09 12:34:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nums;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> Util::split(const string& s) {
|
|
|
|
vector<string> strs;
|
|
|
|
string result = "";
|
|
|
|
|
|
|
|
for(char c : s) {
|
|
|
|
if (c == ' ') {
|
|
|
|
if (result != "") {
|
|
|
|
strs.push_back(result);
|
|
|
|
result = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strs;
|
|
|
|
}
|
2025-02-09 19:58:55 +01:00
|
|
|
|
|
|
|
string Util::read_string(istream &s) {
|
|
|
|
string result;
|
|
|
|
s >> result;
|
|
|
|
if (!s)
|
|
|
|
throw Error(700, "Could not read stream from stream!");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // util
|