forked from JKomskis/GameOfLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cpp
64 lines (56 loc) · 1.19 KB
/
Util.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
#include "Util.h"
using namespace std;
/* reads in a line from input, converts it to integer and returns the result
*/
int fs_atoi(ifstream &input)
{
string l;
getline(input, l);
if(input.eof())
throw "EOF Reached";
return atoi(l.c_str());
}
/* test that string s ends with ending parameter
*/
bool endsWith(string s, string ending)
{
return (s.rfind(ending) + ending.length()) == s.length();
}
/* test that string s starts with ending parameter
*/
bool startsWith(string s, string starting)
{
return (s.find(starting) + starting.length()) == 0;
}
/* helper to return path/directory seperator character
* depending on the build environment
*/
string separator()
{
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
return "\\";
#else
return "/";
#endif
}
/* converts a string, eg: "123"
* to an integer set eg {1, 2, 3}
*/
set<int> rule2set(string input)
{
set<int> ret;
for(auto character : input)
if(isdigit(character))
ret.insert(atoi((const char*)&character));
return ret;
}
/* converts an integer set eg {1, 2, 3}
* to an string, eg: "123"
*/
string set2rule(set<int> input)
{
string ret = "";
for(auto value : input)
ret += '0' + char(value);
return ret;
}