]> Shamusworld >> Repos - virtualjaguar/blob - src/sdlemu_config.cpp
539de4098c099f74b167120bd42a84a25f7e33a5
[virtualjaguar] / src / sdlemu_config.cpp
1 /*
2  * SDLEMU library - Free sdl related functions library
3  * Copyrigh(c) 1999-2002 sdlemu development crew
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "sdlemu_config.h"
21
22 #include <stdio.h>
23 #include <string>
24 #include <list>
25
26 using namespace std;
27
28 class token_list
29 {
30 public:
31         token_list(const string &name) : m_name(name), m_value(""), m_token("") {}
32         void add_token_variable(const string &var) { m_token = var; }
33         void add_token_value(const string &value) { m_value = value; }
34         const string &LineName() const { return m_name; }
35         const string &Token() const { return m_token; }
36         const string &Value() const { return m_value; }
37 private:
38         std::string m_name;
39         std::string m_value;
40         std::string m_token;
41 };
42
43 std::list<token_list> vec;
44
45 void string_tokenize_variable()
46 {
47         list<token_list>::iterator p;
48         const string delim = " ";
49         for(p = vec.begin(); p != vec.end(); p++) {
50                 string::size_type lastPos = (*p).LineName().find_first_not_of(delim, 0);
51                 string::size_type pos     = (*p).LineName().find_first_of(delim, lastPos);
52
53                 if(string::npos != pos && string::npos != lastPos) {
54                         string s = (*p).LineName().substr(lastPos, pos - lastPos);
55                         (*p).add_token_variable(s);
56                 }
57         }
58 }
59
60 void string_tokenize_value()
61 {
62         list<token_list>::iterator p;
63         const string delim = " =\n\t\r";                // "\r" needed for Win32 compatibility...
64
65         for(p = vec.begin(); p != vec.end(); p++) {
66                 string::size_type lastPos = (*p).LineName().find_first_of(delim, 0);
67                 string::size_type pos     = (*p).LineName().find_first_not_of(delim, lastPos);
68
69                 if(string::npos != pos && string::npos != lastPos) {
70                         string s = (*p).LineName().substr(pos);
71                         (*p).add_token_value(s);
72                 }
73         }
74 }
75
76 int sdlemu_init_config(const char *filename)
77 {
78         FILE *f = fopen(filename, "r");
79         if(!f) return 0;
80         
81         fseek(f, 0, SEEK_END);
82         int len = ftell(f);
83         fseek(f, 0, SEEK_SET);
84         
85         char *s = new char[len];
86         fread(s, 1, len, f);
87         string str(s);
88
89         const string delim = "\n\r";            // "\r" needed for Win32 compatibility...
90         string::size_type lastPos = str.find_first_not_of(delim, 0);
91         string::size_type pos     = str.find_first_of(delim, lastPos);
92
93         while (string::npos != pos || string::npos != lastPos) {
94                 string string = str.substr(lastPos, pos - lastPos);
95                 if(string[0] == '#')
96                 {
97                 }
98                 else if(string[0] == '[')
99                 {
100                 }
101                 else
102                 {
103                         vec.push_back(string);
104                 }
105                 lastPos = str.find_first_not_of(delim, pos);
106                 pos = str.find_first_of(delim, lastPos);
107         }
108         string_tokenize_variable();
109         string_tokenize_value();
110         delete [] s;
111         fclose(f);
112         return 1;
113 }
114
115 const char *sdlemu_getval_string(const char *key_string, const char *default_string)
116 {
117         list<token_list>::iterator p;
118         for(p = vec.begin(); p != vec.end(); p++) {
119                 
120                 if(strcmp((*p).Token().c_str(), key_string) == 0)
121                         return (*p).Value().c_str();
122         }
123         return default_string;
124 }
125
126 int sdlemu_getval_int(const char *key_string, int default_int)
127 {
128         list<token_list>::iterator p;
129         for(p = vec.begin(); p != vec.end(); p++) {
130                 
131                 if(strcmp((*p).Token().c_str(), key_string) == 0) {
132                         const char *ret = (*p).Value().c_str();
133                         if(ret) return atoi(ret);
134                 }
135         }
136         return default_int;
137 }
138
139 int sdlemu_getval_bool(const char *key_string, int default_int)
140 {
141         list<token_list>::iterator p;
142         for(p = vec.begin(); p != vec.end(); p++) {
143                 
144                 if(strcmp((*p).Token().c_str(), key_string) == 0) {
145                         const char *ret = (*p).Value().c_str();
146                         if(ret) return atoi(ret)>0;
147                 }
148         }
149         return default_int;
150 }