author | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
commit | 0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (side-by-side diff) | |
tree | f1b82a4bd7582ef2cb722cffb87eecff1e1f96e6 /noncore/games/sfcave-sdl/settings.cpp | |
parent | 50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff) | |
download | opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.zip opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.gz opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.bz2 |
Clean up of code - fixed memory leaks (most of them) and added new custom config menu
Diffstat (limited to 'noncore/games/sfcave-sdl/settings.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/settings.cpp | 88 |
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 @@ -2,32 +2,27 @@ #include <stdlib.h> #include <sys/stat.h> #include <vector> #include "settings.h" +// Defined in util.h +string getHomeDir(); #define DEFAULT_DIR "." #define DEFAULT_FILE "Settings.cfg" #define MAX_LINE_SIZE 2048 Settings::Settings( char * env_file, char * env_dir ) { // Store the correct environment directory if (env_dir == NULL) { - char * homeDir = getenv( "HOME" );; - - if ( homeDir ) - { - envFile.append(homeDir); + envFile = getHomeDir(); envFile.append("/"); - } - else - printf( "Environment var HOME not set!\n" ); envFile.append(DEFAULT_DIR); } else envFile.append(env_dir); @@ -39,21 +34,14 @@ Settings::Settings( char * env_file, char * env_dir ) else envFile.append(env_file); } Settings::Settings() { - char * homeDir = getenv("HOME"); - - if ( homeDir) - { - envFile.append(homeDir); + envFile = getHomeDir(); envFile.append("/"); - } - else - printf( "Environment var HOME not set!\n" ); envFile.append(DEFAULT_DIR); envFile.append("/"); envFile.append(DEFAULT_FILE); } @@ -107,12 +95,24 @@ bool Settings::readSetting(const string key_str,unsigned long& result) return true; } else return false; } +bool Settings::readSetting(const string key_str,double& result) +{ + string Buffer; + if (readSetting(key_str,Buffer)) + { + result = atof( Buffer.c_str() ); + return true; + } + else + return false; +} + bool Settings::readSetting(const string key_str,bool& result) { string Buffer; if (readSetting(key_str,Buffer)) { result = (Buffer == "true"); @@ -156,12 +156,20 @@ bool Settings::readSetting(const string key_str,string& results) void Settings::writeSetting(const string key_str,const bool value) { value ? writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); } +void Settings::writeSetting(const string key_str,const double value) +{ + char Buffer[30]; + + sprintf(Buffer,"%lf",value); + writeSetting(key_str,string(Buffer)); +} + void Settings::writeSetting(const string key_str,const int value) { char Buffer[30]; sprintf(Buffer,"%i",value); writeSetting(key_str,string(Buffer)); @@ -192,47 +200,40 @@ void Settings::writeSetting(const string key_str,const unsigned long value) } void Settings::writeSetting(const string key_str,const string value) { // This function will write a value for the key key_str. If the key_str // already exists then it will be overwritten. - - std::vector<string> FileEntries; FILE *fd=NULL,*ftemp=NULL; - char * dir_str; - char * dir_ptr; char buf[MAX_LINE_SIZE]; - char tempname[12]; - - dir_str = strdup(envFile.c_str()); - printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() ); - if (dir_str) - { - // remove file from the directory string - dir_ptr = strrchr(dir_str, (int)'/'); - if (dir_ptr) - { - *dir_ptr = 0; - // make the directory path if it does not exist -// mkdir(dir_str, 777 ); + string tmp = getHomeDir() + "/tmpsfcave.dat"; // if file exists we need to save contents - if ((fd = fopen(envFile.c_str(), "r")) != NULL) + fd = fopen( envFile.c_str(), "r" ); + ftemp = fopen( tmp.c_str(), "w" ); + if ( fd != NULL && ftemp != NULL ) { while (fgets(buf, MAX_LINE_SIZE-1, fd)) - FileEntries.push_back(string(buf)); + { + if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 ) + fprintf( ftemp, "%s", buf ); + } fclose(fd); } - char *home = getenv( "HOME" ); - string tmp; - if ( home ) - tmp = home + string( "/" ) + "tmpsfcave.dat"; - else - tmp = "./tmpsfcave.dat"; + if ( ftemp != NULL ) + { + fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); + fclose( ftemp ); + + remove(envFile.c_str()); + rename( tmp.c_str(), envFile.c_str() ); + } +/* + string tmp = getHomeDir() + "/tmpsfcave.dat"; strcpy(tempname,tmp.c_str() ); printf( "tmp - %s\n", tempname ); if ((ftemp = fopen(tempname,"w")) != NULL) { char *key1,*key2; char buff1[80],buff2[80]; @@ -251,23 +252,20 @@ void Settings::writeSetting(const string key_str,const string value) } } fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); fflush(ftemp); fclose(ftemp); - - remove(envFile.c_str()); - - rename( tempname, envFile.c_str() ); } else printf( "Can't open file %s\n", envFile.c_str() ); } delete dir_str; } +*/ } void Settings::deleteFile(void) { remove(envFile.c_str()); } |