Open source remastering of the Freespace 2 engine
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
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 the
6  * source.
7  *
8 */
10 #ifndef _PARSELO_H
11 #define _PARSELO_H
13 #include "cfile/cfile.h"
14 #include "globalincs/globals.h"
15 #include "globalincs/pstypes.h"
17 #include <exception>
19 // NOTE: although the main game doesn't need this anymore, FRED2 still does
20 #define MISSION_TEXT_SIZE 1000000
22 extern char *Mission_text;
23 extern char *Mission_text_raw;
24 extern char *Mp;
25 extern const char *token_found;
26 extern int fred_parse_flag;
27 extern int Token_found_flag;
30 #define COMMENT_CHAR (char)';'
31 #define EOF_CHAR (char)-128
32 #define EOLN (char)0x0a
34 #define F_NAME 1
35 #define F_DATE 2
36 #define F_NOTES 3
37 #define F_FILESPEC 4
38 #define F_MULTITEXTOLD 5 // needed for backwards compatability with old briefing format
39 #define F_SEXP 6
40 #define F_PATHNAME 7
41 #define F_SHIPCHOICE 8
42 #define F_MESSAGE 9 // this is now obsolete for mission messages - all messages in missions should now use $MessageNew and stuff strings as F_MULTITEXT
43 #define F_MULTITEXT 10
44 #define F_RAW 11 // for any internal parsing use. Just strips whitespace and copies the text.
45 #define F_LNAME 12 //Filenames
47 #define PARSE_BUF_SIZE 4096
49 #define SHIP_TYPE 0 // used to identify which kind of array to do a search for a name in
50 #define SHIP_INFO_TYPE 1
51 #define WEAPON_LIST_TYPE 2 // to parse an int_list of weapons
52 #define RAW_INTEGER_TYPE 3 // to parse a list of integers
53 #define WEAPON_POOL_TYPE 4
55 // Karajorma - Used by the stuff_ship_list and stuff_weapon_list SEXPs
63 #define SEXP_SAVE_MODE 1
66 // Goober5000 - this seems to be a pretty universal function
67 extern bool end_string_at_first_hash_symbol(char *src);
69 extern char *get_pointer_to_first_hash_symbol(char *src);
70 extern const char *get_pointer_to_first_hash_symbol(const char *src);
73 // white space
74 extern int is_white_space(char ch);
75 extern void ignore_white_space();
76 extern void drop_trailing_white_space(char *str);
77 extern void drop_leading_white_space(char *str);
78 extern char *drop_white_space(char *str);
80 // SCP_string white space
81 extern void drop_trailing_white_space(SCP_string &str);
82 extern void drop_leading_white_space(SCP_string &str);
83 extern void drop_white_space(SCP_string &str);
85 // gray space
86 extern int is_gray_space(char ch);
87 extern void ignore_gray_space();
89 // error
90 extern int get_line_num();
91 extern char *next_tokens();
92 extern void diag_printf(char *format, ...);
93 extern void error_display(int error_level, char *format, ...);
95 // skip
96 extern int skip_to_string(char *pstr, char *end = NULL);
97 extern int skip_to_start_of_string(char *pstr, char *end = NULL);
98 extern int skip_to_start_of_string_either(char *pstr1, char *pstr2, char *end = NULL);
99 extern void advance_to_eoln(char *terminators);
100 extern void skip_token();
102 // optional
103 extern int optional_string(const char *pstr);
104 extern int optional_string_either(char *str1, char *str2);
105 extern int optional_string_one_of(int arg_count, ...);
107 // required
108 extern int required_string(const char *pstr);
109 extern int required_string_either(char *str1, char *str2);
110 extern int required_string_one_of(int arg_count, ...);
112 // stuff
113 extern void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max);
114 extern void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars);
115 extern void stuff_string_white(char *outstr, int len = 0);
116 extern void stuff_string_until(char *outstr, char *endstr, int len = 0);
117 extern void stuff_string(char *outstr, int type, int len, char *terminators = NULL);
118 extern void stuff_string_line(char *outstr, int len);
120 // SCP_string stuff
121 extern void copy_to_eoln(SCP_string &outstr, char *more_terminators, char *instr);
122 extern void copy_text_until(SCP_string &outstr, char *instr, char *endstr);
123 extern void stuff_string_white(SCP_string &outstr);
124 extern void stuff_string_until(SCP_string &outstr, char *endstr);
125 extern void stuff_string(SCP_string &outstr, int type, char *terminators = NULL);
126 extern void stuff_string_line(SCP_string &outstr);
128 //alloc
129 extern char* alloc_block(char* startstr, char* endstr, int extra_chars = 0);
131 // Exactly the same as stuff string only Malloc's the buffer.
132 // Supports various FreeSpace primitive types. If 'len' is supplied, it will override
133 // the default string length if using the F_NAME case.
134 extern char *stuff_and_malloc_string(int type, char *terminators = NULL);
135 extern void stuff_malloc_string(char **dest, int type, char *terminators = NULL);
136 extern void stuff_float(float *f);
137 extern int stuff_float_optional(float *f, bool raw = false);
138 extern int stuff_int_optional(int *i, bool raw = false);
139 extern void stuff_int(int *i);
140 extern void stuff_ubyte(ubyte *i);
142 extern int stuff_string_list(char slp[][NAME_LENGTH], int max_strings);
143 extern int parse_string_flag_list(int *dest, flag_def_list defs[], int defs_size);
144 extern int stuff_int_list(int *ilp, int max_ints, int lookup_type = RAW_INTEGER_TYPE);
145 extern int stuff_float_list(float* flp, int max_floats);
146 extern int stuff_vec3d_list(vec3d *vlp, int max_vecs);
147 extern int stuff_vec3d_list(SCP_vector<vec3d> &vec_list);
148 extern int stuff_bool_list(bool *blp, int max_bools);
149 extern void stuff_vec3d(vec3d *vp);
150 extern void stuff_matrix(matrix *mp);
151 extern int string_lookup(char *str1, char *strlist[], int max, char *description = NULL, int say_errors = 0);
152 extern void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description);
153 extern void find_and_stuff_optional(char *id, int *addr, int f_type, char *strlist[], int max, char *description);
154 extern int match_and_stuff(int f_type, char *strlist[], int max, char *description);
155 extern void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total,
156  int max, char *description);
157 extern int get_string(char *str, int max = -1);
158 extern void get_string(SCP_string &str);
159 extern void stuff_parenthesized_vec3d(vec3d *vp);
160 extern void stuff_boolean(int *i, bool a_to_eol=true);
161 extern void stuff_boolean(bool *b, bool a_to_eol=true);
162 extern void stuff_boolean_flag(int *i, int flag, bool a_to_eol=true);
163 extern int check_for_string(const char *pstr);
164 extern int check_for_string_raw(const char *pstr);
165 extern int check_for_eof();
166 extern int check_for_eoln();
168 // from aicode.cpp
169 extern void parse_float_list(float *plist, int size);
170 extern void parse_int_list(int *ilist, int size);
173 // general
174 extern void reset_parse(char *text = NULL);
175 extern void display_parse_diagnostics();
176 extern void pause_parse();
177 extern void unpause_parse();
178 // stop parsing, basically just free's up the memory from Mission_text and Mission_text_raw
179 extern void stop_parse();
181 // utility
182 extern void mark_int_list(int *ilp, int max_ints, int lookup_type);
183 extern void compact_multitext_string(char *str);
184 extern void compact_multitext_string(SCP_string &str);
185 extern void read_file_text(const char *filename, int mode = CF_TYPE_ANY, char *processed_text = NULL, char *raw_text = NULL);
186 extern void read_file_text_from_array(const char *array, char *processed_text = NULL, char *raw_text = NULL);
187 extern void read_raw_file_text(const char *filename, int mode = CF_TYPE_ANY, char *raw_text = NULL);
188 extern void process_raw_file_text(char *processed_text = NULL, char *raw_text = NULL);
189 extern void debug_show_mission_text();
190 extern void convert_sexp_to_string(SCP_string &dest, int cur_node, int mode);
191 char *split_str_once(char *src, int max_pixel_w);
192 int split_str(const char *src, int max_pixel_w, int *n_chars, const char **p_str, int max_lines, char ignore_char = -1);
193 int split_str(const char *src, int max_pixel_w, SCP_vector<int> &n_chars, SCP_vector<const char*> &p_str, char ignore_char);
195 // fred
196 extern int required_string_fred(char *pstr, char *end = NULL);
197 extern int required_string_either_fred(char *str1, char *str2);
198 extern int optional_string_fred(char *pstr, char *end = NULL, char *end2 = NULL);
200 // Goober5000 - returns position of replacement or -1 for exceeded length (SCP_string variants return the result)
201 extern int replace_one(char *str, char *oldstr, char *newstr, unsigned int max_len, int range = 0);
202 extern SCP_string& replace_one(SCP_string& context, const SCP_string& from, const SCP_string& to);
203 extern SCP_string& replace_one(SCP_string& context, const char* from, const char* to);
205 // Goober5000 - returns number of replacements or -1 for exceeded length (SCP_string variants return the result)
206 extern int replace_all(char *str, char *oldstr, char *newstr, unsigned int max_len, int range = 0);
207 extern SCP_string& replace_all(SCP_string& context, const SCP_string& from, const SCP_string& to);
208 extern SCP_string& replace_all(SCP_string& context, const char* from, const char* to);
210 // Goober5000 (why is this not in the C library?)
211 extern const char *stristr(const char *str, const char *substr);
212 extern char *stristr(char *str, const char *substr);
214 // Goober5000 (ditto)
215 extern bool can_construe_as_integer(const char *text);
217 // Goober5000 (ditto for C++)
218 extern void vsprintf(SCP_string &dest, const char *format, va_list ap);
219 extern void sprintf(SCP_string &dest, const char *format, ...);
221 // Goober5000
222 extern int subsystem_stricmp(const char *str1, const char *str2);
224 //WMC - compares two strings, ignoring the last extension
225 extern int strextcmp(const char *s1, const char *s2);
227 // Goober5000 - truncates a file extension
228 extern bool drop_extension(char *str);
229 extern bool drop_extension(SCP_string &str);
231 //WMC - backspaces the first character of given char pointer
232 extern void backspace(char *src);
234 // Goober5000 - prints a properly comma-separated integer to a string
235 extern void format_integer_with_commas(char *buf, int integer, bool use_comma_with_four_digits);
237 // Goober5000
238 extern int scan_fso_version_string(const char *text, int *major, int *minor, int *build, int *revis);
240 // Goober5000
241 extern void truncate_message_lines(SCP_string &text, int num_allowed_lines);
243 inline void parse_advance(int s){Mp+=s;}
245 // parse a modular table, returns the number of files matching the "name_check" filter or 0 if it did nothing
246 extern int parse_modular_table(const char *name_check, void (*parse_callback)(const char *filename), int path_type = CF_TYPE_TABLES, int sort_type = CF_SORT_REVERSE);
247 // to know that we are parsing a modular table
248 extern bool Parsing_modular_table;
250 //Karajorma - Parses mission and campaign ship loadouts.
251 int stuff_loadout_list (int *ilp, int max_ints, int lookup_type);
252 int get_string_or_variable (char *str);
257 namespace parse
258 {
259  class ParseException : public std::runtime_error
260  {
261  public:
262  ParseException(const std::string& msg) : std::runtime_error(msg) {}
263  ~ParseException() throw() {}
264  };
265 }
267 #endif
GLenum GLsizei GLenum format
Definition: Gl.h:1509
void stuff_string(char *outstr, int type, int len, char *terminators=NULL)
Definition: parselo.cpp:1189
int stuff_float_list(float *flp, int max_floats)
Definition: parselo.cpp:2988
int i
Definition: multi_pxo.cpp:466
void read_file_text(const char *filename, int mode=CF_TYPE_ANY, char *processed_text=NULL, char *raw_text=NULL)
Definition: parselo.cpp:1995
void parse_advance(int s)
Definition: parselo.h:243
void vsprintf(SCP_string &dest, const char *format, va_list ap)
Definition: parselo.cpp:3800
int split_str(const char *src, int max_pixel_w, int *n_chars, const char **p_str, int max_lines, char ignore_char=-1)
Definition: parselo.cpp:3412
#define vp(p)
Definition: modelsinc.h:70
int check_for_string_raw(const char *pstr)
Definition: parselo.cpp:528
void drop_trailing_white_space(char *str)
Definition: parselo.cpp:93
bool Parsing_modular_table
Definition: parselo.cpp:34
void sprintf(SCP_string &dest, const char *format,...)
Definition: parselo.cpp:3824
void stuff_float(float *f)
Definition: parselo.cpp:2328
int stuff_float_optional(float *f, bool raw=false)
Definition: parselo.cpp:2343
int string_lookup(char *str1, char *strlist[], int max, char *description=NULL, int say_errors=0)
Definition: parselo.cpp:3187
void stuff_malloc_string(char **dest, int type, char *terminators=NULL)
Definition: parselo.cpp:1419
char * Mission_text
Definition: parselo.cpp:46
int stuff_int_optional(int *i, bool raw=false)
Definition: parselo.cpp:2387
char * alloc_block(char *startstr, char *endstr, int extra_chars=0)
Definition: parselo.cpp:1014
void process_raw_file_text(char *processed_text=NULL, char *raw_text=NULL)
Definition: parselo.cpp:2194
int check_for_eoln()
Definition: parselo.cpp:505
int stuff_string_list(SCP_vector< SCP_string > &slp)
Definition: parselo.cpp:2689
char * Mission_text_raw
Definition: parselo.cpp:47
const char * stristr(const char *str, const char *substr)
Definition: parselo.cpp:3680
Definition: pstypes.h:88
char * get_pointer_to_first_hash_symbol(char *src)
Definition: parselo.cpp:3868
void stuff_boolean_flag(int *i, int flag, bool a_to_eol=true)
Definition: parselo.cpp:2529
int skip_to_string(char *pstr, char *end=NULL)
Definition: parselo.cpp:375
void stuff_vec3d(vec3d *vp)
Definition: parselo.cpp:3082
bool end_string_at_first_hash_symbol(char *src)
Definition: parselo.cpp:3833
GLclampf f
Definition: Glext.h:7097
int parse_modular_table(const char *name_check, void(*parse_callback)(const char *filename), int path_type=CF_TYPE_TABLES, int sort_type=CF_SORT_REVERSE)
Definition: parselo.cpp:4205
GLenum mode
Definition: Glext.h:5794
int is_white_space(char ch)
Definition: parselo.cpp:59
Definition: cfile.h:95
void mark_int_list(int *ilp, int max_ints, int lookup_type)
Definition: parselo.cpp:3022
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
int check_for_eof()
Definition: parselo.cpp:492
void stop_parse()
Definition: parselo.cpp:2071
GLsizeiptr size
Definition: Glext.h:5496
int subsystem_stricmp(const char *str1, const char *str2)
Definition: parselo.cpp:3648
void convert_sexp_to_string(SCP_string &dest, int cur_node, int mode)
Definition: sexp.cpp:3904
void skip_token()
Definition: parselo.cpp:219
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: Glext.h:7308
GLenum type
Definition: Gl.h:1492
int required_string_fred(char *pstr, char *end=NULL)
Definition: parselo.cpp:593
ParseException(const std::string &msg)
Definition: parselo.h:262
int match_and_stuff(int f_type, char *strlist[], int max, char *description)
Definition: parselo.cpp:3243
int get_string(char *str, int max=-1)
Definition: parselo.cpp:1157
void compact_multitext_string(char *str)
Definition: parselo.cpp:1438
GLenum GLint * range
Definition: Glext.h:7096
int get_string_or_variable(char *str)
Definition: parselo.cpp:1084
void format_integer_with_commas(char *buf, int integer, bool use_comma_with_four_digits)
Definition: parselo.cpp:4084
int optional_string_fred(char *pstr, char *end=NULL, char *end2=NULL)
Definition: parselo.cpp:630
int replace_all(char *str, char *oldstr, char *newstr, unsigned int max_len, int range=0)
Definition: parselo.cpp:3941
void pause_parse()
Definition: parselo.cpp:3272
int get_index_of_first_hash_symbol(SCP_string &src)
Definition: parselo.cpp:3882
void stuff_boolean(int *i, bool a_to_eol=true)
Definition: parselo.cpp:2519
void copy_text_until(char *outstr, char *instr, char *endstr, int max_chars)
Definition: parselo.cpp:923
void advance_to_eoln(char *terminators)
Definition: parselo.cpp:335
int stuff_int_list(int *ilp, int max_ints, int lookup_type=RAW_INTEGER_TYPE)
Definition: parselo.cpp:2782
char * filename
int skip_to_start_of_string_either(char *pstr1, char *pstr2, char *end=NULL)
Definition: parselo.cpp:433
char * next_tokens()
Definition: parselo.cpp:245
Definition: cfile.h:50
void error_display(int error_level, char *format,...)
Definition: parselo.cpp:308
int replace_one(char *str, char *oldstr, char *newstr, unsigned int max_len, int range=0)
Definition: parselo.cpp:3889
void stuff_ubyte(ubyte *i)
Definition: parselo.cpp:2646
void stuff_string_until(char *outstr, char *endstr, int len=0)
Definition: parselo.cpp:988
char * stuff_and_malloc_string(int type, char *terminators=NULL)
Definition: parselo.cpp:1406
GLdouble s
Definition: Glext.h:5321
GLsizei const GLchar ** string
Definition: Glext.h:5636
void backspace(char *src)
Definition: parselo.cpp:4069
int scan_fso_version_string(const char *text, int *major, int *minor, int *build, int *revis)
Definition: parselo.cpp:4126
int optional_string(const char *pstr)
Definition: parselo.cpp:539
void stuff_matrix(matrix *mp)
Definition: parselo.cpp:3174
int optional_string_one_of(int arg_count,...)
Definition: parselo.cpp:567
void copy_to_eoln(char *outstr, char *more_terminators, char *instr, int max)
Definition: parselo.cpp:786
void drop_leading_white_space(char *str)
Definition: parselo.cpp:115
void find_and_stuff(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
Definition: parselo.cpp:3207
int Token_found_flag
Definition: parselo.cpp:44
int required_string_either(char *str1, char *str2)
Checks for one of two required strings.
Definition: parselo.cpp:673
void find_and_stuff_optional(char *id, int *addr, int f_type, char *strlist[], int max, char *description)
Definition: parselo.cpp:3229
unsigned char ubyte
Definition: pstypes.h:62
void debug_show_mission_text()
Definition: parselo.cpp:2280
char * drop_white_space(char *str)
Definition: parselo.cpp:159
void stuff_parenthesized_vec3d(vec3d *vp)
Definition: parselo.cpp:3089
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
int stuff_vec3d_list(vec3d *vlp, int max_vecs)
Definition: parselo.cpp:3114
int stuff_loadout_list(int *ilp, int max_ints, int lookup_type)
Definition: parselo.cpp:2881
int skip_to_start_of_string(char *pstr, char *end=NULL)
Definition: parselo.cpp:404
Definition: parselo.h:257
char * split_str_once(char *src, int max_pixel_w)
Definition: parselo.cpp:3330
void stuff_string_white(char *outstr, int len=0)
Definition: parselo.cpp:969
void parse_float_list(float *plist, int size)
Definition: parselo.cpp:4182
int required_string_one_of(int arg_count,...)
Checks for one of any of the given required strings.
Definition: parselo.cpp:708
Definition: globals.h:15
int check_for_string(const char *pstr)
Definition: parselo.cpp:517
int required_string(const char *pstr)
Definition: parselo.cpp:468
const char * token_found
Definition: parselo.cpp:49
void stuff_int(int *i)
Definition: parselo.cpp:2372
void diag_printf(char *format,...)
Definition: parselo.cpp:229
int required_string_either_fred(char *str1, char *str2)
Definition: parselo.cpp:758
int strextcmp(const char *s1, const char *s2)
Definition: parselo.cpp:4013
int optional_string_either(char *str1, char *str2)
Definition: parselo.cpp:551
void truncate_message_lines(SCP_string &text, int num_allowed_lines)
Definition: parselo.cpp:4154
int parse_string_flag_list(int *dest, flag_def_list defs[], int defs_size)
Definition: parselo.cpp:2665
int get_line_num()
Definition: parselo.cpp:261
void ignore_white_space()
Definition: parselo.cpp:77
GLenum src
Definition: Glext.h:5917
void read_raw_file_text(const char *filename, int mode=CF_TYPE_ANY, char *raw_text=NULL)
Definition: parselo.cpp:2127
int stuff_bool_list(bool *blp, int max_bools)
Definition: parselo.cpp:2591
void parse_int_list(int *ilist, int size)
Definition: parselo.cpp:4194
bool can_construe_as_integer(const char *text)
Definition: parselo.cpp:3778
void read_file_text_from_array(const char *array, char *processed_text=NULL, char *raw_text=NULL)
Definition: parselo.cpp:2022
void reset_parse(char *text=NULL)
Definition: parselo.cpp:3305
GLenum GLsizei len
Definition: Glext.h:6283
int fred_parse_flag
Definition: parselo.cpp:43
bool drop_extension(char *str)
Definition: parselo.cpp:4043
void stuff_string_line(char *outstr, int len)
Definition: parselo.cpp:1354
int is_gray_space(char ch)
Definition: parselo.cpp:65
GLenum const GLvoid * addr
Definition: Glext.h:9092
#define CF_TYPE_ANY
Definition: cfile.h:42
void ignore_gray_space()
Definition: parselo.cpp:83
GLuint GLuint end
Definition: Gl.h:1502
void unpause_parse()
Definition: parselo.cpp:3289
Definition: parselo.h:52
char * Mp
Definition: parselo.cpp:48
void display_parse_diagnostics()
Definition: parselo.cpp:3320
void find_and_stuff_or_add(char *id, int *addr, int f_type, char *strlist[], int *total, int max, char *description)
Definition: parselo.cpp:3251