00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/stat.h>
00004 #include <vector>
00005
00006 #include "settings.h"
00007
00008
00009 string getHomeDir();
00010
00011 #define DEFAULT_DIR "."
00012 #define DEFAULT_FILE "Settings.cfg"
00013 #define MAX_LINE_SIZE 2048
00014
00015
00016 Settings::Settings( char * env_file, char * env_dir )
00017 {
00018
00019 if (env_dir == NULL)
00020 {
00021 envFile = getHomeDir();
00022 envFile.append("/");
00023
00024 envFile.append(DEFAULT_DIR);
00025 }
00026 else
00027 envFile.append(env_dir);
00028
00029 envFile.append("/");
00030
00031
00032 if (env_file == NULL)
00033 envFile.append(DEFAULT_FILE);
00034 else
00035 envFile.append(env_file);
00036 }
00037
00038 Settings::Settings()
00039 {
00040 envFile = getHomeDir();
00041 envFile.append("/");
00042
00043 envFile.append(DEFAULT_DIR);
00044 envFile.append("/");
00045
00046 envFile.append(DEFAULT_FILE);
00047 }
00048
00049 Settings::~Settings()
00050 {
00051 }
00052
00053 bool Settings::readSetting(const string key_str,int& result)
00054 {
00055 string Buffer;
00056 if (readSetting(key_str,Buffer))
00057 {
00058 result = atoi(Buffer.c_str());
00059 return true;
00060 }
00061 else
00062 return false;
00063 }
00064
00065 bool Settings::readSetting(const string key_str,unsigned int& result)
00066 {
00067 string Buffer;
00068 if (readSetting(key_str,Buffer))
00069 {
00070 result = atoi(Buffer.c_str());
00071 return true;
00072 }
00073 else
00074 return false;
00075 }
00076
00077 bool Settings::readSetting(const string key_str,long int& result)
00078 {
00079 string Buffer;
00080 if (readSetting(key_str,Buffer))
00081 {
00082 result = atol(Buffer.c_str());
00083 return true;
00084 }
00085 else
00086 return false;
00087 }
00088
00089 bool Settings::readSetting(const string key_str,unsigned long& result)
00090 {
00091 string Buffer;
00092 if (readSetting(key_str,Buffer))
00093 {
00094 result = atol(Buffer.c_str());
00095 return true;
00096 }
00097 else
00098 return false;
00099 }
00100
00101 bool Settings::readSetting(const string key_str,double& result)
00102 {
00103 string Buffer;
00104 if (readSetting(key_str,Buffer))
00105 {
00106 result = atof( Buffer.c_str() );
00107 return true;
00108 }
00109 else
00110 return false;
00111 }
00112
00113 bool Settings::readSetting(const string key_str,bool& result)
00114 {
00115 string Buffer;
00116 if (readSetting(key_str,Buffer))
00117 {
00118 result = (Buffer == "true");
00119 return true;
00120 }
00121 else
00122 return false;
00123 }
00124
00125 bool Settings::readSetting(const string key_str,string& results)
00126 {
00127
00128
00129 FILE * fd = 0;
00130 char buf[MAX_LINE_SIZE];
00131 bool ret_flag = false;
00132 char* key;
00133 char* value;
00134
00135
00136 fd = fopen(envFile.c_str(), "r");
00137
00138 if (fd)
00139 {
00140 while (fgets(buf, MAX_LINE_SIZE-1, fd))
00141 {
00142 key = strtok(buf, "\t");
00143 value = strtok(NULL, "\n");
00144
00145 if (!strcasecmp(key,key_str.c_str()))
00146 {
00147 results = value;
00148 ret_flag = true;
00149 }
00150 }
00151 fclose(fd);
00152 }
00153
00154 return(ret_flag);
00155 }
00156
00157 void Settings::writeSetting(const string key_str,const bool value)
00158 {
00159 value ? writeSetting(key_str,string("true")) :writeSetting(key_str,string("false"));
00160 }
00161
00162 void Settings::writeSetting(const string key_str,const double value)
00163 {
00164 char Buffer[30];
00165
00166 sprintf(Buffer,"%lf",value);
00167 writeSetting(key_str,string(Buffer));
00168 }
00169
00170 void Settings::writeSetting(const string key_str,const int value)
00171 {
00172 char Buffer[30];
00173
00174 sprintf(Buffer,"%i",value);
00175 writeSetting(key_str,string(Buffer));
00176 }
00177
00178 void Settings::writeSetting(const string key_str,const unsigned int value)
00179 {
00180 char Buffer[30];
00181
00182 sprintf(Buffer,"%i",value);
00183 writeSetting(key_str,string(Buffer));
00184 }
00185
00186 void Settings::writeSetting(const string key_str,const long int value)
00187 {
00188 char Buffer[30];
00189
00190 sprintf(Buffer,"%li",value);
00191 writeSetting(key_str,string(Buffer));
00192 }
00193
00194 void Settings::writeSetting(const string key_str,const unsigned long value)
00195 {
00196 char Buffer[30];
00197
00198 sprintf(Buffer,"%lu",value);
00199 writeSetting(key_str,string(Buffer));
00200 }
00201
00202 void Settings::writeSetting(const string key_str,const string value)
00203 {
00204
00205
00206 FILE *fd=NULL,*ftemp=NULL;
00207 char buf[MAX_LINE_SIZE];
00208
00209 string tmp = getHomeDir() + "/tmpsfcave.dat";
00210
00211
00212 fd = fopen( envFile.c_str(), "r" );
00213 ftemp = fopen( tmp.c_str(), "w" );
00214 if ( fd != NULL && ftemp != NULL )
00215 {
00216 while (fgets(buf, MAX_LINE_SIZE-1, fd))
00217 {
00218 if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 )
00219 fprintf( ftemp, "%s", buf );
00220 }
00221 fclose(fd);
00222 }
00223
00224 if ( ftemp != NULL )
00225 {
00226 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
00227 fclose( ftemp );
00228
00229 remove(envFile.c_str());
00230 rename( tmp.c_str(), envFile.c_str() );
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 }
00267
00268 void Settings::deleteFile(void)
00269 {
00270 remove(envFile.c_str());
00271 }