2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
15 * low level parse routines common to all types of parsers
18 * Revision 1.7 2003/05/25 02:30:43 taylor
21 * Revision 1.6 2002/06/17 06:15:25 relnev
22 * ryan's struct patch (and cr removal)
24 * Revision 1.5 2002/06/09 04:41:25 relnev
25 * added copyright header
27 * Revision 1.4 2002/05/28 06:45:38 theoddone33
30 * Revision 1.3 2002/05/28 06:28:20 theoddone33
31 * Filesystem mods, actually reads some data files now
33 * Revision 1.2 2002/05/07 03:16:48 theoddone33
34 * The Great Newline Fix
36 * Revision 1.1.1.1 2002/05/03 03:28:10 root
40 * 12 8/10/99 6:54p Dave
41 * Mad optimizations. Added paging to the nebula effect.
43 * 11 6/02/99 2:22p Andsager
46 * 10 5/20/99 11:25a Andsager
47 * Added error checking for parsing table files.
49 * 9 2/23/99 11:18a Andsager
50 * Localize launcher using strings.tbl
52 * 8 2/03/99 6:06p Dave
53 * Groundwork for FS2 PXO usertracker support. Gametracker support next.
55 * 7 11/05/98 4:18p Dave
56 * First run nebula support. Beefed up localization a bit. Removed all
57 * conditional compiles for foreign versions. Modified mission file
60 * 6 10/29/98 12:49p Dave
61 * Intermediate checkin for Fred hash table stuff.
63 * 5 10/28/98 11:30a Dave
66 * 4 10/22/98 6:14p Dave
67 * Optimized some #includes in Anim folder. Put in the beginnings of
68 * parse/localization support for externalized strings and tstrings.tbl
70 * 3 10/14/98 1:15p Andsager
73 * 2 10/07/98 10:53a Dave
76 * 1 10/07/98 10:50a Dave
78 * 98 9/21/98 2:37p Adam
79 * fixed font translation bug
81 * 97 7/29/98 9:39a Hoffoss
82 * Fixed bug with stuff_and_malloc_string
84 * 96 6/22/98 11:18a Hoffoss
85 * Fixed bug where mission notes field gets an extra linefeed with each
88 * 95 6/19/98 3:53p Lawrance
89 * account for some new foreign chars
91 * 94 6/12/98 7:37p Hoffoss
92 * Made ß translate to ss, since we don't have this character in our font.
94 * 93 6/12/98 4:52p Hoffoss
95 * Added support for special characters in in forgeign languages.
97 * 92 5/21/98 3:26p Dave
98 * Fixed bug in new split string function. Newline as last character did
101 * 91 5/21/98 3:01p Dave
102 * Make sure split_str handles 0 length strings correctly.
104 * 90 5/21/98 2:14a Hoffoss
105 * Rewrote split_str() to solve a problem and it was too hard for me to
106 * figure out (oow, my head hurts still!)
122 #include "missionparse.h"
125 #include "localize.h"
128 #define ERROR_LENGTH 64
129 #define RS_MAX_TRIES 5
131 char Current_filename[128];
132 char Error_str[ERROR_LENGTH];
134 int Warning_count, Error_count;
135 int fred_parse_flag = 0;
136 int Token_found_flag;
139 char Mission_text[MISSION_TEXT_SIZE];
140 char Mission_text_raw[MISSION_TEXT_SIZE];
144 // Return true if this character is white space, else false.
145 int is_white_space(char ch)
147 return ((ch == ' ') || (ch == '\t') || (ch == EOLN));
150 // Returns true if this character is gray space, else false (gray space is white space except for EOLN).
151 int is_gray_space(char ch)
153 return ((ch == ' ') || (ch == '\t'));
156 // Advance global Mp (mission pointer) past all current white space.
157 // Leaves Mp pointing at first non white space character.
158 void ignore_white_space()
160 while ((*Mp != EOF_CHAR) && is_white_space(*Mp))
164 void ignore_gray_space()
166 while ((*Mp != EOF_CHAR) && is_gray_space(*Mp))
170 // Truncate *str, eliminating all trailing white space.
171 // Eg: "abc " becomes "abc"
172 // "abc abc " becomes "abc abc"
173 // "abc \t" becomes "abc"
174 void drop_trailing_white_space(char *str)
180 while ((i >= 0) && is_white_space(str[i]))
186 // Eliminate any leading whitespace in str
187 void drop_leading_white_space(char *str)
194 while ((i < len) && is_white_space(str[i]))
197 memmove(str, str+i, len-i);
201 // eliminates all leading and trailing white space from a string. Returns pointer passed in.
202 char *drop_white_space(char *str)
207 while (str[s] && is_white_space(str[s]))
212 if (!is_white_space(str[e]))
219 memmove(str, str + s, e - s + 1);
225 // Advances Mp past current token.
228 ignore_white_space();
230 while ((*Mp != EOF_CHAR) && !is_white_space(*Mp))
234 // Display a diagnostic message if Verbose is set.
235 // (Verbose is set if -v command line switch is present.)
236 void diag_printf(char *format, ...)
241 va_start(args, format);
242 vsprintf(buffer, format, args);
245 nprintf(("Parse", "%s", buffer));
248 // Grab and return (a pointer to) a bunch of tokens, terminating at
249 // ERROR_LENGTH chars, or end of line.
256 while (((ch = *pstr++) != EOLN) && (ch != EOF_CHAR) && (count < ERROR_LENGTH-1))
257 Error_str[count++] = ch;
259 Error_str[count] = 0;
263 // Return the line number given by the current mission pointer, ie Mp.
264 // A very slow function (scans all processed text), but who cares how long
265 // an error reporting function takes?
274 p = Mission_text_raw;
275 stoploc = (Mp - Mission_text) + p;
282 if ( !incomment && (*p == COMMENT_CHAR) )
285 if ( !incomment && (*p == '/') && (*(p+1) == '*') ) {
293 if ( multiline && (*(p-1) == '*') && (*p == '/') ) {
299 if ( !incomment && (*p == '/') && (*(p+1) == '/') )
304 if ( !multiline && incomment )
313 // Call this function to display an error message.
314 // error_level == 0 means this is just a warning.
315 // !0 means it's an error message.
316 // Prints line number and other useful information.
317 void error_display(int error_level, char *format, ...)
320 char error_text[128];
323 if (error_level == 0) {
324 strcpy(error_text, "Warning");
327 strcpy(error_text, "Error");
331 nprintf((error_text, "%s(%i):%s: ", Current_filename, get_line_num(), error_text));
333 va_start(args, format);
334 vsprintf(buffer, format, args);
336 Assert(strlen(buffer) < 1024);
338 nprintf((error_text, "%s", buffer));
339 Warning(LOCATION, "%s(%i):\n%s: %s", Current_filename, get_line_num(), error_text, buffer);
342 // Advance Mp to the next eoln character.
343 void advance_to_eoln(char *more_terminators)
345 char terminators[128];
347 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
349 terminators[0] = EOLN;
350 terminators[1] = (char)EOF_CHAR;
351 if (more_terminators != NULL)
352 strcpy(&terminators[2], more_terminators);
356 while (strchr(terminators, *Mp) == NULL)
360 // Advance Mp to the next white space (ignoring white space inside of " marks)
361 void advance_to_next_white()
365 while ((*Mp != EOLN) && (*Mp != EOF_CHAR)) {
367 in_quotes = !in_quotes;
369 if (!in_quotes && is_white_space(*Mp))
375 // Search for specified string, skipping everything up to that point. Returns 1 if found,
376 // 0 if string wasn't found (and hit end of file), or -1 if not found, but end of checking
377 // block was reached.
378 int skip_to_string(char *pstr, char *end)
382 ignore_white_space();
387 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, len)) {
388 if (end && *Mp == '#')
391 if (end && !strnicmp(pstr, Mp, len2))
394 advance_to_eoln(NULL);
395 ignore_white_space();
398 if (!Mp || (*Mp == EOF_CHAR))
405 // Advance to start of either pstr1 or pstr2. Return 0 is successful, otherwise return !0
406 int skip_to_start_of_strings(char *pstr1, char *pstr2)
410 ignore_white_space();
411 len1 = strlen(pstr1);
412 len2 = strlen(pstr2);
414 while ( (*Mp != EOF_CHAR) && strnicmp(pstr1, Mp, len1) && strnicmp(pstr2, Mp, len2) ) {
415 advance_to_eoln(NULL);
416 ignore_white_space();
419 if (!Mp || (*Mp == EOF_CHAR))
425 // Find a required string.
426 // If not found, display an error message, but try up to RS_MAX_TRIES times
427 // to find the string. (This is the groundwork for ignoring non-understood
429 // If unable to find the required string after RS_MAX_TRIES tries, then
430 // abort using longjmp to parse_abort.
431 int required_string(char *pstr)
435 ignore_white_space();
437 while (strnicmp(pstr, Mp, strlen(pstr)) && (count < RS_MAX_TRIES)) {
438 error_display(1, "Required token = [%s], found [%.32s].\n", pstr, next_tokens());
439 advance_to_eoln(NULL);
440 ignore_white_space();
444 if (count == RS_MAX_TRIES) {
445 nprintf(("Error", "Error: Unable to find required token [%s]\n", pstr));
446 Warning(LOCATION, "Error: Unable to find required token [%s]\n", pstr);
447 longjmp(parse_abort, 1);
451 diag_printf("Found required string [%s]\n", token_found = pstr);
455 // similar to optional_string, but just checks if next token is a match.
456 // It doesn't advance Mp.
458 int check_for_string(char *pstr)
460 ignore_white_space();
462 if (!strnicmp(pstr, Mp, strlen(pstr)))
468 // like check for string, but doesn't skip past any whitespace
469 int check_for_string_raw(char *pstr)
471 if (!strnicmp(pstr, Mp, strlen(pstr))){
478 // Find an optional string.
479 // If found, return 1, else return 0.
480 // If found, point past string, else don't update pointer.
481 int optional_string(char *pstr)
483 ignore_white_space();
485 if (!strnicmp(pstr, Mp, strlen(pstr))) {
493 int required_string_fred(char *pstr, char *end)
501 ignore_white_space();
502 while (*Mp != EOF_CHAR && strnicmp(pstr, Mp, strlen(pstr))) {
503 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end)))) {
508 advance_to_eoln(NULL);
509 ignore_white_space();
512 if (!Mp || (*Mp == EOF_CHAR)) {
513 diag_printf("Required string [%s] not found\n", pstr);
515 Token_found_flag = 0;
520 diag_printf("Found required string [%s]\n", pstr);
521 Token_found_flag = 1;
525 // attempt to find token in buffer. It might not exist, however, in which case we don't need
526 // to do anything. If it is found, then we advance the pointer to just after the token. To
527 // further complicate things, we should only search to a certain point, since we don't want
528 // a token that belongs to another section which might match the token we want. Thus, we
529 // also pass in an ending token, which marks the point we should stop looking at.
530 int optional_string_fred(char *pstr, char *end, char *end2)
538 ignore_white_space();
539 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, strlen(pstr))) {
540 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end))) ||
541 (end2 && !strnicmp(end2, Mp, strlen(end2)))) {
546 advance_to_eoln(NULL);
547 ignore_white_space();
550 if (!Mp || (*Mp == EOF_CHAR)) {
551 diag_printf("Optional string [%s] not found\n", pstr);
553 Token_found_flag = 0;
558 diag_printf("Found optional string [%s]\n", pstr);
559 Token_found_flag = 1;
563 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
564 // Does not update Mp if token found. If not found, advances, trying to
565 // find the string. Doesn't advance past the found string.
566 int required_string_either(char *str1, char *str2)
570 ignore_white_space();
572 while (count < RS_MAX_TRIES) {
573 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
574 // Mp += strlen(str1);
575 diag_printf("Found required string [%s]\n", token_found = str1);
577 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
578 // Mp += strlen(str2);
579 diag_printf("Found required string [%s]\n", token_found = str2);
583 error_display(1, "Required token = [%s] or [%s], found [%.32s].\n", str1, str2, next_tokens());
585 advance_to_eoln(NULL);
586 ignore_white_space();
590 if (count == RS_MAX_TRIES) {
591 nprintf(("Error", "Error: Unable to find either required token [%s] or [%s]\n", str1, str2));
592 Warning(LOCATION, "Error: Unable to find either required token [%s] or [%s]\n", str1, str2);
593 longjmp(parse_abort, 2);
600 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
601 // Does not update Mp if token found. If not found, advances, trying to
602 // find the string. Doesn't advance past the found string.
603 int required_string_3(char *str1, char *str2, char *str3)
607 ignore_white_space();
609 while (count < RS_MAX_TRIES) {
610 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
611 // Mp += strlen(str1);
612 diag_printf("Found required string [%s]\n", token_found = str1);
614 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
615 // Mp += strlen(str2);
616 diag_printf("Found required string [%s]\n", token_found = str2);
618 } else if (strnicmp(str3, Mp, strlen(str3)) == 0) {
619 diag_printf("Found required string [%s]\n", token_found = str3);
623 error_display(1, "Required token = [%s], [%s] or [%s], found [%.32s].\n", str1, str2, str3, next_tokens());
625 advance_to_eoln(NULL);
626 ignore_white_space();
634 int required_string_either_fred(char *str1, char *str2)
636 ignore_white_space();
638 while (*Mp != EOF_CHAR) {
639 if (!strnicmp(str1, Mp, strlen(str1))) {
640 // Mp += strlen(str1);
641 diag_printf("Found required string [%s]\n", token_found = str1);
642 return fred_parse_flag = 0;
644 } else if (!strnicmp(str2, Mp, strlen(str2))) {
645 // Mp += strlen(str2);
646 diag_printf("Found required string [%s]\n", token_found = str2);
647 return fred_parse_flag = 1;
650 advance_to_eoln(NULL);
651 ignore_white_space();
655 diag_printf("Unable to find either required token [%s] or [%s]\n", str1, str2);
661 // Copy characters from instr to outstr until eoln is found, or until max
662 // characters have been copied (including terminator).
663 void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max)
667 char terminators[128];
669 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
671 terminators[0] = EOLN;
672 terminators[1] = (char)EOF_CHAR;
673 if (more_terminators != NULL)
674 strcpy(&terminators[2], more_terminators);
678 while (((ch = *instr++) != 0) && (strchr(terminators, ch) == NULL) && (count < max)) {
684 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
689 // Copy characters from instr to outstr until next white space is found, or until max
690 // characters have been copied (including terminator).
691 void copy_to_next_white(char *outstr, char *instr, int max)
697 while (((ch = *instr++)>0) && (ch != EOLN) && (ch != EOF_CHAR) && (count < max)) {
699 in_quotes = !in_quotes;
702 if ( !in_quotes && is_white_space(ch) ) // not in quotes, white space terminates string
709 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
714 // Copy text until a certain string is matched.
715 // For example, this is used to copy mission notes, scanning until $END NOTES:
717 void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars)
721 foundstr = strstr(instr, endstr);
723 if (foundstr == NULL) {
724 nprintf(("Error", "Error. Looking for [%s], but never found it.\n", endstr));
725 longjmp(parse_abort, 3);
728 if (foundstr - instr + strlen(endstr) < (uint) max_chars) {
729 strncpy(outstr, instr, foundstr - instr);
730 outstr[foundstr - instr] = 0;
733 nprintf(("Error", "Error. Too much text (%i chars, %i allowed) before %s\n",
734 foundstr - instr - strlen(endstr), max_chars, endstr));
736 longjmp(parse_abort, 4);
739 diag_printf("Here's the partial wad of text:\n%.30s", outstr);
742 // stuffs a string into a buffer. Can get a string between " marks and stops
743 // when whitespace in encounted -- not to end of line
744 void stuff_string_white(char *pstr)
746 ignore_white_space();
747 copy_to_next_white(pstr, Mp, NAME_LENGTH-1);
748 advance_to_next_white();
751 // Stuff a string into a string buffer.
752 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
753 // the default string length if using the F_NAME case.
754 void stuff_string(char *pstr, int type, char *terminators, int len)
756 char read_str[2048] = "";
764 final_len = NAME_LENGTH;
767 copy_to_eoln(read_str, terminators, Mp, read_len);
768 drop_trailing_white_space(read_str);
769 advance_to_eoln(terminators);
774 final_len = DATE_LENGTH;
775 copy_to_eoln(read_str, terminators, Mp, read_len);
776 drop_trailing_white_space(read_str);
777 advance_to_eoln(terminators);
781 ignore_white_space();
782 final_len = NOTES_LENGTH;
783 copy_text_until(read_str, Mp, "$End Notes:", read_len);
784 Mp += strlen(read_str);
785 required_string("$End Notes:");
790 final_len = FILESPEC_LENGTH;
791 copy_to_eoln(read_str, terminators, Mp, read_len);
792 drop_trailing_white_space(read_str);
793 advance_to_eoln(terminators);
795 // F_MULTITEXTOLD keeping for backwards compatability with old missions
796 // can be deleted once all missions are using new briefing format
799 ignore_white_space();
800 final_len = NOTES_LENGTH;
801 copy_text_until(read_str, Mp, "$End Briefing Text:", read_len);
802 Mp += strlen(read_str);
803 required_string("$End Briefing Text:");
808 final_len = MULTITEXT_LENGTH;
810 ignore_white_space();
811 copy_text_until(read_str, Mp, "$end_multi_text", read_len);
812 Mp += strlen(read_str);
813 drop_trailing_white_space(read_str);
814 required_string("$end_multi_text");
819 final_len = PATHNAME_LENGTH;
820 copy_to_eoln(read_str, terminators, Mp, read_len);
821 drop_trailing_white_space(read_str);
822 advance_to_eoln(terminators);
827 final_len = MESSAGE_LENGTH;
828 copy_to_eoln(read_str, terminators, Mp, read_len);
829 drop_trailing_white_space(read_str);
830 advance_to_eoln(terminators);
837 // now we want to do any final localization
838 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
840 // if the hash localized text hash table is active and we have a valid external string - hash it
841 if(fhash_active() && (tag_id > -2)){
842 fhash_add_str(pstr, tag_id);
845 diag_printf("Stuffed string = [%.30s]\n", pstr);
848 // stuff a string, but only until the end of a line. don't ignore leading whitespace. close analog of fgets()/cfgets()
849 void stuff_string_line(char *pstr, int len)
851 char read_str[2048] = "";
858 copy_to_eoln(read_str, "\n", Mp, read_len);
859 drop_trailing_white_space(read_str);
863 // now we want to do any final localization
864 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
866 // if the hash localized text hash table is active and we have a valid external string - hash it
867 if(fhash_active() && (tag_id > -2)){
868 fhash_add_str(pstr, tag_id);
871 diag_printf("Stuffed string = [%.30s]\n", pstr);
874 // 1K on the stack? seems to work...
875 // JH: 1k isn't enough! Command briefs can be 16k max, so changed this.
876 #define MAX_TMP_STRING_LENGTH 16384
878 // Exactly the same as stuff string only Malloc's the buffer.
879 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
880 // the default string length if using the F_NAME case.
881 char *stuff_and_malloc_string( int type, char *terminators, int len)
885 char tmp_result[MAX_TMP_STRING_LENGTH];
887 stuff_string(tmp_result, type, terminators, len);
888 drop_white_space(tmp_result);
890 l = strlen(tmp_result);
891 Assert(l < MAX_TMP_STRING_LENGTH); // Get John!!
895 return strdup(tmp_result);
898 // After reading a multitext string, you can call this function to convert any newlines into
899 // spaces, so it's a one paragraph string (I.e. as in MS-Word).
901 void compact_multitext_string(char *str)
905 for (i=0; i<strlen(str); i++)
910 #define BUF_SIZE 2048
912 // Strip comments from a line of input.
913 int strip_comments_fred(char *readp, int in_comment)
916 char *writep = readp;
918 while ((ch = *readp) != COMMENT_CHAR) {
939 // Strip comments from a line of input.
940 int strip_comments(char *readp, int in_comment)
943 char *writep = readp;
945 while ((ch = *readp) != COMMENT_CHAR) {
952 if (!in_comment && (*readp == '/') && (*(readp+1) == '/')) {
959 // time to do some special foreign character conversion
1076 void strip_all_comments( char *readp, char *writep )
1079 //char *writep = readp;
1081 while ( *readp && *readp != EOF_CHAR ) {
1083 if ( ch == COMMENT_CHAR ) {
1084 while ( *readp != EOLN )
1089 // get to next character after EOLN
1091 } else if ( (ch == '/') && (readp[1] == '*')) { // Start of multi-line comment
1096 while ( *readp != '*' )
1098 if ( readp[1] == '/' ) {
1112 *writep = (char)EOF_CHAR;
1116 int parse_get_line(char *lineout, int max_line_len, char *start, int max_size, char *cur)
1119 int i, num_chars_read=0;
1123 for ( i = 0; i < max_line_len-1; i++ ) {
1125 if ( (cur - start) >= max_size ) {
1127 if ( lineout > t ) {
1128 return num_chars_read;
1135 } while ( c == 13 );
1138 if ( c=='\n' ) break;
1142 return num_chars_read;
1145 // Read mission text, stripping comments.
1146 // When a comment is found, it is removed. If an entire line
1147 // consisted of a comment, a blank line is left in the input file.
1148 void read_file_text(char *filename, int mode)
1151 char outbuf[BUF_SIZE], *str;
1152 char *mp = Mission_text;
1153 char *mp2 = Mission_text_raw;
1154 int file_is_encrypted = 0, in_comment = 0;
1157 longjmp(parse_abort, 10);
1159 strcpy(Current_filename, filename);
1160 mf = cfopen(filename, "rb", CFILE_NORMAL, mode);
1162 nprintf(("Error", "Wokka! Error opening mission.txt!\n"));
1163 longjmp(parse_abort, 5);
1166 // read the entire file in
1167 int file_len = cfilelength(mf);
1169 // read first 10 bytes to determine if file is encrypted
1170 cfread(Mission_text_raw, min(file_len, 10), 1, mf);
1171 file_is_encrypted = is_encrpyted(Mission_text_raw);
1172 cfseek(mf, 0, CF_SEEK_SET);
1174 if ( file_is_encrypted ) {
1175 int unscrambled_len;
1176 char *scrambled_text;
1177 scrambled_text = (char*)malloc(file_len+1);
1178 Assert(scrambled_text);
1179 cfread(scrambled_text, file_len, 1, mf);
1181 unencrypt(scrambled_text, file_len, Mission_text_raw, &unscrambled_len);
1182 file_len = unscrambled_len;
1183 free(scrambled_text);
1185 cfread(Mission_text_raw, file_len, 1, mf);
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(file_len < MISSION_TEXT_SIZE);
1194 // strip comments from raw text, reading into Mission_text
1196 int num_chars_read = 0;
1199 mp2 = Mission_text_raw;
1201 strip_all_comments(Mission_text_raw, mp2);
1203 while ( (num_chars_read = parse_get_line(outbuf, BUF_SIZE, Mission_text_raw, file_len, mp2)) != 0 ) {
1204 mp2 += num_chars_read;
1207 in_comment = strip_comments_fred(outbuf, in_comment);
1209 in_comment = strip_comments(outbuf, in_comment);
1222 // strcpy(mp, outbuf);
1223 // mp += strlen(outbuf);
1226 *mp = *mp2 = (char)EOF_CHAR;
1228 while (cfgets(outbuf, BUF_SIZE, mf) != NULL) {
1229 if (strlen(outbuf) >= BUF_SIZE-1)
1230 error_display(0, "Input string too long. Max is %i characters.\n%.256s\n", BUF_SIZE, outbuf);
1232 // If you hit this assert, it is probably telling you the obvious. The file
1233 // you are trying to read is truly too large. Look at *filename to see the file name.
1234 Assert(mp2 - Mission_text_raw + strlen(outbuf) < MISSION_TEXT_SIZE);
1235 strcpy(mp2, outbuf);
1236 mp2 += strlen(outbuf);
1238 in_comment = strip_comments(outbuf, in_comment);
1240 mp += strlen(outbuf);
1243 *mp = *mp2 = (char)EOF_CHAR;
1248 void debug_show_mission_text()
1250 char *mp = Mission_text;
1253 while ((ch = *mp++) != EOF_CHAR)
1262 ignore_white_space();
1266 if ((ch != '.') && (ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1267 error_display(1, "Expecting float, found [%.32s].\n", next_tokens());
1271 return (float)atof(Mp);
1281 ignore_white_space();
1285 if ((ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1286 error_display(1, "Expecting int, found [%.32s].\n", next_tokens());
1295 // Stuff a floating point value pointed at by Mp.
1296 // Advances past float characters.
1297 void stuff_float(float *f)
1304 Mp += strspn(Mp, "+-0123456789.");
1309 diag_printf("Stuffed float: %f\n", *f);
1312 // Stuff an integer value pointed at by Mp.
1313 // Advances past integer characters.
1314 void stuff_int(int *i)
1321 Mp += strspn(Mp, "+-0123456789");
1326 diag_printf("Stuffed int: %i\n", *i);
1330 // Stuffs a boolean value pointed at by Mp.
1331 // YES/NO (supporting 1/0 now as well)
1333 void stuff_boolean(int *i)
1336 ignore_white_space();
1337 copy_to_eoln(token, NULL, Mp, 512);
1338 drop_trailing_white_space(token);
1339 advance_to_eoln(NULL);
1341 if ( isdigit(token[0]) ) { // using 1/0 instead of YES/NO
1343 int_value = atoi(token);
1350 if ( !stricmp(token, "yes") ) {
1353 else if ( !stricmp(token, "no") ) {
1357 Assert(0); // can't happen
1362 // Stuff an integer value pointed at by Mp.
1363 // Advances past integer characters.
1364 void stuff_byte(ubyte *i)
1375 Mp += strspn(Mp, "+-0123456789");
1380 diag_printf("Stuffed byte: %i\n", *i);
1383 // Stuffs a list of strings
1384 int stuff_string_list(char slp[][NAME_LENGTH], int max_strings)
1387 ignore_white_space();
1390 error_display(1, "Reading string list. Found [%c]. Expecting '('.\n", *Mp);
1391 longjmp(parse_abort, 100);
1396 ignore_white_space();
1398 while (*Mp != ')') {
1399 assert ( count < max_strings );
1400 Assert ( *Mp == '\"' ); // should always be enclosed in quotes
1402 get_string( slp[count++] );
1403 ignore_white_space();
1411 // Stuffs an integer list.
1412 // This is of the form ( i* )
1413 // where i is an integer.
1414 // For example, (1) () (1 2 3) ( 1 ) are legal integer lists.
1415 int stuff_int_list(int *ilp, int max_ints, int lookup_type)
1417 int count = 0, ok_flag = 1, dummy;
1418 ignore_white_space();
1421 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1422 longjmp(parse_abort, 6);
1426 ignore_white_space();
1428 while (*Mp != ')') {
1429 Assert(count < max_ints);
1435 switch (lookup_type) {
1437 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1440 case SHIP_INFO_TYPE:
1442 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1447 case WEAPON_POOL_TYPE:
1449 num = weapon_info_lookup(str);
1454 case WEAPON_LIST_TYPE:
1455 num = weapon_info_lookup(str);
1460 case RAW_INTEGER_TYPE:
1465 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1471 Error(LOCATION, "Unable to find string \"%s\" in stuff_int_list\n\nMany possible sources for this error. Get a programmer!\n", str);
1472 } else if (num == -2) {
1473 if (strlen(str) > 0) {
1474 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);
1478 if (num < 0) // other negatives used to bypass the above error trap, but should be -1
1486 stuff_int(&ilp[count++]);
1491 ignore_white_space();
1499 // Marks an integer list.
1500 // This is of the form ( i* )
1501 // where i is an integer.
1502 // If a specified string is found in the lookup and its value is 7, then the 7th value
1503 // in the array is set.
1504 void mark_int_list(int *ilp, int max_ints, int lookup_type)
1506 ignore_white_space();
1509 error_display(1, "Marking integer list. Found [%c]. Expecting '('.\n", *Mp);
1510 longjmp(parse_abort, 6);
1514 ignore_white_space();
1516 while (*Mp != ')') {
1522 switch(lookup_type) {
1524 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1527 case SHIP_INFO_TYPE:
1528 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1531 case WEAPON_LIST_TYPE:
1532 num = weapon_info_lookup(str);
1536 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1540 if ( (num < 0) || (num >= max_ints) )
1541 Error(LOCATION, "Unable to find string \"%s\" in mark_int_list.\n", str);
1549 Assert((tval >= 0) && (tval < max_ints));
1553 ignore_white_space();
1561 // Stuff a vector, which is 3 floats.
1562 void stuff_vector(vector *vp)
1564 stuff_float(&vp->xyz.x);
1565 stuff_float(&vp->xyz.y);
1566 stuff_float(&vp->xyz.z);
1569 void stuff_parenthesized_vector(vector *vp)
1571 ignore_white_space();
1574 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting '('.\n", *Mp);
1575 longjmp(parse_abort, 11);
1579 ignore_white_space();
1581 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting ')'.\n", *Mp);
1582 longjmp(parse_abort, 12);
1589 // Stuffs vector list.
1590 // This is of the form ( (vector)* )
1591 // where vector is a vector
1592 // For example, ( (1 2 3) (2 3 4) (2 3 5) )
1593 // is a vector list of three vectors.
1594 int stuff_vector_list(vector *vlp, int max_vecs)
1598 ignore_white_space();
1601 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1602 longjmp(parse_abort, 6);
1607 ignore_white_space();
1609 while (*Mp != ')') {
1610 Assert(count < max_vecs);
1611 stuff_parenthesized_vector(&vlp[count++]);
1613 ignore_white_space();
1622 // Stuff a matrix, which is 3 vectors.
1623 void stuff_matrix(matrix *mp)
1625 stuff_vector(&mp->v.rvec);
1626 stuff_vector(&mp->v.uvec);
1627 stuff_vector(&mp->v.fvec);
1631 // Given a string, find it in a string array.
1632 // *descrtiption is only used for diagnostics in case it can't be found.
1633 // *str1 is the string to be found.
1634 // *strlist is the list of strings to search.
1635 // max is the number of entries in *strlist to scan.
1636 int string_lookup(char *str1, char *strlist[], int max, char *description, int say_errors)
1640 for (i=0; i<max; i++) {
1641 Assert(strlen(strlist[i]) != 0);
1643 if (!stricmp(str1, strlist[i]))
1648 error_display(0, "Unable to find [%s] in %s list.\n", str1, description);
1653 // Find a required string (*id), then stuff the text of type f_type that
1654 // follows it at *addr. *strlist[] contains the strings it should try to
1656 void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
1660 required_string(id);
1661 stuff_string(token, f_type, NULL);
1662 *addr = string_lookup(token, strlist, max, description, 1);
1665 // Mp points at a string.
1666 // Find the string in the list of strings *strlist[].
1667 // Returns the index of the match, -1 if none.
1668 int match_and_stuff(int f_type, char *strlist[], int max, char *description)
1672 stuff_string(token, f_type, NULL);
1673 return string_lookup(token, strlist, max, description, 0);
1676 void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total,
1677 int max, char *description)
1682 required_string(id);
1683 stuff_string(token, f_type, NULL);
1685 *addr = string_lookup(token, strlist, *total, description, 0);
1687 if (*addr == -1) // not in list, so lets try and add it.
1689 Assert(*total < max);
1690 strcpy(strlist[*total], token);
1695 // Initialize a parse process.
1704 for (i=0; i<MAX_CARGO; i++)
1705 Cargo_names[i] = Cargo_names_buf[i]; // make a pointer array for compatibility
1707 Total_goal_ship_names = 0;
1719 // Display number of warnings and errors at the end of a parse.
1720 void display_parse_diagnostics()
1722 nprintf(("Parse", "\nParse complete.\n"));
1723 nprintf(("Parse", "%i errors. %i warnings.\n", Error_count, Warning_count));
1726 // Splits a string into 2 lines if the string is wider than max_pixel_w pixels. A null
1727 // terminator is placed where required to make the first line <= max_pixel_w. The remaining
1728 // text is returned (leading whitespace removed). If the line doesn't need to be split,
1729 // NULL is returned.
1730 char *split_str_once(char *src, int max_pixel_w)
1733 int i, w, len, last_was_white = 0;
1736 Assert(max_pixel_w > 0);
1738 gr_get_string_size(&w, NULL, src);
1739 if ( w <= max_pixel_w )
1740 return NULL; // string doesn't require a cut
1743 for (i=0; i<len; i++) {
1744 gr_get_string_size(&w, NULL, src, i);
1745 if ( w > max_pixel_w )
1748 if (src[i] == '\n') { // reached natural end of line
1753 if (is_white_space(src[i])) {
1754 if (!last_was_white)
1770 while (is_white_space(*src))
1774 return NULL; // end of the string anyway
1782 #define SPLIT_STR_BUFFER_SIZE 512
1784 // --------------------------------------------------------------------------------------
1787 // A general function that will split a string into several lines. Lines are allowed up
1788 // to max_pixel_w pixels. Breaks are found in white space.
1790 // Supports \n's in the strings!
1792 // parameters: src => source string to be broken up
1793 // max_pixel_w => max width of line in pixels
1794 // n_chars => output array that will hold number of characters in each line
1795 // p_str => output array of pointers to start of lines within src
1796 // max_lines => limit of number of lines to break src up into
1797 // ignore_char => OPTIONAL parameter (default val -1). Ignore words starting with this character
1798 // This is useful when you want to ignore embedded control information that starts
1799 // with a specific character, like $ or #
1801 // returns: number of lines src is broken into
1802 // -1 is returned when an error occurs
1804 int split_str(char *src, int max_pixel_w, int *n_chars, char **p_str, int max_lines, char ignore_char)
1806 char buffer[SPLIT_STR_BUFFER_SIZE];
1807 char *breakpoint = NULL;
1808 int sw, new_line = 1, line_num = 0, last_was_white = 0;
1809 int ignore_until_whitespace, buf_index;
1811 // check our assumptions..
1812 Assert(src != NULL);
1813 Assert(n_chars != NULL);
1814 Assert(p_str != NULL);
1815 Assert(max_lines > 0);
1816 Assert(max_pixel_w > 0);
1818 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1820 ignore_until_whitespace = 0;
1822 // get rid of any leading whitespace
1823 while (is_white_space(*src))
1829 // iterate through chars in line, keeping track of most recent "white space" location that can be used
1830 // as a line splitting point if necessary
1831 for (; *src; src++) {
1832 if (line_num >= max_lines)
1833 return line_num; // time to bail out
1835 // starting a new line of text, init stuff for that
1837 p_str[line_num] = NULL;
1838 if (is_gray_space(*src))
1841 p_str[line_num] = src;
1846 // maybe skip leading whitespace
1847 if (ignore_until_whitespace) {
1848 if ( is_white_space(*src) )
1849 ignore_until_whitespace = 0;
1854 // if we have a newline, split the line here
1856 n_chars[line_num] = src - p_str[line_num]; // track length of line
1858 p_str[line_num] = NULL;
1861 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1866 if (*src == ignore_char) {
1867 ignore_until_whitespace = 1;
1871 if (is_gray_space(*src)) {
1872 if (!last_was_white) // track at first whitespace in a series of whitespace
1878 // indicate next time around that this wasn't a whitespace character
1882 // throw it in our buffer
1883 buffer[buf_index] = *src;
1885 buffer[buf_index] = 0; // null terminate it
1887 gr_get_string_size(&sw, NULL, buffer);
1888 if (sw >= max_pixel_w) {
1892 end = src = breakpoint;
1895 end = src; // force a split here since to whitespace
1896 src--; // reuse this character in next line
1899 n_chars[line_num] = end - p_str[line_num]; // track length of line
1900 Assert(n_chars[line_num]);
1902 p_str[line_num] = NULL;
1905 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1911 if (p_str[line_num]) {
1912 n_chars[line_num] = src - p_str[line_num]; // track length of line
1913 Assert(n_chars[line_num]);