-
Notifications
You must be signed in to change notification settings - Fork 2
/
spx.cpp
712 lines (598 loc) · 16.1 KB
/
spx.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 2010 Extreme Tuxracer Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
---------------------------------------------------------------------*/
#include "spx.h"
#include <sstream>
#include <iomanip>
#include <iostream>
static char cbuffer[4096];
static string EnumStr;
void SetEnum (string s) { EnumStr = s;}
// --------------------------------------------------------------------
// elementary string functions
// --------------------------------------------------------------------
char *NewStr (const char *s) {
char *dest;
dest = (char *) malloc (sizeof(char) * (strlen(s) + 1));
if (dest == NULL) printf ("malloc failed\n");
else strcpy (dest, s);
return dest;
}
string MakePathStr (string src, string add) {
string res = src;
res += SEP;
res += add;
return res;
}
void SInsertN (string &s, unsigned int pos, const string ins) {
if (pos > s.size()) pos = s.size();
s.insert (pos, ins);
}
void SDeleteN (string &s, unsigned int pos, int count) {
if (pos > s.size()) pos = s.size();
s.erase (pos, count);
}
int SPosN (string &s, const string find) {
return s.find (find);
}
void STrimLeftN (string &s) {
if (s.empty()) return;
size_t i = 0;
while (i < s.size() && (s[i] == ' ' || s[i] == '\t')) i++;
if (i > 0) SDeleteN (s, 0, i);
}
void STrimRightN (string &s) {
if (s.empty()) return;
int i = s.size() - 1;
while (i >= 0 && (s[i] == ' ' || s[i] == '\t')) i--;
s.erase (i+1);
}
void STrimN (string &s) {
STrimLeftN (s);
STrimRightN (s);
}
// --------------------------------------------------------------------
// conversion functions
// --------------------------------------------------------------------
void Int_StrN (string &s, const int val) {
ostringstream os;
os << val;
s = os.str();
}
string Int_StrN (const int val) {
ostringstream os;
os << val;
return os.str();
}
string Int_StrN (const int val, const int count) {
ostringstream os;
os << val;
string s = os.str();
while (s.size() < (unsigned int)count) SInsertN (s, 0, "0");
return s;
}
void Float_StrN (string &s, const float val, const int count) {
ostringstream os;
os << setprecision(count) << fixed << val;
s = os.str();
}
string Float_StrN (const float val, const int count) {
ostringstream os;
os << setprecision(count) << fixed << val;
return os.str();
}
string Vector_StrN (const TVector3 v, const int count) {
string res = Float_StrN (v.x, count);
res += " " + Float_StrN (v.y, count);
res += " " + Float_StrN (v.z, count);
return res;
}
int Str_IntN (const string &s, const int def) {
int val;
istringstream is(s);
is >> val;
if (is.fail()) return def; else return val;
}
bool Str_BoolN (const string &s, const bool def) {
int val;
istringstream is(s);
is >> val;
if (is.fail()) return def;
return (val != 0);
}
bool Str_BoolNX (const string &s, const bool def) {
string decode = "[0]0[1]1[true]1[false]0";
string valstr;
if (def == true) valstr = SPStrN (decode, s, "1");
else valstr = SPStrN (decode, s, "0");
int val;
istringstream is(valstr);
is >> val;
if (is.fail()) return def;
return (val != 0);
}
float Str_FloatN (const string &s, const float def) {
float val;
istringstream is(s);
is >> val;
if (is.fail()) return def; else return val;
}
TVector2 Str_Vector2N (const string &s, const TVector2 def) {
float x, y;
istringstream is(s);
is >> x >> y;
if (is.fail()) return def;
else return MakeVector2 (x, y);
}
TVector2 Str_Vector2N (const string &s) {
float x, y;
istringstream is(s);
is >> x >> y;
if (is.fail()) return MakeVector2 (0, 0);
else return MakeVector2 (x, y);
}
TVector3 Str_Vector3N (const string &s, const TVector3 def) {
float x, y, z;
istringstream is(s);
is >> x >> y >> z;
if (is.fail()) return def;
else return MakeVector (x, y, z);
}
TIndex3 Str_Index3N (const string &s, const TIndex3 def) {
int i, j, k;
istringstream is(s);
is >> i >> j >> k;
if (is.fail()) return def;
else return MakeIndex3 (i, j, k);
}
TVector3 Str_Vector3N (const string &s) {
float x, y, z;
istringstream is(s);
is >> x >> y >> z;
if (is.fail()) return MakeVector (0, 0, 0);
else return MakeVector (x, y, z);
}
TTuple4 MakeTuple4 (double a, double b, double c, double d) {
TTuple4 res;
res.a = a;
res.b = b;
res.c = c;
res.d = d;
return res;
}
TTuple4 Str_Tuple4N (const string &s) {
float a, b, c, d;
istringstream is(s);
is >> a >> b >> c >> d;
if (is.fail()) return MakeTuple4 (0, 0, 0, 0);
else return MakeTuple4 (a, b, c, d);
}
TVector4 Str_Vector4N (const string &s, const TVector4 def) {
float x, y, z, w;
istringstream is(s);
is >> x >> y >> z >> w;
if (is.fail()) return def;
else return MakeVector4 (x, y, z, w);
}
TColor Str_ColorN (const string &s, const TColor def) {
float r, g, b, a;
istringstream is(s);
is >> r >> g >> b >> a;
if (is.fail()) return def;
else return MakeColor (r, g, b, a);
}
TColor3 Str_Color3N (const string &s, const TColor3 def) {
float r, g, b;
istringstream is(s);
is >> r >> g >> b;
if (is.fail()) return def;
else return MakeColor3 (r, g, b);
}
void Str_ArrN (const string &s, float *arr, int count, float def) {
istringstream is(s);
switch (count) {
case 1: is >> arr[0]; break;
case 2: is >> arr[0] >> arr[1]; break;
case 3: is >> arr[0] >> arr[1] >> arr[2]; break;
case 4: is >> arr[0] >> arr[1] >> arr[2] >> arr[3]; break;
default: break;
}
if (is.fail()) for (int i=0; i<count; i++) arr[i] = def;
}
string Bool_StrN (const bool val) {
if (val == true) return "true"; else return "false";
}
// --------------------------------------------------------------------
// SP functions for parsing lines
// --------------------------------------------------------------------
string SPItemN (string &s, const string &tag) {
int i = 0;
unsigned int ii = 0;
string item = "";
if (s.size() == 0 || tag.size() == 0) return item;
string tg = "[" + tag + "]";
i = SPosN (s, tg);
if (i < 0) return item;
ii = i + tg.size();
while (ii < s.size() && s[ii] != '[' && s[ii] != '#') {
item += s[ii];
ii++;
}
return item;
}
void SPItemN (string &s, const string &tag, string &item) {
int i = 0;
unsigned int ii = 0;
item = "";
if (s.size() == 0 || tag.size() == 0) return;
string tg = "[" + tag + "]";
i = SPosN (s, tg);
if (i < 0) return;
ii = i + tg.size();
while (s[ii] != '[' && s[ii] != '#' && ii < s.size()) {
item += s[ii];
ii++;
}
}
string SPStrN (string &s, const string &tag, const string def) {
string item = SPItemN (s, tag);
if (item.size() < 1) return def;
STrimN (item);
return item;
}
int SPIntN (string &s, const string &tag, const int def) {
return (Str_IntN (SPItemN (s, tag), def));
}
int SPEnumN (string &s, const string &tag, int def) {
string item = SPItemN (s, tag);
STrimN (item);
if (item.size() < 1) return def;
return SPIntN (EnumStr, item, def);;
}
bool SPBoolN (string &s, const string &tag, const bool def) {
return (Str_BoolN (SPItemN (s, tag), def));
}
bool SPBoolNX (string &s, const string &tag, const bool def) {
string item = SPItemN (s, tag);
STrimN (item);
return Str_BoolNX (item, def);
}
float SPFloatN (string &s, const string &tag, const float def) {
return (Str_FloatN (SPItemN (s, tag), def));
}
TVector2 SPVector2N (string &s, const string &tag, const TVector2 def) {
return (Str_Vector2N (SPItemN (s, tag), def));
}
TVector2 SPVector2N (string &s, const string &tag) {
return (Str_Vector2N (SPItemN (s, tag)));
}
TVector3 SPVector3N (string &s, const string &tag, const TVector3 def) {
return (Str_Vector3N (SPItemN (s, tag), def));
}
TIndex3 SPIndex3N (string &s, const string &tag, const TIndex3 def) {
return (Str_Index3N (SPItemN (s, tag), def));
}
TVector3 SPVector3N (string &s, const string &tag) {
return (Str_Vector3N (SPItemN (s, tag)));
}
TTuple4 SPTuple4N (string &s, const string &tag) {
return (Str_Tuple4N (SPItemN (s, tag)));
}
TVector4 SPVector4N (string &s, const string &tag, const TVector4 def) {
return (Str_Vector4N (SPItemN (s, tag), def));
}
TColor SPColorN (string &s, const string &tag, const TColor def) {
return (Str_ColorN (SPItemN (s, tag), def));
}
TColor3 SPColor3N (string &s, const string &tag, const TColor3 def) {
return (Str_Color3N (SPItemN (s, tag), def));
}
void SPArrN (string &s, const string &tag, float *arr, int count, float def) {
Str_ArrN (SPItemN (s, tag), arr, count, def);
}
bool SPExistsN (string &s, const string &tag) {
string tg = "[" + tag + "]";
int i = SPosN (s, tg);
if (i < 0) return false;
return true;
}
int SPPosN (string &s, const string &tag) {
string tg = "[" + tag + "]";
return SPosN (s, tg);
}
// --------------------------------------------------------------------
// compatibility
// --------------------------------------------------------------------
void SInsertO (char *s, int pos, const char *str) {
char buff[5012];
int lng = strlen (s);
if (pos > lng) pos = lng;
if (pos == lng) {
strcat (s, str);
} else {
memmove (buff, s+pos, lng-pos+1);
s[pos] = '\0';
strcat (s, str);
strcat (s, buff);
}
}
void Int_CharN (char *s, const int val) {
sprintf (s, "%i", val);
}
void Int_CharN (char *s, const int val, const int cnt) {
sprintf (s, "%i", val);
while (strlen (s) < (unsigned int)cnt) SInsertO (s, 0, "0");
}
char *NewStrN (const char *s) {
char *dest;
dest = (char *) malloc (sizeof(char) * (strlen(s) + 1));
if (dest == NULL) Message ("malloc failed","");
else strcpy (dest, s);
return dest;
}
void SPCharN (string &s, const string &tag, char *result) {
string item = SPItemN (s, tag);
if (item.size() < 1) return;
STrimN (item);
strcpy (result, item.c_str());
}
char *SPNewCharN (string &s, const string &tag) {
string item = SPItemN (s, tag);
if (item.size() < 1) return 0;
STrimN (item);
return NewStrN (item.c_str());
}
// ------------------ add ---------------------------------------------
void SPAddIntN (string &s, const string &tag, const int val) {
s += "[";
s += tag;
s += "]";
s += Int_StrN (val);
}
void SPAddFloatN (string &s, const string &tag, const float val, int count) {
s += "[";
s += tag;
s += "]";
s += Float_StrN (val, count);
}
void SPAddStrN (string &s, const string &tag, const string &val) {
s += "[";
s += tag;
s += "]";
s += val;
}
void SPAddVec2N (string &s, const string &tag, const TVector2 val, int count) {
s += "[";
s += tag;
s += "]";
s += ' ';
s += Float_StrN (val.x, count);
s += " ";
s += Float_StrN (val.y, count);
}
void SPAddVec3N (string &s, const string &tag, const TVector3 val, int count) {
s += "[";
s += tag;
s += "]";
s += ' ';
s += Float_StrN (val.x, count);
s += " ";
s += Float_StrN (val.y, count);
s += " ";
s += Float_StrN (val.z, count);
}
void SPAddIndx3N (string &s, const string &tag, const TIndex3 val) {
s += "[";
s += tag;
s += "]";
s += ' ';
s += Int_StrN (val.i);
s += ' ';
s += Int_StrN (val.j);
s += ' ';
s += Int_StrN (val.k);
}
void SPAddIndx4N (string &s, const string &tag, const TIndex4 val) {
s += '[';
s += tag;
s += ']';
s += ' ';
s += Int_StrN (val.i);
s += ' ';
s += Int_StrN (val.j);
s += ' ';
s += Int_StrN (val.k);
s += ' ';
s += Int_StrN (val.l);
}
void SPAddBoolN (string &s, const string &tag, const bool val) {
s += '[';
s += tag;
s += ']';
if (val == true) s += "true"; else s+= "false";
}
// --------------------------------------------------------------------
void SPSetIntN (string &s, const string &tag, const int val) {
int pos = SPPosN (s, tag);
if (pos >= 0) {
int ipos = pos + tag.size() + 2;
string item = SPItemN (s, tag);
if (item.size() > 0) SDeleteN (s, ipos, item.size());
SInsertN (s, ipos, Int_StrN (val));
} else SPAddIntN (s, tag, val);
}
void SPSetFloatN (string &s, const string &tag, const float val, int count) {
int pos = SPPosN (s, tag);
if (pos >= 0) {
int ipos = pos + tag.size() + 2;
string item = SPItemN (s, tag);
if (item.size() > 0) SDeleteN (s, ipos, item.size());
SInsertN (s, ipos, Float_StrN (val, count));
} else SPAddFloatN (s, tag, val, count);
}
void SPSetStrN (string &s, const string &tag, const string &val) {
int pos = SPPosN (s, tag);
if (pos >= 0) {
int ipos = pos + tag.size() + 2;
string item = SPItemN (s, tag);
if (item.size() > 0) SDeleteN (s, ipos, item.size());
SInsertN (s, ipos, val);
} else SPAddStrN (s, tag, val);
}
// --------------------------------------------------------------------
// class CSPList
// --------------------------------------------------------------------
CSPList::CSPList (int maxlines) {
fmax = maxlines;
flines = new string [maxlines];
fflag = new int [maxlines];
fcount = 0;
fnewlineflag = 0;
}
CSPList::CSPList (int maxlines, int newlineflag) {
fmax = maxlines;
flines = new string [maxlines];
fflag = new int [maxlines];
fcount = 0;
fnewlineflag = newlineflag;
}
CSPList::~CSPList () {
if (flines != 0) delete []flines;
delete []fflag;
}
string CSPList::Line (int idx) {
if (idx < 0 || idx >= fcount) return "";
return flines[idx];
}
const char *CSPList::LineC (int idx) {
if (idx < 0 || idx >= fcount) return "";
return flines[idx].c_str();
}
int CSPList::Count () { return fcount; }
void CSPList::Clear () {
fcount = 0;
}
void CSPList::Add (string line) {
if (fcount < fmax) {
flines[fcount] = line;
fcount++;
}
}
void CSPList::Add (string line, int flag) {
if (fcount < fmax) {
flines[fcount] = line;
fflag[fcount] = flag;
fcount++;
}
}
void CSPList::Append (string line, int idx) {
if (idx < 0 || idx >= fcount) return;
flines[idx] += line;
}
void CSPList::SetFlag (int idx, int flag) {
if (idx < 0 || idx >= fcount) return;
fflag[idx] = flag;
}
int CSPList::Flag (int idx) {
if (idx < 0 || idx >= fcount) return 0;
return fflag[idx];
}
void CSPList::Print () {
for (int i=0; i<fcount; i++) cout << flines[i] << endl;
}
char lastchar (const string s) {
return s[s.length()-1];
}
bool CSPList::Load (const string &filepath) {
FILE *tempfile;
string line;
bool valid;
bool fwdflag;
bool backflag = false;
tempfile = fopen (filepath.c_str(), "r");
if (tempfile == 0) {
Message ("CSPList::Load - unable to open","");
return false;
} else {
while (fgets (cbuffer, 4000, tempfile)) {
line = cbuffer;
STrimN (line);
// delete new line char if in string
int npos = line.rfind ('\n');
if (npos >= 0) SDeleteN (line, npos, 1);
valid = true;
if (line.size() < 1) valid = false; // empty line
else if (line[0] == '#') valid = false; // comment line
if (valid) {
if (fcount < fmax) {
if (fnewlineflag == 0) {
if (cbuffer[0] == '*' || fcount < 1) Add (line);
else Append (line, fcount-1);
} else if (fnewlineflag == 1) {
if (lastchar (line) == '\\') {
SDeleteN (line, line.length()-1, 1);
fwdflag = true;
} else {
fwdflag = false;
}
if (backflag == false) Add (line);
else Append (line, fcount-1);
backflag = fwdflag;
}
} else {
Message ("CSPList::Load - not enough lines","");
return false;
}
}
}
fclose (tempfile);
return true;
}
}
bool CSPList::Load (string dir, string filename) {
return Load (dir + SEP + filename);
}
bool CSPList::Save (const string &filepath) {
FILE *tempfile;
string line;
tempfile = fopen (filepath.c_str(), "w");
if (tempfile == 0) {
Message ("CSPList::Save - unable to open","");
return false;
} else {
for (int i=0; i<fcount; i++) {
line = flines[i] + '\n';
fputs (line.c_str(), tempfile);
}
fclose (tempfile);
return true;
}
}
bool CSPList::Save (string dir, string filename) {
return Save (dir + SEP + filename);
}
void CSPList::MakeIndex (string &index, const string &tag) {
index = "";
string item;
int idx = 0;
for (int i=0; i<fcount; i++) {
item = SPItemN (flines[i], tag);
STrimN (item);
if (item.size() > 0) {
index += "[";
index += item;
index += "]";
index += Int_StrN (idx);
idx++;
}
}
}