summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/settings.cpp
Unidiff
Diffstat (limited to 'noncore/games/sfcave-sdl/settings.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/settings.cpp88
1 files changed, 43 insertions, 45 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
@@ -7,2 +7,4 @@
7 7
8// Defined in util.h
9string getHomeDir();
8 10
@@ -18,11 +20,4 @@ Settings::Settings( char * env_file, char * env_dir )
18 { 20 {
19 char * homeDir = getenv( "HOME" );; 21 envFile = getHomeDir();
20
21 if ( homeDir )
22 {
23 envFile.append(homeDir);
24 envFile.append("/"); 22 envFile.append("/");
25 }
26 else
27 printf( "Environment var HOME not set!\n" );
28 23
@@ -44,11 +39,4 @@ Settings::Settings()
44{ 39{
45 char * homeDir = getenv("HOME"); 40 envFile = getHomeDir();
46
47 if ( homeDir)
48 {
49 envFile.append(homeDir);
50 envFile.append("/"); 41 envFile.append("/");
51 }
52 else
53 printf( "Environment var HOME not set!\n" );
54 42
@@ -112,2 +100,14 @@ bool Settings::readSetting(const string key_str,unsigned long& result)
112 100
101bool 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
113bool Settings::readSetting(const string key_str,bool& result) 113bool Settings::readSetting(const string key_str,bool& result)
@@ -161,2 +161,10 @@ void Settings::writeSetting(const string key_str,const bool value)
161 161
162void 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
162void Settings::writeSetting(const string key_str,const int value) 170void Settings::writeSetting(const string key_str,const int value)
@@ -197,28 +205,17 @@ void Settings::writeSetting(const string key_str,const string value)
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
206 dir_str = strdup(envFile.c_str());
207 printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() );
208 if (dir_str)
209 {
210 // remove file from the directory string
211 dir_ptr = strrchr(dir_str, (int)'/');
212 if (dir_ptr)
213 {
214 *dir_ptr = 0;
215 208
216 // make the directory path if it does not exist 209 string tmp = getHomeDir() + "/tmpsfcave.dat";
217 // mkdir(dir_str, 777 );
218 210
219 // if file exists we need to save contents 211 // if file exists we need to save contents
220 if ((fd = fopen(envFile.c_str(), "r")) != NULL) 212 fd = fopen( envFile.c_str(), "r" );
213 ftemp = fopen( tmp.c_str(), "w" );
214 if ( fd != NULL && ftemp != NULL )
221 { 215 {
222 while (fgets(buf, MAX_LINE_SIZE-1, fd)) 216 while (fgets(buf, MAX_LINE_SIZE-1, fd))
223 FileEntries.push_back(string(buf)); 217 {
218 if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 )
219 fprintf( ftemp, "%s", buf );
220 }
224 fclose(fd); 221 fclose(fd);
@@ -226,8 +223,12 @@ void Settings::writeSetting(const string key_str,const string value)
226 223
227 char *home = getenv( "HOME" ); 224 if ( ftemp != NULL )
228 string tmp; 225 {
229 if ( home ) 226 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
230 tmp = home + string( "/" ) + "tmpsfcave.dat"; 227 fclose( ftemp );
231 else 228
232 tmp = "./tmpsfcave.dat"; 229 remove(envFile.c_str());
230 rename( tmp.c_str(), envFile.c_str() );
231 }
232/*
233 string tmp = getHomeDir() + "/tmpsfcave.dat";
233 strcpy(tempname,tmp.c_str() ); 234 strcpy(tempname,tmp.c_str() );
@@ -256,6 +257,2 @@ void Settings::writeSetting(const string key_str,const string value)
256 fclose(ftemp); 257 fclose(ftemp);
257
258 remove(envFile.c_str());
259
260 rename( tempname, envFile.c_str() );
261 } 258 }
@@ -267,2 +264,3 @@ void Settings::writeSetting(const string key_str,const string value)
267 } 264 }
265*/
268} 266}