7 * low level parse routines common to all types of parsers
10 * Revision 1.3 2002/05/28 06:28:20 theoddone33
11 * Filesystem mods, actually reads some data files now
13 * Revision 1.2 2002/05/07 03:16:48 theoddone33
14 * The Great Newline Fix
16 * Revision 1.1.1.1 2002/05/03 03:28:10 root
20 * 12 8/10/99 6:54p Dave
21 * Mad optimizations. Added paging to the nebula effect.
23 * 11 6/02/99 2:22p Andsager
26 * 10 5/20/99 11:25a Andsager
27 * Added error checking for parsing table files.
29 * 9 2/23/99 11:18a Andsager
30 * Localize launcher using strings.tbl
32 * 8 2/03/99 6:06p Dave
33 * Groundwork for FS2 PXO usertracker support. Gametracker support next.
35 * 7 11/05/98 4:18p Dave
36 * First run nebula support. Beefed up localization a bit. Removed all
37 * conditional compiles for foreign versions. Modified mission file
40 * 6 10/29/98 12:49p Dave
41 * Intermediate checkin for Fred hash table stuff.
43 * 5 10/28/98 11:30a Dave
46 * 4 10/22/98 6:14p Dave
47 * Optimized some #includes in Anim folder. Put in the beginnings of
48 * parse/localization support for externalized strings and tstrings.tbl
50 * 3 10/14/98 1:15p Andsager
53 * 2 10/07/98 10:53a Dave
56 * 1 10/07/98 10:50a Dave
58 * 98 9/21/98 2:37p Adam
59 * fixed font translation bug
61 * 97 7/29/98 9:39a Hoffoss
62 * Fixed bug with stuff_and_malloc_string
64 * 96 6/22/98 11:18a Hoffoss
65 * Fixed bug where mission notes field gets an extra linefeed with each
68 * 95 6/19/98 3:53p Lawrance
69 * account for some new foreign chars
71 * 94 6/12/98 7:37p Hoffoss
72 * Made ß translate to ss, since we don't have this character in our font.
74 * 93 6/12/98 4:52p Hoffoss
75 * Added support for special characters in in forgeign languages.
77 * 92 5/21/98 3:26p Dave
78 * Fixed bug in new split string function. Newline as last character did
81 * 91 5/21/98 3:01p Dave
82 * Make sure split_str handles 0 length strings correctly.
84 * 90 5/21/98 2:14a Hoffoss
85 * Rewrote split_str() to solve a problem and it was too hard for me to
86 * figure out (oow, my head hurts still!)
102 #include "missionparse.h"
105 #include "localize.h"
108 #define ERROR_LENGTH 64
109 #define RS_MAX_TRIES 5
111 char Current_filename[128];
112 char Error_str[ERROR_LENGTH];
114 int Warning_count, Error_count;
115 int fred_parse_flag = 0;
116 int Token_found_flag;
119 char Mission_text[MISSION_TEXT_SIZE];
120 char Mission_text_raw[MISSION_TEXT_SIZE];
124 // Return true if this character is white space, else false.
125 int is_white_space(char ch)
127 return ((ch == ' ') || (ch == '\t') || (ch == EOLN));
130 // Returns true if this character is gray space, else false (gray space is white space except for EOLN).
131 int is_gray_space(char ch)
133 return ((ch == ' ') || (ch == '\t'));
136 // Advance global Mp (mission pointer) past all current white space.
137 // Leaves Mp pointing at first non white space character.
138 void ignore_white_space()
140 while ((*Mp != EOF_CHAR) && is_white_space(*Mp))
144 void ignore_gray_space()
146 while ((*Mp != EOF_CHAR) && is_gray_space(*Mp))
150 // Truncate *str, eliminating all trailing white space.
151 // Eg: "abc " becomes "abc"
152 // "abc abc " becomes "abc abc"
153 // "abc \t" becomes "abc"
154 void drop_trailing_white_space(char *str)
160 while ((i >= 0) && is_white_space(str[i]))
166 // Eliminate any leading whitespace in str
167 void drop_leading_white_space(char *str)
174 while ((i < len) && is_white_space(str[i]))
177 memmove(str, str+i, len-i);
181 // eliminates all leading and trailing white space from a string. Returns pointer passed in.
182 char *drop_white_space(char *str)
187 while (str[s] && is_white_space(str[s]))
192 if (!is_white_space(str[e]))
199 memmove(str, str + s, e - s + 1);
205 // Advances Mp past current token.
208 ignore_white_space();
210 while ((*Mp != EOF_CHAR) && !is_white_space(*Mp))
214 // Display a diagnostic message if Verbose is set.
215 // (Verbose is set if -v command line switch is present.)
216 void diag_printf(char *format, ...)
221 va_start(args, format);
222 vsprintf(buffer, format, args);
225 nprintf(("Parse", "%s", buffer));
228 // Grab and return (a pointer to) a bunch of tokens, terminating at
229 // ERROR_LENGTH chars, or end of line.
236 while (((ch = *pstr++) != EOLN) && (ch != EOF_CHAR) && (count < ERROR_LENGTH-1))
237 Error_str[count++] = ch;
239 Error_str[count] = 0;
243 // Return the line number given by the current mission pointer, ie Mp.
244 // A very slow function (scans all processed text), but who cares how long
245 // an error reporting function takes?
254 p = Mission_text_raw;
255 stoploc = (Mp - Mission_text) + p;
262 if ( !incomment && (*p == COMMENT_CHAR) )
265 if ( !incomment && (*p == '/') && (*(p+1) == '*') ) {
273 if ( multiline && (*(p-1) == '*') && (*p == '/') ) {
279 if ( !multiline && incomment )
288 // Call this function to display an error message.
289 // error_level == 0 means this is just a warning.
290 // !0 means it's an error message.
291 // Prints line number and other useful information.
292 void error_display(int error_level, char *format, ...)
295 char error_text[128];
298 if (error_level == 0) {
299 strcpy(error_text, "Warning");
302 strcpy(error_text, "Error");
306 nprintf((error_text, "%s(%i):%s: ", Current_filename, get_line_num(), error_text));
308 va_start(args, format);
309 vsprintf(buffer, format, args);
311 Assert(strlen(buffer) < 1024);
313 nprintf((error_text, "%s", buffer));
314 Warning(LOCATION, "%s(%i):\n%s: %s", Current_filename, get_line_num(), error_text, buffer);
317 // Advance Mp to the next eoln character.
318 void advance_to_eoln(char *more_terminators)
320 char terminators[128];
322 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
324 terminators[0] = EOLN;
325 terminators[1] = (char)EOF_CHAR;
326 if (more_terminators != NULL)
327 strcpy(&terminators[2], more_terminators);
331 while (strchr(terminators, *Mp) == NULL)
335 // Advance Mp to the next white space (ignoring white space inside of " marks)
336 void advance_to_next_white()
340 while ((*Mp != EOLN) && (*Mp != EOF_CHAR)) {
342 in_quotes = !in_quotes;
344 if (!in_quotes && is_white_space(*Mp))
350 // Search for specified string, skipping everything up to that point. Returns 1 if found,
351 // 0 if string wasn't found (and hit end of file), or -1 if not found, but end of checking
352 // block was reached.
353 int skip_to_string(char *pstr, char *end)
357 ignore_white_space();
362 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, len)) {
363 if (end && *Mp == '#')
366 if (end && !strnicmp(pstr, Mp, len2))
369 advance_to_eoln(NULL);
370 ignore_white_space();
373 if (!Mp || (*Mp == EOF_CHAR))
380 // Advance to start of either pstr1 or pstr2. Return 0 is successful, otherwise return !0
381 int skip_to_start_of_strings(char *pstr1, char *pstr2)
385 ignore_white_space();
386 len1 = strlen(pstr1);
387 len2 = strlen(pstr2);
389 while ( (*Mp != EOF_CHAR) && strnicmp(pstr1, Mp, len1) && strnicmp(pstr2, Mp, len2) ) {
390 advance_to_eoln(NULL);
391 ignore_white_space();
394 if (!Mp || (*Mp == EOF_CHAR))
400 // Find a required string.
401 // If not found, display an error message, but try up to RS_MAX_TRIES times
402 // to find the string. (This is the groundwork for ignoring non-understood
404 // If unable to find the required string after RS_MAX_TRIES tries, then
405 // abort using longjmp to parse_abort.
406 int required_string(char *pstr)
410 ignore_white_space();
412 while (strnicmp(pstr, Mp, strlen(pstr)) && (count < RS_MAX_TRIES)) {
413 error_display(1, "Required token = [%s], found [%.32s].\n", pstr, next_tokens());
414 advance_to_eoln(NULL);
415 ignore_white_space();
419 if (count == RS_MAX_TRIES) {
420 nprintf(("Error", "Error: Unable to find required token [%s]\n", pstr));
421 Warning(LOCATION, "Error: Unable to find required token [%s]\n", pstr);
422 longjmp(parse_abort, 1);
426 diag_printf("Found required string [%s]\n", token_found = pstr);
430 // similar to optional_string, but just checks if next token is a match.
431 // It doesn't advance Mp.
433 int check_for_string(char *pstr)
435 ignore_white_space();
437 if (!strnicmp(pstr, Mp, strlen(pstr)))
443 // like check for string, but doesn't skip past any whitespace
444 int check_for_string_raw(char *pstr)
446 if (!strnicmp(pstr, Mp, strlen(pstr))){
453 // Find an optional string.
454 // If found, return 1, else return 0.
455 // If found, point past string, else don't update pointer.
456 int optional_string(char *pstr)
458 ignore_white_space();
460 if (!strnicmp(pstr, Mp, strlen(pstr))) {
468 int required_string_fred(char *pstr, char *end)
476 ignore_white_space();
477 while (*Mp != EOF_CHAR && strnicmp(pstr, Mp, strlen(pstr))) {
478 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end)))) {
483 advance_to_eoln(NULL);
484 ignore_white_space();
487 if (!Mp || (*Mp == EOF_CHAR)) {
488 diag_printf("Required string [%s] not found\n", pstr);
490 Token_found_flag = 0;
495 diag_printf("Found required string [%s]\n", pstr);
496 Token_found_flag = 1;
500 // attempt to find token in buffer. It might not exist, however, in which case we don't need
501 // to do anything. If it is found, then we advance the pointer to just after the token. To
502 // further complicate things, we should only search to a certain point, since we don't want
503 // a token that belongs to another section which might match the token we want. Thus, we
504 // also pass in an ending token, which marks the point we should stop looking at.
505 int optional_string_fred(char *pstr, char *end, char *end2)
513 ignore_white_space();
514 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, strlen(pstr))) {
515 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end))) ||
516 (end2 && !strnicmp(end2, Mp, strlen(end2)))) {
521 advance_to_eoln(NULL);
522 ignore_white_space();
525 if (!Mp || (*Mp == EOF_CHAR)) {
526 diag_printf("Optional string [%s] not found\n", pstr);
528 Token_found_flag = 0;
533 diag_printf("Found optional string [%s]\n", pstr);
534 Token_found_flag = 1;
538 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
539 // Does not update Mp if token found. If not found, advances, trying to
540 // find the string. Doesn't advance past the found string.
541 int required_string_either(char *str1, char *str2)
545 ignore_white_space();
547 while (count < RS_MAX_TRIES) {
548 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
549 // Mp += strlen(str1);
550 diag_printf("Found required string [%s]\n", token_found = str1);
552 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
553 // Mp += strlen(str2);
554 diag_printf("Found required string [%s]\n", token_found = str2);
558 error_display(1, "Required token = [%s] or [%s], found [%.32s].\n", str1, str2, next_tokens());
560 advance_to_eoln(NULL);
561 ignore_white_space();
565 if (count == RS_MAX_TRIES) {
566 nprintf(("Error", "Error: Unable to find either required token [%s] or [%s]\n", str1, str2));
567 Warning(LOCATION, "Error: Unable to find either required token [%s] or [%s]\n", str1, str2);
568 longjmp(parse_abort, 2);
575 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
576 // Does not update Mp if token found. If not found, advances, trying to
577 // find the string. Doesn't advance past the found string.
578 int required_string_3(char *str1, char *str2, char *str3)
582 ignore_white_space();
584 while (count < RS_MAX_TRIES) {
585 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
586 // Mp += strlen(str1);
587 diag_printf("Found required string [%s]\n", token_found = str1);
589 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
590 // Mp += strlen(str2);
591 diag_printf("Found required string [%s]\n", token_found = str2);
593 } else if (strnicmp(str3, Mp, strlen(str3)) == 0) {
594 diag_printf("Found required string [%s]\n", token_found = str3);
598 error_display(1, "Required token = [%s], [%s] or [%s], found [%.32s].\n", str1, str2, str3, next_tokens());
600 advance_to_eoln(NULL);
601 ignore_white_space();
609 int required_string_either_fred(char *str1, char *str2)
611 ignore_white_space();
613 while (*Mp != EOF_CHAR) {
614 if (!strnicmp(str1, Mp, strlen(str1))) {
615 // Mp += strlen(str1);
616 diag_printf("Found required string [%s]\n", token_found = str1);
617 return fred_parse_flag = 0;
619 } else if (!strnicmp(str2, Mp, strlen(str2))) {
620 // Mp += strlen(str2);
621 diag_printf("Found required string [%s]\n", token_found = str2);
622 return fred_parse_flag = 1;
625 advance_to_eoln(NULL);
626 ignore_white_space();
630 diag_printf("Unable to find either required token [%s] or [%s]\n", str1, str2);
636 // Copy characters from instr to outstr until eoln is found, or until max
637 // characters have been copied (including terminator).
638 void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max)
642 char terminators[128];
644 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
646 terminators[0] = EOLN;
647 terminators[1] = (char)EOF_CHAR;
648 if (more_terminators != NULL)
649 strcpy(&terminators[2], more_terminators);
653 while (((ch = *instr++) != 0) && (strchr(terminators, ch) == NULL) && (count < max)) {
659 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
664 // Copy characters from instr to outstr until next white space is found, or until max
665 // characters have been copied (including terminator).
666 void copy_to_next_white(char *outstr, char *instr, int max)
672 while (((ch = *instr++)>0) && (ch != EOLN) && (ch != EOF_CHAR) && (count < max)) {
674 in_quotes = !in_quotes;
677 if ( !in_quotes && is_white_space(ch) ) // not in quotes, white space terminates string
684 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
689 // Copy text until a certain string is matched.
690 // For example, this is used to copy mission notes, scanning until $END NOTES:
692 void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars)
696 foundstr = strstr(instr, endstr);
698 if (foundstr == NULL) {
699 nprintf(("Error", "Error. Looking for [%s], but never found it.\n", endstr));
700 longjmp(parse_abort, 3);
703 if (foundstr - instr + strlen(endstr) < (uint) max_chars) {
704 strncpy(outstr, instr, foundstr - instr);
705 outstr[foundstr - instr] = 0;
708 nprintf(("Error", "Error. Too much text (%i chars, %i allowed) before %s\n",
709 foundstr - instr - strlen(endstr), max_chars, endstr));
711 longjmp(parse_abort, 4);
714 diag_printf("Here's the partial wad of text:\n%.30s", outstr);
717 // stuffs a string into a buffer. Can get a string between " marks and stops
718 // when whitespace in encounted -- not to end of line
719 void stuff_string_white(char *pstr)
721 ignore_white_space();
722 copy_to_next_white(pstr, Mp, NAME_LENGTH-1);
723 advance_to_next_white();
726 // Stuff a string into a string buffer.
727 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
728 // the default string length if using the F_NAME case.
729 void stuff_string(char *pstr, int type, char *terminators, int len)
731 char read_str[2048] = "";
739 final_len = NAME_LENGTH;
742 copy_to_eoln(read_str, terminators, Mp, read_len);
743 drop_trailing_white_space(read_str);
744 advance_to_eoln(terminators);
749 final_len = DATE_LENGTH;
750 copy_to_eoln(read_str, terminators, Mp, read_len);
751 drop_trailing_white_space(read_str);
752 advance_to_eoln(terminators);
756 ignore_white_space();
757 final_len = NOTES_LENGTH;
758 copy_text_until(read_str, Mp, "$End Notes:", read_len);
759 Mp += strlen(read_str);
760 required_string("$End Notes:");
765 final_len = FILESPEC_LENGTH;
766 copy_to_eoln(read_str, terminators, Mp, read_len);
767 drop_trailing_white_space(read_str);
768 advance_to_eoln(terminators);
770 // F_MULTITEXTOLD keeping for backwards compatability with old missions
771 // can be deleted once all missions are using new briefing format
774 ignore_white_space();
775 final_len = NOTES_LENGTH;
776 copy_text_until(read_str, Mp, "$End Briefing Text:", read_len);
777 Mp += strlen(read_str);
778 required_string("$End Briefing Text:");
783 final_len = MULTITEXT_LENGTH;
785 ignore_white_space();
786 copy_text_until(read_str, Mp, "$end_multi_text", read_len);
787 Mp += strlen(read_str);
788 drop_trailing_white_space(read_str);
789 required_string("$end_multi_text");
794 final_len = PATHNAME_LENGTH;
795 copy_to_eoln(read_str, terminators, Mp, read_len);
796 drop_trailing_white_space(read_str);
797 advance_to_eoln(terminators);
802 final_len = MESSAGE_LENGTH;
803 copy_to_eoln(read_str, terminators, Mp, read_len);
804 drop_trailing_white_space(read_str);
805 advance_to_eoln(terminators);
812 // now we want to do any final localization
813 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
815 // if the hash localized text hash table is active and we have a valid external string - hash it
816 if(fhash_active() && (tag_id > -2)){
817 fhash_add_str(pstr, tag_id);
820 diag_printf("Stuffed string = [%.30s]\n", pstr);
823 // stuff a string, but only until the end of a line. don't ignore leading whitespace. close analog of fgets()/cfgets()
824 void stuff_string_line(char *pstr, int len)
826 char read_str[2048] = "";
833 copy_to_eoln(read_str, "\n", Mp, read_len);
834 drop_trailing_white_space(read_str);
838 // now we want to do any final localization
839 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
841 // if the hash localized text hash table is active and we have a valid external string - hash it
842 if(fhash_active() && (tag_id > -2)){
843 fhash_add_str(pstr, tag_id);
846 diag_printf("Stuffed string = [%.30s]\n", pstr);
849 // 1K on the stack? seems to work...
850 // JH: 1k isn't enough! Command briefs can be 16k max, so changed this.
851 #define MAX_TMP_STRING_LENGTH 16384
853 // Exactly the same as stuff string only Malloc's the buffer.
854 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
855 // the default string length if using the F_NAME case.
856 char *stuff_and_malloc_string( int type, char *terminators, int len)
860 char tmp_result[MAX_TMP_STRING_LENGTH];
862 stuff_string(tmp_result, type, terminators, len);
863 drop_white_space(tmp_result);
865 l = strlen(tmp_result);
866 Assert(l < MAX_TMP_STRING_LENGTH); // Get John!!
870 return strdup(tmp_result);
873 // After reading a multitext string, you can call this function to convert any newlines into
874 // spaces, so it's a one paragraph string (I.e. as in MS-Word).
876 void compact_multitext_string(char *str)
880 for (i=0; i<strlen(str); i++)
885 #define BUF_SIZE 2048
887 // Strip comments from a line of input.
888 int strip_comments_fred(char *readp, int in_comment)
891 char *writep = readp;
893 while ((ch = *readp) != COMMENT_CHAR) {
914 // Strip comments from a line of input.
915 int strip_comments(char *readp, int in_comment)
918 char *writep = readp;
920 while ((ch = *readp) != COMMENT_CHAR) {
927 // time to do some special foreign character conversion
1044 void strip_all_comments( char *readp, char *writep )
1047 //char *writep = readp;
1049 while ( *readp != EOF_CHAR ) {
1051 if ( ch == COMMENT_CHAR ) {
1052 while ( *readp != EOLN )
1057 // get to next character after EOLN
1059 } else if ( (ch == '/') && (readp[1] == '*')) { // Start of multi-line comment
1064 while ( *readp != '*' )
1066 if ( readp[1] == '/' ) {
1080 *writep = (char)EOF_CHAR;
1084 int parse_get_line(char *lineout, int max_line_len, char *start, int max_size, char *cur)
1087 int i, num_chars_read=0;
1091 for ( i = 0; i < max_line_len-1; i++ ) {
1093 if ( (cur - start) >= max_size ) {
1095 if ( lineout > t ) {
1096 return num_chars_read;
1103 } while ( c == 13 );
1106 if ( c=='\n' ) break;
1110 return num_chars_read;
1113 // Read mission text, stripping comments.
1114 // When a comment is found, it is removed. If an entire line
1115 // consisted of a comment, a blank line is left in the input file.
1116 void read_file_text(char *filename, int mode)
1119 char outbuf[BUF_SIZE], *str;
1120 char *mp = Mission_text;
1121 char *mp2 = Mission_text_raw;
1122 int file_is_encrypted = 0, in_comment = 0;
1125 longjmp(parse_abort, 10);
1127 strcpy(Current_filename, filename);
1128 mf = cfopen(filename, "rb", CFILE_NORMAL, mode);
1130 nprintf(("Error", "Wokka! Error opening mission.txt!\n"));
1132 nprintf (("DDOI", "Which is really %s!\n",filename));
1135 longjmp(parse_abort, 5);
1138 // read the entire file in
1139 int file_len = cfilelength(mf);
1141 // read first 10 bytes to determine if file is encrypted
1142 cfread(Mission_text_raw, min(file_len, 10), 1, mf);
1143 file_is_encrypted = is_encrpyted(Mission_text_raw);
1144 cfseek(mf, 0, CF_SEEK_SET);
1146 if ( file_is_encrypted ) {
1147 int unscrambled_len;
1148 char *scrambled_text;
1149 scrambled_text = (char*)malloc(file_len+1);
1150 Assert(scrambled_text);
1151 cfread(scrambled_text, file_len, 1, mf);
1153 unencrypt(scrambled_text, file_len, Mission_text_raw, &unscrambled_len);
1154 file_len = unscrambled_len;
1155 free(scrambled_text);
1157 cfread(Mission_text_raw, file_len, 1, mf);
1162 // If you hit this assert, it is probably telling you the obvious. The file
1163 // you are trying to read is truly too large. Look at *filename to see the file name.
1164 Assert(file_len < MISSION_TEXT_SIZE);
1166 // strip comments from raw text, reading into Mission_text
1168 int num_chars_read = 0;
1170 mp2 = Mission_text_raw;
1171 while ( (num_chars_read = parse_get_line(outbuf, BUF_SIZE, Mission_text_raw, file_len, mp2)) != 0 ) {
1172 mp2 += num_chars_read;
1175 in_comment = strip_comments_fred(outbuf, in_comment);
1177 in_comment = strip_comments(outbuf, in_comment);
1190 // strcpy(mp, outbuf);
1191 // mp += strlen(outbuf);
1194 *mp = *mp2 = (char)EOF_CHAR;
1196 while (cfgets(outbuf, BUF_SIZE, mf) != NULL) {
1197 if (strlen(outbuf) >= BUF_SIZE-1)
1198 error_display(0, "Input string too long. Max is %i characters.\n%.256s\n", BUF_SIZE, outbuf);
1200 // If you hit this assert, it is probably telling you the obvious. The file
1201 // you are trying to read is truly too large. Look at *filename to see the file name.
1202 Assert(mp2 - Mission_text_raw + strlen(outbuf) < MISSION_TEXT_SIZE);
1203 strcpy(mp2, outbuf);
1204 mp2 += strlen(outbuf);
1206 in_comment = strip_comments(outbuf, in_comment);
1208 mp += strlen(outbuf);
1211 *mp = *mp2 = (char)EOF_CHAR;
1216 void debug_show_mission_text()
1218 char *mp = Mission_text;
1221 while ((ch = *mp++) != EOF_CHAR)
1230 ignore_white_space();
1234 if ((ch != '.') && (ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1235 error_display(1, "Expecting float, found [%.32s].\n", next_tokens());
1239 return (float)atof(Mp);
1249 ignore_white_space();
1253 if ((ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1254 error_display(1, "Expecting int, found [%.32s].\n", next_tokens());
1263 // Stuff a floating point value pointed at by Mp.
1264 // Advances past float characters.
1265 void stuff_float(float *f)
1272 Mp += strspn(Mp, "+-0123456789.");
1277 diag_printf("Stuffed float: %f\n", *f);
1280 // Stuff an integer value pointed at by Mp.
1281 // Advances past integer characters.
1282 void stuff_int(int *i)
1289 Mp += strspn(Mp, "+-0123456789");
1294 diag_printf("Stuffed int: %i\n", *i);
1298 // Stuffs a boolean value pointed at by Mp.
1299 // YES/NO (supporting 1/0 now as well)
1301 void stuff_boolean(int *i)
1304 ignore_white_space();
1305 copy_to_eoln(token, NULL, Mp, 512);
1306 drop_trailing_white_space(token);
1307 advance_to_eoln(NULL);
1309 if ( isdigit(token[0]) ) { // using 1/0 instead of YES/NO
1311 int_value = atoi(token);
1318 if ( !stricmp(token, "yes") ) {
1321 else if ( !stricmp(token, "no") ) {
1325 Assert(0); // can't happen
1330 // Stuff an integer value pointed at by Mp.
1331 // Advances past integer characters.
1332 void stuff_byte(ubyte *i)
1343 Mp += strspn(Mp, "+-0123456789");
1348 diag_printf("Stuffed byte: %i\n", *i);
1351 // Stuffs a list of strings
1352 int stuff_string_list(char slp[][NAME_LENGTH], int max_strings)
1355 ignore_white_space();
1358 error_display(1, "Reading string list. Found [%c]. Expecting '('.\n", *Mp);
1359 longjmp(parse_abort, 100);
1364 ignore_white_space();
1366 while (*Mp != ')') {
1367 assert ( count < max_strings );
1368 Assert ( *Mp == '\"' ); // should always be enclosed in quotes
1370 get_string( slp[count++] );
1371 ignore_white_space();
1379 // Stuffs an integer list.
1380 // This is of the form ( i* )
1381 // where i is an integer.
1382 // For example, (1) () (1 2 3) ( 1 ) are legal integer lists.
1383 int stuff_int_list(int *ilp, int max_ints, int lookup_type)
1385 int count = 0, ok_flag = 1, dummy;
1386 ignore_white_space();
1389 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1390 longjmp(parse_abort, 6);
1394 ignore_white_space();
1396 while (*Mp != ')') {
1397 Assert(count < max_ints);
1403 switch (lookup_type) {
1405 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1408 case SHIP_INFO_TYPE:
1410 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1415 case WEAPON_POOL_TYPE:
1417 num = weapon_info_lookup(str);
1422 case WEAPON_LIST_TYPE:
1423 num = weapon_info_lookup(str);
1428 case RAW_INTEGER_TYPE:
1433 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1439 Error(LOCATION, "Unable to find string \"%s\" in stuff_int_list\n\nMany possible sources for this error. Get a programmer!\n", str);
1440 } else if (num == -2) {
1441 if (strlen(str) > 0) {
1442 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);
1446 if (num < 0) // other negatives used to bypass the above error trap, but should be -1
1454 stuff_int(&ilp[count++]);
1459 ignore_white_space();
1467 // Marks an integer list.
1468 // This is of the form ( i* )
1469 // where i is an integer.
1470 // If a specified string is found in the lookup and its value is 7, then the 7th value
1471 // in the array is set.
1472 void mark_int_list(int *ilp, int max_ints, int lookup_type)
1474 ignore_white_space();
1477 error_display(1, "Marking integer list. Found [%c]. Expecting '('.\n", *Mp);
1478 longjmp(parse_abort, 6);
1482 ignore_white_space();
1484 while (*Mp != ')') {
1490 switch(lookup_type) {
1492 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1495 case SHIP_INFO_TYPE:
1496 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1499 case WEAPON_LIST_TYPE:
1500 num = weapon_info_lookup(str);
1504 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1508 if ( (num < 0) || (num >= max_ints) )
1509 Error(LOCATION, "Unable to find string \"%s\" in mark_int_list.\n", str);
1517 Assert((tval >= 0) && (tval < max_ints));
1521 ignore_white_space();
1529 // Stuff a vector, which is 3 floats.
1530 void stuff_vector(vector *vp)
1532 stuff_float(&vp->x);
1533 stuff_float(&vp->y);
1534 stuff_float(&vp->z);
1537 void stuff_parenthesized_vector(vector *vp)
1539 ignore_white_space();
1542 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting '('.\n", *Mp);
1543 longjmp(parse_abort, 11);
1547 ignore_white_space();
1549 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting ')'.\n", *Mp);
1550 longjmp(parse_abort, 12);
1557 // Stuffs vector list.
1558 // This is of the form ( (vector)* )
1559 // where vector is a vector
1560 // For example, ( (1 2 3) (2 3 4) (2 3 5) )
1561 // is a vector list of three vectors.
1562 int stuff_vector_list(vector *vlp, int max_vecs)
1566 ignore_white_space();
1569 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1570 longjmp(parse_abort, 6);
1575 ignore_white_space();
1577 while (*Mp != ')') {
1578 Assert(count < max_vecs);
1579 stuff_parenthesized_vector(&vlp[count++]);
1581 ignore_white_space();
1590 // Stuff a matrix, which is 3 vectors.
1591 void stuff_matrix(matrix *mp)
1593 stuff_vector(&mp->rvec);
1594 stuff_vector(&mp->uvec);
1595 stuff_vector(&mp->fvec);
1599 // Given a string, find it in a string array.
1600 // *descrtiption is only used for diagnostics in case it can't be found.
1601 // *str1 is the string to be found.
1602 // *strlist is the list of strings to search.
1603 // max is the number of entries in *strlist to scan.
1604 int string_lookup(char *str1, char *strlist[], int max, char *description, int say_errors)
1608 for (i=0; i<max; i++) {
1609 Assert(strlen(strlist[i]) != 0);
1611 if (!stricmp(str1, strlist[i]))
1616 error_display(0, "Unable to find [%s] in %s list.\n", str1, description);
1621 // Find a required string (*id), then stuff the text of type f_type that
1622 // follows it at *addr. *strlist[] contains the strings it should try to
1624 void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
1628 required_string(id);
1629 stuff_string(token, f_type, NULL);
1630 *addr = string_lookup(token, strlist, max, description, 1);
1633 // Mp points at a string.
1634 // Find the string in the list of strings *strlist[].
1635 // Returns the index of the match, -1 if none.
1636 int match_and_stuff(int f_type, char *strlist[], int max, char *description)
1640 stuff_string(token, f_type, NULL);
1641 return string_lookup(token, strlist, max, description, 0);
1644 void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total,
1645 int max, char *description)
1650 required_string(id);
1651 stuff_string(token, f_type, NULL);
1653 *addr = string_lookup(token, strlist, *total, description, 0);
1655 if (*addr == -1) // not in list, so lets try and add it.
1657 Assert(*total < max);
1658 strcpy(strlist[*total], token);
1663 // Initialize a parse process.
1672 for (i=0; i<MAX_CARGO; i++)
1673 Cargo_names[i] = Cargo_names_buf[i]; // make a pointer array for compatibility
1675 Total_goal_ship_names = 0;
1687 // Display number of warnings and errors at the end of a parse.
1688 void display_parse_diagnostics()
1690 nprintf(("Parse", "\nParse complete.\n"));
1691 nprintf(("Parse", "%i errors. %i warnings.\n", Error_count, Warning_count));
1694 // Splits a string into 2 lines if the string is wider than max_pixel_w pixels. A null
1695 // terminator is placed where required to make the first line <= max_pixel_w. The remaining
1696 // text is returned (leading whitespace removed). If the line doesn't need to be split,
1697 // NULL is returned.
1698 char *split_str_once(char *src, int max_pixel_w)
1701 int i, w, len, last_was_white = 0;
1704 Assert(max_pixel_w > 0);
1706 gr_get_string_size(&w, NULL, src);
1707 if ( w <= max_pixel_w )
1708 return NULL; // string doesn't require a cut
1711 for (i=0; i<len; i++) {
1712 gr_get_string_size(&w, NULL, src, i);
1713 if ( w > max_pixel_w )
1716 if (src[i] == '\n') { // reached natural end of line
1721 if (is_white_space(src[i])) {
1722 if (!last_was_white)
1738 while (is_white_space(*src))
1742 return NULL; // end of the string anyway
1750 #define SPLIT_STR_BUFFER_SIZE 512
1752 // --------------------------------------------------------------------------------------
1755 // A general function that will split a string into several lines. Lines are allowed up
1756 // to max_pixel_w pixels. Breaks are found in white space.
1758 // Supports \n's in the strings!
1760 // parameters: src => source string to be broken up
1761 // max_pixel_w => max width of line in pixels
1762 // n_chars => output array that will hold number of characters in each line
1763 // p_str => output array of pointers to start of lines within src
1764 // max_lines => limit of number of lines to break src up into
1765 // ignore_char => OPTIONAL parameter (default val -1). Ignore words starting with this character
1766 // This is useful when you want to ignore embedded control information that starts
1767 // with a specific character, like $ or #
1769 // returns: number of lines src is broken into
1770 // -1 is returned when an error occurs
1772 int split_str(char *src, int max_pixel_w, int *n_chars, char **p_str, int max_lines, char ignore_char)
1774 char buffer[SPLIT_STR_BUFFER_SIZE];
1775 char *breakpoint = NULL;
1776 int sw, new_line = 1, line_num = 0, last_was_white = 0;
1777 int ignore_until_whitespace, buf_index;
1779 // check our assumptions..
1780 Assert(src != NULL);
1781 Assert(n_chars != NULL);
1782 Assert(p_str != NULL);
1783 Assert(max_lines > 0);
1784 Assert(max_pixel_w > 0);
1786 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1788 ignore_until_whitespace = 0;
1790 // get rid of any leading whitespace
1791 while (is_white_space(*src))
1797 // iterate through chars in line, keeping track of most recent "white space" location that can be used
1798 // as a line splitting point if necessary
1799 for (; *src; src++) {
1800 if (line_num >= max_lines)
1801 return line_num; // time to bail out
1803 // starting a new line of text, init stuff for that
1805 p_str[line_num] = NULL;
1806 if (is_gray_space(*src))
1809 p_str[line_num] = src;
1814 // maybe skip leading whitespace
1815 if (ignore_until_whitespace) {
1816 if ( is_white_space(*src) )
1817 ignore_until_whitespace = 0;
1822 // if we have a newline, split the line here
1824 n_chars[line_num] = src - p_str[line_num]; // track length of line
1826 p_str[line_num] = NULL;
1829 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1834 if (*src == ignore_char) {
1835 ignore_until_whitespace = 1;
1839 if (is_gray_space(*src)) {
1840 if (!last_was_white) // track at first whitespace in a series of whitespace
1846 // indicate next time around that this wasn't a whitespace character
1850 // throw it in our buffer
1851 buffer[buf_index] = *src;
1853 buffer[buf_index] = 0; // null terminate it
1855 gr_get_string_size(&sw, NULL, buffer);
1856 if (sw >= max_pixel_w) {
1860 end = src = breakpoint;
1863 end = src; // force a split here since to whitespace
1864 src--; // reuse this character in next line
1867 n_chars[line_num] = end - p_str[line_num]; // track length of line
1868 Assert(n_chars[line_num]);
1870 p_str[line_num] = NULL;
1873 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1879 if (p_str[line_num]) {
1880 n_chars[line_num] = src - p_str[line_num]; // track length of line
1881 Assert(n_chars[line_num]);