File: consoleparse.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (1456 lines) | stat: -rw-r--r-- 32,386 bytes parent folder | download | duplicates (4)
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Command-line parsing functions for z64555's debug console, created for the FreeSpace Source Code project
//
// Portions of this source code are based on works by Volition, Inc. circa 1999. You may not sell or otherwise
// commercially exploit the source or things you created based on the source.
//
// This file contains documentation on locally referenced functions. For documentation on globally referenced
// functions, see consoleparse.h
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////
// @todo Make a fast version of the parse_long, parse_ulong, etc. That just checks the first 1-3 characters. The fast version will be used in retail builds while the slow/safe version will be in debug's
// @todo Maybe make parser functions case-insensitive
////////////////

#include "debugconsole/consoleparse.h"
#include "parse/parselo.h"

#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <cstring>
#include <climits>


// ========================= LOCALS =========================
char Command_string[MAX_CLI_LEN];   //!< Command string buffer.
char *Cp = NULL;    //!< Pointer to the commant string

enum state_int {
	si_start   = 0,
	si_end     = 1,
	si_invalid,
	si_sign,        //!< Sign character, '-' '+'
	si_prefix,      //!< prefix character sequence, 0b, 0o, or 0x
	si_numeral,     //!< Numeral state, 0 - 9
	si_numeral_bin,     //!< Numeral altstate, 0, 1
	si_numeral_octal,   //!< Numeral altstate, 0 - 7
	si_numeral_hex      //!< Numeral altstate, 0 - 9 and 'a' - 'f'
};

enum state_float {
	sf_start = 0,
	sf_end = 1,
	sf_invalid,
	sf_sign,		//!< Sign character for mantessa
	sf_whole,		//!< Whole value numeral
	sf_decimal,		//!< Decimal character
	sf_fraction,	//!< Fractional value numeral
	sf_expprefix,	//!< Exponent prefix, 'e', 'E'
	sf_expsign,		//!< Exponent sign
	sf_exponent		//!< Exponent value numeral
};

bool ch_is_sign(char ch);
bool ch_is_numeral(char ch);
bool ch_is_decimal(char ch);
bool ch_is_binary(char ch);
bool ch_is_octal(char ch);
bool ch_is_hex(char ch);

bool ch_is_prefix(char ch);
bool ch_is_binary_prefix(char ch);
bool ch_is_octal_prefix(char ch);
bool ch_is_hex_prefix(char ch);
bool ch_is_exp_prefix(char ch);

/**
 * @brief Returns/Advances past a single token
 *
 * @details Similar in operation to dc_stuff_string_white, but won't throw any error messages and will only grab the
 *   first word. (dc_stuff_string_white may grab quoted strings)
 */
void dc_get_token(SCP_string &out_str);

/**
 * @brief Returns a single token, but does not advances Cp
 *
 * @details Similar in operation to dc_stuff_string_white, but won't throw any error messages and will only grab the
 *   first word. (dc_stuff_string_white may grab quoted strings).
 */
void dc_get_token_no_advance(SCP_string &out_str);

/**
 * @brief Parses a double-precision floating point type. Supports, Whole, Fractional, and Mixed numbers, and supports
 *   scientific notation (exponent prefixed by 'e' or 'E').
 *
 * @param[in] ch   Points to the start of the string to parse
 * @param[in] type The expected type. is thrown along with ch when an unexpected/malformed float is found
 *
 * @return The value of the parsed token
 * @details
 *   The returned double may be cast to a single-precision float, but be sure to check it before doing so!
 */
double dc_parse_double(const char *ch, dc_token type);

/**
 * @brief Parses a long integral type. Supports decimal, binary, octal, and hexidecimal strings.
 *
 * @param[in] ch    Points to the start of the string to parse.
 * @param[in] type  The expected type. Is thrown along with ch when an unexpected/malformed integral is found
 *
 * @details
 * ! Non-decimal values must be prefixed by their corresponding sequence. Binary: "0b", Octal: "0o", Hex: "0x"
 *
 *   The returned long may be cast to a smaller integral, but be sure to check it before doing so!
 *
 *   The only thing left making this function specific to the DC is the expected type. So, if you want to use this
 *   for parsing something other than the debug CL, you'll have to make a set of errParse classes that take a
 *   different expected type.
 */
long   dc_parse_long(const char *ch, dc_token type);

/**
 * @brief Parses an unsigned long integral type. Supports decimal, binary, octal, and hexidecimal strings.
 *
 * @param[in] ch    Points to the start of the string to parse.
 * @param[in] type  The expected type. Is thrown along with ch when an unexpected/malformed integral is found
 *
 * @details
 * ! Non-decimal values must be delimited by their corresponding sequence. Binary: "0b", Octal: "0o", Hex: "0x"
 *   The returned long may be cast to a smaller integral, but be sure to check it before doing so!
 *   The only thing left making this function specific to the DC is the expected type. So, if you want to use this
 *   for parsing something other than the debug CL, you'll have to make a set of errParse classes that take a
 *   different expected type.
 */
ulong  dc_parse_ulong(const char *ch, dc_token type);

// State processes for dc_parse_double
state_float dc_parse_double_sign(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_whole(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_decimal(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_fraction(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_expprefix(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_expsign(const char* &ch_ptr, SCP_string &buffer_str);
state_float dc_parse_double_exponent(const char* &ch_ptr, SCP_string &buffer_str);

// State processes for dc_parse_long and dc_parse_ulong.
state_int dc_parse_long_prefix(const char* &ch_ptr, SCP_string &buffer_str, int &base);
state_int dc_parse_long_sign(const char* &ch_ptr, SCP_string &buffer_str);
state_int dc_parse_long_numeral(const char* &ch_ptr, SCP_string &buffer_str);
state_int dc_parse_long_numeral_bin(const char* &ch_ptr, SCP_string &buffer_str);
state_int dc_parse_long_numeral_hex(const char* &ch_ptr, SCP_string &buffer_str);
state_int dc_parse_long_numeral_octal(const char* &ch_ptr, SCP_string &buffer_str);


// ============================== IMPLEMENTATIONS =============================
inline
bool ch_is_sign(char ch)
{
	return ((ch == '-') || (ch == '+'));
}

inline
bool ch_is_numeral(char ch)
{
	return ((ch >= '0') && (ch <= '9'));
}

inline
bool ch_is_decimal(char ch)
{
	return (ch == '.');
}

inline
bool ch_is_binary(char ch)
{
	return ((ch == '0') || (ch == '1'));
}

inline
bool ch_is_octal(char ch)
{
	return ((ch >= '0') && (ch <= '7'));
}

inline
bool ch_is_hex(char ch)
{
	return (((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'f')) || ((ch >= 'A') && (ch <= 'F')));
};

inline
bool ch_is_prefix(char ch)
{
	return (ch == '0');
}

inline
bool ch_is_binary_prefix(char ch)
{
	return ((ch == 'b') || (ch == 'B'));
}

inline
bool ch_is_octal_prefix(char ch)
{
	return ((ch == 'o') || (ch == 'O'));
}

inline
bool ch_is_hex_prefix(char ch)
{
	return ((ch == 'x') || (ch == 'X'));
}

inline
bool ch_is_exp_prefix(char ch)
{
	return ((ch == 'e') || (ch == 'E'));
}

void dc_get_token(SCP_string &out_str) {
	size_t count = 0;
	char *c_ptr;

	Assert(Cp);

	dc_ignore_gray_space();

	out_str = "";
	// Bail if we're at the terminator
	if (*Cp == '\0') {
		return;
	}

	// Scan the string, stopping at grayspace, null terminator, or before we go over MAX_TOKEN_LENGTH
	c_ptr = Cp;
	while (!is_gray_space(*c_ptr) && (*c_ptr != '\0') && (count < MAX_TOKEN_LENGTH)) {
		count++;
		c_ptr++;
	}

	// Copy string into out_str
	out_str.assign(Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;
}

void dc_get_token_no_advance(SCP_string &out_str) {
	size_t count = 0;
	char *c_ptr;

	Assert(Cp);

	dc_ignore_gray_space();

	out_str = "";
	// Bail if we're at the terminator
	if (*Cp == '\0') {
		return;
	}

	// Scan the string, stopping at grayspace, null terminator, or before we go over MAX_TOKEN_LENGTH
	c_ptr = Cp;
	while (!is_gray_space(*c_ptr) && (*c_ptr != '\0') && (count < MAX_TOKEN_LENGTH)) {
		count++;
		c_ptr++;
	}

	// Copy string into out_str
	out_str.assign(Cp, count);
}

void dc_ignore_white_space(void) {
	while (is_white_space(*Cp) && (*Cp != '\0')) {
		Cp++;
	}
}

void dc_ignore_gray_space(void) {
	while (is_gray_space(*Cp) && (*Cp != '\0')) {
		Cp++;
	}
}

bool dc_maybe_stuff_float(float *f)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_float(f);
		return true;
	
	} else {
		*f = 0;
		return false;
	}
}

bool dc_maybe_stuff_int(int *i)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_int(i);
		return true;
	} else {
		*i = 0;
		return false;
	}
}

bool dc_maybe_stuff_uint(uint *i)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_uint(i);
		return true;
	} else {
		*i = 0;
		return false;
	}
}

bool dc_maybe_stuff_ubyte(ubyte *i)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_ubyte(i);
		return true;
	} else {
		*i = 0;
		return false;
	}
}

bool dc_maybe_stuff_boolean(bool *b)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_boolean(b);
		return true;
	} else {
		*b = false;
		return false;
	}
}

bool dc_maybe_stuff_boolean(int *i)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_boolean(i);
		return true;
	} else {
		*i = 0;
		return false;
	}
}

bool dc_maybe_stuff_string(char *out_str, size_t maxlen)
{
	size_t count = 0;
	char *c_ptr = Cp;

	Assert(Cp);
	Assert(out_str);

	// Advance past grayspace, stopping at null terminator
	while (is_gray_space(*c_ptr) && (*c_ptr != '\0')) {
		c_ptr++;
	}

	// Bail if we're at the terminator
	if (*c_ptr == '\0') {
		return false;
	}

	// Scan the string, stopping at null terminator, or before we overflow
	while ((*c_ptr != '\0') && (count < maxlen)) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if (count == maxlen) {
		throw errParse("", DCT_STRING);
	}

	// Copy string into out_str
	strncpy(out_str, Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;

	return true;
}

bool dc_maybe_stuff_string(SCP_string &out_str)
{
	size_t count = 0;
	char *c_ptr = Cp;

	Assert(Cp);

	// Advance past grayspace, stopping at null terminator
	while (is_gray_space(*c_ptr) && (*c_ptr != '\0')) {
		c_ptr++;
	}

	// Bail if we're at the terminator
	if (*c_ptr == '\0') {
		return false;
	}

	// Scan the string, stopping at null terminator, or before we overflow
	while ((*c_ptr != '\0') && (count < out_str.max_size())) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if (count == out_str.max_size()) {
		return false;
	}

	// Copy string into out_str
	out_str.assign(Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;

	return true;
}

bool dc_maybe_stuff_string_white(char *str, size_t len)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_string_white(str, len);
		return true;
	} else {
		*str = '\0';
		return false;
	}
}

bool dc_maybe_stuff_string_white(SCP_string &str)
{
	dc_ignore_gray_space();

	if (*Cp != '\0') {
		dc_stuff_string_white(str);
		return true;
	} else {
		str = "";
		return false;
	}
}

void dc_required_string(char *pstr)
{
	char *str_found = NULL;

	dc_ignore_gray_space();

	if (strncmp(pstr, Cp, strlen(pstr)) == 0) {
		str_found = pstr;
	}

	if (str_found != NULL) {
		// Found a required string
		if (Dc_debug_on) {
			dc_printf("<debug> Found required string [%s]\n", str_found);
		}

		Cp += strlen(str_found);
	} else {
		// Didn't find a required string.
		SCP_string token;
		dc_get_token_no_advance(token);
		throw errParseString(token.c_str(), pstr);
	}
}

int dc_required_string_either(char *str1, char *str2)
{
	char *str_found = NULL;
	int i = -1;

	dc_ignore_gray_space();

	if (strncmp(str1, Cp, strlen(str1)) == 0) {
		str_found = str1;
		i = 0;
	} else if (strncmp(str2, Cp, strlen(str2)) == 0) {
		str_found = str2;
		i = 1;
	}

	if (i > -1) {
		// Found a required string
		if (Dc_debug_on) {
			dc_printf("<debug> Found required string [%s]\n", str_found);
		}

		Cp += strlen(str_found);
	} else {
		// Didn't find a required string.
		SCP_string token;
		dc_get_token_no_advance(token);
		throw errParseString(token.c_str(), str1, str2);
	}

	return i;
}

uint dc_required_string_any(const uint n, ...)
{
	va_list vl;
	SCP_vector<SCP_string> strings;
	const char *str_found = NULL;
	uint i;

	dc_ignore_gray_space();

	// Populate the vector.
	va_start(vl, n);
	for (i = 0; i < n; ++i) {
		strings.push_back(va_arg(vl, char *));
	}
	va_end(vl);

	// Search for the required string. If found, i = index of the string passed
	for (i = 0; i < n; ++i) {
		if (strcmp(Cp, strings[i].c_str()) == 0)
		{
			str_found = strings[i].c_str();
			break;
		}
	}

	if (str_found != NULL) {
		// Found a required string
		if (Dc_debug_on) {
			dc_printf("<debug> Found required string [%s}\n", str_found);
		}

		Cp += strlen(str_found);
	} else {
		// Didn't find a required string.
		SCP_string token;
		dc_get_token_no_advance(token);
		throw errParseString(token.c_str(), strings);
	}

	return i;
}

bool dc_optional_string(const char *pstr)
{
	dc_ignore_gray_space();

	if (strncmp(pstr, Cp, strlen(pstr)) != 0) {
		return false;
	} // Else, optional string was found

	if (Dc_debug_on) {
		dc_printf("<debug> Found optional string [%s]\n", pstr);
	}

	Cp += strlen(pstr);
	return true;
}

bool dc_optional_string_either(const char *str1, const char *str2)
{
	const char *str_found = NULL;

	dc_ignore_gray_space();

	if (strncmp(str1, Cp, strlen(str1)) == 0) {
		str_found = str1;
	} else if (strncmp(str2, Cp, strlen(str2)) == 0) {
		str_found = str2;
	} else {
		return false;
	}

	if (Dc_debug_on) {
		dc_printf("<debug> Found optional string [%s]\n",str_found);
	}

	Cp += strlen(str_found);
	return true;;
}

void dc_parse_init(SCP_string &str)
{
	strcpy_s(Command_string, str.c_str());
	Cp = Command_string;
}

double dc_parse_double(const char *ch, dc_token type) {
	const char *ch_ptr = ch;
	char *end_ptr;
	double ret;
	state_float state = sf_start;
	SCP_string buffer_str;

	while ((*ch_ptr != '\0') && is_white_space(*ch_ptr)) {
		ch_ptr++;
	}

	if (*ch_ptr == '\0') {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] no argument found\n");
		}
		throw errParse("", type);
	}

	do {
		switch (state) {
		case sf_start:
			if (ch_is_sign(*ch_ptr)) {
				state = sf_sign;
				if (*ch_ptr == '-') {
					buffer_str.push_back(*ch_ptr);
				} // Else, sign is positive, and isn't needed on the buffer
				ch_ptr++;

			} else if (ch_is_numeral(*ch_ptr)) {
				state = sf_whole;
				buffer_str.push_back(*ch_ptr);
				ch_ptr++;

			} else if (ch_is_decimal(*ch_ptr)) {
				state = sf_decimal;
				buffer_str.push_back(*ch_ptr);
				ch_ptr++;

			} else {
				if (Dc_debug_on) {
					dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_start\n", *ch_ptr);
				}
				state = sf_invalid;

			}
			break;
		case sf_end:
			if (buffer_str == "") {
				state = sf_invalid;
			} // Else, we can convert the token.
			// Do nothing, and allow the while loop exit condition to trigger
			break;
		case sf_sign:
			state = dc_parse_double_sign(ch_ptr, buffer_str);
			break;

		case sf_whole:
			state = dc_parse_double_whole(ch_ptr, buffer_str);
			break;

		case sf_decimal:
			state = dc_parse_double_decimal(ch_ptr, buffer_str);
			break;

		case sf_fraction:
			state = dc_parse_double_fraction(ch_ptr, buffer_str);
			break;

		case sf_expprefix:
			state = dc_parse_double_expprefix(ch_ptr, buffer_str);
			break;

		case sf_expsign:
			state = dc_parse_double_expsign(ch_ptr, buffer_str);
			break;

		case sf_exponent:
			state = dc_parse_double_exponent(ch_ptr, buffer_str);
			break;

		case sf_invalid:
		default:
			throw errParse(ch, type);
		}
	} while (state != sf_end);

	ret = strtod(buffer_str.c_str(), &end_ptr);

	return ret;
}

state_float dc_parse_double_sign(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_numeral(*ch_ptr)) {
		state = sf_whole;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_decimal(*ch_ptr)) {
		state = sf_decimal;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_sign\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_whole(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_numeral(*ch_ptr)) {
		state = sf_whole;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_decimal(*ch_ptr)) {
		state = sf_decimal;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_exp_prefix(*ch_ptr)) {
		state = sf_expprefix;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = sf_end;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_whole\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_decimal(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_numeral(*ch_ptr)) {
		state = sf_fraction;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_exp_prefix(*ch_ptr)) {
		state = sf_expprefix;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = sf_end;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_decimal\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_fraction(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_numeral(*ch_ptr)) {
		state = sf_fraction;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_exp_prefix(*ch_ptr)) {
		state = sf_expprefix;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = sf_end;
	
	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_faction\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_expprefix(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_sign(*ch_ptr)) {
		state = sf_expsign;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_numeral(*ch_ptr)) {
		state = sf_exponent;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_expprefix\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_expsign(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;

	if (ch_is_numeral(*ch_ptr)) {
		state = sf_exponent;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_expsign\n", *ch_ptr);
		}
	}

	return state;
}

state_float dc_parse_double_exponent(const char* &ch_ptr, SCP_string &buffer_str)
{
	state_float state = sf_invalid;
	
	if (ch_is_numeral(*ch_ptr)) {
		state = sf_exponent;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = sf_end;

	} else {
		state = sf_invalid;
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_double] Invalid character '%c' found in sf_exponent\n", *ch_ptr);
		}
	}

	return state;
}

long dc_parse_long(const char *ch, dc_token type) {
	const char *ch_ptr = ch;
	char *end_ptr = NULL;
	int base = 10;
	long ret;
	state_int state = si_start;
	SCP_string buffer_str;

	while ((*ch_ptr != '\0') && is_white_space(*ch_ptr)) {
		ch_ptr++;
	}

	if (*ch_ptr == '\0') {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] no argument found\n");
		}
		throw errParse("", type);
	}

	do {
		switch (state) {
		case si_start:
			if (ch_is_sign(*ch_ptr)) {
				state = si_sign;
				if (*ch_ptr == '-') {
					buffer_str.push_back(*ch_ptr);
				} // Else, it's positive. Positive sign isn't needed for conversion
				ch_ptr++;

			} else if (ch_is_prefix(*ch_ptr)) {
				// prefixes must be checked before numeral, because they all start with a numeral zero
				state = si_prefix;
				ch_ptr++;
		
			} else if (ch_is_numeral(*ch_ptr)) {
				state = si_numeral;
				buffer_str.push_back(*ch_ptr);
				ch_ptr++;
		
			} else {
				if (Dc_debug_on) {
					dc_printf("<debug> [parse_long] Invalid character '%c' found in si_start\n", *ch_ptr);
				}
				state = si_invalid;

			}
			break;
		case si_end:
			if (buffer_str == "") {
				state = si_invalid;
			} // Else, we can convert the token.
			// Do nothing, and allow the while loop exit condition to trigger
			break;

		case si_sign:
			state = dc_parse_long_sign(ch_ptr, buffer_str);
			break;

		case si_prefix:
			state = dc_parse_long_prefix(ch_ptr, buffer_str, base);
			break;

		case si_numeral_bin:
			state = dc_parse_long_numeral_bin(ch_ptr, buffer_str);
			break;

		case si_numeral_octal:
			state = dc_parse_long_numeral_octal(ch_ptr, buffer_str);
			break;

		case si_numeral_hex:
			state = dc_parse_long_numeral_hex(ch_ptr, buffer_str);
			break;

		case si_numeral:
			state = dc_parse_long_numeral(ch_ptr, buffer_str);
			break;

		case si_invalid:
		default:
			throw errParse(ch, type);
			break;
		}
	} while (state != si_end);

	ret = strtol(buffer_str.c_str(), &end_ptr, base);

	// This last check can be omitted once I can verify the operation of strtol in this sense
	if (*end_ptr != '\0') {
		dc_printf("Error: Could not convert all of the buffer '%s'.\n", buffer_str.c_str());
		if (Dc_debug_on) {
			dc_printf("<debug> Buffer value: %s\n", buffer_str.c_str());
			dc_printf("<debug> Return value: %ld", ret);
		}
		throw errParse(ch, type);
	}

	return ret;
}

ulong dc_parse_ulong(const char *ch, dc_token type) {
	const char *ch_ptr = ch;
	char *end_ptr;
	int base = 10;
	ulong ret;
	state_int state = si_start;
	SCP_string buffer_str;

	while ((*ch_ptr != '\0') && is_white_space(*ch_ptr)) {
		ch_ptr++;
	}

	if (*ch_ptr == '\0') {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] no argument found\n");
		}
		throw errParse("", type);
	}

	do {
		switch (state) {
		case si_start:
			if (ch_is_prefix(*ch_ptr)) {
				// prefixes must be checked before numeral, because they all start with a numeral zero
				state = si_prefix;
				ch_ptr++;
		
			} else if (ch_is_numeral(*ch_ptr)) {
				state = si_numeral;
				buffer_str.push_back(*ch_ptr);
				ch_ptr++;
		
			} else {
				if (Dc_debug_on) {
					dc_printf("<debug> [parse_long] Invalid character '%c' found in si_start\n", *ch_ptr);
				}
				state = si_invalid;

			}
			break;
		case si_end:
			if (buffer_str == "")
			{
				state = si_invalid;
			} // Else, we can convert the token.
			// Do nothing, and allow the while loop exit condition to trigger
			break;

		case si_prefix:
			state = dc_parse_long_prefix(ch_ptr, buffer_str, base);
			break;

		case si_numeral_bin:
			state = dc_parse_long_numeral_bin(ch_ptr, buffer_str);
			break;

		case si_numeral_octal:
			state = dc_parse_long_numeral_octal(ch_ptr, buffer_str);
			break;

		case si_numeral_hex:
			state = dc_parse_long_numeral_hex(ch_ptr, buffer_str);
			break;

		case si_numeral:
			state = dc_parse_long_numeral(ch_ptr, buffer_str);
			break;

		case si_invalid:
		default:
			throw errParse(ch, type);
		}
	} while (state != si_end);

	ret = strtoul(buffer_str.c_str(), &end_ptr, base);

	// This last check can be omitted once I can verify the operation of strtol in this sense
	if (end_ptr != ch_ptr) {
		dc_printf("Error: Could not convert all of the buffer '%s'.\n", buffer_str.c_str());
		if (Dc_debug_on) {
			dc_printf("<debug> Buffer value: %s\n", buffer_str.c_str());
			dc_printf("<debug> Return value: %ld", ret);
		}
		throw errParse(ch, type);
	}

	return ret;
}

void dc_stuff_float(float *f)
{
	double value_d;
	SCP_string token;

	dc_ignore_gray_space();

	dc_get_token(token);	// Grab the token
	value_d = dc_parse_double(token.c_str(), DCT_FLOAT);	// Parse and convert
	
	// Stuff value if within bounds
	if ((std::abs(value_d) < FLT_MAX) && (std::abs(value_d) > FLT_MIN)) {
		*f = static_cast<float>(value_d);
	} else {
		throw errParse(token.c_str(), DCT_FLOAT);
	}
}

void dc_stuff_int(int *i)
{
	long value_l;
	SCP_string token;

	dc_ignore_gray_space();

	dc_get_token(token);
	value_l = dc_parse_long(token.c_str(), DCT_INT);

	if ((value_l < INT_MAX) && (value_l > INT_MIN)) {
		*i = value_l;

	} else {
		throw errParse(token.c_str(), DCT_INT);
	}
}

void dc_stuff_uint(uint *i)
{
	ulong value_l;
	SCP_string token;

	dc_ignore_gray_space();

	dc_get_token(token);
	value_l = dc_parse_long(Cp, DCT_INT);

	if (value_l < UINT_MAX) {
		*i = value_l;

	} else {
		throw errParse(token.c_str(), DCT_INT);
	}
}

void dc_stuff_ubyte(ubyte *i)
{
	ulong value_ul;
	SCP_string token;

	dc_ignore_gray_space();

	dc_get_token(token);
	value_ul = dc_parse_ulong(Cp, DCT_UBYTE);

	// Since some system's chars may be greater than 1 byte, we can't use UCHAR_MAX for a UBYTE
	if (value_ul <= 255) {
		*i = static_cast<ubyte>(value_ul);

	} else {
		throw errParse(token.c_str(), DCT_UBYTE);
	}
}

void dc_stuff_boolean(bool *b)
{
	SCP_string token;

	dc_get_token(token);

	if ((token == "yes")
		|| (token == "true")
		|| (token == "ja")          // German
		|| (token == "Oui")         // French
		|| (token == "si")          // Spanish
//		|| (token == "ita vero")    // Latin, not supported
		|| (token == "HIja") || (token == "HISLaH")     // Klingon
		|| (token == "1"))
	{
		*b = true;

	} else if ((token == "no")
		|| (token == "false")
		|| (token == "nein")    // German
		|| (token == "Non")     // French
//		|| (token == "no")      // Spanish, redundant with English "no"
//		|| (token == "minime")  // Latin, not supported
		|| (token == "ghobe'")  // Klingon
		|| (token == "0"))
	{
		*b = false;

	} else {
		throw errParse(token.c_str(), DCT_BOOL);
	}
}

void dc_stuff_boolean(int *i)
{
	bool value_b;

	dc_stuff_boolean(&value_b);

	value_b ? *i = 1 : *i = 0;
}

void dc_stuff_string(char *out_str, size_t maxlen = MAX_TOKEN_LENGTH)
{
	size_t count = 0;
	char *c_ptr = Cp;
	SCP_string token;

	Assert(Cp);
	Assert(out_str);

	dc_ignore_gray_space();

	// Bail if we're at the terminator
	if (*Cp == '\0') {
		throw errParse("Nothing!", DCT_STRING);
	}

	// Scan the string, stopping at null terminator, or before we overflow
	c_ptr = Cp;
	while ((*c_ptr != '\0') && (count < maxlen)) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if (count == maxlen) {
		token.assign(Cp, maxlen);
		throw errParseOverflow(token.c_str(), maxlen);
	}

	// Copy string into out_str
	strncpy(out_str, Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;
}

void dc_stuff_string(SCP_string &out_str)
{
	size_t count = 0;
	char *c_ptr = Cp;

	Assert(Cp);

	dc_ignore_gray_space();

	// Bail if we're at the terminator
	if (*Cp == '\0') {
		throw errParse("Nothing!", DCT_STRING);
	}

	// Scan the string, stopping at null terminator, or before we overflow
	c_ptr = Cp;
	while ((*c_ptr != '\0') && (count < out_str.max_size())) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if ((count == out_str.max_size()) && (*c_ptr != '\0')) {
		throw errParse("SCP_string overflow!", DCT_STRING);
	}

	// Copy string into out_str
	out_str.assign(Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;
}

void dc_stuff_string_white(char *out_str, size_t maxlen)
{
	size_t count = 0;
	char *c_ptr = Cp;

	Assert(Cp);
	Assert(out_str);

	dc_ignore_gray_space();

	// Bail if we're at the terminator
	if (*Cp == '\0') {
		throw errParse("Nothing!", DCT_STRING);
	}

	// Scan the string, stopping at grayspace, null terminator, or before we overflow
	c_ptr = Cp;
	while (!is_gray_space(*c_ptr) && (*c_ptr != '\0') && (count < maxlen)) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if (count == maxlen) {
		throw errParse("", DCT_STRING);
	}

	// Copy string into out_str
	strncpy(out_str, Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;
}

void dc_stuff_string_white(SCP_string &out_str)
{
	size_t count = 0;
	char *c_ptr;

	Assert(Cp);

	dc_ignore_gray_space();

	// Bail if we're at the terminator
	if (*Cp == '\0') {
		throw errParse("Nothing!", DCT_STRING);
	}

	// Scan the string, stopping at grayspace, null terminator, or before we overflow
	c_ptr = Cp;
	while (!is_gray_space(*c_ptr) && (*c_ptr != '\0') && (count < out_str.max_size())) {
		count++;
		c_ptr++;
	}

	// Bail if overflow
	if (count == out_str.max_size()) {
		throw errParse("", DCT_STRING);
	}

	// Copy string into out_str
	out_str.assign(Cp, count);
	
	// Advance the parser pointer past what we copied
	Cp = c_ptr;
}

inline
state_int dc_parse_long_prefix(const char* &ch_ptr, SCP_string &buffer_str, int &base) {
	state_int state = si_invalid;

	if (ch_is_binary_prefix(*ch_ptr)) {
		state = si_numeral_bin;
		base = 2;
		ch_ptr++;
	
	} else if (ch_is_octal_prefix(*ch_ptr)) {
		state = si_numeral_octal;
		base = 8;
		ch_ptr++;
	
	} else if (ch_is_hex_prefix(*ch_ptr)) {
		state = si_numeral_hex;
		base = 16;
		ch_ptr++;

	} else if (ch_is_numeral(*ch_ptr)) {
		// User passed something like 0123
		// Just ignore the first 0, if the user passed something like 00001 then state si_numeral can handle it
		state = si_numeral;
		ch_ptr++;
	
	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		// Just a 0 was passed.
		buffer_str.push_back('0');
		state = si_end;

	} else {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_prefix\n", *ch_ptr);
		}
		state = si_invalid;

	}
	return state;
}

inline
state_int dc_parse_long_sign(const char* &ch_ptr, SCP_string &buffer_str) {
	state_int state;
	if (ch_is_numeral(*ch_ptr)) {
		state = si_numeral;
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;

	} else if (ch_is_prefix(*ch_ptr)) {
		state = si_prefix;
		ch_ptr++;

	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		// Error, no value found
		state = si_invalid;

	} else {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_sign\n", *ch_ptr);
		}
		state = si_invalid;

	}
	return state;
}

inline
state_int dc_parse_long_numeral(const char* &ch_ptr, SCP_string &buffer_str) {
	state_int state = si_numeral;

	if (ch_is_numeral(*ch_ptr)) {
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;
	
	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = si_end;
	
	} else {
		// Invalid character, throw and bail
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_numeral\n", *ch_ptr);
		}
		state = si_invalid;

	}
	return state;
}

inline
state_int dc_parse_long_numeral_bin(const char* &ch_ptr, SCP_string &buffer_str) {
	state_int state = si_numeral_bin;

	if (ch_is_binary(*ch_ptr)) {
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;
	
	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = si_end;
	
	} else {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_numeral_bin\n", *ch_ptr);
		}
		state = si_invalid;

	}
	return state;
}

inline
state_int dc_parse_long_numeral_hex(const char* &ch_ptr, SCP_string &buffer_str) {
	state_int state = si_numeral_hex;

	if (ch_is_hex(*ch_ptr)) {
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;
	
	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = si_end;
	
	} else {
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_numeral_hex\n", *ch_ptr);
		}
		state = si_invalid;
	}
	return state;
}

inline
state_int dc_parse_long_numeral_octal(const char* &ch_ptr, SCP_string &buffer_str) {
	state_int state = si_numeral_octal;

	if (ch_is_octal(*ch_ptr)) {
		buffer_str.push_back(*ch_ptr);
		ch_ptr++;
	
	} else if ((*ch_ptr == '\0') || is_white_space(*ch_ptr)) {
		state = si_end;
	
	} else {
		// Invalid character, throw and bail
		if (Dc_debug_on) {
			dc_printf("<debug> [parse_long] Invalid character '%c' found in si_numeral_octal\n", *ch_ptr);
		}
		state = si_invalid;
	}
	return state;
}