-
Notifications
You must be signed in to change notification settings - Fork 24
/
ldpl_lib.cpp
227 lines (200 loc) · 5.53 KB
/
ldpl_lib.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
#include <sstream>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <time.h>
#define NVM_FLOAT_EPSILON 0.00000001
#define ldpl_number double
#define CRLF \"\\n\"
using namespace std;
//Global variables
ifstream file_loading_stream;
ofstream file_writing_stream;
string file_loading_line;
string joinvar;
template<typename T>
struct ldpl_vector {
unordered_map<string, T> inner_map;
T& operator [] (const string& i) {
return inner_map[i];
}
T& operator [] (ldpl_number i) {
return inner_map[to_string(i)];
}
};
ldpl_number input_number(){
string s = \"\";
while(true){
getline(cin, s);
try {
ldpl_number num = stod(s);
return num;
}
catch (const std::invalid_argument& ia) {
cout << \"Redo from start: \" << flush;
}
}
}
ldpl_number to_number(const string & a){
try {
ldpl_number num = stod(a);
return num;
}
catch (const std::invalid_argument& ia) {
return 0;
}
}
string input_string(){
string s = \"\";
getline(cin, s);
return s;
}
string input_until_eof(){
stringstream full;
string s = \"\";
bool firstLine = true;
while (getline(cin, s)) {
if(!firstLine) s = \"\\n\" + s;
firstLine = false;
full << s;
}
return full.str();
}
bool num_equal(ldpl_number a, ldpl_number b){
return fabs(a - b) < NVM_FLOAT_EPSILON;
}
ldpl_number modulo(ldpl_number a, ldpl_number b){
return (int) floor(a) % (int) floor(b);
}
void join(const string & a, const string & b, string & c){
c = a + b;
}
//http://www.zedwood.com/article/cpp-utf8-strlen-function
int utf8_strlen(const string& str)
{
int c,i,ix,q;
for (q=0, i=0, ix=str.length(); i < ix; i++, q++)
{
c = (unsigned char) str[i];
if (c>=0 && c<=127) i+=0;
else if ((c & 0xE0) == 0xC0) i+=1;
else if ((c & 0xF0) == 0xE0) i+=2;
else if ((c & 0xF8) == 0xF0) i+=3;
else return 0;
}
return q;
}
//http://www.zedwood.com/article/cpp-utf-8-mb_substr-function
string utf8_substr(const string &str,int start, int length=INT_MAX)
{
int i,ix,j,realstart,reallength;
if (length==0) return \"\";
if (start<0 || length <0)
{
//find j=utf8_strlen(str);
for(j=0,i=0,ix=str.length(); i<ix; i+=1, j++)
{
unsigned char c= str[i];
if (c>=0 && c<=127) i+=0;
else if (c>=192 && c<=223) i+=1;
else if (c>=224 && c<=239) i+=2;
else if (c>=240 && c<=247) i+=3;
else if (c>=248 && c<=255) return \"\";//invalid utf8
}
if (length !=INT_MAX && j+length-start<=0) return \"\";
if (start < 0 ) start+=j;
if (length < 0 ) length=j+length-start;
}
j=0,realstart=0,reallength=0;
for(i=0,ix=str.length(); i<ix; i+=1, j++)
{
if (j==start) { realstart=i; }
if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
unsigned char c= str[i];
if (c>=0 && c<=127) i+=0;
else if (c>=192 && c<=223) i+=1;
else if (c>=224 && c<=239) i+=2;
else if (c>=240 && c<=247) i+=3;
else if (c>=248 && c<=255) return \"\";//invalid utf8
}
if (j==start) { realstart=i; }
if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
return str.substr(realstart,reallength);
}
ldpl_number str_len(const string & a){
return utf8_strlen(a);
}
ldpl_number get_char_num(const string & chr){
if (chr.size() != 1) {
cerr << \"Runtime Error: GET CHAR AT expects a string of length 1, got: \";
cerr << chr << endl;
exit(1);
}
int ord = chr[0];
return ord;
}
string charat(const string & s, ldpl_number pos){
unsigned int _pos = floor(pos);
return utf8_substr(s, pos, 1);
}
//Convert ldpl_number to LDPL string, killing trailing 0's
//https://stackoverflow.com/questions/16605967/ & https://stackoverflow.com/questions/13686482/
string to_ldpl_string(double x){
ostringstream out;
out.precision(10);
out << fixed << x;
string str = out.str();
str.erase(str.find_last_not_of('0') + 1, string::npos);
str.erase(str.find_last_not_of('.') + 1, string::npos);
return str;
}
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
string exec(const char* cmd) {
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, \"r\"), pclose);
if (!pipe) {
throw runtime_error(\"popen() failed!\");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
#include <random>
ldpl_number get_random(){
ldpl_number r = ((ldpl_number) rand() / (RAND_MAX)) + 1;
r = r - (int) r;
return r;
}
void load_file(string filename, string & destination)
{
//Load file
ifstream file(filename);
//Fail if the file couldn't be loaded
if(!file.is_open()){
cerr << (\"Error: The file '\" + filename + \"' couldn't be opened.\") << endl;
exit(1);
}
//TODO: Turn this into a control variable that can be checked from LDPL.
//Get file contents
string text = \"\";
string line = \"\";
while(getline(file, line))
{
text += line + \"\\n\";
}
destination = text;
file.close();
}