1
// $Id: config.cxx,v 1.13 2003/04/10 18:28:30 grumbel Exp $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "globals.hxx"
24
#include "pingus_error.hxx"
28
#include "my_gettext.hxx"
30
struct ConfigParserEOF {};
32
// Create a PLF object and start parsing the given file
33
ConfigParser::ConfigParser()
38
ConfigParser::~ConfigParser()
43
ConfigParser::init(std::string filename)
55
catch (PingusError& err) {
56
std::cout << err.get_message () << std::endl;
60
// Open the file and do some error checking.
62
ConfigParser::open(std::string filename)
64
in.open(filename.c_str());
68
PingusError::raise(_("Couldn't open: ") + filename);
71
std::cout << "Successfully opened plf file." << std::endl;
74
// Return the next char from file and check for eof.
76
ConfigParser::get_char(void)
81
if (verbose > 1) std::cout << "ConfigParser: Result of get_char() will be undefined" << std::endl;
82
PingusError::raise("");
88
if (verbose > 1) std::cout << "PLF::get_char(): Found EOF!" << std::endl;
90
throw ConfigParserEOF();
99
// Return the next char from file, remove all comments and spaces before.
101
ConfigParser::get_raw_atom(void)
109
while(get_char() != '\n'); // Ignoring until EOL
120
while (isspace(c = get_char()));
122
if (isspace(last_atom))
130
// Return the next atom and keep it.
132
ConfigParser::get_atom(void)
134
last_atom = get_raw_atom();
139
// Return the name of the value identiver.
141
ConfigParser::get_valueid(void)
152
if (isgraph(atom) && atom != '=')
156
else if (isspace(atom))
160
else if (atom == '=')
167
syntax_error(std::string(_("Unexpected char: '")) + atom + "'");
175
ConfigParser::get_value(void)
185
if (atom == '"' && ret_val == "") {
186
while((atom = get_char()) != '"') {
197
if (!isalnum(atom) && atom != '-' && atom != '_') {
201
syntax_error(std::string(_("Unexpected char '")) + atom + "'");
211
// Jumps to the position of the next token after 'a', no other char's
212
// then spaces are allowed before 'a'.
214
ConfigParser::jump_after(char c)
221
if (isspace(atom) || atom == c)
231
syntax_error(std::string("jump_after(): Expected '") + c + "', got '" + atom + "'" );
235
ConfigParser::jump(void)
247
// Print a error message with lineno and filename.
249
ConfigParser::syntax_error(std::string error = "")
251
std::string error_str;
254
snprintf(tmp, 16, "%d\n", lineno);
256
error_str = std::string("PLF: Syntax Error at line ") + tmp;
259
error_str += "PLF:" + error + "\n";
261
PingusError::raise(error_str);
264
// Parse the file and fill all structs with the values.
266
ConfigParser::parse(void)
275
valueid = get_valueid();
279
set_value(valueid, value);
282
catch (ConfigParserEOF)
292
Config::Config(std::string f)
299
Config::set_value(const std::string& valueid, const std::string& value)
301
if (valueid == "fullscreen")
303
fullscreen_enabled = str_to_bool(value);
305
else if (valueid == "tile-size")
307
tile_size = str_to_int(value);
309
else if (valueid == "game-speed")
311
game_speed = str_to_int(value);
313
else if (valueid == "print-fps")
315
print_fps = str_to_bool(value);
317
else if (valueid == "fast-mode")
319
fast_mode = str_to_bool(value);
321
else if (valueid == "maintainer-mode")
323
maintainer_mode = str_to_bool(value);
325
else if (valueid == "unlimited-actions")
327
Cheat::unlimited_actions = str_to_bool(value);
329
else if (valueid == "cursor-enabled")
331
cursor_enabled = str_to_bool(value);
333
else if (valueid == "auto-scrolling")
335
auto_scrolling = str_to_bool(value);
337
else if (valueid == "verbose")
339
verbose = str_to_int(value);
341
else if (valueid == "width")
343
screen_width = str_to_int(value);
345
else if (valueid == "height")
347
screen_height = str_to_int(value);
349
else if (valueid == "sound")
351
sound_enabled = str_to_bool(value);
353
else if (valueid == "music")
355
music_enabled = str_to_bool(value);
357
else if (valueid == "swcursor-enabled")
359
swcursor_enabled = str_to_bool(value);
361
else if (valueid == "username")
363
global_username = valueid;
365
else if (valueid == "email")
367
global_email = valueid;
371
PingusError::raise("Config: Unknown valueid: " + valueid);
376
Config::str_to_bool(const std::string& str)
378
if (str == "true" || str == "1")
382
else if (str == "false" || str == "0")
388
PingusError::raise("Config: value: " + str + " is not of type bool.");
391
return false; // never reached
395
Config::str_to_int(const std::string& str)
399
if (sscanf(str.c_str(), "%d", &ret_val) != 1)
401
PingusError::raise("Config: Couldn't convert std::string to integer: " + str);