Diffstat (limited to 'noncore/games/sfcave-sdl/settings.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/settings.cpp | 98 |
1 files changed, 48 insertions, 50 deletions
diff --git a/noncore/games/sfcave-sdl/settings.cpp b/noncore/games/sfcave-sdl/settings.cpp index 914c4ec..20cce4f 100644 --- a/noncore/games/sfcave-sdl/settings.cpp +++ b/noncore/games/sfcave-sdl/settings.cpp | |||
@@ -1,77 +1,65 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <sys/stat.h> | 3 | #include <sys/stat.h> |
4 | #include <vector> | 4 | #include <vector> |
5 | 5 | ||
6 | #include "settings.h" | 6 | #include "settings.h" |
7 | 7 | ||
8 | // Defined in util.h | ||
9 | string getHomeDir(); | ||
8 | 10 | ||
9 | #define DEFAULT_DIR "." | 11 | #define DEFAULT_DIR "." |
10 | #define DEFAULT_FILE "Settings.cfg" | 12 | #define DEFAULT_FILE "Settings.cfg" |
11 | #define MAX_LINE_SIZE 2048 | 13 | #define MAX_LINE_SIZE 2048 |
12 | 14 | ||
13 | 15 | ||
14 | Settings::Settings( char * env_file, char * env_dir ) | 16 | Settings::Settings( char * env_file, char * env_dir ) |
15 | { | 17 | { |
16 | // Store the correct environment directory | 18 | // Store the correct environment directory |
17 | if (env_dir == NULL) | 19 | if (env_dir == NULL) |
18 | { | 20 | { |
19 | char * homeDir = getenv( "HOME" );; | 21 | envFile = getHomeDir(); |
20 | 22 | envFile.append("/"); | |
21 | if ( homeDir ) | ||
22 | { | ||
23 | envFile.append(homeDir); | ||
24 | envFile.append("/"); | ||
25 | } | ||
26 | else | ||
27 | printf( "Environment var HOME not set!\n" ); | ||
28 | 23 | ||
29 | envFile.append(DEFAULT_DIR); | 24 | envFile.append(DEFAULT_DIR); |
30 | } | 25 | } |
31 | else | 26 | else |
32 | envFile.append(env_dir); | 27 | envFile.append(env_dir); |
33 | 28 | ||
34 | envFile.append("/"); | 29 | envFile.append("/"); |
35 | 30 | ||
36 | // Store the correct environment file | 31 | // Store the correct environment file |
37 | if (env_file == NULL) | 32 | if (env_file == NULL) |
38 | envFile.append(DEFAULT_FILE); | 33 | envFile.append(DEFAULT_FILE); |
39 | else | 34 | else |
40 | envFile.append(env_file); | 35 | envFile.append(env_file); |
41 | } | 36 | } |
42 | 37 | ||
43 | Settings::Settings() | 38 | Settings::Settings() |
44 | { | 39 | { |
45 | char * homeDir = getenv("HOME"); | 40 | envFile = getHomeDir(); |
46 | 41 | envFile.append("/"); | |
47 | if ( homeDir) | ||
48 | { | ||
49 | envFile.append(homeDir); | ||
50 | envFile.append("/"); | ||
51 | } | ||
52 | else | ||
53 | printf( "Environment var HOME not set!\n" ); | ||
54 | 42 | ||
55 | envFile.append(DEFAULT_DIR); | 43 | envFile.append(DEFAULT_DIR); |
56 | envFile.append("/"); | 44 | envFile.append("/"); |
57 | 45 | ||
58 | envFile.append(DEFAULT_FILE); | 46 | envFile.append(DEFAULT_FILE); |
59 | } | 47 | } |
60 | 48 | ||
61 | Settings::~Settings() | 49 | Settings::~Settings() |
62 | { | 50 | { |
63 | } | 51 | } |
64 | 52 | ||
65 | bool Settings::readSetting(const string key_str,int& result) | 53 | bool Settings::readSetting(const string key_str,int& result) |
66 | { | 54 | { |
67 | string Buffer; | 55 | string Buffer; |
68 | if (readSetting(key_str,Buffer)) | 56 | if (readSetting(key_str,Buffer)) |
69 | { | 57 | { |
70 | result = atoi(Buffer.c_str()); | 58 | result = atoi(Buffer.c_str()); |
71 | return true; | 59 | return true; |
72 | } | 60 | } |
73 | else | 61 | else |
74 | return false; | 62 | return false; |
75 | } | 63 | } |
76 | 64 | ||
77 | bool Settings::readSetting(const string key_str,unsigned int& result) | 65 | bool Settings::readSetting(const string key_str,unsigned int& result) |
@@ -89,48 +77,60 @@ bool Settings::readSetting(const string key_str,unsigned int& result) | |||
89 | bool Settings::readSetting(const string key_str,long int& result) | 77 | bool Settings::readSetting(const string key_str,long int& result) |
90 | { | 78 | { |
91 | string Buffer; | 79 | string Buffer; |
92 | if (readSetting(key_str,Buffer)) | 80 | if (readSetting(key_str,Buffer)) |
93 | { | 81 | { |
94 | result = atol(Buffer.c_str()); | 82 | result = atol(Buffer.c_str()); |
95 | return true; | 83 | return true; |
96 | } | 84 | } |
97 | else | 85 | else |
98 | return false; | 86 | return false; |
99 | } | 87 | } |
100 | 88 | ||
101 | bool Settings::readSetting(const string key_str,unsigned long& result) | 89 | bool Settings::readSetting(const string key_str,unsigned long& result) |
102 | { | 90 | { |
103 | string Buffer; | 91 | string Buffer; |
104 | if (readSetting(key_str,Buffer)) | 92 | if (readSetting(key_str,Buffer)) |
105 | { | 93 | { |
106 | result = atol(Buffer.c_str()); | 94 | result = atol(Buffer.c_str()); |
107 | return true; | 95 | return true; |
108 | } | 96 | } |
109 | else | 97 | else |
110 | return false; | 98 | return false; |
111 | } | 99 | } |
112 | 100 | ||
101 | bool Settings::readSetting(const string key_str,double& result) | ||
102 | { | ||
103 | string Buffer; | ||
104 | if (readSetting(key_str,Buffer)) | ||
105 | { | ||
106 | result = atof( Buffer.c_str() ); | ||
107 | return true; | ||
108 | } | ||
109 | else | ||
110 | return false; | ||
111 | } | ||
112 | |||
113 | bool Settings::readSetting(const string key_str,bool& result) | 113 | bool Settings::readSetting(const string key_str,bool& result) |
114 | { | 114 | { |
115 | string Buffer; | 115 | string Buffer; |
116 | if (readSetting(key_str,Buffer)) | 116 | if (readSetting(key_str,Buffer)) |
117 | { | 117 | { |
118 | result = (Buffer == "true"); | 118 | result = (Buffer == "true"); |
119 | return true; | 119 | return true; |
120 | } | 120 | } |
121 | else | 121 | else |
122 | return false; | 122 | return false; |
123 | } | 123 | } |
124 | 124 | ||
125 | bool Settings::readSetting(const string key_str,string& results) | 125 | bool Settings::readSetting(const string key_str,string& results) |
126 | { | 126 | { |
127 | // This function will read a string from the env file that corresponds to the | 127 | // This function will read a string from the env file that corresponds to the |
128 | // key key_str passed in. | 128 | // key key_str passed in. |
129 | FILE * fd = 0; | 129 | FILE * fd = 0; |
130 | char buf[MAX_LINE_SIZE]; | 130 | char buf[MAX_LINE_SIZE]; |
131 | bool ret_flag = false; | 131 | bool ret_flag = false; |
132 | char* key; | 132 | char* key; |
133 | char* value; | 133 | char* value; |
134 | 134 | ||
135 | // open file | 135 | // open file |
136 | fd = fopen(envFile.c_str(), "r"); | 136 | fd = fopen(envFile.c_str(), "r"); |
@@ -138,136 +138,134 @@ bool Settings::readSetting(const string key_str,string& results) | |||
138 | if (fd) | 138 | if (fd) |
139 | { | 139 | { |
140 | while (fgets(buf, MAX_LINE_SIZE-1, fd)) | 140 | while (fgets(buf, MAX_LINE_SIZE-1, fd)) |
141 | { | 141 | { |
142 | key = strtok(buf, "\t"); | 142 | key = strtok(buf, "\t"); |
143 | value = strtok(NULL, "\n"); | 143 | value = strtok(NULL, "\n"); |
144 | // find key in file | 144 | // find key in file |
145 | if (!strcasecmp(key,key_str.c_str())) | 145 | if (!strcasecmp(key,key_str.c_str())) |
146 | { | 146 | { |
147 | results = value; | 147 | results = value; |
148 | ret_flag = true; | 148 | ret_flag = true; |
149 | } | 149 | } |
150 | } | 150 | } |
151 | fclose(fd); | 151 | fclose(fd); |
152 | } | 152 | } |
153 | 153 | ||
154 | return(ret_flag); | 154 | return(ret_flag); |
155 | } | 155 | } |
156 | 156 | ||
157 | void Settings::writeSetting(const string key_str,const bool value) | 157 | void Settings::writeSetting(const string key_str,const bool value) |
158 | { | 158 | { |
159 | value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); | 159 | value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); |
160 | } | 160 | } |
161 | 161 | ||
162 | void Settings::writeSetting(const string key_str,const double value) | ||
163 | { | ||
164 | char Buffer[30]; | ||
165 | |||
166 | sprintf(Buffer,"%lf",value); | ||
167 | writeSetting(key_str,string(Buffer)); | ||
168 | } | ||
169 | |||
162 | void Settings::writeSetting(const string key_str,const int value) | 170 | void Settings::writeSetting(const string key_str,const int value) |
163 | { | 171 | { |
164 | char Buffer[30]; | 172 | char Buffer[30]; |
165 | 173 | ||
166 | sprintf(Buffer,"%i",value); | 174 | sprintf(Buffer,"%i",value); |
167 | writeSetting(key_str,string(Buffer)); | 175 | writeSetting(key_str,string(Buffer)); |
168 | } | 176 | } |
169 | 177 | ||
170 | void Settings::writeSetting(const string key_str,const unsigned int value) | 178 | void Settings::writeSetting(const string key_str,const unsigned int value) |
171 | { | 179 | { |
172 | char Buffer[30]; | 180 | char Buffer[30]; |
173 | 181 | ||
174 | sprintf(Buffer,"%i",value); | 182 | sprintf(Buffer,"%i",value); |
175 | writeSetting(key_str,string(Buffer)); | 183 | writeSetting(key_str,string(Buffer)); |
176 | } | 184 | } |
177 | 185 | ||
178 | void Settings::writeSetting(const string key_str,const long int value) | 186 | void Settings::writeSetting(const string key_str,const long int value) |
179 | { | 187 | { |
180 | char Buffer[30]; | 188 | char Buffer[30]; |
181 | 189 | ||
182 | sprintf(Buffer,"%li",value); | 190 | sprintf(Buffer,"%li",value); |
183 | writeSetting(key_str,string(Buffer)); | 191 | writeSetting(key_str,string(Buffer)); |
184 | } | 192 | } |
185 | 193 | ||
186 | void Settings::writeSetting(const string key_str,const unsigned long value) | 194 | void Settings::writeSetting(const string key_str,const unsigned long value) |
187 | { | 195 | { |
188 | char Buffer[30]; | 196 | char Buffer[30]; |
189 | 197 | ||
190 | sprintf(Buffer,"%lu",value); | 198 | sprintf(Buffer,"%lu",value); |
191 | writeSetting(key_str,string(Buffer)); | 199 | writeSetting(key_str,string(Buffer)); |
192 | } | 200 | } |
193 | 201 | ||
194 | void Settings::writeSetting(const string key_str,const string value) | 202 | void Settings::writeSetting(const string key_str,const string value) |
195 | { | 203 | { |
196 | // This function will write a value for the key key_str. If the key_str | 204 | // This function will write a value for the key key_str. If the key_str |
197 | // already exists then it will be overwritten. | 205 | // already exists then it will be overwritten. |
198 | |||
199 | std::vector<string> FileEntries; | ||
200 | FILE *fd=NULL,*ftemp=NULL; | 206 | FILE *fd=NULL,*ftemp=NULL; |
201 | char * dir_str; | ||
202 | char * dir_ptr; | ||
203 | char buf[MAX_LINE_SIZE]; | 207 | char buf[MAX_LINE_SIZE]; |
204 | char tempname[12]; | ||
205 | 208 | ||
206 | dir_str = strdup(envFile.c_str()); | 209 | string tmp = getHomeDir() + "/tmpsfcave.dat"; |
207 | printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() ); | 210 | |
208 | if (dir_str) | 211 | // if file exists we need to save contents |
212 | fd = fopen( envFile.c_str(), "r" ); | ||
213 | ftemp = fopen( tmp.c_str(), "w" ); | ||
214 | if ( fd != NULL && ftemp != NULL ) | ||
209 | { | 215 | { |
210 | // remove file from the directory string | 216 | while (fgets(buf, MAX_LINE_SIZE-1, fd)) |
211 | dir_ptr = strrchr(dir_str, (int)'/'); | ||
212 | if (dir_ptr) | ||
213 | { | 217 | { |
214 | *dir_ptr = 0; | 218 | if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 ) |
215 | 219 | fprintf( ftemp, "%s", buf ); | |
216 | // make the directory path if it does not exist | 220 | } |
217 | // mkdir(dir_str, 777 ); | 221 | fclose(fd); |
222 | } | ||
218 | 223 | ||
219 | // if file exists we need to save contents | 224 | if ( ftemp != NULL ) |
220 | if ((fd = fopen(envFile.c_str(), "r")) != NULL) | 225 | { |
221 | { | 226 | fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); |
222 | while (fgets(buf, MAX_LINE_SIZE-1, fd)) | 227 | fclose( ftemp ); |
223 | FileEntries.push_back(string(buf)); | ||
224 | fclose(fd); | ||
225 | } | ||
226 | 228 | ||
227 | char *home = getenv( "HOME" ); | 229 | remove(envFile.c_str()); |
228 | string tmp; | 230 | rename( tmp.c_str(), envFile.c_str() ); |
229 | if ( home ) | 231 | } |
230 | tmp = home + string( "/" ) + "tmpsfcave.dat"; | 232 | /* |
231 | else | 233 | string tmp = getHomeDir() + "/tmpsfcave.dat"; |
232 | tmp = "./tmpsfcave.dat"; | ||
233 | strcpy(tempname,tmp.c_str() ); | 234 | strcpy(tempname,tmp.c_str() ); |
234 | printf( "tmp - %s\n", tempname ); | 235 | printf( "tmp - %s\n", tempname ); |
235 | if ((ftemp = fopen(tempname,"w")) != NULL) | 236 | if ((ftemp = fopen(tempname,"w")) != NULL) |
236 | { | 237 | { |
237 | char *key1,*key2; | 238 | char *key1,*key2; |
238 | char buff1[80],buff2[80]; | 239 | char buff1[80],buff2[80]; |
239 | 240 | ||
240 | strncpy(buff1,key_str.c_str(),80); | 241 | strncpy(buff1,key_str.c_str(),80); |
241 | key1 = strtok(buff1,"\t"); | 242 | key1 = strtok(buff1,"\t"); |
242 | for (std::vector<string>::iterator iter = FileEntries.begin(); iter < FileEntries.end(); iter++) | 243 | for (std::vector<string>::iterator iter = FileEntries.begin(); iter < FileEntries.end(); iter++) |
243 | { | 244 | { |
244 | strncpy(buff2,(*iter).c_str(),80); | 245 | strncpy(buff2,(*iter).c_str(),80); |
245 | key2 = strtok(buff2,"\t"); | 246 | key2 = strtok(buff2,"\t"); |
246 | // IF not the key string then write out to file | 247 | // IF not the key string then write out to file |
247 | if (strcmp(key1,key2) != 0) | 248 | if (strcmp(key1,key2) != 0) |
248 | { | 249 | { |
249 | fprintf(ftemp,"%s",iter->c_str()); | 250 | fprintf(ftemp,"%s",iter->c_str()); |
250 | fflush(ftemp); | 251 | fflush(ftemp); |
251 | } | 252 | } |
252 | } | 253 | } |
253 | 254 | ||
254 | fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); | 255 | fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); |
255 | fflush(ftemp); | 256 | fflush(ftemp); |
256 | fclose(ftemp); | 257 | fclose(ftemp); |
257 | |||
258 | remove(envFile.c_str()); | ||
259 | |||
260 | rename( tempname, envFile.c_str() ); | ||
261 | } | 258 | } |
262 | else | 259 | else |
263 | printf( "Can't open file %s\n", envFile.c_str() ); | 260 | printf( "Can't open file %s\n", envFile.c_str() ); |
264 | } | 261 | } |
265 | 262 | ||
266 | delete dir_str; | 263 | delete dir_str; |
267 | } | 264 | } |
265 | */ | ||
268 | } | 266 | } |
269 | 267 | ||
270 | void Settings::deleteFile(void) | 268 | void Settings::deleteFile(void) |
271 | { | 269 | { |
272 | remove(envFile.c_str()); | 270 | remove(envFile.c_str()); |
273 | } | 271 | } |