5 #include "filesystem.h"
6 #include "stringfile.h"
10 this->filename[0] = 0;
17 BC_Hash::BC_Hash(char *filename)
19 strcpy(this->filename, filename);
27 directory.parse_tildas(this->filename);
33 for(int i = 0; i < total; i++)
42 void BC_Hash::reallocate_table(int new_total)
44 if(allocated < new_total)
46 int new_allocated = new_total * 2;
47 char **new_names = new char*[new_allocated];
48 char **new_values = new char*[new_allocated];
50 for(int i = 0; i < total; i++)
52 new_names[i] = names[i];
53 new_values[i] = values[i];
61 allocated = new_allocated;
67 StringFile stringfile(filename);
68 load_stringfile(&stringfile);
72 void BC_Hash::load_stringfile(StringFile *file)
74 char arg1[1024], arg2[1024];
76 while(file->get_pointer() < file->get_length())
78 file->readline(arg1, arg2);
79 reallocate_table(total + 1);
80 names[total] = new char[strlen(arg1) + 1];
81 values[total] = new char[strlen(arg2) + 1];
82 strcpy(names[total], arg1);
83 strcpy(values[total], arg2);
88 void BC_Hash::save_stringfile(StringFile *file)
90 for(int i = 0; i < total; i++)
92 file->writeline(names[i], values[i], 0);
98 StringFile stringfile;
99 save_stringfile(&stringfile);
100 stringfile.write_to_file(filename);
104 int BC_Hash::load_string(char *string)
106 StringFile stringfile;
107 stringfile.read_from_string(string);
108 load_stringfile(&stringfile);
112 int BC_Hash::save_string(char* &string)
114 StringFile stringfile;
115 save_stringfile(&stringfile);
116 string = new char[stringfile.get_length() + 1];
117 memcpy(string, stringfile.string, stringfile.get_length() + 1);
123 int32_t BC_Hash::get(char *name, int32_t default_)
125 for(int i = 0; i < total; i++)
127 if(!strcmp(names[i], name))
129 return (int32_t)atol(values[i]);
132 return default_; // failed
135 int64_t BC_Hash::get(char *name, int64_t default_)
137 int64_t result = default_;
138 for(int i = 0; i < total; i++)
140 if(!strcmp(names[i], name))
142 sscanf(values[i], "%lld", &result);
149 double BC_Hash::get(char *name, double default_)
151 for(int i = 0; i < total; i++)
153 if(!strcmp(names[i], name))
155 return atof(values[i]);
158 return default_; // failed
161 float BC_Hash::get(char *name, float default_)
163 for(int i = 0; i < total; i++)
165 if(!strcmp(names[i], name))
167 return atof(values[i]);
170 return default_; // failed
173 char* BC_Hash::get(char *name, char *default_)
175 for(int i = 0; i < total; i++)
177 if(!strcmp(names[i], name))
179 strcpy(default_, values[i]);
183 return default_; // failed
186 int BC_Hash::update(char *name, double value) // update a value if it exists
189 sprintf(string, "%.16e", value);
190 return update(name, string);
193 int BC_Hash::update(char *name, float value) // update a value if it exists
196 sprintf(string, "%.6e", value);
197 return update(name, string);
200 int32_t BC_Hash::update(char *name, int32_t value) // update a value if it exists
203 sprintf(string, "%d", value);
204 return update(name, string);
207 int BC_Hash::update(char *name, int64_t value) // update a value if it exists
210 sprintf(string, "%lld", value);
211 return update(name, string);
214 int BC_Hash::update(char *name, char *value)
216 for(int i = 0; i < total; i++)
218 if(!strcmp(names[i], name))
221 values[i] = new char[strlen(value) + 1];
222 strcpy(values[i], value);
227 // didn't find so create new entry
228 reallocate_table(total + 1);
229 names[total] = new char[strlen(name) + 1];
230 strcpy(names[total], name);
231 values[total] = new char[strlen(value) + 1];
232 strcpy(values[total], value);
238 void BC_Hash::copy_from(BC_Hash *src)
240 // Can't delete because this is used by file decoders after plugins
242 // for(int i = 0; i < total; i++)
244 // delete [] names[i];
245 // delete [] values[i];
256 reallocate_table(src->total);
257 // total = src->total;
259 for(int i = 0; i < src->total; i++)
261 update(src->names[i], src->values[i]);
262 // names[i] = new char[strlen(src->names[i]) + 1];
263 // values[i] = new char[strlen(src->values[i]) + 1];
264 // strcpy(names[i], src->names[i]);
265 // strcpy(values[i], src->values[i]);
270 int BC_Hash::equivalent(BC_Hash *src)
272 for(int i = 0; i < total && i < src->total; i++)
274 if(strcmp(names[i], src->names[i]) ||
275 strcmp(values[i], src->values[i])) return 0;
282 printf("BC_Hash::dump\n");
283 for(int i = 0; i < total; i++)
284 printf(" key=%s value=%s\n", names[i], values[i]);