File: reader.l

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (186 lines) | stat: -rw-r--r-- 5,869 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
%{
/*
 * static char *reader_l =
 *   "$Id$";
 */

/*
    CrossFire, A Multiplayer game for X-windows

    Copyright (C) 1994 Mark Wedel
    Copyright (C) 1992 Frank Tore Johansen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    The author can be reached via e-mail to mark@pyramid.com
*/

#include <global.h>
#include <loader.h>
#include <newserver.h>
#include <random_map.h>

#define YY_DECL int rmap_lex_read(RMParms *RP)

static char *rmap_yval(void);
static int rmap_lex_read(RMParms *RP);

static int rmap_lex_error;

#define IVAL atoi(rmap_yval())
#define FVAL atof(rmap_yval())

/**
 * Error handler.
 *
 * @param s
 * lex-generated error message.
 */
static int yyerror(const char *s)
{
    LOG(llevError, "%s: %s\n", s, yytext);
    return -1;
}

%}

S       [ \t]+.+
WS      [ \t]*

%x MESSAGE

/* Don't have to link with -lfl with this */
%option noyywrap

/* those functions are unused, not sure of compatibility with Flex versions. */
%option nounput
%option noyy_top_state
%option noinput

%%

%{
/* Declare some local variables */

    rmap_lex_error = 0;

%}

^wallstyle{S}            strncpy(RP->wallstyle, rmap_yval(), RM_SIZE);
^floorstyle{S}           strncpy(RP->floorstyle, rmap_yval(), RM_SIZE);
^monsterstyle{S}         strncpy(RP->monsterstyle, rmap_yval(), RM_SIZE);
^treasurestyle{S}        strncpy(RP->treasurestyle, rmap_yval(), RM_SIZE);
^layoutstyle{S}          strncpy(RP->layoutstyle, rmap_yval(), RM_SIZE);
^doorstyle{S}            strncpy(RP->doorstyle, rmap_yval(), RM_SIZE);
^decorstyle{S}           strncpy(RP->decorstyle, rmap_yval(), RM_SIZE);
^cheststyle{S}           strncpy(RP->cheststyle, rmap_yval(), RM_SIZE);
^xsize{S}                RP->Xsize = IVAL;
^ysize{S}                RP->Ysize = IVAL;
^expand2x{S}             RP->expand2x = IVAL;
^layoutoptions1{S}       RP->layoutoptions1 = IVAL;
^layoutoptions2{S}       RP->layoutoptions2 = IVAL;
^symmetry{S}             RP->symmetry = IVAL;
^difficulty{S}           RP->difficulty = IVAL;
^difficulty_increase{S}  RP->difficulty_increase = FVAL;
^decoroptions{S}         RP->decoroptions = IVAL;
^exitstyle{S}            strncpy(RP->exitstyle, rmap_yval(), RM_SIZE);
^dungeon_level{S}        RP->dungeon_level = IVAL;
^dungeon_name{S}         strncpy(RP->dungeon_name, rmap_yval(), RM_SIZE);
^dungeon_depth{S}        RP->dungeon_depth = IVAL;
^final_map{S}            strncpy(RP->final_map, rmap_yval(), RM_SIZE);
^final_exit_archetype{S} strncpy(RP->final_exit_archetype, rmap_yval(), RM_SIZE);
^orientation{S}          RP-> orientation = IVAL;
^origin_x{S}             RP->origin_x = IVAL;
^origin_y{S}             RP-> origin_y = IVAL;
^origin_map{S}           strncpy(RP->origin_map, rmap_yval(), RM_SIZE);
^random_seed{S}          RP->random_seed = IVAL;
^treasureoptions{S}      RP->treasureoptions = IVAL;
^exit_on_final_map{S}    strncpy(RP->exit_on_final_map, rmap_yval(), RM_SIZE);
^multiple_floors{S}      RP->multiple_floors = IVAL;

<*>(^{WS}$)|\n           {/* ignore empty lines, newlines we don't do above */}
#.*\n                    { }

<<EOF>>                  {/* If we got an error, return the error.  Otherwise, return that we got EOF */
                             if (rmap_lex_error != 0)
                                 return rmap_lex_error;
                             else
                                 return LL_EOF;
                         }
.*                       {
                             yyerror("Unrecognized string");
                             rmap_lex_error= -1;
                         }
%%

/* Our save file syntax is very simple, so we can use a very simple
 * processing mechanism here instead using something like bison
 * This skips over the space and returns the value, or "" if no value
 * is found.
 */
static char *rmap_yval(void) {
    static char em[] = "";
    char *cp;

    cp = strchr(yytext, ' ');
    if (cp)
        return cp+1;
    else
        return em;
}

int load_parameters(FILE *fp, int bufstate, RMParms *RP) {
    int retval;
    char inbuf[MAX_BUF];

    if (bufstate == LO_NEWFILE || bufstate == LO_NOREAD) {
        yy_delete_buffer(YY_CURRENT_BUFFER);
        yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
        if (bufstate == LO_NOREAD)
            return LL_NORMAL;
    }
    if (bufstate == LO_LINEMODE) {
        YY_BUFFER_STATE yybufstate;
        while (fgets(inbuf, MAX_BUF-3, fp)) {
            yybufstate = yy_scan_string(inbuf);
            retval = rmap_lex_read(RP);
            yy_delete_buffer(yybufstate);
            if (retval == LL_NORMAL)
                return retval;
        }
        return LL_EOF;
    }

    retval = rmap_lex_read(RP);
/*    LOG(llevDebug, " load completed, object=%s\n", op->name);*/
    return retval;
}


/* This takes a buffer, scans it for variables, and sets those variables
 * as appropriate in op.
 *
 * This function appears to be used in only 2 places - in crossedit to
 * override values and in c_wiz to mutate values.
 */
int set_random_map_variable(RMParms *rp, const char *buf) {
    YY_BUFFER_STATE yybufstate;
    int retval;

    yybufstate = yy_scan_string(buf);
    retval = rmap_lex_read(rp);
    yy_delete_buffer(yybufstate);
    return retval;
}