File: config.cc

package info (click to toggle)
console-apt 0.7.7.2potato2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 612 kB
  • ctags: 658
  • sloc: cpp: 6,883; sh: 152; makefile: 47
file content (153 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include <std.h>

#include "config.h"

#include <fstream>
#include <vector>

struct param
{
	char *param_name;
	char *param_value;
	bool override;
};

struct param valid_params[] = {
	{"description", "1", false},
	{"sort", "4/2", false},
	{"view", "verbose", false},
	{"captris", "0", false},
	{"captris_score", "0", false},
	{NULL, NULL, false}
};

bool writeconfig(void)
{
	string filename;

	filename += getenv("HOME");
	filename += "/";
	filename += ".captrc";

	ofstream CCHandle(filename.c_str());

	if (!CCHandle.is_open())
		return false;

	for (struct param * p = valid_params; p->param_name != NULL; p++)
	{
		CCHandle << p->param_name << "=" << p->param_value << endl;
	}

	return true;
}

bool initconfig(void)
{
	string filename;

	filename += getenv("HOME");
	filename += "/";
	filename += ".captrc";

	ifstream CCHandle(filename.c_str());

	if (!CCHandle.is_open())
		return false;

	string buffer;

	char buf[BUFSIZ], *x;

	while (1)
	{
		CCHandle.getline(buf, BUFSIZ - 1, '\n');

		if (CCHandle.eof() == true)
			break;

		if ((x = strchr(buf, '\n')))
			*x = 0;

		for (x = buf; *x != '=' && *x != 0; x++);

		if (*x == 0)
		{
			printf("initconfig() parse error: %s (must be name=value pair)\n", buf);
			continue;
		}

		*x = 0;

		x++;

		if (*x == 0)
		{
			printf("initconfig() parse error: %s (must be name=value pair)\n", buf);
			continue;
		}

		if (valid_config_param(buf) == false)
		{
			printf("initconfig(): invalid option: %s (do not edit the config yourself)\n", buf);
			continue;
		}

		store_config_param(buf, x);
	}

	return true;
}

char *get_config_string(char *name)
{
	for (struct param * p = valid_params; p->param_name != NULL; p++)
	{
		if (strcmp(name, p->param_name) == 0)
			return p->param_value;
	}

	return NULL;
}

bool set_config_string(char *name, char *val)
{
	for (struct param * p = valid_params; p->param_name != NULL; p++)
	{
		if (strcmp(name, p->param_name) == 0)
		{
			if (p->override == true)
				free(p->param_value);
			else
				p->override = true;
			p->param_value = strdup(val);
			return true;
		}
	}

	return false;
}

bool valid_config_param(char *name)
{
	for (struct param * p = valid_params; p->param_name != NULL; p++)
	{
		if (strcmp(name, p->param_name) == 0)
			return true;
	}

	return false;
}

void store_config_param(char *name, char *val)
{
	for (struct param * p = valid_params; p->param_name != NULL; p++)
	{
		if (strcmp(name, p->param_name) == 0)
		{
			p->param_value = strdup(val);
			return;
		}
	}
}