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.5 2002/06/09 04:41:25 relnev
19 * added copyright header
21 * Revision 1.4 2002/05/28 06:45:38 theoddone33
24 * Revision 1.3 2002/05/28 06:28:20 theoddone33
25 * Filesystem mods, actually reads some data files now
27 * Revision 1.2 2002/05/07 03:16:48 theoddone33
28 * The Great Newline Fix
30 * Revision 1.1.1.1 2002/05/03 03:28:10 root
34 * 12 8/10/99 6:54p Dave
35 * Mad optimizations. Added paging to the nebula effect.
37 * 11 6/02/99 2:22p Andsager
40 * 10 5/20/99 11:25a Andsager
41 * Added error checking for parsing table files.
43 * 9 2/23/99 11:18a Andsager
44 * Localize launcher using strings.tbl
46 * 8 2/03/99 6:06p Dave
47 * Groundwork for FS2 PXO usertracker support. Gametracker support next.
49 * 7 11/05/98 4:18p Dave
50 * First run nebula support. Beefed up localization a bit. Removed all
51 * conditional compiles for foreign versions. Modified mission file
54 * 6 10/29/98 12:49p Dave
55 * Intermediate checkin for Fred hash table stuff.
57 * 5 10/28/98 11:30a Dave
60 * 4 10/22/98 6:14p Dave
61 * Optimized some #includes in Anim folder. Put in the beginnings of
62 * parse/localization support for externalized strings and tstrings.tbl
64 * 3 10/14/98 1:15p Andsager
67 * 2 10/07/98 10:53a Dave
70 * 1 10/07/98 10:50a Dave
72 * 98 9/21/98 2:37p Adam
73 * fixed font translation bug
75 * 97 7/29/98 9:39a Hoffoss
76 * Fixed bug with stuff_and_malloc_string
78 * 96 6/22/98 11:18a Hoffoss
79 * Fixed bug where mission notes field gets an extra linefeed with each
82 * 95 6/19/98 3:53p Lawrance
83 * account for some new foreign chars
85 * 94 6/12/98 7:37p Hoffoss
86 * Made ß translate to ss, since we don't have this character in our font.
88 * 93 6/12/98 4:52p Hoffoss
89 * Added support for special characters in in forgeign languages.
91 * 92 5/21/98 3:26p Dave
92 * Fixed bug in new split string function. Newline as last character did
95 * 91 5/21/98 3:01p Dave
96 * Make sure split_str handles 0 length strings correctly.
98 * 90 5/21/98 2:14a Hoffoss
99 * Rewrote split_str() to solve a problem and it was too hard for me to
100 * figure out (oow, my head hurts still!)
116 #include "missionparse.h"
119 #include "localize.h"
122 #define ERROR_LENGTH 64
123 #define RS_MAX_TRIES 5
125 char Current_filename[128];
126 char Error_str[ERROR_LENGTH];
128 int Warning_count, Error_count;
129 int fred_parse_flag = 0;
130 int Token_found_flag;
133 char Mission_text[MISSION_TEXT_SIZE];
134 char Mission_text_raw[MISSION_TEXT_SIZE];
138 // Return true if this character is white space, else false.
139 int is_white_space(char ch)
141 return ((ch == ' ') || (ch == '\t') || (ch == EOLN));
144 // Returns true if this character is gray space, else false (gray space is white space except for EOLN).
145 int is_gray_space(char ch)
147 return ((ch == ' ') || (ch == '\t'));
150 // Advance global Mp (mission pointer) past all current white space.
151 // Leaves Mp pointing at first non white space character.
152 void ignore_white_space()
154 while ((*Mp != EOF_CHAR) && is_white_space(*Mp))
158 void ignore_gray_space()
160 while ((*Mp != EOF_CHAR) && is_gray_space(*Mp))
164 // Truncate *str, eliminating all trailing white space.
165 // Eg: "abc " becomes "abc"
166 // "abc abc " becomes "abc abc"
167 // "abc \t" becomes "abc"
168 void drop_trailing_white_space(char *str)
174 while ((i >= 0) && is_white_space(str[i]))
180 // Eliminate any leading whitespace in str
181 void drop_leading_white_space(char *str)
188 while ((i < len) && is_white_space(str[i]))
191 memmove(str, str+i, len-i);
195 // eliminates all leading and trailing white space from a string. Returns pointer passed in.
196 char *drop_white_space(char *str)
201 while (str[s] && is_white_space(str[s]))
206 if (!is_white_space(str[e]))
213 memmove(str, str + s, e - s + 1);
219 // Advances Mp past current token.
222 ignore_white_space();
224 while ((*Mp != EOF_CHAR) && !is_white_space(*Mp))
228 // Display a diagnostic message if Verbose is set.
229 // (Verbose is set if -v command line switch is present.)
230 void diag_printf(char *format, ...)
235 va_start(args, format);
236 vsprintf(buffer, format, args);
239 nprintf(("Parse", "%s", buffer));
242 // Grab and return (a pointer to) a bunch of tokens, terminating at
243 // ERROR_LENGTH chars, or end of line.
250 while (((ch = *pstr++) != EOLN) && (ch != EOF_CHAR) && (count < ERROR_LENGTH-1))
251 Error_str[count++] = ch;
253 Error_str[count] = 0;
257 // Return the line number given by the current mission pointer, ie Mp.
258 // A very slow function (scans all processed text), but who cares how long
259 // an error reporting function takes?
268 p = Mission_text_raw;
269 stoploc = (Mp - Mission_text) + p;
276 if ( !incomment && (*p == COMMENT_CHAR) )
279 if ( !incomment && (*p == '/') && (*(p+1) == '*') ) {
287 if ( multiline && (*(p-1) == '*') && (*p == '/') ) {
293 if ( !multiline && incomment )
302 // Call this function to display an error message.
303 // error_level == 0 means this is just a warning.
304 // !0 means it's an error message.
305 // Prints line number and other useful information.
306 void error_display(int error_level, char *format, ...)
309 char error_text[128];
312 if (error_level == 0) {
313 strcpy(error_text, "Warning");
316 strcpy(error_text, "Error");
320 nprintf((error_text, "%s(%i):%s: ", Current_filename, get_line_num(), error_text));
322 va_start(args, format);
323 vsprintf(buffer, format, args);
325 Assert(strlen(buffer) < 1024);
327 nprintf((error_text, "%s", buffer));
328 Warning(LOCATION, "%s(%i):\n%s: %s", Current_filename, get_line_num(), error_text, buffer);
331 // Advance Mp to the next eoln character.
332 void advance_to_eoln(char *more_terminators)
334 char terminators[128];
336 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
338 terminators[0] = EOLN;
339 terminators[1] = (char)EOF_CHAR;
340 if (more_terminators != NULL)
341 strcpy(&terminators[2], more_terminators);
345 while (strchr(terminators, *Mp) == NULL)
349 // Advance Mp to the next white space (ignoring white space inside of " marks)
350 void advance_to_next_white()
354 while ((*Mp != EOLN) && (*Mp != EOF_CHAR)) {
356 in_quotes = !in_quotes;
358 if (!in_quotes && is_white_space(*Mp))
364 // Search for specified string, skipping everything up to that point. Returns 1 if found,
365 // 0 if string wasn't found (and hit end of file), or -1 if not found, but end of checking
366 // block was reached.
367 int skip_to_string(char *pstr, char *end)
371 ignore_white_space();
376 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, len)) {
377 if (end && *Mp == '#')
380 if (end && !strnicmp(pstr, Mp, len2))
383 advance_to_eoln(NULL);
384 ignore_white_space();
387 if (!Mp || (*Mp == EOF_CHAR))
394 // Advance to start of either pstr1 or pstr2. Return 0 is successful, otherwise return !0
395 int skip_to_start_of_strings(char *pstr1, char *pstr2)
399 ignore_white_space();
400 len1 = strlen(pstr1);
401 len2 = strlen(pstr2);
403 while ( (*Mp != EOF_CHAR) && strnicmp(pstr1, Mp, len1) && strnicmp(pstr2, Mp, len2) ) {
404 advance_to_eoln(NULL);
405 ignore_white_space();
408 if (!Mp || (*Mp == EOF_CHAR))
414 // Find a required string.
415 // If not found, display an error message, but try up to RS_MAX_TRIES times
416 // to find the string. (This is the groundwork for ignoring non-understood
418 // If unable to find the required string after RS_MAX_TRIES tries, then
419 // abort using longjmp to parse_abort.
420 int required_string(char *pstr)
424 ignore_white_space();
426 while (strnicmp(pstr, Mp, strlen(pstr)) && (count < RS_MAX_TRIES)) {
427 error_display(1, "Required token = [%s], found [%.32s].\n", pstr, next_tokens());
428 advance_to_eoln(NULL);
429 ignore_white_space();
433 if (count == RS_MAX_TRIES) {
434 nprintf(("Error", "Error: Unable to find required token [%s]\n", pstr));
435 Warning(LOCATION, "Error: Unable to find required token [%s]\n", pstr);
436 longjmp(parse_abort, 1);
440 diag_printf("Found required string [%s]\n", token_found = pstr);
444 // similar to optional_string, but just checks if next token is a match.
445 // It doesn't advance Mp.
447 int check_for_string(char *pstr)
449 ignore_white_space();
451 if (!strnicmp(pstr, Mp, strlen(pstr)))
457 // like check for string, but doesn't skip past any whitespace
458 int check_for_string_raw(char *pstr)
460 if (!strnicmp(pstr, Mp, strlen(pstr))){
467 // Find an optional string.
468 // If found, return 1, else return 0.
469 // If found, point past string, else don't update pointer.
470 int optional_string(char *pstr)
472 ignore_white_space();
474 if (!strnicmp(pstr, Mp, strlen(pstr))) {
482 int required_string_fred(char *pstr, char *end)
490 ignore_white_space();
491 while (*Mp != EOF_CHAR && strnicmp(pstr, Mp, strlen(pstr))) {
492 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end)))) {
497 advance_to_eoln(NULL);
498 ignore_white_space();
501 if (!Mp || (*Mp == EOF_CHAR)) {
502 diag_printf("Required string [%s] not found\n", pstr);
504 Token_found_flag = 0;
509 diag_printf("Found required string [%s]\n", pstr);
510 Token_found_flag = 1;
514 // attempt to find token in buffer. It might not exist, however, in which case we don't need
515 // to do anything. If it is found, then we advance the pointer to just after the token. To
516 // further complicate things, we should only search to a certain point, since we don't want
517 // a token that belongs to another section which might match the token we want. Thus, we
518 // also pass in an ending token, which marks the point we should stop looking at.
519 int optional_string_fred(char *pstr, char *end, char *end2)
527 ignore_white_space();
528 while ((*Mp != EOF_CHAR) && strnicmp(pstr, Mp, strlen(pstr))) {
529 if ((*Mp == '#') || (end && !strnicmp(end, Mp, strlen(end))) ||
530 (end2 && !strnicmp(end2, Mp, strlen(end2)))) {
535 advance_to_eoln(NULL);
536 ignore_white_space();
539 if (!Mp || (*Mp == EOF_CHAR)) {
540 diag_printf("Optional string [%s] not found\n", pstr);
542 Token_found_flag = 0;
547 diag_printf("Found optional string [%s]\n", pstr);
548 Token_found_flag = 1;
552 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
553 // Does not update Mp if token found. If not found, advances, trying to
554 // find the string. Doesn't advance past the found string.
555 int required_string_either(char *str1, char *str2)
559 ignore_white_space();
561 while (count < RS_MAX_TRIES) {
562 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
563 // Mp += strlen(str1);
564 diag_printf("Found required string [%s]\n", token_found = str1);
566 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
567 // Mp += strlen(str2);
568 diag_printf("Found required string [%s]\n", token_found = str2);
572 error_display(1, "Required token = [%s] or [%s], found [%.32s].\n", str1, str2, next_tokens());
574 advance_to_eoln(NULL);
575 ignore_white_space();
579 if (count == RS_MAX_TRIES) {
580 nprintf(("Error", "Error: Unable to find either required token [%s] or [%s]\n", str1, str2));
581 Warning(LOCATION, "Error: Unable to find either required token [%s] or [%s]\n", str1, str2);
582 longjmp(parse_abort, 2);
589 // Return 0 or 1 for str1 match, str2 match. Return -1 if neither matches.
590 // Does not update Mp if token found. If not found, advances, trying to
591 // find the string. Doesn't advance past the found string.
592 int required_string_3(char *str1, char *str2, char *str3)
596 ignore_white_space();
598 while (count < RS_MAX_TRIES) {
599 if (strnicmp(str1, Mp, strlen(str1)) == 0) {
600 // Mp += strlen(str1);
601 diag_printf("Found required string [%s]\n", token_found = str1);
603 } else if (strnicmp(str2, Mp, strlen(str2)) == 0) {
604 // Mp += strlen(str2);
605 diag_printf("Found required string [%s]\n", token_found = str2);
607 } else if (strnicmp(str3, Mp, strlen(str3)) == 0) {
608 diag_printf("Found required string [%s]\n", token_found = str3);
612 error_display(1, "Required token = [%s], [%s] or [%s], found [%.32s].\n", str1, str2, str3, next_tokens());
614 advance_to_eoln(NULL);
615 ignore_white_space();
623 int required_string_either_fred(char *str1, char *str2)
625 ignore_white_space();
627 while (*Mp != EOF_CHAR) {
628 if (!strnicmp(str1, Mp, strlen(str1))) {
629 // Mp += strlen(str1);
630 diag_printf("Found required string [%s]\n", token_found = str1);
631 return fred_parse_flag = 0;
633 } else if (!strnicmp(str2, Mp, strlen(str2))) {
634 // Mp += strlen(str2);
635 diag_printf("Found required string [%s]\n", token_found = str2);
636 return fred_parse_flag = 1;
639 advance_to_eoln(NULL);
640 ignore_white_space();
644 diag_printf("Unable to find either required token [%s] or [%s]\n", str1, str2);
650 // Copy characters from instr to outstr until eoln is found, or until max
651 // characters have been copied (including terminator).
652 void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max)
656 char terminators[128];
658 Assert((more_terminators == NULL) || (strlen(more_terminators) < 125));
660 terminators[0] = EOLN;
661 terminators[1] = (char)EOF_CHAR;
662 if (more_terminators != NULL)
663 strcpy(&terminators[2], more_terminators);
667 while (((ch = *instr++) != 0) && (strchr(terminators, ch) == NULL) && (count < max)) {
673 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
678 // Copy characters from instr to outstr until next white space is found, or until max
679 // characters have been copied (including terminator).
680 void copy_to_next_white(char *outstr, char *instr, int max)
686 while (((ch = *instr++)>0) && (ch != EOLN) && (ch != EOF_CHAR) && (count < max)) {
688 in_quotes = !in_quotes;
691 if ( !in_quotes && is_white_space(ch) ) // not in quotes, white space terminates string
698 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", next_tokens(), strlen(next_tokens()), max);
703 // Copy text until a certain string is matched.
704 // For example, this is used to copy mission notes, scanning until $END NOTES:
706 void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars)
710 foundstr = strstr(instr, endstr);
712 if (foundstr == NULL) {
713 nprintf(("Error", "Error. Looking for [%s], but never found it.\n", endstr));
714 longjmp(parse_abort, 3);
717 if (foundstr - instr + strlen(endstr) < (uint) max_chars) {
718 strncpy(outstr, instr, foundstr - instr);
719 outstr[foundstr - instr] = 0;
722 nprintf(("Error", "Error. Too much text (%i chars, %i allowed) before %s\n",
723 foundstr - instr - strlen(endstr), max_chars, endstr));
725 longjmp(parse_abort, 4);
728 diag_printf("Here's the partial wad of text:\n%.30s", outstr);
731 // stuffs a string into a buffer. Can get a string between " marks and stops
732 // when whitespace in encounted -- not to end of line
733 void stuff_string_white(char *pstr)
735 ignore_white_space();
736 copy_to_next_white(pstr, Mp, NAME_LENGTH-1);
737 advance_to_next_white();
740 // Stuff a string into a string buffer.
741 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
742 // the default string length if using the F_NAME case.
743 void stuff_string(char *pstr, int type, char *terminators, int len)
745 char read_str[2048] = "";
753 final_len = NAME_LENGTH;
756 copy_to_eoln(read_str, terminators, Mp, read_len);
757 drop_trailing_white_space(read_str);
758 advance_to_eoln(terminators);
763 final_len = DATE_LENGTH;
764 copy_to_eoln(read_str, terminators, Mp, read_len);
765 drop_trailing_white_space(read_str);
766 advance_to_eoln(terminators);
770 ignore_white_space();
771 final_len = NOTES_LENGTH;
772 copy_text_until(read_str, Mp, "$End Notes:", read_len);
773 Mp += strlen(read_str);
774 required_string("$End Notes:");
779 final_len = FILESPEC_LENGTH;
780 copy_to_eoln(read_str, terminators, Mp, read_len);
781 drop_trailing_white_space(read_str);
782 advance_to_eoln(terminators);
784 // F_MULTITEXTOLD keeping for backwards compatability with old missions
785 // can be deleted once all missions are using new briefing format
788 ignore_white_space();
789 final_len = NOTES_LENGTH;
790 copy_text_until(read_str, Mp, "$End Briefing Text:", read_len);
791 Mp += strlen(read_str);
792 required_string("$End Briefing Text:");
797 final_len = MULTITEXT_LENGTH;
799 ignore_white_space();
800 copy_text_until(read_str, Mp, "$end_multi_text", read_len);
801 Mp += strlen(read_str);
802 drop_trailing_white_space(read_str);
803 required_string("$end_multi_text");
808 final_len = PATHNAME_LENGTH;
809 copy_to_eoln(read_str, terminators, Mp, read_len);
810 drop_trailing_white_space(read_str);
811 advance_to_eoln(terminators);
816 final_len = MESSAGE_LENGTH;
817 copy_to_eoln(read_str, terminators, Mp, read_len);
818 drop_trailing_white_space(read_str);
819 advance_to_eoln(terminators);
826 // now we want to do any final localization
827 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
829 // if the hash localized text hash table is active and we have a valid external string - hash it
830 if(fhash_active() && (tag_id > -2)){
831 fhash_add_str(pstr, tag_id);
834 diag_printf("Stuffed string = [%.30s]\n", pstr);
837 // stuff a string, but only until the end of a line. don't ignore leading whitespace. close analog of fgets()/cfgets()
838 void stuff_string_line(char *pstr, int len)
840 char read_str[2048] = "";
847 copy_to_eoln(read_str, "\n", Mp, read_len);
848 drop_trailing_white_space(read_str);
852 // now we want to do any final localization
853 lcl_ext_localize(read_str, pstr, final_len, &tag_id);
855 // if the hash localized text hash table is active and we have a valid external string - hash it
856 if(fhash_active() && (tag_id > -2)){
857 fhash_add_str(pstr, tag_id);
860 diag_printf("Stuffed string = [%.30s]\n", pstr);
863 // 1K on the stack? seems to work...
864 // JH: 1k isn't enough! Command briefs can be 16k max, so changed this.
865 #define MAX_TMP_STRING_LENGTH 16384
867 // Exactly the same as stuff string only Malloc's the buffer.
868 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
869 // the default string length if using the F_NAME case.
870 char *stuff_and_malloc_string( int type, char *terminators, int len)
874 char tmp_result[MAX_TMP_STRING_LENGTH];
876 stuff_string(tmp_result, type, terminators, len);
877 drop_white_space(tmp_result);
879 l = strlen(tmp_result);
880 Assert(l < MAX_TMP_STRING_LENGTH); // Get John!!
884 return strdup(tmp_result);
887 // After reading a multitext string, you can call this function to convert any newlines into
888 // spaces, so it's a one paragraph string (I.e. as in MS-Word).
890 void compact_multitext_string(char *str)
894 for (i=0; i<strlen(str); i++)
899 #define BUF_SIZE 2048
901 // Strip comments from a line of input.
902 int strip_comments_fred(char *readp, int in_comment)
905 char *writep = readp;
907 while ((ch = *readp) != COMMENT_CHAR) {
928 // Strip comments from a line of input.
929 int strip_comments(char *readp, int in_comment)
932 char *writep = readp;
934 while ((ch = *readp) != COMMENT_CHAR) {
941 // time to do some special foreign character conversion
1058 void strip_all_comments( char *readp, char *writep )
1061 //char *writep = readp;
1063 while ( *readp != EOF_CHAR ) {
1065 if ( ch == COMMENT_CHAR ) {
1066 while ( *readp != EOLN )
1071 // get to next character after EOLN
1073 } else if ( (ch == '/') && (readp[1] == '*')) { // Start of multi-line comment
1078 while ( *readp != '*' )
1080 if ( readp[1] == '/' ) {
1094 *writep = (char)EOF_CHAR;
1098 int parse_get_line(char *lineout, int max_line_len, char *start, int max_size, char *cur)
1101 int i, num_chars_read=0;
1105 for ( i = 0; i < max_line_len-1; i++ ) {
1107 if ( (cur - start) >= max_size ) {
1109 if ( lineout > t ) {
1110 return num_chars_read;
1117 } while ( c == 13 );
1120 if ( c=='\n' ) break;
1124 return num_chars_read;
1127 // Read mission text, stripping comments.
1128 // When a comment is found, it is removed. If an entire line
1129 // consisted of a comment, a blank line is left in the input file.
1130 void read_file_text(char *filename, int mode)
1133 char outbuf[BUF_SIZE], *str;
1134 char *mp = Mission_text;
1135 char *mp2 = Mission_text_raw;
1136 int file_is_encrypted = 0, in_comment = 0;
1139 longjmp(parse_abort, 10);
1141 strcpy(Current_filename, filename);
1142 mf = cfopen(filename, "rb", CFILE_NORMAL, mode);
1144 nprintf(("Error", "Wokka! Error opening mission.txt!\n"));
1145 longjmp(parse_abort, 5);
1148 // read the entire file in
1149 int file_len = cfilelength(mf);
1151 // read first 10 bytes to determine if file is encrypted
1152 cfread(Mission_text_raw, min(file_len, 10), 1, mf);
1153 file_is_encrypted = is_encrpyted(Mission_text_raw);
1154 cfseek(mf, 0, CF_SEEK_SET);
1156 if ( file_is_encrypted ) {
1157 int unscrambled_len;
1158 char *scrambled_text;
1159 scrambled_text = (char*)malloc(file_len+1);
1160 Assert(scrambled_text);
1161 cfread(scrambled_text, file_len, 1, mf);
1163 unencrypt(scrambled_text, file_len, Mission_text_raw, &unscrambled_len);
1164 file_len = unscrambled_len;
1165 free(scrambled_text);
1167 cfread(Mission_text_raw, file_len, 1, mf);
1172 // If you hit this assert, it is probably telling you the obvious. The file
1173 // you are trying to read is truly too large. Look at *filename to see the file name.
1174 Assert(file_len < MISSION_TEXT_SIZE);
1176 // strip comments from raw text, reading into Mission_text
1178 int num_chars_read = 0;
1180 mp2 = Mission_text_raw;
1181 while ( (num_chars_read = parse_get_line(outbuf, BUF_SIZE, Mission_text_raw, file_len, mp2)) != 0 ) {
1182 mp2 += num_chars_read;
1185 in_comment = strip_comments_fred(outbuf, in_comment);
1187 in_comment = strip_comments(outbuf, in_comment);
1200 // strcpy(mp, outbuf);
1201 // mp += strlen(outbuf);
1204 *mp = *mp2 = (char)EOF_CHAR;
1206 while (cfgets(outbuf, BUF_SIZE, mf) != NULL) {
1207 if (strlen(outbuf) >= BUF_SIZE-1)
1208 error_display(0, "Input string too long. Max is %i characters.\n%.256s\n", BUF_SIZE, outbuf);
1210 // If you hit this assert, it is probably telling you the obvious. The file
1211 // you are trying to read is truly too large. Look at *filename to see the file name.
1212 Assert(mp2 - Mission_text_raw + strlen(outbuf) < MISSION_TEXT_SIZE);
1213 strcpy(mp2, outbuf);
1214 mp2 += strlen(outbuf);
1216 in_comment = strip_comments(outbuf, in_comment);
1218 mp += strlen(outbuf);
1221 *mp = *mp2 = (char)EOF_CHAR;
1226 void debug_show_mission_text()
1228 char *mp = Mission_text;
1231 while ((ch = *mp++) != EOF_CHAR)
1240 ignore_white_space();
1244 if ((ch != '.') && (ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1245 error_display(1, "Expecting float, found [%.32s].\n", next_tokens());
1249 return (float)atof(Mp);
1259 ignore_white_space();
1263 if ((ch != '-') && (ch != '+') && ((ch < '0') || (ch > '9'))) {
1264 error_display(1, "Expecting int, found [%.32s].\n", next_tokens());
1273 // Stuff a floating point value pointed at by Mp.
1274 // Advances past float characters.
1275 void stuff_float(float *f)
1282 Mp += strspn(Mp, "+-0123456789.");
1287 diag_printf("Stuffed float: %f\n", *f);
1290 // Stuff an integer value pointed at by Mp.
1291 // Advances past integer characters.
1292 void stuff_int(int *i)
1299 Mp += strspn(Mp, "+-0123456789");
1304 diag_printf("Stuffed int: %i\n", *i);
1308 // Stuffs a boolean value pointed at by Mp.
1309 // YES/NO (supporting 1/0 now as well)
1311 void stuff_boolean(int *i)
1314 ignore_white_space();
1315 copy_to_eoln(token, NULL, Mp, 512);
1316 drop_trailing_white_space(token);
1317 advance_to_eoln(NULL);
1319 if ( isdigit(token[0]) ) { // using 1/0 instead of YES/NO
1321 int_value = atoi(token);
1328 if ( !stricmp(token, "yes") ) {
1331 else if ( !stricmp(token, "no") ) {
1335 Assert(0); // can't happen
1340 // Stuff an integer value pointed at by Mp.
1341 // Advances past integer characters.
1342 void stuff_byte(ubyte *i)
1353 Mp += strspn(Mp, "+-0123456789");
1358 diag_printf("Stuffed byte: %i\n", *i);
1361 // Stuffs a list of strings
1362 int stuff_string_list(char slp[][NAME_LENGTH], int max_strings)
1365 ignore_white_space();
1368 error_display(1, "Reading string list. Found [%c]. Expecting '('.\n", *Mp);
1369 longjmp(parse_abort, 100);
1374 ignore_white_space();
1376 while (*Mp != ')') {
1377 assert ( count < max_strings );
1378 Assert ( *Mp == '\"' ); // should always be enclosed in quotes
1380 get_string( slp[count++] );
1381 ignore_white_space();
1389 // Stuffs an integer list.
1390 // This is of the form ( i* )
1391 // where i is an integer.
1392 // For example, (1) () (1 2 3) ( 1 ) are legal integer lists.
1393 int stuff_int_list(int *ilp, int max_ints, int lookup_type)
1395 int count = 0, ok_flag = 1, dummy;
1396 ignore_white_space();
1399 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1400 longjmp(parse_abort, 6);
1404 ignore_white_space();
1406 while (*Mp != ')') {
1407 Assert(count < max_ints);
1413 switch (lookup_type) {
1415 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1418 case SHIP_INFO_TYPE:
1420 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1425 case WEAPON_POOL_TYPE:
1427 num = weapon_info_lookup(str);
1432 case WEAPON_LIST_TYPE:
1433 num = weapon_info_lookup(str);
1438 case RAW_INTEGER_TYPE:
1443 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1449 Error(LOCATION, "Unable to find string \"%s\" in stuff_int_list\n\nMany possible sources for this error. Get a programmer!\n", str);
1450 } else if (num == -2) {
1451 if (strlen(str) > 0) {
1452 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);
1456 if (num < 0) // other negatives used to bypass the above error trap, but should be -1
1464 stuff_int(&ilp[count++]);
1469 ignore_white_space();
1477 // Marks an integer list.
1478 // This is of the form ( i* )
1479 // where i is an integer.
1480 // If a specified string is found in the lookup and its value is 7, then the 7th value
1481 // in the array is set.
1482 void mark_int_list(int *ilp, int max_ints, int lookup_type)
1484 ignore_white_space();
1487 error_display(1, "Marking integer list. Found [%c]. Expecting '('.\n", *Mp);
1488 longjmp(parse_abort, 6);
1492 ignore_white_space();
1494 while (*Mp != ')') {
1500 switch(lookup_type) {
1502 num = ship_name_lookup(str); // returns index of Ship[] entry with name
1505 case SHIP_INFO_TYPE:
1506 num = ship_info_lookup(str); // returns index of Ship_info[] entry with name
1509 case WEAPON_LIST_TYPE:
1510 num = weapon_info_lookup(str);
1514 Error(LOCATION,"Unknown lookup_type in stuff_int_list");
1518 if ( (num < 0) || (num >= max_ints) )
1519 Error(LOCATION, "Unable to find string \"%s\" in mark_int_list.\n", str);
1527 Assert((tval >= 0) && (tval < max_ints));
1531 ignore_white_space();
1539 // Stuff a vector, which is 3 floats.
1540 void stuff_vector(vector *vp)
1542 stuff_float(&vp->x);
1543 stuff_float(&vp->y);
1544 stuff_float(&vp->z);
1547 void stuff_parenthesized_vector(vector *vp)
1549 ignore_white_space();
1552 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting '('.\n", *Mp);
1553 longjmp(parse_abort, 11);
1557 ignore_white_space();
1559 error_display(1, "Reading parenthesized vector. Found [%c]. Expecting ')'.\n", *Mp);
1560 longjmp(parse_abort, 12);
1567 // Stuffs vector list.
1568 // This is of the form ( (vector)* )
1569 // where vector is a vector
1570 // For example, ( (1 2 3) (2 3 4) (2 3 5) )
1571 // is a vector list of three vectors.
1572 int stuff_vector_list(vector *vlp, int max_vecs)
1576 ignore_white_space();
1579 error_display(1, "Reading integer list. Found [%c]. Expecting '('.\n", *Mp);
1580 longjmp(parse_abort, 6);
1585 ignore_white_space();
1587 while (*Mp != ')') {
1588 Assert(count < max_vecs);
1589 stuff_parenthesized_vector(&vlp[count++]);
1591 ignore_white_space();
1600 // Stuff a matrix, which is 3 vectors.
1601 void stuff_matrix(matrix *mp)
1603 stuff_vector(&mp->rvec);
1604 stuff_vector(&mp->uvec);
1605 stuff_vector(&mp->fvec);
1609 // Given a string, find it in a string array.
1610 // *descrtiption is only used for diagnostics in case it can't be found.
1611 // *str1 is the string to be found.
1612 // *strlist is the list of strings to search.
1613 // max is the number of entries in *strlist to scan.
1614 int string_lookup(char *str1, char *strlist[], int max, char *description, int say_errors)
1618 for (i=0; i<max; i++) {
1619 Assert(strlen(strlist[i]) != 0);
1621 if (!stricmp(str1, strlist[i]))
1626 error_display(0, "Unable to find [%s] in %s list.\n", str1, description);
1631 // Find a required string (*id), then stuff the text of type f_type that
1632 // follows it at *addr. *strlist[] contains the strings it should try to
1634 void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
1638 required_string(id);
1639 stuff_string(token, f_type, NULL);
1640 *addr = string_lookup(token, strlist, max, description, 1);
1643 // Mp points at a string.
1644 // Find the string in the list of strings *strlist[].
1645 // Returns the index of the match, -1 if none.
1646 int match_and_stuff(int f_type, char *strlist[], int max, char *description)
1650 stuff_string(token, f_type, NULL);
1651 return string_lookup(token, strlist, max, description, 0);
1654 void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total,
1655 int max, char *description)
1660 required_string(id);
1661 stuff_string(token, f_type, NULL);
1663 *addr = string_lookup(token, strlist, *total, description, 0);
1665 if (*addr == -1) // not in list, so lets try and add it.
1667 Assert(*total < max);
1668 strcpy(strlist[*total], token);
1673 // Initialize a parse process.
1682 for (i=0; i<MAX_CARGO; i++)
1683 Cargo_names[i] = Cargo_names_buf[i]; // make a pointer array for compatibility
1685 Total_goal_ship_names = 0;
1697 // Display number of warnings and errors at the end of a parse.
1698 void display_parse_diagnostics()
1700 nprintf(("Parse", "\nParse complete.\n"));
1701 nprintf(("Parse", "%i errors. %i warnings.\n", Error_count, Warning_count));
1704 // Splits a string into 2 lines if the string is wider than max_pixel_w pixels. A null
1705 // terminator is placed where required to make the first line <= max_pixel_w. The remaining
1706 // text is returned (leading whitespace removed). If the line doesn't need to be split,
1707 // NULL is returned.
1708 char *split_str_once(char *src, int max_pixel_w)
1711 int i, w, len, last_was_white = 0;
1714 Assert(max_pixel_w > 0);
1716 gr_get_string_size(&w, NULL, src);
1717 if ( w <= max_pixel_w )
1718 return NULL; // string doesn't require a cut
1721 for (i=0; i<len; i++) {
1722 gr_get_string_size(&w, NULL, src, i);
1723 if ( w > max_pixel_w )
1726 if (src[i] == '\n') { // reached natural end of line
1731 if (is_white_space(src[i])) {
1732 if (!last_was_white)
1748 while (is_white_space(*src))
1752 return NULL; // end of the string anyway
1760 #define SPLIT_STR_BUFFER_SIZE 512
1762 // --------------------------------------------------------------------------------------
1765 // A general function that will split a string into several lines. Lines are allowed up
1766 // to max_pixel_w pixels. Breaks are found in white space.
1768 // Supports \n's in the strings!
1770 // parameters: src => source string to be broken up
1771 // max_pixel_w => max width of line in pixels
1772 // n_chars => output array that will hold number of characters in each line
1773 // p_str => output array of pointers to start of lines within src
1774 // max_lines => limit of number of lines to break src up into
1775 // ignore_char => OPTIONAL parameter (default val -1). Ignore words starting with this character
1776 // This is useful when you want to ignore embedded control information that starts
1777 // with a specific character, like $ or #
1779 // returns: number of lines src is broken into
1780 // -1 is returned when an error occurs
1782 int split_str(char *src, int max_pixel_w, int *n_chars, char **p_str, int max_lines, char ignore_char)
1784 char buffer[SPLIT_STR_BUFFER_SIZE];
1785 char *breakpoint = NULL;
1786 int sw, new_line = 1, line_num = 0, last_was_white = 0;
1787 int ignore_until_whitespace, buf_index;
1789 // check our assumptions..
1790 Assert(src != NULL);
1791 Assert(n_chars != NULL);
1792 Assert(p_str != NULL);
1793 Assert(max_lines > 0);
1794 Assert(max_pixel_w > 0);
1796 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1798 ignore_until_whitespace = 0;
1800 // get rid of any leading whitespace
1801 while (is_white_space(*src))
1807 // iterate through chars in line, keeping track of most recent "white space" location that can be used
1808 // as a line splitting point if necessary
1809 for (; *src; src++) {
1810 if (line_num >= max_lines)
1811 return line_num; // time to bail out
1813 // starting a new line of text, init stuff for that
1815 p_str[line_num] = NULL;
1816 if (is_gray_space(*src))
1819 p_str[line_num] = src;
1824 // maybe skip leading whitespace
1825 if (ignore_until_whitespace) {
1826 if ( is_white_space(*src) )
1827 ignore_until_whitespace = 0;
1832 // if we have a newline, split the line here
1834 n_chars[line_num] = src - p_str[line_num]; // track length of line
1836 p_str[line_num] = NULL;
1839 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1844 if (*src == ignore_char) {
1845 ignore_until_whitespace = 1;
1849 if (is_gray_space(*src)) {
1850 if (!last_was_white) // track at first whitespace in a series of whitespace
1856 // indicate next time around that this wasn't a whitespace character
1860 // throw it in our buffer
1861 buffer[buf_index] = *src;
1863 buffer[buf_index] = 0; // null terminate it
1865 gr_get_string_size(&sw, NULL, buffer);
1866 if (sw >= max_pixel_w) {
1870 end = src = breakpoint;
1873 end = src; // force a split here since to whitespace
1874 src--; // reuse this character in next line
1877 n_chars[line_num] = end - p_str[line_num]; // track length of line
1878 Assert(n_chars[line_num]);
1880 p_str[line_num] = NULL;
1883 memset(buffer, 0, SPLIT_STR_BUFFER_SIZE);
1889 if (p_str[line_num]) {
1890 n_chars[line_num] = src - p_str[line_num]; // track length of line
1891 Assert(n_chars[line_num]);