00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include "defaults.h"
00004 #include "filesystem.h"
00005 #include "stringfile.h"
00006
00007 Defaults::Defaults()
00008 {
00009 this->filename[0] = 0;
00010 total = 0;
00011 }
00012
00013 Defaults::Defaults(char *filename)
00014 {
00015 strcpy(this->filename, filename);
00016 FileSystem directory;
00017
00018 directory.parse_tildas(this->filename);
00019 total = 0;
00020 }
00021
00022 Defaults::~Defaults()
00023 {
00024 for(int i = 0; i < total; i++)
00025 {
00026 delete [] names[i];
00027 delete [] values[i];
00028 }
00029 }
00030
00031 int Defaults::load()
00032 {
00033 StringFile stringfile(filename);
00034 load_stringfile(&stringfile);
00035 return 0;
00036 }
00037
00038 void Defaults::load_stringfile(StringFile *file)
00039 {
00040 char arg1[1024], arg2[1024];
00041 total = 0;
00042 while(file->get_pointer() < file->get_length())
00043 {
00044 file->readline(arg1, arg2);
00045 names[total] = new char[strlen(arg1) + 1];
00046 values[total] = new char[strlen(arg2) + 1];
00047 strcpy(names[total], arg1);
00048 strcpy(values[total], arg2);
00049 total++;
00050 }
00051 }
00052
00053 void Defaults::save_stringfile(StringFile *file)
00054 {
00055 for(int i = 0; i < total; i++)
00056 {
00057 file->writeline(names[i], values[i], 0);
00058 }
00059 }
00060
00061 int Defaults::save()
00062 {
00063 StringFile stringfile;
00064 save_stringfile(&stringfile);
00065 stringfile.write_to_file(filename);
00066 return 0;
00067 }
00068
00069 int Defaults::load_string(char *string)
00070 {
00071 StringFile stringfile;
00072 stringfile.read_from_string(string);
00073 load_stringfile(&stringfile);
00074 return 0;
00075 }
00076
00077 int Defaults::save_string(char* &string)
00078 {
00079 StringFile stringfile;
00080 save_stringfile(&stringfile);
00081 string = new char[stringfile.get_length() + 1];
00082 memcpy(string, stringfile.string, stringfile.get_length() + 1);
00083 return 0;
00084 }
00085
00086
00087
00088 int32_t Defaults::get(char *name, int32_t default_)
00089 {
00090 for(int i = 0; i < total; i++)
00091 {
00092 if(!strcmp(names[i], name))
00093 {
00094 return (int32_t)atol(values[i]);
00095 }
00096 }
00097 return default_;
00098 }
00099
00100 int64_t Defaults::get(char *name, int64_t default_)
00101 {
00102 int64_t result = default_;
00103 for(int i = 0; i < total; i++)
00104 {
00105 if(!strcmp(names[i], name))
00106 {
00107 sscanf(values[i], "%lld", &result);
00108 return result;
00109 }
00110 }
00111 return result;
00112 }
00113
00114 double Defaults::get(char *name, double default_)
00115 {
00116 for(int i = 0; i < total; i++)
00117 {
00118 if(!strcmp(names[i], name))
00119 {
00120 return atof(values[i]);
00121 }
00122 }
00123 return default_;
00124 }
00125
00126 float Defaults::get(char *name, float default_)
00127 {
00128 for(int i = 0; i < total; i++)
00129 {
00130 if(!strcmp(names[i], name))
00131 {
00132 return atof(values[i]);
00133 }
00134 }
00135 return default_;
00136 }
00137
00138 char* Defaults::get(char *name, char *default_)
00139 {
00140 for(int i = 0; i < total; i++)
00141 {
00142 if(!strcmp(names[i], name))
00143 {
00144 strcpy(default_, values[i]);
00145 return values[i];
00146 }
00147 }
00148 return default_;
00149 }
00150
00151 int Defaults::update(char *name, double value)
00152 {
00153 char string[1024];
00154 sprintf(string, "%.16e", value);
00155 return update(name, string);
00156 }
00157
00158 int Defaults::update(char *name, float value)
00159 {
00160 char string[1024];
00161 sprintf(string, "%.6e", value);
00162 return update(name, string);
00163 }
00164
00165 int32_t Defaults::update(char *name, int32_t value)
00166 {
00167 char string[1024];
00168 sprintf(string, "%d", value);
00169 return update(name, string);
00170 }
00171
00172 int Defaults::update(char *name, int64_t value)
00173 {
00174 char string[1024];
00175 sprintf(string, "%lld", value);
00176 return update(name, string);
00177 }
00178
00179 int Defaults::update(char *name, char *value)
00180 {
00181 for(int i = 0; i < total; i++)
00182 {
00183 if(!strcmp(names[i], name))
00184 {
00185 delete [] values[i];
00186 values[i] = new char[strlen(value) + 1];
00187 strcpy(values[i], value);
00188 return 0;
00189 }
00190 }
00191
00192 names[total] = new char[strlen(name) + 1];
00193 strcpy(names[total], name);
00194 values[total] = new char[strlen(value) + 1];
00195 strcpy(values[total], value);
00196 total++;
00197 return 1;
00198 }