r877: Fix files that were missing from a make dist tarball.
[cinelerra_cv.git] / guicast / bchash.C
blob5455cd8b06b17c7f7212e11e25b7172c048259e0
1 #include <stdlib.h>
2 #include <string.h>
3 #include "bchash.h"
4 #include "bcsignals.h"
5 #include "filesystem.h"
6 #include "stringfile.h"
8 BC_Hash::BC_Hash()
10         this->filename[0] = 0;
11         total = 0;
12         allocated = 0;
13         names = 0;
14         values = 0;
17 BC_Hash::BC_Hash(char *filename)
19         strcpy(this->filename, filename);
20         total = 0;
21         allocated = 0;
22         names = 0;
23         values = 0;
25         FileSystem directory;
26         
27         directory.parse_tildas(this->filename);
28         total = 0;
31 BC_Hash::~BC_Hash()
33         for(int i = 0; i < total; i++)
34         {
35                 delete [] names[i];
36                 delete [] values[i];
37         }
38         delete [] names;
39         delete [] values;
42 void BC_Hash::reallocate_table(int new_total)
44         if(allocated < new_total)
45         {
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++)
51                 {
52                         new_names[i] = names[i];
53                         new_values[i] = values[i];
54                 }
56                 delete [] names;
57                 delete [] values;
59                 names = new_names;
60                 values = new_values;
61                 allocated = new_allocated;
62         }
65 int BC_Hash::load()
67         StringFile stringfile(filename);
68         load_stringfile(&stringfile);
69         return 0;
72 void BC_Hash::load_stringfile(StringFile *file)
74         char arg1[1024], arg2[1024];
75         total = 0;
76         while(file->get_pointer() < file->get_length())
77         {
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);
84                 total++;
85         }
88 void BC_Hash::save_stringfile(StringFile *file)
90         for(int i = 0; i < total; i++)
91         {
92                 file->writeline(names[i], values[i], 0);
93         }
96 int BC_Hash::save()
98         StringFile stringfile;
99         save_stringfile(&stringfile);
100         stringfile.write_to_file(filename);
101         return 0;
104 int BC_Hash::load_string(char *string)
106         StringFile stringfile;
107         stringfile.read_from_string(string);
108         load_stringfile(&stringfile);
109         return 0;
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);
118         return 0;
123 int32_t BC_Hash::get(char *name, int32_t default_)
125         for(int i = 0; i < total; i++)
126         {
127                 if(!strcmp(names[i], name))
128                 {
129                         return (int32_t)atol(values[i]);
130                 }
131         }
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++)
139         {
140                 if(!strcmp(names[i], name))
141                 {
142                         sscanf(values[i], "%lld", &result);
143                         return result;
144                 }
145         }
146         return result;
149 double BC_Hash::get(char *name, double default_)
151         for(int i = 0; i < total; i++)
152         {
153                 if(!strcmp(names[i], name))
154                 {
155                         return atof(values[i]);
156                 }
157         }
158         return default_;  // failed
161 float BC_Hash::get(char *name, float default_)
163         for(int i = 0; i < total; i++)
164         {
165                 if(!strcmp(names[i], name))
166                 {
167                         return atof(values[i]);
168                 }
169         }
170         return default_;  // failed
173 char* BC_Hash::get(char *name, char *default_)
175         for(int i = 0; i < total; i++)
176         {
177                 if(!strcmp(names[i], name))
178                 {
179                         strcpy(default_, values[i]);
180                         return values[i];
181                 }
182         }
183         return default_;  // failed
186 int BC_Hash::update(char *name, double value) // update a value if it exists
188         char string[1024];
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
195         char string[1024];
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
202         char string[1024];
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
209         char string[1024];
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++)
217         {
218                 if(!strcmp(names[i], name))
219                 {
220                         delete [] values[i];
221                         values[i] = new char[strlen(value) + 1];
222                         strcpy(values[i], value);
223                         return 0;
224                 }
225         }
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);
233         total++;
234         return 1;
238 void BC_Hash::copy_from(BC_Hash *src)
240 // Can't delete because this is used by file decoders after plugins
241 // request data.
242 //      for(int i = 0; i < total; i++)
243 //      {
244 //              delete [] names[i];
245 //              delete [] values[i];
246 //      }
247 //      delete [] names;
248 //      delete [] values;
249 // 
250 //      allocated = 0;
251 //      names = 0;
252 //      values = 0;
253 //      total = 0;
255 SET_TRACE
256         reallocate_table(src->total);
257 //      total = src->total;
258 SET_TRACE
259         for(int i = 0; i < src->total; i++)
260         {
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]);
266         }
267 SET_TRACE
270 int BC_Hash::equivalent(BC_Hash *src)
272         for(int i = 0; i < total && i < src->total; i++)
273         {
274                 if(strcmp(names[i], src->names[i]) ||
275                         strcmp(values[i], src->values[i])) return 0;
276         }
277         return 1;
280 void BC_Hash::dump()
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]);