4 #include "filesystem.h"
5 #include "stringfile.h"
13 Defaults::Defaults(char *filename)
15 strcpy(this->filename, filename);
18 directory.parse_tildas(this->filename);
24 for(int i = 0; i < total; i++)
33 StringFile stringfile(filename);
34 load_stringfile(&stringfile);
38 void Defaults::load_stringfile(StringFile *file)
40 char arg1[1024], arg2[1024];
42 while(file->get_pointer() < file->get_length())
44 file->readline(arg1, arg2);
45 names[total] = new char[strlen(arg1) + 1];
46 values[total] = new char[strlen(arg2) + 1];
47 strcpy(names[total], arg1);
48 strcpy(values[total], arg2);
53 void Defaults::save_stringfile(StringFile *file)
55 for(int i = 0; i < total; i++)
57 file->writeline(names[i], values[i], 0);
63 StringFile stringfile;
64 save_stringfile(&stringfile);
65 stringfile.write_to_file(filename);
69 int Defaults::load_string(char *string)
71 StringFile stringfile;
72 stringfile.read_from_string(string);
73 load_stringfile(&stringfile);
77 int Defaults::save_string(char* &string)
79 StringFile stringfile;
80 save_stringfile(&stringfile);
81 string = new char[stringfile.get_length() + 1];
82 memcpy(string, stringfile.string, stringfile.get_length() + 1);
88 int32_t Defaults::get(char *name, int32_t default_)
90 for(int i = 0; i < total; i++)
92 if(!strcmp(names[i], name))
94 return (int32_t)atol(values[i]);
97 return default_; // failed
100 int64_t Defaults::get(char *name, int64_t default_)
102 int64_t result = default_;
103 for(int i = 0; i < total; i++)
105 if(!strcmp(names[i], name))
107 sscanf(values[i], "%lld", &result);
114 double Defaults::get(char *name, double default_)
116 for(int i = 0; i < total; i++)
118 if(!strcmp(names[i], name))
120 return atof(values[i]);
123 return default_; // failed
126 float Defaults::get(char *name, float default_)
128 for(int i = 0; i < total; i++)
130 if(!strcmp(names[i], name))
132 return atof(values[i]);
135 return default_; // failed
138 char* Defaults::get(char *name, char *default_)
140 for(int i = 0; i < total; i++)
142 if(!strcmp(names[i], name))
144 strcpy(default_, values[i]);
148 return default_; // failed
151 int Defaults::update(char *name, double value) // update a value if it exists
154 sprintf(string, "%.16e", value);
155 return update(name, string);
158 int Defaults::update(char *name, float value) // update a value if it exists
161 sprintf(string, "%.6e", value);
162 return update(name, string);
165 int32_t Defaults::update(char *name, int32_t value) // update a value if it exists
168 sprintf(string, "%d", value);
169 return update(name, string);
172 int Defaults::update(char *name, int64_t value) // update a value if it exists
175 sprintf(string, "%lld", value);
176 return update(name, string);
179 int Defaults::update(char *name, char *value)
181 for(int i = 0; i < total; i++)
183 if(!strcmp(names[i], name))
186 values[i] = new char[strlen(value) + 1];
187 strcpy(values[i], value);
191 // didn't find so create new entry
192 names[total] = new char[strlen(name) + 1];
193 strcpy(names[total], name);
194 values[total] = new char[strlen(value) + 1];
195 strcpy(values[total], value);