summaryrefslogtreecommitdiff
path: root/mg_valmap.c
blob: 2361b00b254a8c246e669e8ade75fe4f4c1939c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "mg_valmap.h"
#include "mg_order.h"

mgValmap::mgValmap(const char *key) {
	m_key = key;
}

void mgValmap::Read(FILE *f) {
	char *line=(char*)malloc(1000);
	char *prefix=(char*)malloc(strlen(m_key)+2);
	strcpy(prefix,m_key);
	strcat(prefix,".");
	rewind(f);
	while (fgets(line,1000,f)) {
		if (strncmp(line,prefix,strlen(prefix))) continue;
		if (line[strlen(line)-1]=='\n')
				line[strlen(line)-1]=0;
		char *name = line + strlen(prefix);
		char *eq = strchr(name,'=');
		if (!eq) continue;
		*(eq-1)=0;
		char *value = eq + 2;
		(*this)[string(name)]=string(value);
	}
	free(prefix);
	free(line);
}

void mgValmap::Write(FILE *f) {
	for (mgValmap::const_iterator it=begin();it!=end();++it) {
		char b[1000];
		sprintf(b,"%s.%s = %s\n",
			m_key,it->first.c_str(),
			it->second.c_str());
		fputs(b,f);
	}
}

void mgValmap::put(const char* name, const string value) {
	if (value.empty()) return;
	(*this)[string(name)] = value;
}

void mgValmap::put(const char* name, const char* value) {
	if (!value || *value==0) return;
	(*this)[string(name)] = value;
}

void mgValmap::put(const char* name, const int value) {
	put(name,ltos(value));
}

void mgValmap::put(const char* name, const unsigned int value) {
	put(name,ltos(value));
}

void mgValmap::put(const char* name, const long value) {
	put(name,ltos(value));
}

void mgValmap::put(const char* name, const bool value) {
	string s;
	if (value)
		s = "true";
	else
		s = "false";
	put(name,s);
}