| 1 |
#include "Config.h" |
| 2 |
|
| 3 |
#include "configfile.h" |
| 4 |
#include "avm_output.h" |
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
AVM_BEGIN_NAMESPACE; |
| 14 |
|
| 15 |
static int compare_entry(const void* e1, const void* e2) |
| 16 |
{ |
| 17 |
const ConfigEntry* ce1 = (const ConfigEntry*) e1; |
| 18 |
const ConfigEntry* ce2 = (const ConfigEntry*) e2; |
| 19 |
int r = strcmp(ce1->appname.c_str(), ce2->appname.c_str()); |
| 20 |
if (r == 0) |
| 21 |
r = strcmp(ce1->valname.c_str(), ce2->valname.c_str()); |
| 22 |
return r; |
| 23 |
} |
| 24 |
|
| 25 |
ConfigEntry::~ConfigEntry() |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
ConfigFile::ConfigFile(const char* fn) : dirty(false), opened(false) |
| 30 |
{ |
| 31 |
Open(fn); |
| 32 |
} |
| 33 |
|
| 34 |
ConfigFile::~ConfigFile() |
| 35 |
{ |
| 36 |
Close(); |
| 37 |
} |
| 38 |
|
| 39 |
ConfigEntry* ConfigFile::Find(const char* appname, const char* valname) |
| 40 |
{ |
| 41 |
if (opened) |
| 42 |
{ |
| 43 |
for (size_t i = 0; i < entries.size(); ++i) |
| 44 |
{ |
| 45 |
#if 0 |
| 46 |
printf("FIND %d:%d %s %s %s %s \n", |
| 47 |
i, entries.size(), |
| 48 |
(const char*)appname, (const char*)valname, |
| 49 |
(const char*)entries[i].appname, |
| 50 |
(const char*)entries[i].valname); |
| 51 |
#endif |
| 52 |
if (entries[i].appname == appname |
| 53 |
&& entries[i].valname == valname) |
| 54 |
return &entries[i]; |
| 55 |
} |
| 56 |
} |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
void ConfigFile::Open(const char* fn) |
| 61 |
{ |
| 62 |
if (opened) |
| 63 |
{ |
| 64 |
if (filename == fn) |
| 65 |
return; |
| 66 |
Close(); |
| 67 |
} |
| 68 |
|
| 69 |
FILE* f = fopen(fn, "rb"); |
| 70 |
filename = fn; |
| 71 |
opened = true; |
| 72 |
dirty = false; |
| 73 |
if(!f) |
| 74 |
return; |
| 75 |
|
| 76 |
char appname[256], buf[4096], type; |
| 77 |
|
| 78 |
while (fgets(buf, sizeof(buf), f)) |
| 79 |
{ |
| 80 |
char* bp = buf; |
| 81 |
buf[sizeof(buf) - 1] = 0; |
| 82 |
while (isspace(*bp)) bp++; |
| 83 |
|
| 84 |
if (!*bp) |
| 85 |
continue; |
| 86 |
if (*bp == '[') |
| 87 |
{ |
| 88 |
sscanf(++bp, "%s", appname); |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
char* bpt = bp; |
| 93 |
while (!isspace(*bp) && *bp) bp++; |
| 94 |
if (*bp) |
| 95 |
*bp++ = 0; |
| 96 |
while (isspace(*bp)) bp++; |
| 97 |
|
| 98 |
ConfigEntry e(appname, bpt); |
| 99 |
|
| 100 |
bpt = bp; |
| 101 |
while (*bp != '\n' && *bp) bp++; |
| 102 |
*bp = 0; |
| 103 |
e.value = bpt; |
| 104 |
|
| 105 |
if (e.valname.size() > 0) |
| 106 |
{ |
| 107 |
e.type = ConfigEntry::Binary; |
| 108 |
entries.push_back(e); |
| 109 |
} |
| 110 |
} |
| 111 |
fclose(f); |
| 112 |
} |
| 113 |
|
| 114 |
void ConfigFile::Save() |
| 115 |
{ |
| 116 |
if (!opened || !dirty) |
| 117 |
return; |
| 118 |
|
| 119 |
avm::string tempfile = filename; |
| 120 |
avm::string::size_type pos; |
| 121 |
const char* dname = tempfile.c_str(); |
| 122 |
const char* bname = ""; |
| 123 |
|
| 124 |
if ((pos = tempfile.rfind('/')) != avm::string::npos) { |
| 125 |
tempfile[pos] = 0; |
| 126 |
bname = &tempfile[pos] + 1; |
| 127 |
} |
| 128 |
|
| 129 |
char* tname = tempnam(dname, bname); |
| 130 |
if (!tname) { |
| 131 |
AVM_WRITE("Config", "WARNING: can't create temporary name\n"); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
qsort(entries.begin(), entries.size(), sizeof(ConfigEntry), compare_entry); |
| 136 |
|
| 137 |
//printf("PATHNAME %s< %s\n", filename.c_str(), tname); |
| 138 |
FILE* f = fopen(tname, "wb"); |
| 139 |
if (!f) |
| 140 |
{ |
| 141 |
free(tname); |
| 142 |
AVM_WRITE("Config", "WARNING: can't save configuration %s\n", strerror(errno)); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
avm::string last; |
| 147 |
for (unsigned i = 0; !ferror(f) && (i < entries.size()); ++i) |
| 148 |
{ |
| 149 |
if (entries[i].appname != last) |
| 150 |
{ |
| 151 |
if (fprintf(f, "\n[ %s ]\n", entries[i].appname.c_str()) < 0) |
| 152 |
break; |
| 153 |
last = entries[i].appname; |
| 154 |
} |
| 155 |
|
| 156 |
if (fprintf(f, "%s ", entries[i].valname.c_str()) < 0) |
| 157 |
break; |
| 158 |
switch(entries[i].type) |
| 159 |
{ |
| 160 |
case ConfigEntry::Int: |
| 161 |
fprintf(f, "%d\n", entries[i].i); |
| 162 |
break; |
| 163 |
case ConfigEntry::Float: |
| 164 |
fprintf(f, "%f\n", entries[i].f); |
| 165 |
break; |
| 166 |
case ConfigEntry::Binary: |
| 167 |
fwrite(entries[i].value.c_str(), entries[i].value.size(), 1, f); |
| 168 |
if (!ferror(f)) |
| 169 |
fprintf(f, "\n"); |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// note: always needed to run all commands ferror(), fsync(),fclose() |
| 175 |
if ((ferror(f) | fsync(fileno(f)) | fclose(f)) |
| 176 |
|| rename(tname, filename.c_str()) != 0) |
| 177 |
{ |
| 178 |
AVM_WRITE("Config", "WARNING: can't save configuration %s\n", strerror(errno)); |
| 179 |
unlink(tname); |
| 180 |
} |
| 181 |
|
| 182 |
free(tname); |
| 183 |
} |
| 184 |
|
| 185 |
void ConfigFile::Close() |
| 186 |
{ |
| 187 |
Save(); |
| 188 |
entries.clear(); |
| 189 |
opened = false; |
| 190 |
} |
| 191 |
|
| 192 |
void ConfigFile::push_back(const ConfigEntry& e) |
| 193 |
{ |
| 194 |
dirty = true; |
| 195 |
entries.push_back(e); |
| 196 |
} |
| 197 |
|
| 198 |
ConfigEntry& ConfigEntry::operator=(const ConfigEntry& e) |
| 199 |
{ |
| 200 |
type = e.type; |
| 201 |
appname = e.appname; |
| 202 |
valname = e.valname; |
| 203 |
value.erase(); |
| 204 |
switch (type) |
| 205 |
{ |
| 206 |
case Binary: |
| 207 |
value = e.value; |
| 208 |
break; |
| 209 |
case Int: |
| 210 |
i = e.i; |
| 211 |
break; |
| 212 |
case Float: |
| 213 |
f = e.f; |
| 214 |
break; |
| 215 |
} |
| 216 |
return *this; |
| 217 |
} |
| 218 |
|
| 219 |
AVM_END_NAMESPACE; |