7 * low level parse routines common to all types of parsers
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 12 8/10/99 6:54p Dave
15 * Mad optimizations. Added paging to the nebula effect.
17 * 11 6/02/99 2:22p Andsager
20 * 10 5/20/99 11:25a Andsager
21 * Added error checking for parsing table files.
23 * 9 2/23/99 11:18a Andsager
24 * Localize launcher using strings.tbl
26 * 8 2/03/99 6:06p Dave
27 * Groundwork for FS2 PXO usertracker support. Gametracker support next.
29 * 7 11/05/98 4:18p Dave
30 * First run nebula support. Beefed up localization a bit. Removed all
31 * conditional compiles for foreign versions. Modified mission file
34 * 6 10/29/98 12:49p Dave
35 * Intermediate checkin for Fred hash table stuff.
37 * 5 10/28/98 11:30a Dave
40 * 4 10/22/98 6:14p Dave
41 * Optimized some #includes in Anim folder. Put in the beginnings of
42 * parse/localization support for externalized strings and tstrings.tbl
44 * 3 10/14/98 1:15p Andsager
47 * 2 10/07/98 10:53a Dave
50 * 1 10/07/98 10:50a Dave
52 * 98 9/21/98 2:37p Adam
53 * fixed font translation bug
55 * 97 7/29/98 9:39a Hoffoss
56 * Fixed bug with stuff_and_malloc_string
58 * 96 6/22/98 11:18a Hoffoss
59 * Fixed bug where mission notes field gets an extra linefeed with each
62 * 95 6/19/98 3:53p Lawrance
63 * account for some new foreign chars
65 * 94 6/12/98 7:37p Hoffoss
66 * Made ß translate to ss, since we don't have this character in our font.
68 * 93 6/12/98 4:52p Hoffoss
69 * Added support for special characters in in forgeign languages.
71 * 92 5/21/98 3:26p Dave
72 * Fixed bug in new split string function. Newline as last character did
75 * 91 5/21/98 3:01p Dave
76 * Make sure split_str handles 0 length strings correctly.
78 * 90 5/21/98 2:14a Hoffoss
79 * Rewrote split_str() to solve a problem and it was too hard for me to
80 * figure out (oow, my head hurts still!)
96 #include "missionparse.h"
102 #define ERROR_LENGTH 64
103 #define RS_MAX_TRIES 5
105 char Current_filename[128];
106 char Error_str[ERROR_LENGTH];
108 int Warning_count, Error_count;
109 int fred_parse_flag = 0;
110 int Token_found_flag;
113 char Mission_text[MISSION_TEXT_SIZE];
114 char Mission_text_raw[MISSION_TEXT_SIZE];
118 // Return true if this character is white space, else false.
119 int is_white_space(char ch)
121 return ((ch == ' ') || (ch == '\t') || (ch == EOLN));
124 // Returns true if this character is gray space, else false (gray space is white space except for EOLN).
125 int is_gray_space(char ch)
127 return ((ch == ' ') || (ch == '\t'));
130 // Advance global Mp (mission pointer) past all current white space.
131 // Leaves Mp pointing at first non white space character.
132 void ignore_white_space()
134 while ((*Mp != EOF_CHAR) && is_white_space(*Mp))
138 void ignore_gray_space()
140 while ((*Mp != EOF_CHAR) && is_gray_space(*Mp))
144 // Truncate *str, eliminating all trailing white space.
145 // Eg: "abc " becomes "abc"
146 // "abc abc " becomes "abc abc"
147 // "abc \t" becomes "abc"
148 void drop_trailing_white_space(char *str)
154 while ((i >= 0) && is_white_space(str[i]))
160 // Eliminate any leading whitespace in str
161 void drop_leading_white_space(char *str)
168 while ((i < len) && is_white_space(str[i]))
171 memmove(str, str+i, len-i);
175 // eliminates all leading and trailing white space from a string. Returns pointer passed in.
176 char *drop_white_space(char *str)
181 while (str[s] && is_white_space(str[s]))
186 if (!is_white_space(str[e]))
193 memmove(str, str + s, e - s + 1);
199 // Advances Mp past current token.
202 ignore_white_space();
204 while ((*Mp != EOF_CHAR) && !is_white_space(*Mp))
208 // Display a diagnostic message if Verbose is set.
209 // (Verbose is set if -v command line switch is present.)
210 void diag_printf(char *format, ...)
215 va_start(args, format);
216 vsprintf(buffer, format, args);
219 nprintf(("Parse", "%s", buffer));
222 // Grab and return (a pointer to) a bunch of tokens, terminating at
223 // ERROR_LENGTH chars, or end of line.
230 while (((ch = *pstr++) != EOLN) && (ch != EOF_CHAR) && (count < ERROR_LENGTH-1))
231 Error_str[count++] = ch;
233 Error_str[count] = 0;
237 // Return the line number given by the current mission pointer, ie Mp.
238 // A very slow function (scans all processed text), but who cares how long
239 // an error reporting function takes?
248 p = Mission_text_raw;
249 stoploc = (Mp - Mission_text) + p;
256 if ( !incomment && (*p == COMMENT_CHAR) )
259 if ( !incomment && (*p == '/') && (*(p+1) == '*') ) {
267 if ( multiline && (*(p-1) == '*') && (*p == '/') ) {
273 if ( !multiline && incomment )
282 // Call this function to display an error message.
283 // error_level == 0 means this is just a warning.
284 // !0 means it's an error message.
285 // Prints line number and other useful information.
286 void error_display(int error_level, char *format, ...)
289 char error_text[128];
292 if (error_level == 0) {
293 strcpy(error_text, "Warning");
296 strcpy(error_text, "Error");
300 nprintf((error_text, "%s(%i):%s: ", Current_filename, get_line_num(), error_text));
302 va_start(args, format);
303 vsprintf(buffer, format, args);
305 Assert(strlen(buffer) < 1024);
307 nprintf((error_text, "%s", buffer));
308 Warning(LOCATION, "%s(%i):\n%s: %s", Current_filename, get_line_num(), error_text, buffer);
311 // Advance Mp to the next eoln character.
312 void advance_to_eoln(char *more_terminators)
314 char terminators[128];
316 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
318 terminators[0] = EOLN;
319 terminators[1] = (char)EOF_CHAR;
320 if (more_terminators != NULL)
321 strcpy(&terminators[2], more_terminators);
325 while (strchr(terminators, *Mp) == NULL)
329 // Advance Mp to the next white space (ignoring white space inside of " marks)
330 void advance_to_next_white()
334 while ((*Mp != EOLN) && (*Mp != EOF_CHAR)) {
336 in_quotes = !in_quotes;
338 if (!in_quotes && is_white_space(*Mp))
344 // Search for specified string, skipping everything up to that point. Returns 1 if found,
345 // 0 if string wasn't found (and hit end of file), or -1 if not found, but end of checking
346 // block was reached.
347 int skip_to_string(char *pstr, char *end)
351 ignore_white_space();
356 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, len)) {
357 if (end && *Mp == '#')
360 if (end && !strnicmp(pstr, Mp, len2))
363 advance_to_eoln(NULL);
364 ignore_white_space();
367 if (!Mp || (*Mp == EOF_CHAR))
374 // Advance to start of either pstr1 or pstr2. Return 0 is successful, otherwise return !0
375 int skip_to_start_of_strings(char *pstr1, char *pstr2)
379 ignore_white_space();
380 len1 = strlen(pstr1);
381 len2 = strlen(pstr2);
383 while ( (*Mp != EOF_CHAR) && strnicmp(pstr1, Mp, len1) && strnicmp(pstr2, Mp, len2) ) {
384 advance_to_eoln(NULL);
385 ignore_white_space();
388 if (!Mp || (*Mp == EOF_CHAR))
394 // Find a required string.
395 // If not found, display an error message, but try up to RS_MAX_TRIES times
396 // to find the string. (This is the groundwork for ignoring non-understood
398 // If unable to find the required string after RS_MAX_TRIES tries, then
399 // abort using longjmp to parse_abort.
400 int required_string(char *pstr)
404 ignore_white_space();
406 while (strnicmp(pstr, Mp, strlen(pstr)) && (count < RS_MAX_TRIES)) {
407 error_display(1, "Required token = [%s], found [%.32s].\n", pstr, next_tokens());
408 advance_to_eoln(NULL);
409 ignore_white_space();
413 if (count == RS_MAX_TRIES) {
414 nprintf(("Error", "Error: Unable to find required token [%s]\n", pstr));
415 Warning(LOCATION, "Error: Unable to find required token [%s]\n", pstr);
416 longjmp(parse_abort, 1);
420 diag_printf("Found required string [%s]\n", token_found = pstr);
424 // similar to optional_string, but just checks if next token is a match.
425 // It doesn't advance Mp.
427 int check_for_string(char *pstr)
429 ignore_white_space();
431 if (!strnicmp(pstr, Mp, strlen(pstr)))
437 // like check for string, but doesn't skip past any whitespace
438 int check_for_string_raw(char *pstr)
440 if (!strnicmp(pstr, Mp, strlen(pstr))){
447 // Find an optional string.
448 // If found, return 1, else return 0.
449 // If found, point past string, else don't update pointer.
450 int optional_string(char *pstr)
452 ignore_white_space();
454 if (!strnicmp(pstr, Mp, strlen(pstr))) {
462 int required_string_fred(char *pstr, char *end)
470 ignore_white_space();
471 while (*Mp != EOF_CHAR && strnicmp(pstr, Mp, strlen(pstr))) {
472 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end)))) {
477 advance_to_eoln(NULL);
478 ignore_white_space();
481 if (!Mp || (*Mp == EOF_CHAR)) {
482 diag_printf("Required string [%s] not found\n", pstr);
484 Token_found_flag = 0;
489 diag_printf("Found required string [%s]\n", pstr);
490 Token_found_flag = 1;
494 // attempt to find token in buffer. It might not exist, however, in which case we don't need
495 // to do anything. If it is found, then we advance the pointer to just after the token. To
496 // further complicate things, we should only search to a certain point, since we don't want
497 // a token that belongs to another section which might match the token we want. Thus, we
498 // also pass in an ending token, which marks the point we should stop looking at.
499 int optional_string_fred(char *pstr, char *end, char *end2)
507 ignore_white_space();
508 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, strlen(pstr))) {
509 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end))) ||
510 (end2 && !strnicmp(end2, Mp, strlen(end2)))) {
515 advance_to_eoln(NULL);
516 ignore_white_space();
519 if (!Mp || (*Mp == EOF_CHAR)) {
520 diag_printf("Optional string [%s] not found\n", pstr);
522 Token_found_flag = 0;
527 diag_printf("Found optional string [%s]\n", pstr);
528 Token_found_flag = 1;
532 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
533 // Does not update Mp if token found. If not found, advances, trying to
534 // find the string. Doesn't advance past the found string.
535 int required_string_either(char *str1, char *str2)
539 ignore_white_space();
541 while (count < RS_MAX_TRIES) {
542 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
543 // Mp += strlen(str1);
544 diag_printf("Found required string [%s]\n", token_found = str1);
546 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
547 // Mp += strlen(str2);
548 diag_printf("Found required string [%s]\n", token_found = str2);
552 error_display(1, "Required token = [%s] or [%s], found [%.32s].\n", str1, str2, next_tokens());
554 advance_to_eoln(NULL);
555 ignore_white_space();
559 if (count == RS_MAX_TRIES) {
560 nprintf(("Error", "Error: Unable to find either required token [%s] or [%s]\n", str1, str2));
561 Warning(LOCATION, "Error: Unable to find either required token [%s] or [%s]\n", str1, str2);
562 longjmp(parse_abort, 2);
569 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
570 // Does not update Mp if token found. If not found, advances, trying to
571 // find the string. Doesn't advance past the found string.
572 int required_string_3(char *str1, char *str2, char *str3)
576 ignore_white_space();
578 while (count < RS_MAX_TRIES) {
579 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
580 // Mp += strlen(str1);
581 diag_printf("Found required string [%s]\n", token_found = str1);
583 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
584 // Mp += strlen(str2);
585 diag_printf("Found required string [%s]\n", token_found = str2);
587 } else if (strnicmp(str3, Mp, strlen(str3)) == 0) {
588 diag_printf("Found required string [%s]\n", token_found = str3);
592 error_display(1, "Required token = [%s], [%s] or [%s], found [%.32s].\n", str1, str2, str3, next_tokens());
594 advance_to_eoln(NULL);
595 ignore_white_space();
603 int required_string_either_fred(char *str1, char *str2)
605 ignore_white_space();
607 while (*Mp != EOF_CHAR) {
608 if (!strnicmp(str1, Mp, strlen(str1))) {
609 // Mp += strlen(str1);
610 diag_printf("Found required string [%s]\n", token_found = str1);
611 return fred_parse_flag = 0;
613 } else if (!strnicmp(str2, Mp, strlen(str2))) {
614 // Mp += strlen(str2);
615 diag_printf("Found required string [%s]\n", token_found = str2);
616 return fred_parse_flag = 1;
619 advance_to_eoln(NULL);
620 ignore_white_space();
624 diag_printf("Unable to find either required token [%s] or [%s]\n", str1, str2);
630 // Copy characters from instr to outstr until eoln is found, or until max
631 // characters have been copied (including terminator).
632 void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max)
636 char terminators[128];
638 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
640 terminators[0] = EOLN;
641 terminators[1] = (char)EOF_CHAR;
642 if (more_terminators != NULL)
643 strcpy(&terminators[2], more_terminators);
647 while (((ch = *instr++) != 0) && (strchr(terminators, ch) == NULL) && (count < max)) {
653 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
658 // Copy characters from instr to outstr until next white space is found, or until max
659 // characters have been copied (including terminator).
660 void copy_to_next_white(char *outstr, char *instr, int max)
666 while (((ch = *instr++)>0) && (ch != EOLN) && (ch != EOF_CHAR) && (count < max)) {
668 in_quotes = !in_quotes;
671 if ( !in_quotes && is_white_space(ch) ) // not in quotes, white space terminates string
678 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
683 // Copy text until a certain string is matched.
684 // For example, this is used to copy mission notes, scanning until $END NOTES:
686 void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars)
690 foundstr = strstr(instr, endstr);
692 if (foundstr == NULL) {
693 nprintf(("Error", "Error. Looking for [%s], but never found it.\n", endstr));
694 longjmp(parse_abort, 3);
697 if (foundstr - instr + strlen(endstr) < (uint) max_chars) {
698 strncpy(outstr, instr, foundstr - instr);
699 outstr[foundstr - instr] = 0;
702 nprintf(("Error", "Error. Too much text (%i chars, %i allowed) before %s\n",
703 foundstr - instr - strlen(endstr), max_chars, endstr));
705 longjmp(parse_abort, 4);
708 diag_printf("Here's the partial wad of text:\n%.30s", outstr);
711 // stuffs a string into a buffer. Can get a string between " marks and stops
712 // when whitespace in encounted -- not to end of line
713 void stuff_string_white(char *pstr)
715 ignore_white_space();
716 copy_to_next_white(pstr, Mp, NAME_LENGTH-1);
717 advance_to_next_white();
720 // Stuff a string into a string buffer.
721 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
722 // the default string length if using the F_NAME case.
723 void stuff_string(char *pstr, int type, char *terminators, int len)
725 char read_str[2048] = "";
733 final_len = NAME_LENGTH;
736 copy_to_eoln(read_str, terminators, Mp, read_len);
737 drop_trailing_white_space(read_str);
738 advance_to_eoln(terminators);
743 final_len = DATE_LENGTH;
744 copy_to_eoln(read_str, terminators, Mp, read_len);
745 drop_trailing_white_space(read_str);
746 advance_to_eoln(terminators);
750 ignore_white_space();
751 final_len = NOTES_LENGTH;
752 copy_text_until(read_str, Mp, "$End Notes:", read_len);
753 Mp += strlen(read_str);
754 required_string("$End Notes:");
759 final_len = FILESPEC_LENGTH;
760 copy_to_eoln(read_str, terminators, Mp, read_len);
761 drop_trailing_white_space(read_str);
762 advance_to_eoln(terminators);
764 // F_MULTITEXTOLD keeping for backwards compatability with old missions
765 // can be deleted once all missions are using new briefing format
768 ignore_white_space();
769 final_len = NOTES_LENGTH;
770 copy_text_until(read_str, Mp, "$End Briefing Text:", read_len);
771 Mp += strlen(read_str);
772 required_string("$End Briefing Text:");
777 final_len = MULTITEXT_LENGTH;
779 ignore_white_space();
780 copy_text_until(read_str, Mp, "$end_multi_text", read_len);
781 Mp += strlen(read_str);
782 drop_trailing_white_space(read_str);
783 required_string("$end_multi_text");
788 final_len = PATHNAME_LENGTH;
789 copy_to_eoln(read_str, terminators, Mp, read_len);
790 drop_trailing_white_space(read_str);
791 advance_to_eoln(terminators);
796 final_len = MESSAGE_LENGTH;
797 copy_to_eoln(read_str, terminators, Mp, read_len);
798 drop_trailing_white_space(read_str);
799 advance_to_eoln(terminators);
806 // now we want to do any final localization
807 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
809 // if the hash localized text hash table is active and we have a valid external string - hash it
810 if(fhash_active() && (tag_id > -2)){
811 fhash_add_str(pstr, tag_id);
814 diag_printf("Stuffed string = [%.30s]\n", pstr);
817 // stuff a string, but only until the end of a line. don't ignore leading whitespace. close analog of fgets()/cfgets()
818 void stuff_string_line(char *pstr, int len)
820 char read_str[2048] = "";
827 copy_to_eoln(read_str, "\n", Mp, read_len);
828 drop_trailing_white_space(read_str);
832 // now we want to do any final localization
833 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
835 // if the hash localized text hash table is active and we have a valid external string - hash it
836 if(fhash_active() && (tag_id > -2)){
837 fhash_add_str(pstr, tag_id);
840 diag_printf("Stuffed string = [%.30s]\n", pstr);
843 // 1K on the stack? seems to work...
844 // JH: 1k isn't enough! Command briefs can be 16k max, so changed this.
845 #define MAX_TMP_STRING_LENGTH 16384
847 // Exactly the same as stuff string only Malloc's the buffer.
848 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
849 // the default string length if using the F_NAME case.
850 char *stuff_and_malloc_string( int type, char *terminators, int len)
854 char tmp_result[MAX_TMP_STRING_LENGTH];
856 stuff_string(tmp_result, type, terminators, len);
857 drop_white_space(tmp_result);
859 l = strlen(tmp_result);
860 Assert(l < MAX_TMP_STRING_LENGTH); // Get John!!
864 return strdup(tmp_result);
867 // After reading a multitext string, you can call this function to convert any newlines into
868 // spaces, so it's a one paragraph string (I.e. as in MS-Word).
870 void compact_multitext_string(char *str)
874 for (i=0; i<strlen(str); i++)
879 #define BUF_SIZE 2048
881 // Strip comments from a line of input.
882 int strip_comments_fred(char *readp, int in_comment)
885 char *writep = readp;
887 while ((ch = *readp) != COMMENT_CHAR) {
908 // Strip comments from a line of input.
909 int strip_comments(char *readp, int in_comment)
912 char *writep = readp;
914 while ((ch = *readp) != COMMENT_CHAR) {
921 // time to do some special foreign character conversion
1038 void strip_all_comments( char *readp, char *writep )
1041 //char *writep = readp;
1043 while ( *readp != EOF_CHAR ) {
1045 if ( ch == COMMENT_CHAR ) {
1046 while ( *readp != EOLN )
1051 // get to next character after EOLN
1053 } else if ( (ch == '/') && (readp[1] == '*')) { // Start of multi-line comment
1058 while ( *readp != '*' )
1060 if ( readp[1] == '/' ) {
1074 *writep = (char)EOF_CHAR;
1078 int parse_get_line(char *lineout, int max_line_len, char *start, int max_size, char *cur)
1081 int i, num_chars_read=0;
1085 for ( i = 0; i < max_line_len-1; i++ ) {
1087 if ( (cur - start) >= max_size ) {
1089 if ( lineout > t ) {
1090 return num_chars_read;
1097 } while ( c == 13 );
1100 if ( c=='\n' ) break;
1104 return num_chars_read;
1107 // Read mission text, stripping comments.
1108 // When a comment is found, it is removed. If an entire line
1109 // consisted of a comment, a blank line is left in the input file.
1110 void read_file_text(char *filename, int mode)
1113 char outbuf[BUF_SIZE], *str;
1114 char *mp = Mission_text;
1115 char *mp2 = Mission_text_raw;
1116 int file_is_encrypted = 0, in_comment = 0;
1119 longjmp(parse_abort, 10);
1121 strcpy(Current_filename, filename);
1122 mf = cfopen(filename, "rb", CFILE_NORMAL, mode);
1124 nprintf(("Error", "Wokka! Error opening mission.txt!\n"));
1125 longjmp(parse_abort, 5);
1128 // read the entire file in
1129 int file_len = cfilelength(mf);
1131 // read first 10 bytes to determine if file is encrypted
1132 cfread(Mission_text_raw, min(file_len, 10), 1, mf);
1133 file_is_encrypted = is_encrpyted(Mission_text_raw);
1134 cfseek(mf, 0, CF_SEEK_SET);
1136 if ( file_is_encrypted ) {
1137 int unscrambled_len;
1138 char *scrambled_text;
1139 scrambled_text = (char*)malloc(file_len+1);
1140 Assert(scrambled_text);
1141 cfread(scrambled_text, file_len, 1, mf);
1143 unencrypt(scrambled_text, file_len, Mission_text_raw, &unscrambled_len);
1144 file_len = unscrambled_len;
1145 free(scrambled_text);
1147 cfread(Mission_text_raw, file_len, 1, mf);
1152 // If you hit this assert, it is probably telling you the obvious. The file
1153 // you are trying to read is truly too large. Look at *filename to see the file name.
1154 Assert(file_len < MISSION_TEXT_SIZE);
1156 // strip comments from raw text, reading into Mission_text
1158 int num_chars_read = 0;
1160 mp2 = Mission_text_raw;
1161 while ( (num_chars_read = parse_get_line(outbuf, BUF_SIZE, Mission_text_raw, file_len, mp2)) != 0 ) {
1162 mp2 += num_chars_read;
1165 in_comment = strip_comments_fred(outbuf, in_comment);
1167 in_comment = strip_comments(outbuf, in_comment);
1180 // strcpy(mp, outbuf);
1181 // mp += strlen(outbuf);
1184 *mp = *mp2 = (char)EOF_CHAR;
1186 while (cfgets(outbuf, BUF_SIZE, mf) != NULL) {
1187 if (strlen(outbuf) >= BUF_SIZE-1)
1188 error_display(0, "Input string too long. Max is %i characters.\n%.256s\n", BUF_SIZE, outbuf);
1190 // If you hit this assert, it is probably telling you the obvious. The file
1191 // you are trying to read is truly too large. Look at *filename to see the file name.
1192 Assert(mp2 - Mission_text_raw + strlen(outbuf) < MISSION_TEXT_SIZE);
1193 strcpy(mp2, outbuf);
1194 mp2 += strlen(outbuf);
1196 in_comment = strip_comments(outbuf, in_comment);
1198 mp += strlen(outbuf);
1201 *mp = *mp2 = (char)EOF_CHAR;
1206 void debug_show_mission_text()
1208 char *mp = Mission_text;
1211 while ((ch = *mp++) != EOF_CHAR)
1220 ignore_white_space();
1224 if ((ch != '.') && (ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1225 error_display(1, "Expecting float, found [%.32s].\n", next_tokens());
1229 return (float)atof(Mp);
1239 ignore_white_space();
1243 if ((ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1244 error_display(1, "Expecting int, found [%.32s].\n", next_tokens());
1253 // Stuff a floating point value pointed at by Mp.
1254 // Advances past float characters.
1255 void stuff_float(float *f)
1262 Mp += strspn(Mp, "+-0123456789.");
1267 diag_printf("Stuffed float: %f\n", *f);
1270 // Stuff an integer value pointed at by Mp.
1271 // Advances past integer characters.
1272 void stuff_int(int *i)
1279 Mp += strspn(Mp, "+-0123456789");
1284 diag_printf("Stuffed int: %i\n", *i);
1288 // Stuffs a boolean value pointed at by Mp.
1289 // YES/NO (supporting 1/0 now as well)
1291 void stuff_boolean(int *i)
1294 ignore_white_space();
1295 copy_to_eoln(token, NULL, Mp, 512);
1296 drop_trailing_white_space(token);
1297 advance_to_eoln(NULL);
1299 if ( isdigit(token[0]) ) { // using 1/0 instead of YES/NO
1301 int_value = atoi(token);
1308 if ( !stricmp(token, "yes") ) {
1311 else if ( !stricmp(token, "no") ) {
1315 Assert(0); // can't happen
1320 // Stuff an integer value pointed at by Mp.
1321 // Advances past integer characters.
1322 void stuff_byte(ubyte *i)
1333 Mp += strspn(Mp, "+-0123456789");
1338 diag_printf("Stuffed byte: %i\n", *i);
1341 // Stuffs a list of strings
1342 int stuff_string_list(char slp[][NAME_LENGTH], int max_strings)
1345 ignore_white_space();
1348 error_display(1, "Reading string list. Found [%c]. Expecting '('.\n", *Mp);
1349 longjmp(parse_abort, 100);
1354 ignore_white_space();
1356 while (*Mp != ')') {
1357 assert ( count < max_strings );
1358 Assert ( *Mp == '\"' ); // should always be enclosed in quotes
1360 get_string( slp[count++] );
1361 ignore_white_space();
1369 // Stuffs an integer list.
1370 // This is of the form ( i* )
1371 // where i is an integer.
1372 // For example, (1) () (1 2 3) ( 1 ) are legal integer lists.
1373 int stuff_int_list(int *ilp, int max_ints, int lookup_type)
1375 int count = 0, ok_flag = 1, dummy;
1376 ignore_white_space();
1379 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1380 longjmp(parse_abort, 6);
1384 ignore_white_space();
1386 while (*Mp != ')') {
1387 Assert(count < max_ints);
1393 switch (lookup_type) {
1395 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1398 case SHIP_INFO_TYPE:
1400 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1405 case WEAPON_POOL_TYPE:
1407 num = weapon_info_lookup(str);
1412 case WEAPON_LIST_TYPE:
1413 num = weapon_info_lookup(str);
1418 case RAW_INTEGER_TYPE:
1423 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1429 Error(LOCATION, "Unable to find string \"%s\" in stuff_int_list\n\nMany possible sources for this error. Get a programmer!\n", str);
1430 } else if (num == -2) {
1431 if (strlen(str) > 0) {
1432 Warning(LOCATION, "Unable to find WEAPON_LIST_TYPE string \"%s\" in stuff_int_list\n\nMany possible sources for this error. Get a programmer!\n", str);
1436 if (num < 0) // other negatives used to bypass the above error trap, but should be -1
1444 stuff_int(&ilp[count++]);
1449 ignore_white_space();
1457 // Marks an integer list.
1458 // This is of the form ( i* )
1459 // where i is an integer.
1460 // If a specified string is found in the lookup and its value is 7, then the 7th value
1461 // in the array is set.
1462 void mark_int_list(int *ilp, int max_ints, int lookup_type)
1464 ignore_white_space();
1467 error_display(1, "Marking integer list. Found [%c]. Expecting '('.\n", *Mp);
1468 longjmp(parse_abort, 6);
1472 ignore_white_space();
1474 while (*Mp != ')') {
1480 switch(lookup_type) {
1482 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1485 case SHIP_INFO_TYPE:
1486 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1489 case WEAPON_LIST_TYPE:
1490 num = weapon_info_lookup(str);
1494 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1498 if ( (num < 0) || (num >= max_ints) )
1499 Error(LOCATION, "Unable to find string \"%s\" in mark_int_list.\n", str);
1507 Assert((tval >= 0) && (tval < max_ints));
1511 ignore_white_space();
1519 // Stuff a vector, which is 3 floats.
1520 void stuff_vector(vector *vp)
1522 stuff_float(&vp->x);
1523 stuff_float(&vp->y);
1524 stuff_float(&vp->z);
1527 void stuff_parenthesized_vector(vector *vp)
1529 ignore_white_space();
1532 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting '('.\n", *Mp);
1533 longjmp(parse_abort, 11);
1537 ignore_white_space();
1539 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting ')'.\n", *Mp);
1540 longjmp(parse_abort, 12);
1547 // Stuffs vector list.
1548 // This is of the form ( (vector)* )
1549 // where vector is a vector
1550 // For example, ( (1 2 3) (2 3 4) (2 3 5) )
1551 // is a vector list of three vectors.
1552 int stuff_vector_list(vector *vlp, int max_vecs)
1556 ignore_white_space();
1559 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1560 longjmp(parse_abort, 6);
1565 ignore_white_space();
1567 while (*Mp != ')') {
1568 Assert(count < max_vecs);
1569 stuff_parenthesized_vector(&vlp[count++]);
1571 ignore_white_space();
1580 // Stuff a matrix, which is 3 vectors.
1581 void stuff_matrix(matrix *mp)
1583 stuff_vector(&mp->rvec);
1584 stuff_vector(&mp->uvec);
1585 stuff_vector(&mp->fvec);
1589 // Given a string, find it in a string array.
1590 // *descrtiption is only used for diagnostics in case it can't be found.
1591 // *str1 is the string to be found.
1592 // *strlist is the list of strings to search.
1593 // max is the number of entries in *strlist to scan.
1594 int string_lookup(char *str1, char *strlist[], int max, char *description, int say_errors)
1598 for (i=0; i<max; i++) {
1599 Assert(strlen(strlist[i]) != 0);
1601 if (!stricmp(str1, strlist[i]))
1606 error_display(0, "Unable to find [%s] in %s list.\n", str1, description);
1611 // Find a required string (*id), then stuff the text of type f_type that
1612 // follows it at *addr. *strlist[] contains the strings it should try to
1614 void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
1618 required_string(id);
1619 stuff_string(token, f_type, NULL);
1620 *addr = string_lookup(token, strlist, max, description, 1);
1623 // Mp points at a string.
1624 // Find the string in the list of strings *strlist[].
1625 // Returns the index of the match, -1 if none.
1626 int match_and_stuff(int f_type, char *strlist[], int max, char *description)
1630 stuff_string(token, f_type, NULL);
1631 return string_lookup(token, strlist, max, description, 0);
1634 void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total,
1635 int max, char *description)
1640 required_string(id);
1641 stuff_string(token, f_type, NULL);
1643 *addr = string_lookup(token, strlist, *total, description, 0);
1645 if (*addr == -1) // not in list, so lets try and add it.
1647 Assert(*total < max);
1648 strcpy(strlist[*total], token);
1653 // Initialize a parse process.
1662 for (i=0; i<MAX_CARGO; i++)
1663 Cargo_names[i] = Cargo_names_buf[i]; // make a pointer array for compatibility
1665 Total_goal_ship_names = 0;
1677 // Display number of warnings and errors at the end of a parse.
1678 void display_parse_diagnostics()
1680 nprintf(("Parse", "\nParse complete.\n"));
1681 nprintf(("Parse", "%i errors. %i warnings.\n", Error_count, Warning_count));
1684 // Splits a string into 2 lines if the string is wider than max_pixel_w pixels. A null
1685 // terminator is placed where required to make the first line <= max_pixel_w. The remaining
1686 // text is returned (leading whitespace removed). If the line doesn't need to be split,
1687 // NULL is returned.
1688 char *split_str_once(char *src, int max_pixel_w)
1691 int i, w, len, last_was_white = 0;
1694 Assert(max_pixel_w > 0);
1696 gr_get_string_size(&w, NULL, src);
1697 if ( w <= max_pixel_w )
1698 return NULL; // string doesn't require a cut
1701 for (i=0; i<len; i++) {
1702 gr_get_string_size(&w, NULL, src, i);
1703 if ( w > max_pixel_w )
1706 if (src[i] == '\n') { // reached natural end of line
1711 if (is_white_space(src[i])) {
1712 if (!last_was_white)
1728 while (is_white_space(*src))
1732 return NULL; // end of the string anyway
1740 #define SPLIT_STR_BUFFER_SIZE 512
1742 // --------------------------------------------------------------------------------------
1745 // A general function that will split a string into several lines. Lines are allowed up
1746 // to max_pixel_w pixels. Breaks are found in white space.
1748 // Supports \n's in the strings!
1750 // parameters: src => source string to be broken up
1751 // max_pixel_w => max width of line in pixels
1752 // n_chars => output array that will hold number of characters in each line
1753 // p_str => output array of pointers to start of lines within src
1754 // max_lines => limit of number of lines to break src up into
1755 // ignore_char => OPTIONAL parameter (default val -1). Ignore words starting with this character
1756 // This is useful when you want to ignore embedded control information that starts
1757 // with a specific character, like $ or #
1759 // returns: number of lines src is broken into
1760 // -1 is returned when an error occurs
1762 int split_str(char *src, int max_pixel_w, int *n_chars, char **p_str, int max_lines, char ignore_char)
1764 char buffer[SPLIT_STR_BUFFER_SIZE];
1765 char *breakpoint = NULL;
1766 int sw, new_line = 1, line_num = 0, last_was_white = 0;
1767 int ignore_until_whitespace, buf_index;
1769 // check our assumptions..
1770 Assert(src != NULL);
1771 Assert(n_chars != NULL);
1772 Assert(p_str != NULL);
1773 Assert(max_lines > 0);
1774 Assert(max_pixel_w > 0);
1776 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1778 ignore_until_whitespace = 0;
1780 // get rid of any leading whitespace
1781 while (is_white_space(*src))
1787 // iterate through chars in line, keeping track of most recent "white space" location that can be used
1788 // as a line splitting point if necessary
1789 for (; *src; src++) {
1790 if (line_num >= max_lines)
1791 return line_num; // time to bail out
1793 // starting a new line of text, init stuff for that
1795 p_str[line_num] = NULL;
1796 if (is_gray_space(*src))
1799 p_str[line_num] = src;
1804 // maybe skip leading whitespace
1805 if (ignore_until_whitespace) {
1806 if ( is_white_space(*src) )
1807 ignore_until_whitespace = 0;
1812 // if we have a newline, split the line here
1814 n_chars[line_num] = src - p_str[line_num]; // track length of line
1816 p_str[line_num] = NULL;
1819 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1824 if (*src == ignore_char) {
1825 ignore_until_whitespace = 1;
1829 if (is_gray_space(*src)) {
1830 if (!last_was_white) // track at first whitespace in a series of whitespace
1836 // indicate next time around that this wasn't a whitespace character
1840 // throw it in our buffer
1841 buffer[buf_index] = *src;
1843 buffer[buf_index] = 0; // null terminate it
1845 gr_get_string_size(&sw, NULL, buffer);
1846 if (sw >= max_pixel_w) {
1850 end = src = breakpoint;
1853 end = src; // force a split here since to whitespace
1854 src--; // reuse this character in next line
1857 n_chars[line_num] = end - p_str[line_num]; // track length of line
1858 Assert(n_chars[line_num]);
1860 p_str[line_num] = NULL;
1863 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1869 if (p_str[line_num]) {
1870 n_chars[line_num] = src - p_str[line_num]; // track length of line
1871 Assert(n_chars[line_num]);