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