| 1 |
#include "configfile.h" |
| 2 |
#include "Config.h" |
| 3 |
#include "avm_output.h" |
| 4 |
|
| 5 |
#include <sys/types.h> |
| 6 |
#include <sys/stat.h> |
| 7 |
#include <unistd.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdlib.h> // atexit |
| 10 |
#include <stdio.h> |
| 11 |
#include <pwd.h> |
| 12 |
|
| 13 |
AVM_BEGIN_NAMESPACE |
| 14 |
|
| 15 |
/* avoiding using static objects - using just pointers */ |
| 16 |
|
| 17 |
#define DEFAULT_DIR ".avm" |
| 18 |
#define DEFAULT_NAME "default" |
| 19 |
|
| 20 |
static avm::string sConfigDir(DEFAULT_DIR); |
| 21 |
static avm::string sConfigName(DEFAULT_NAME); |
| 22 |
static ConfigFile* config = 0; |
| 23 |
|
| 24 |
// called at exit |
| 25 |
static void destroy_config() |
| 26 |
{ |
| 27 |
delete config; |
| 28 |
config = 0; |
| 29 |
} |
| 30 |
|
| 31 |
static ConfigFile* get_config() |
| 32 |
{ |
| 33 |
if (!config) |
| 34 |
{ |
| 35 |
const char* home = getenv("HOME"); |
| 36 |
if (home == 0) |
| 37 |
{ |
| 38 |
const struct passwd* pwent = getpwuid(getuid()); |
| 39 |
home = pwent->pw_dir; |
| 40 |
} |
| 41 |
avm::string s(home); |
| 42 |
s += "/"; |
| 43 |
s += sConfigDir; |
| 44 |
|
| 45 |
struct stat st; |
| 46 |
if (stat(s.c_str(), &st)) |
| 47 |
{ |
| 48 |
AVM_WRITE("Registry", "creating dir: %s\n", s.c_str()); |
| 49 |
mkdir(s.c_str(), 0755); |
| 50 |
} |
| 51 |
|
| 52 |
s += "/"; |
| 53 |
s += sConfigName; |
| 54 |
|
| 55 |
config = new ConfigFile(s.c_str()); |
| 56 |
atexit(destroy_config); |
| 57 |
} |
| 58 |
|
| 59 |
return config; |
| 60 |
} |
| 61 |
|
| 62 |
void* RegInit(const char* regname, const char* dirname) |
| 63 |
{ |
| 64 |
destroy_config(); |
| 65 |
sConfigDir = dirname ? : DEFAULT_DIR; |
| 66 |
sConfigName = regname ? : DEFAULT_NAME; |
| 67 |
|
| 68 |
return get_config(); |
| 69 |
} |
| 70 |
|
| 71 |
void RegSave() |
| 72 |
{ |
| 73 |
get_config()->Save(); |
| 74 |
} |
| 75 |
|
| 76 |
int RegReadInt(const char* appname, const char* valname, int def_value) |
| 77 |
{ |
| 78 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 79 |
{ |
| 80 |
if (e->type == ConfigEntry::Int) |
| 81 |
return e->i; |
| 82 |
if (e->type == ConfigEntry::Binary |
| 83 |
&& (sscanf(e->value.c_str(), "%d", &e->i) == 1)) |
| 84 |
{ |
| 85 |
e->type = ConfigEntry::Int; |
| 86 |
return e->i; |
| 87 |
} |
| 88 |
} |
| 89 |
RegWriteInt(appname, valname, def_value); |
| 90 |
return def_value; |
| 91 |
} |
| 92 |
|
| 93 |
int RegWriteInt(const char* appname, const char* valname, int value) |
| 94 |
{ |
| 95 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 96 |
{ |
| 97 |
e->SetInt(value); |
| 98 |
get_config()->dirty = true; |
| 99 |
} |
| 100 |
else |
| 101 |
get_config()->push_back(ConfigEntry(appname, valname, value)); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
float RegReadFloat(const char* appname, const char* valname, float def_value) |
| 106 |
{ |
| 107 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 108 |
{ |
| 109 |
if (e->type != ConfigEntry::Float) |
| 110 |
{ |
| 111 |
if (e->type != ConfigEntry::Binary |
| 112 |
|| (sscanf(e->value.c_str(), "%f", &e->f) != 1)) |
| 113 |
return -1; |
| 114 |
e->type = ConfigEntry::Float; |
| 115 |
} |
| 116 |
return e->f; |
| 117 |
} |
| 118 |
RegWriteFloat(appname, valname, def_value); |
| 119 |
return def_value; |
| 120 |
} |
| 121 |
|
| 122 |
int RegWriteFloat(const char* appname, const char* valname, float value) |
| 123 |
{ |
| 124 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 125 |
{ |
| 126 |
e->SetFloat(value); |
| 127 |
get_config()->dirty = true; |
| 128 |
} |
| 129 |
else |
| 130 |
get_config()->push_back(ConfigEntry(appname, valname, value)); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
const char* RegReadString(const char* appname, const char* valname, const char* def_value) |
| 135 |
{ |
| 136 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 137 |
return (e->type == ConfigEntry::Binary) ? e->value.c_str() : ""; |
| 138 |
|
| 139 |
RegWriteString(appname, valname, def_value); |
| 140 |
return def_value; |
| 141 |
} |
| 142 |
|
| 143 |
int RegWriteString(const char* appname, const char* valname, const char* value) |
| 144 |
{ |
| 145 |
if (ConfigEntry* e = get_config()->Find(appname, valname)) |
| 146 |
{ |
| 147 |
e->SetString(value); |
| 148 |
get_config()->dirty = true; |
| 149 |
} |
| 150 |
else |
| 151 |
get_config()->push_back(ConfigEntry(appname, valname, value)); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
AVM_END_NAMESPACE; |