2 * SDLEMU library - Free sdl related functions library
3 * Copyrigh(c) 1999-2002 sdlemu development crew
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "sdlemu_config.h"
22 #include <stdlib.h> // For atoi()
23 #include <stdio.h> // For fopen() and friends
24 #include <cstring> // For strcmp()
33 token_list(const string &name) : m_name(name), m_value(""), m_token("") {}
34 void add_token_variable(const string &var) { m_token = var; }
35 void add_token_value(const string &value) { m_value = value; }
36 const string &LineName() const { return m_name; }
37 const string &Token() const { return m_token; }
38 const string &Value() const { return m_value; }
45 std::list<token_list> vec;
47 void string_tokenize_variable()
49 list<token_list>::iterator p;
50 const string delim = " ";
51 for(p = vec.begin(); p != vec.end(); p++) {
52 string::size_type lastPos = (*p).LineName().find_first_not_of(delim, 0);
53 string::size_type pos = (*p).LineName().find_first_of(delim, lastPos);
55 if(string::npos != pos && string::npos != lastPos) {
56 string s = (*p).LineName().substr(lastPos, pos - lastPos);
57 (*p).add_token_variable(s);
62 void string_tokenize_value()
64 list<token_list>::iterator p;
65 const string delim = " =\n\t\r"; // "\r" needed for Win32 compatibility...
67 for(p = vec.begin(); p != vec.end(); p++) {
68 string::size_type lastPos = (*p).LineName().find_first_of(delim, 0);
69 string::size_type pos = (*p).LineName().find_first_not_of(delim, lastPos);
71 if(string::npos != pos && string::npos != lastPos) {
72 string s = (*p).LineName().substr(pos);
73 (*p).add_token_value(s);
78 int sdlemu_init_config(const char *filename)
80 FILE *f = fopen(filename, "r");
83 fseek(f, 0, SEEK_END);
85 fseek(f, 0, SEEK_SET);
87 char *s = new char[len];
91 const string delim = "\n\r"; // "\r" needed for Win32 compatibility...
92 string::size_type lastPos = str.find_first_not_of(delim, 0);
93 string::size_type pos = str.find_first_of(delim, lastPos);
95 while (string::npos != pos || string::npos != lastPos) {
96 string string = str.substr(lastPos, pos - lastPos);
100 else if(string[0] == '[')
105 vec.push_back(string);
107 lastPos = str.find_first_not_of(delim, pos);
108 pos = str.find_first_of(delim, lastPos);
110 string_tokenize_variable();
111 string_tokenize_value();
117 const char *sdlemu_getval_string(const char *key_string, const char *default_string)
119 list<token_list>::iterator p;
120 for(p = vec.begin(); p != vec.end(); p++) {
122 if(strcmp((*p).Token().c_str(), key_string) == 0)
123 return (*p).Value().c_str();
125 return default_string;
128 int sdlemu_getval_int(const char *key_string, int default_int)
130 list<token_list>::iterator p;
131 for(p = vec.begin(); p != vec.end(); p++) {
133 if(strcmp((*p).Token().c_str(), key_string) == 0) {
134 const char *ret = (*p).Value().c_str();
135 if(ret) return atoi(ret);
141 int sdlemu_getval_bool(const char *key_string, int default_int)
143 list<token_list>::iterator p;
144 for(p = vec.begin(); p != vec.end(); p++) {
146 if(strcmp((*p).Token().c_str(), key_string) == 0) {
147 const char *ret = (*p).Value().c_str();
148 if(ret) return atoi(ret)>0;