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
|
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009 Alexey Yakovenko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "conf.h"
struct conf_item_t {
char *key;
char *value;
struct conf_item_t *next;
};
static struct conf_item_t *conf_items;
int
conf_load (void) {
extern char dbconfdir[1024]; // $HOME/.config/deadbeef
char str[1024];
snprintf (str, 1024, "%s/config", dbconfdir);
FILE *fp = fopen (str, "rt");
if (!fp) {
fprintf (stderr, "failed to load config file\n");
return -1;
}
int line = 0;
while (fgets (str, 1024, fp) != NULL) {
line++;
if (str[0] == '#' || str[0] <= 0x20) {
continue;
}
uint8_t *p = (uint8_t *)str;
while (*p && *p > 0x20) {
p++;
}
if (!*p) {
fprintf (stderr, "error in config file line %d\n", line);
continue;
}
*p = 0;
p++;
// skip whitespace
while (*p && *p <= 0x20) {
p++;
}
if (!*p) {
fprintf (stderr, "error in config file line %d\n", line);
continue;
}
char *value = p;
// remove trailing trash
while (*p && *p >= 0x20) {
p++;
}
*p = 0;
conf_set_str (str, value);
}
fclose (fp);
return 0;
}
int
conf_save (void) {
return 0;
}
void
conf_free (void) {
}
const char *
conf_get_str (const char *key, const char *def) {
for (struct conf_item_t *it = conf_items; it; it = it->next) {
if (!strcasecmp (key, it->key)) {
return it->value;
}
}
return def;
}
float
conf_get_float (const char *key, float def) {
const char *v = conf_get_str (key, NULL);
return v ? atof (v) : def;
}
int
conf_get_int (const char *key, int def) {
const char *v = conf_get_str (key, NULL);
return v ? atoi (v) : def;
}
void
conf_set_str (const char *key, const char *val) {
for (struct conf_item_t *it = conf_items; it; it = it->next) {
if (!strcasecmp (key, it->key)) {
free (it->value);
it->value = strdup (val);
return;
}
}
struct conf_item_t *it = malloc (sizeof (struct conf_item_t));
memset (it, 0, sizeof (struct conf_item_t));
it->next = conf_items;
it->key = strdup (key);
it->value = strdup (val);
conf_items = it;
}
|