forked from University/epr24pr5-ojanssen2
68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
#include "Util.h"
|
|
#include "FormatError.h"
|
|
#include <cstdio>
|
|
#include <exception>
|
|
#include <string>
|
|
|
|
using err::FormatError;
|
|
|
|
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)
|
|
throw FormatError("Could not read char!");
|
|
if (c == '%') {
|
|
read = !read;
|
|
break;
|
|
}
|
|
if (!read)
|
|
continue;
|
|
s += c;
|
|
}
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
vector<int> Util::read_numbers(istream &is) {
|
|
int i;
|
|
string until_eol;
|
|
getline(is, until_eol);
|
|
|
|
vector<string> numbers_as_string = Util::split(until_eol);
|
|
vector<int> nums;
|
|
|
|
try {
|
|
for(string s : numbers_as_string)
|
|
nums.push_back(stoi(s));
|
|
} catch (exception& _) {
|
|
throw FormatError("Could not convert string to int");
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|