-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.cpp
More file actions
433 lines (294 loc) · 8.67 KB
/
text.cpp
File metadata and controls
433 lines (294 loc) · 8.67 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// name: text.cpp
// version: 1.0
// description: opens a file and displays contents
// libraries
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <stdlib.h>
// namespaces
using namespace std;
// text parser header
#include "text.h"
// constructors
text::text () { /* do nothing */ }
// constructors
text::text (char *raw) { storage = raw; }
// operators
void text::operator = (char *raw) { storage = raw; }
// remove whitespace from string
void text::clean (char *data) {
// declare variables
int i, x;
// formulate page for digestion
for (i = 0; i < strlen(data); i++) {
// check for skipped whitespace
if (data[i-1] == ' ' || data[i-1] == '\t') { i = i-1; }
// remove whitespace
if (data[i] == ' ' || data[i] == '\t') {
// erase current character
for (x = i; x < strlen(data); x++) { data[x] = data[x+1]; }
}
}
}
// remove whitespace from string
void text::clean (char *data, char delim) {
// declare variables
int i, x;
// formulate page for digestion
for (i = 0; i < strlen(data); i++) {
// check for skipped whitespace
if (data[i-1] == ' ' || data[i-1] == '\t') { i = i-1; }
// check for a delimiter to stop
if (data[i] == delim || data[i-1] == delim) { break; }
// remove whitespace
if (data[i] == ' ' || data[i] == '\t') {
// erase current character
for (x = i; x < strlen(data); x++) { data[x] = data[x+1]; }
}
}
}
// finds if a needle exists in a haystack
bool text::contains (char *haystack, char needle) {
// declare variables
bool cont = false;
char *temp;
// find first instance of needle
temp = strchr(haystack, needle);
// check if found
if (temp != NULL) { cont = true; }
// return condition
return cont;
}
// finds first occurrence of data in
int text::find (char *haystack) {
// declare variables
int i, pos = 0;
// save and break on first alphanumeric
for (i = 0; i < strlen(haystack); i++) {
if (isalnum(haystack[i])) { pos = i; break; }
}
// return condition
return pos;
}
// finds first occurrence of needle in a haystack
int text::find (char *haystack, char needle) {
// declare variables
int pos = 0;
char *temp;
// find first instance of needle
temp = strchr(haystack, needle);
// determine position
if (temp != NULL) { pos = strlen(haystack) - strlen(temp); }
// return condition
return pos;
}
// determines the type of data in a string
// unknown=0, int=1, float=2, char*=3
int text::type (char *data) {
// declare variables
int datatype, i;
// initial unknown
datatype = 0;
// evaluate data
for (i = 0; i < strlen(data); i++) {
// detect numeric
if (isdigit(data[i])) { if (datatype == 0) { datatype = 1; } }
// detect float
if (datatype == 1 && data[i] == '.') { datatype = 2; }
// detect alphabet
if (isalpha(data[i])) { datatype = 3; }
}
// return type
return datatype;
}
// returns a value from a text file
char *text::get (const char *section, const char *key) { return get (section, key, storage); }
// returns a value from a text file
char *text::getchar (const char *section, const char *key) { return get (section, key, storage); }
// converts a value to float
float text::getfloat (const char *section, const char *key) { return (float)atof(get(section, key, storage)); }
// converts a value to int
int text::getint (const char *section, const char *key) { return (float)atoi(get(section, key, storage)); }
// parses a text file
char *text::get (const char *section, const char *key, const char *file) {
// declare variables
bool spres=false, kpres=false, vpres = false;
char ch, *line, pline[2048], *temp;
ifstream fin(file);
// check if file exists
if (fin) {
// initialize array
line = pline;
// erase array
memset(line, 0, strlen(line));
// cycle through data
while (fin.get(ch)) {
// append character to string
line[strlen(line)] = ch;
line[strlen(line)+1] = '\0';
// check if character was end of a line
if (line[strlen(line)-1] == '\n' && line[strlen(line)-2] == '\r') {
// save line
temp = parseline(line);
// pass complete line to interpreter
if (strcmp(temp, section) == 0) { spres = true; kpres = false;}
if (strcmp(temp, key) == 0) { kpres = true; }
// return value
if (spres && kpres) {
// set
spres = false;
kpres = false;
vpres = true;
// stop searching
break;
}
// erase array for new line
memset(line, 0, strlen(line));
}
}
}
fin.close();
if (vpres) { return parseline(line, true); }
else { return '\0'; }
}
// parses a text string into an array
char *text::parseline (char *data) { parseline(data, false); }
// parses a text string into an array
char *text::parseline (char *data, bool rvalue) {
/* clean lines */
// data strings
if (contains(data, ':')) { clean(data, ':'); }
// sections and empty lines
else if (contains(data, ']') || type(data) == 0) { clean(data); }
// extended data
else { clean(data, data[find(data)]); }
/* parse data */
// skip any blank lines
if(strlen(data) == 0 || strlen(data) == 1 && data[0] == '\n') { return (char*)"\0\0"; }
// skip any comment lines
//else if(data[0] == '#') { /* do nothing */ }
// handle bracketed lines
else if(data[0] == '[') {
// determine section name
string stemp = data;
stemp = stemp.substr(1, find(data, ']')-1);
char *sname = (char*)stemp.c_str();
return sname;
}
// process valid lines
else {
// initial data
if (contains(data, ':')) {
// find data splitter
int pos = find(data, ':');
// split key & data
string dtemp = data;
string keytemp = dtemp.substr(0, pos);
string valuetemp = dtemp.substr(pos+1, strlen(data)-pos+1);
// assign data
char *key = (char*)keytemp.c_str();
char *value = (char*)valuetemp.c_str();
// clean data
clean(value, value[find(value)]);
// return data
if (rvalue == false) { return key; }
else { return value; }
}
// data continuation
else {
// move characters backwards
int x;
for (x = strlen(data); x > 0; x--) { data[x] = data[x-1]; }
data[0] = ' ';
data[strlen(data)] = '\0';
return data;
}
}
}
// sets a value in a text file
bool text::set (const char *section, const char *key, const char *data) { return set (section, key, data, storage); }
// sets a value in a text file
bool text::set (const char *section, const char *key, float data) {
// declare variables
string cdata;
ostringstream converter;
// convert data
converter << data;
cdata = converter.str();
// append end of data
cdata += " \r\n";
// return success
return set (section, key, cdata.c_str(), storage);
}
// sets a value in a text file
bool text::set (const char *section, const char *key, int data) {
// declare variables
string cdata;
stringstream converter;
// convert data
converter << data;
cdata = converter.str();
// append end of data
cdata += "\r\n";
// return success
return set (section, key, cdata.c_str(), storage);
}
// sets a value in a text file
bool text::set (const char *section, const char *key, const char *data, const char *file) {
// declare variables
bool spres=false, kpres=false, vpres = false;
char ch, *line, pline[2048], *temp;
string ofile = file;
// backup file name
ofile += ".bak";
// rename original file
rename(file, ofile.c_str());
// construct streams
ofstream fout(file);
ifstream fin(ofile.c_str());
// check if file exists
if (fin) {
// initialize array
line = pline;
// erase array
memset(line, 0, strlen(line));
// cycle through data
while (fin.get(ch)) {
// append character to string
line[strlen(line)] = ch;
line[strlen(line)+1] = '\0';
// check if character was end of a line
if (line[strlen(line)-1] == '\n' && line[strlen(line)-2] == '\r') {
// save line
temp = parseline(line);
// pass complete line to interpreter
if (strcmp(temp, section) == 0) { spres = true; kpres = false;}
if (strcmp(temp, key) == 0) { kpres = true; }
// check for value
if (spres && kpres) {
// set
spres = false;
kpres = false;
vpres = true;
// replace value
if (data[strlen(data)] == '\n' && data[strlen(data)-1] == '\r') { fout << key << ": " << data; }
else { fout << key << ": " << data << "\r\n"; }
}
// change nothing
else { fout << line; }
// erase array for new line
memset(line, 0, strlen(line));
}
}
}
// close files
fin.close();
fout.close();
// remove original file
remove(ofile.c_str());
// return success
if (vpres) { return true; }
else { return false; }
}