File: emdlparser.cpp

package info (click to toggle)
epiphany 0.7.0+0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,532 kB
  • sloc: cpp: 6,404; ansic: 141; makefile: 78; sh: 7
file content (276 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download | duplicates (6)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/***************************************************************************
                    emdlparser.cpp  - implements emdlparser
                             -------------------
    begin                : Mar Jan 14 2003
    copyright            : (C) 2003 by Giuseppe "denever" Martino
    email                : rdmartin@infinito.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License, Version 2,      *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

#include "emdlparser.h"

Entity_Type map_entity(const std::string& str)
{
	if(str == "grass")
		return	GRASS;
	if(str == "player")
		return	PLAYER;
	if(str == "steel")
		return STEEL;
	if(str == "emerald")
		return EMERALD;
	if(str == "boulder")
		return BOULDER;
	if(str == "sapphire")
		return SAPPHIRE;
	if(str == "exit")
		return EXIT;
	if(str == "explosion")
		return EXPLOSION;
	if(str == "door")
		return DOOR;
	if(str == "red_door")
		return DOOR_RED;
	if(str == "blue_door")
		return DOOR_BLUE;
	if(str == "green_door")
		return DOOR_GREEN;
	if(str == "yellow_door")
		return DOOR_YELLOW;
	if(str == "key")
		return KEY;
	if(str == "red_key")
		return KEY_RED;
	if(str == "blue_key")
		return KEY_BLUE;
	if(str == "green_key")
		return KEY_GREEN;
	if(str == "yellow_key")
		return KEY_YELLOW;
	if(str == "flintstone")
		return FLINTSTONE;
	if(str == "peperon")
		return PEPERON;
	if(str == "peperon_up")
		return PEPERON_UP;
	if(str == "peperon_right")
		return PEPERON_RIGHT;
	if(str == "peperon_down")
		return PEPERON_DOWN;
	if(str == "peperon_left")
		return PEPERON_LEFT;
	if(str == "brick")
		return BRICK;
	if(str == "wood")
		return  WOOD;
	if(str == "tomato")
		return TOMATO;
	if(str == "tomato_up")
		return TOMATO_UP;
	if(str == "tomato_right")
		return TOMATO_RIGHT;
	if(str == "tomato_down")
		return TOMATO_DOWN;
	if(str == "tomato_left")
		return TOMATO_LEFT;
	return UNKNOWN;
}

EMDLParser::EMDLParser()
{
	clear();
}

void EMDLParser::clear()
{
	//m_command.clear();
	m_command = "";

	//m_object.clear();
	m_object = "";

	//m_string_value.clear();
	m_string_value = "";
	
	m_x0 = 0;
	m_x1 = 0;
	
	m_y0 = 0;
	m_y1 = 0;
	
	m_value = 0;
}

void EMDLParser::operator<<(std::ifstream& stream)
{
	char ch;
	std::string alfa;

//    alfa.clear(); //ONLY ON GCC 3.x
	alfa = "";

	bool comment = false;
	bool string_value = false;
	bool first_coord = true;
	bool command = true;

	while(stream.get(ch))
		switch(ch)
		{
		case '(':
			if(!comment)
			{
				if(first_coord)
					stream>>m_x0;
				else
					stream>>m_x1;
		
				stream.get(ch);

				if(ch != ',')
				{
					handle_sintaxerror_comma();
					break;
				}
	
				if(first_coord)
					stream>>m_y0;
				else
					stream>>m_y1;
		
				stream.get(ch);
				if(ch != ')')
				{
					handle_sintaxerror_par();
					break;
				}

				if(first_coord)
					first_coord = false;
				break;
			}

		case '#':
			comment = true;
			break;

		case '"':
			if(!comment)
				if(!string_value)
					string_value = true;
				else
					if(!alfa.empty())
					{
						string_value = false;
						m_string_value = alfa;

						//alfa.clear(); //ONLY ON GCC 3.x
						alfa = "";
					}
			break;

		case '\n':
			comment = false;
			first_coord = true;
			command = true;
			break;	

		case ';':
		{
			if(command)
			m_command = alfa;
	    
			// alfa.clear(); //ONLY ON GCC 3.x
			alfa = "";
   
			if(!comment && !m_command.empty())
			    read(m_command[0]);
			
			break;
		}
		case ' ':
			if(!comment && !string_value && !alfa.empty())
			{
				if(command)
				{
					m_command = alfa;
		    
					//alfa.clear(); //ONLY GCC 3.x
					alfa = "";
		    
					command = false;
					break;
				}
				else
				{
					m_object = alfa;
		    
					//alfa.clear(); //ONLY ON GCC 3.x
					alfa = "";
				}
			}
	    
		default:
			if(!comment)
				if(!isdigit(ch))
					alfa += ch;
				else
				{
					stream.putback(ch);
					stream >> m_value;
				}
		}
}


void EMDLParser::read(char ch)
{
    switch(ch)
    {
    case 's':
	if(!m_string_value.empty())
	    handle_command_set_str(m_object, m_string_value);
	else
	    handle_command_set_int(m_object, m_value);
	break;
    case 'p':
    {
	Entity_Type e = map_entity(m_object);
	handle_command_put(e,m_x0,m_y0);
	break;
    }
    case 'l':
    {
	Entity_Type e = map_entity(m_object);
	handle_command_line(e, m_x0, m_y0, m_x1, m_y1);
	break;
    }
    case 'r':
    {
	Entity_Type e = map_entity(m_object);
	handle_command_rect(e, m_x0, m_y0, m_x1, m_y1);
	break;
    }
    case 'v':
    {
	handle_command_view();
	break;
    }
    case 'w':
    {
	handle_command_write();
	break;
    }
    default:
	break;
    };
    
    clear();
}