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