| 1 |
#include "conf.h" |
| 2 |
#include <avm_except.h> |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <fstream> |
| 7 |
|
| 8 |
#define __MODULE__ "Read config" |
| 9 |
|
| 10 |
using namespace avm; |
| 11 |
|
| 12 |
ReadConfig::ReadConfig(const char* fn) |
| 13 |
{ |
| 14 |
FILE *is = fopen(fn, "r"); |
| 15 |
string s; |
| 16 |
if (!is) |
| 17 |
throw FATAL("Could not open file"); |
| 18 |
while (!feof(is)) |
| 19 |
{ |
| 20 |
char z[500]; |
| 21 |
z[499]=0; |
| 22 |
//is.gets(&z); |
| 23 |
if (!fgets(z, 499, is)) |
| 24 |
{ |
| 25 |
fprintf(stderr,"Z=0!\n"); |
| 26 |
break; |
| 27 |
} |
| 28 |
int endpos=strlen(z)-1; |
| 29 |
while((endpos>=0) && (z[endpos]=='\n')) |
| 30 |
{ |
| 31 |
z[endpos]=0; |
| 32 |
endpos--; |
| 33 |
} |
| 34 |
s=string(z); |
| 35 |
// cerr<<s<<endl; |
| 36 |
avm::string::size_type pos=s.find(':'); |
| 37 |
if(pos==avm::string::npos) |
| 38 |
{ |
| 39 |
// cerr<<"NO : !"<<endl; |
| 40 |
break; |
| 41 |
} |
| 42 |
avm::string key=s; |
| 43 |
key.erase(pos); |
| 44 |
string value=s; |
| 45 |
value.erase(0, pos+1); |
| 46 |
_map.insert(key, value); |
| 47 |
fprintf(stderr,"Key: %s Value: %s\n", key.c_str(), value.c_str()); |
| 48 |
//delete z; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
double ReadConfig::getDouble(const avm::string& key) const |
| 53 |
{ |
| 54 |
return atof(_map.find_default(key)->c_str()); |
| 55 |
} |
| 56 |
int ReadConfig::getInt(const avm::string& key) const |
| 57 |
{ |
| 58 |
return strtol(_map.find_default(key)->c_str(), 0, 16); |
| 59 |
} |
| 60 |
const char* ReadConfig::getString(const avm::string& key) const |
| 61 |
{ |
| 62 |
return _map.find_default(key)->c_str(); |
| 63 |
} |
| 64 |
|
| 65 |
#undef __MODULE__ |
| 66 |
#define __MODULE__ "Write config" |
| 67 |
WriteConfig::WriteConfig(const char* fn) |
| 68 |
{ |
| 69 |
fh = fopen(fn, "w"); |
| 70 |
if (!fh) |
| 71 |
throw FATAL("Could not open file"); |
| 72 |
} |
| 73 |
|
| 74 |
WriteConfig::~WriteConfig() |
| 75 |
{ |
| 76 |
avm::avm_map<avm::string, avm::string>::const_iterator it; |
| 77 |
for (it = _map.begin(); it != _map.end(); it++) |
| 78 |
if (it->key != "") |
| 79 |
fprintf((FILE*)fh, "%s:%s", it->key.c_str(), it->value.c_str()); |
| 80 |
fclose((FILE*)fh); |
| 81 |
} |
| 82 |
|
| 83 |
void WriteConfig::add(const avm::string& key, const avm::string& value) |
| 84 |
{ |
| 85 |
_map.insert(key, value); |
| 86 |
} |
| 87 |
|
| 88 |
void WriteConfig::add(const avm::string& key, int value) |
| 89 |
{ |
| 90 |
char s[64]; |
| 91 |
sprintf(s, "%d", value); |
| 92 |
_map.insert(key, string(s)); |
| 93 |
} |
| 94 |
|
| 95 |
void WriteConfig::add(const avm::string& key, double value) |
| 96 |
{ |
| 97 |
char s[128]; |
| 98 |
sprintf(s, "%f", value); |
| 99 |
_map.insert(key, string(s)); |
| 100 |
} |