Debian Codegraph @

random_maps/reader.l

raw
1 %{
2 /*
3 * static char *reader_l =
4 * "$Id$";
5 */
6
7 /*
8 CrossFire, A Multiplayer game for X-windows
9
10 Copyright (C) 1994 Mark Wedel
11 Copyright (C) 1992 Frank Tore Johansen
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 The author can be reached via e-mail to [email protected]
28 */
29
30 #include <global.h>
31 #include <loader.h>
32 #include <newserver.h>
33 #include <random_map.h>
34
35 #define YY_DECL int rmap_lex_read(RMParms *RP)
36
37 static char *rmap_yval(void);
38 static int rmap_lex_read(RMParms *RP);
39
40 static int rmap_lex_error;
41
42 #define IVAL atoi(rmap_yval())
43 #define FVAL atof(rmap_yval())
44
45 /**
46 * Error handler.
47 *
48 * @param s
49 * lex-generated error message.
50 */
51 static int yyerror(const char *s)
52 {
53 LOG(llevError, "%s: %s\n", s, yytext);
54 return -1;
55 }
56
57 %}
58
59 S [ \t]+.+
60 WS [ \t]*
61
62 %x MESSAGE
63
64 /* Don't have to link with -lfl with this */
65 %option noyywrap
66
67 /* those functions are unused, not sure of compatibility with Flex versions. */
68 %option nounput
69 %option noyy_top_state
70 %option noinput
71
72 %%
73
74 %{
75 /* Declare some local variables */
76
77 rmap_lex_error = 0;
78
79 %}
80
81 ^wallstyle{S} strncpy(RP->wallstyle, rmap_yval(), RM_SIZE);
82 ^floorstyle{S} strncpy(RP->floorstyle, rmap_yval(), RM_SIZE);
83 ^monsterstyle{S} strncpy(RP->monsterstyle, rmap_yval(), RM_SIZE);
84 ^treasurestyle{S} strncpy(RP->treasurestyle, rmap_yval(), RM_SIZE);
85 ^layoutstyle{S} strncpy(RP->layoutstyle, rmap_yval(), RM_SIZE);
86 ^doorstyle{S} strncpy(RP->doorstyle, rmap_yval(), RM_SIZE);
87 ^decorstyle{S} strncpy(RP->decorstyle, rmap_yval(), RM_SIZE);
88 ^cheststyle{S} strncpy(RP->cheststyle, rmap_yval(), RM_SIZE);
89 ^xsize{S} RP->Xsize = IVAL;
90 ^ysize{S} RP->Ysize = IVAL;
91 ^expand2x{S} RP->expand2x = IVAL;
92 ^layoutoptions1{S} RP->layoutoptions1 = IVAL;
93 ^layoutoptions2{S} RP->layoutoptions2 = IVAL;
94 ^symmetry{S} RP->symmetry = IVAL;
95 ^difficulty{S} RP->difficulty = IVAL;
96 ^difficulty_increase{S} RP->difficulty_increase = FVAL;
97 ^decoroptions{S} RP->decoroptions = IVAL;
98 ^exitstyle{S} strncpy(RP->exitstyle, rmap_yval(), RM_SIZE);
99 ^dungeon_level{S} RP->dungeon_level = IVAL;
100 ^dungeon_name{S} strncpy(RP->dungeon_name, rmap_yval(), RM_SIZE);
101 ^dungeon_depth{S} RP->dungeon_depth = IVAL;
102 ^final_map{S} strncpy(RP->final_map, rmap_yval(), RM_SIZE);
103 ^final_exit_archetype{S} strncpy(RP->final_exit_archetype, rmap_yval(), RM_SIZE);
104 ^orientation{S} RP-> orientation = IVAL;
105 ^origin_x{S} RP->origin_x = IVAL;
106 ^origin_y{S} RP-> origin_y = IVAL;
107 ^origin_map{S} strncpy(RP->origin_map, rmap_yval(), RM_SIZE);
108 ^random_seed{S} RP->random_seed = IVAL;
109 ^treasureoptions{S} RP->treasureoptions = IVAL;
110 ^exit_on_final_map{S} strncpy(RP->exit_on_final_map, rmap_yval(), RM_SIZE);
111 ^multiple_floors{S} RP->multiple_floors = IVAL;
112
113 <*>(^{WS}$)|\n {/* ignore empty lines, newlines we don't do above */}
114 #.*\n { }
115
116 <<EOF>> {/* If we got an error, return the error. Otherwise, return that we got EOF */
117 if (rmap_lex_error != 0)
118 return rmap_lex_error;
119 else
120 return LL_EOF;
121 }
122 .* {
123 yyerror("Unrecognized string");
124 rmap_lex_error= -1;
125 }
126 %%
127
128 /* Our save file syntax is very simple, so we can use a very simple
129 * processing mechanism here instead using something like bison
130 * This skips over the space and returns the value, or "" if no value
131 * is found.
132 */
133 static char *rmap_yval(void) {
134 static char em[] = "";
135 char *cp;
136
137 cp = strchr(yytext, ' ');
138 if (cp)
139 return cp+1;
140 else
141 return em;
142 }
143
144 int load_parameters(FILE *fp, int bufstate, RMParms *RP) {
145 int retval;
146 char inbuf[MAX_BUF];
147
148 if (bufstate == LO_NEWFILE || bufstate == LO_NOREAD) {
149 yy_delete_buffer(YY_CURRENT_BUFFER);
150 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
151 if (bufstate == LO_NOREAD)
152 return LL_NORMAL;
153 }
154 if (bufstate == LO_LINEMODE) {
155 YY_BUFFER_STATE yybufstate;
156 while (fgets(inbuf, MAX_BUF-3, fp)) {
157 yybufstate = yy_scan_string(inbuf);
158 retval = rmap_lex_read(RP);
159 yy_delete_buffer(yybufstate);
160 if (retval == LL_NORMAL)
161 return retval;
162 }
163 return LL_EOF;
164 }
165
166 retval = rmap_lex_read(RP);
167 /* LOG(llevDebug, " load completed, object=%s\n", op->name);*/
168 return retval;
169 }
170
171
172 /* This takes a buffer, scans it for variables, and sets those variables
173 * as appropriate in op.
174 *
175 * This function appears to be used in only 2 places - in crossedit to
176 * override values and in c_wiz to mutate values.
177 */
178 int set_random_map_variable(RMParms *rp, const char *buf) {
179 YY_BUFFER_STATE yybufstate;
180 int retval;
181
182 yybufstate = yy_scan_string(buf);
183 retval = rmap_lex_read(rp);
184 yy_delete_buffer(yybufstate);
185 return retval;
186 }