00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include "bchash.h"
00004 #include "bcsignals.h"
00005 #include "filesystem.h"
00006 #include "stringfile.h"
00007
00008 BC_Hash::BC_Hash()
00009 {
00010 this->filename[0] = 0;
00011 total = 0;
00012 allocated = 0;
00013 names = 0;
00014 values = 0;
00015 }
00016
00017 BC_Hash::BC_Hash(char *filename)
00018 {
00019 strcpy(this->filename, filename);
00020 total = 0;
00021 allocated = 0;
00022 names = 0;
00023 values = 0;
00024
00025 FileSystem directory;
00026
00027 directory.parse_tildas(this->filename);
00028 total = 0;
00029 }
00030
00031 BC_Hash::~BC_Hash()
00032 {
00033 for(int i = 0; i < total; i++)
00034 {
00035 delete [] names[i];
00036 delete [] values[i];
00037 }
00038 delete [] names;
00039 delete [] values;
00040 }
00041
00042 void BC_Hash::reallocate_table(int new_total)
00043 {
00044 if(allocated < new_total)
00045 {
00046 int new_allocated = new_total * 2;
00047 char **new_names = new char*[new_allocated];
00048 char **new_values = new char*[new_allocated];
00049
00050 for(int i = 0; i < total; i++)
00051 {
00052 new_names[i] = names[i];
00053 new_values[i] = values[i];
00054 }
00055
00056 delete [] names;
00057 delete [] values;
00058
00059 names = new_names;
00060 values = new_values;
00061 allocated = new_allocated;
00062 }
00063 }
00064
00065 int BC_Hash::load()
00066 {
00067 StringFile stringfile(filename);
00068 load_stringfile(&stringfile);
00069 return 0;
00070 }
00071
00072 void BC_Hash::load_stringfile(StringFile *file)
00073 {
00074 char arg1[1024], arg2[1024];
00075 total = 0;
00076 while(file->get_pointer() < file->get_length())
00077 {
00078 file->readline(arg1, arg2);
00079 reallocate_table(total + 1);
00080 names[total] = new char[strlen(arg1) + 1];
00081 values[total] = new char[strlen(arg2) + 1];
00082 strcpy(names[total], arg1);
00083 strcpy(values[total], arg2);
00084 total++;
00085 }
00086 }
00087
00088 void BC_Hash::save_stringfile(StringFile *file)
00089 {
00090 for(int i = 0; i < total; i++)
00091 {
00092 file->writeline(names[i], values[i], 0);
00093 }
00094 }
00095
00096 int BC_Hash::save()
00097 {
00098 StringFile stringfile;
00099 save_stringfile(&stringfile);
00100 stringfile.write_to_file(filename);
00101 return 0;
00102 }
00103
00104 int BC_Hash::load_string(char *string)
00105 {
00106 StringFile stringfile;
00107 stringfile.read_from_string(string);
00108 load_stringfile(&stringfile);
00109 return 0;
00110 }
00111
00112 int BC_Hash::save_string(char* &string)
00113 {
00114 StringFile stringfile;
00115 save_stringfile(&stringfile);
00116 string = new char[stringfile.get_length() + 1];
00117 memcpy(string, stringfile.string, stringfile.get_length() + 1);
00118 return 0;
00119 }
00120
00121
00122
00123 int32_t BC_Hash::get(char *name, int32_t default_)
00124 {
00125 for(int i = 0; i < total; i++)
00126 {
00127 if(!strcmp(names[i], name))
00128 {
00129 return (int32_t)atol(values[i]);
00130 }
00131 }
00132 return default_;
00133 }
00134
00135 int64_t BC_Hash::get(char *name, int64_t default_)
00136 {
00137 int64_t result = default_;
00138 for(int i = 0; i < total; i++)
00139 {
00140 if(!strcmp(names[i], name))
00141 {
00142 sscanf(values[i], "%lld", &result);
00143 return result;
00144 }
00145 }
00146 return result;
00147 }
00148
00149 double BC_Hash::get(char *name, double default_)
00150 {
00151 for(int i = 0; i < total; i++)
00152 {
00153 if(!strcmp(names[i], name))
00154 {
00155 return atof(values[i]);
00156 }
00157 }
00158 return default_;
00159 }
00160
00161 float BC_Hash::get(char *name, float default_)
00162 {
00163 for(int i = 0; i < total; i++)
00164 {
00165 if(!strcmp(names[i], name))
00166 {
00167 return atof(values[i]);
00168 }
00169 }
00170 return default_;
00171 }
00172
00173 char* BC_Hash::get(char *name, char *default_)
00174 {
00175 for(int i = 0; i < total; i++)
00176 {
00177 if(!strcmp(names[i], name))
00178 {
00179 strcpy(default_, values[i]);
00180 return values[i];
00181 }
00182 }
00183 return default_;
00184 }
00185
00186 int BC_Hash::update(char *name, double value)
00187 {
00188 char string[1024];
00189 sprintf(string, "%.16e", value);
00190 return update(name, string);
00191 }
00192
00193 int BC_Hash::update(char *name, float value)
00194 {
00195 char string[1024];
00196 sprintf(string, "%.6e", value);
00197 return update(name, string);
00198 }
00199
00200 int32_t BC_Hash::update(char *name, int32_t value)
00201 {
00202 char string[1024];
00203 sprintf(string, "%d", value);
00204 return update(name, string);
00205 }
00206
00207 int BC_Hash::update(char *name, int64_t value)
00208 {
00209 char string[1024];
00210 sprintf(string, "%lld", value);
00211 return update(name, string);
00212 }
00213
00214 int BC_Hash::update(char *name, char *value)
00215 {
00216 for(int i = 0; i < total; i++)
00217 {
00218 if(!strcmp(names[i], name))
00219 {
00220 delete [] values[i];
00221 values[i] = new char[strlen(value) + 1];
00222 strcpy(values[i], value);
00223 return 0;
00224 }
00225 }
00226
00227
00228 reallocate_table(total + 1);
00229 names[total] = new char[strlen(name) + 1];
00230 strcpy(names[total], name);
00231 values[total] = new char[strlen(value) + 1];
00232 strcpy(values[total], value);
00233 total++;
00234 return 1;
00235 }
00236
00237
00238 void BC_Hash::copy_from(BC_Hash *src)
00239 {
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 SET_TRACE
00256 reallocate_table(src->total);
00257
00258 SET_TRACE
00259 for(int i = 0; i < src->total; i++)
00260 {
00261 update(src->names[i], src->values[i]);
00262
00263
00264
00265
00266 }
00267 SET_TRACE
00268 }
00269
00270 int BC_Hash::equivalent(BC_Hash *src)
00271 {
00272 for(int i = 0; i < total && i < src->total; i++)
00273 {
00274 if(strcmp(names[i], src->names[i]) ||
00275 strcmp(values[i], src->values[i])) return 0;
00276 }
00277 return 1;
00278 }
00279
00280 void BC_Hash::dump()
00281 {
00282 printf("BC_Hash::dump\n");
00283 for(int i = 0; i < total; i++)
00284 printf(" key=%s value=%s\n", names[i], values[i]);
00285 }