-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.cpp
141 lines (116 loc) · 3.39 KB
/
general.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
#include "general.h"
/* ------------------------------------------------------------------------------------------*/
void nextParam(char sep, istream& I) {
char c;
while(I.get(c) && c!= sep); // eat all chars til separtor character is found
}
/* ------------------------------------------------------------------------------------------*/
double getDouble(char sep, istream& I) {
nextParam(sep, I);
double d;
I>>d; // get the number
return d;
}
/* ------------------------------------------------------------------------------------------*/
unsigned long getLongInt(char sep, istream& I) {
nextParam(sep, I);
unsigned long i;
I>>i; // get the number
return i;
}
/* ------------------------------------------------------------------------------------------*/
int getInt(char sep, istream& I) {
nextParam(sep, I);
int i;
I>>i; // get the number
return i;
}
/* ------------------------------------------------------------------------------------------*/
void getStr(char sep, istream& I, char* s) {
nextParam(sep, I);
I>>s;
}
/* ------------------------------------------------------------------------------------------*/
char getChar(char sep, istream& I) {
nextParam(sep, I);
char c;
I>>c;
return c;
}
/* ------------------------------------------------------------------------------------------*/
bool getYesNo(char sep, istream& I) {
char c = getChar(sep, I);
bool result;
switch(c) {
case 'y': case 'Y':
result = true;
break;
case 'n': case 'N':
result = false;
break;
default:
cerr << "ERROR: getYesNo(...) neither y nor n encountered" << endl;
cerr << "Read: " << c << " instead" << endl;
exit(-1);
break;
}
return result;
}
/* ------------------------------------------------------------------------------------------*/
int string2Int(char* s) {
int len = strlen(s);
int val_start = 0; // where the (unsigned) value starts
if(*s == '-') { // negative value
val_start = 1;
}
int pow = 1, result = 0;
for(int i=len-1; i>=val_start; i--) {
result += (int)(s[i]-48) * pow;
pow*=10;
}
if(val_start == 1) { // a negative value
result = -result;
}
return result;
}
/* ------------------------------------------------------------------------------------------*/
bool isIntString(char* s) {
if(*s == '\0') {
return false; // an empty string
}
if(*s == '-') { // first char may be a minus sign
++s;
if(*s == '\0') { // check its not a solitary '-'
return false;
}
}
while(*s != '\0') { // do until we hit termination char
if(*s < 48 || *s >57) {
return false;
}
++s;
}
return true;
}
/* ------------------------------------------------------------------------------------------*/
bool fileExists(char* F) {
bool exists = true;
ifstream In;
In.open(F);
if(!In)
exists = false;
In.close();
return exists;
}
/* ------------------------------------------------------------------------------------------*/
void fatal(bool condition, char* location, char* message) {
if(condition) {
cerr << endl;
cerr << "Exiting due to Fatal Error" << endl
<< "Location: " << location << endl
<< "Message: " << message << endl;
exit(-1);
}
}
/* ------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------*/