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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/* -*-c-*- -------------- mix_config.c :
* Implementation of the functions declared in mix_config.h
* ------------------------------------------------------------------
* Last change: Time-stamp: "01/02/12 00:35:28 jose"
* ------------------------------------------------------------------
* Copyright (C) 2001 Free Software Foundation, Inc.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "mix_config.h"
static const gchar COMMENT_PREFIX_ = '#';
static const gchar *DEF_DIR_ = ".mdk";
static const gchar *AUTOSAVE_KEY_ = "Autosave";
static const gchar *AUTOSAVE_YES_ = "True";
static const gchar *AUTOSAVE_NO_ = "False";
/* the config type */
struct mix_config_t
{
gchar *filename; /* full path to configuration file */
gboolean autosave; /* whether save on destroy */
GHashTable *items; /* configuration items */
};
/* create a new config handler, giving the dir and name of the config file */
mix_config_t *
mix_config_new (const gchar *dirname, const gchar *filename)
{
static const gchar *DEF_DIRNAME_ = NULL;
static const gchar *DEF_FILENAME_ = "config";
const gchar *autosave;
FILE *f;
mix_config_t *result = NULL;
if (DEF_DIRNAME_ == NULL)
DEF_DIRNAME_ = g_strdup_printf ("%s/%s", g_get_home_dir (), DEF_DIR_);
if (dirname == NULL) dirname = DEF_DIRNAME_;
if (filename == NULL) filename = DEF_FILENAME_;
if (!mix_stat_dir (dirname, "configuration")) return NULL;
result = g_new (mix_config_t, 1);
result->filename = g_strdup_printf ("%s/%s", dirname, filename);
result->items = g_hash_table_new (g_str_hash, g_str_equal);
f = fopen (result->filename, "r");
if (f != NULL)
{
enum {LEN = 256};
gchar buffer[LEN];
gchar *line = buffer;
while (!feof (f))
{
line = fgets (line, LEN, f);
if (line) line = g_strstrip (line);
if (line && line[0] != COMMENT_PREFIX_)
{
gchar **vals = g_strsplit (line, "=", 2);
g_hash_table_insert (result->items,
(gpointer)g_strdup (g_strstrip (vals[0])),
(gpointer)g_strdup (g_strstrip (vals[1])));
g_strfreev (vals);
}
}
fclose (f);
}
autosave = mix_config_get (result, AUTOSAVE_KEY_);
result->autosave = autosave && !g_strcasecmp (autosave, AUTOSAVE_YES_);
return result;
}
/* delete a config handler, saving the configuration if needed */
void
mix_config_delete (mix_config_t *config)
{
g_return_if_fail (config != NULL);
if (mix_config_is_autosave (config)) mix_config_save (config);
g_free (config->filename);
g_hash_table_destroy (config->items);
g_free (config);
}
/* get a config item's value from its key */
const gchar *
mix_config_get (const mix_config_t *config, const gchar *key)
{
g_return_val_if_fail (config != NULL, NULL);
g_return_val_if_fail (key != NULL, NULL);
return (const gchar*)g_hash_table_lookup (config->items, key);
}
/* update (or create if it does not exist) a new config item */
void
mix_config_update (mix_config_t *config, const gchar *key, const gchar *value)
{
gpointer tmp = NULL;
g_return_if_fail (config != NULL);
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
tmp = g_hash_table_lookup (config->items, key);
if (tmp)
{
g_free (tmp);
tmp = (gpointer)key;
}
else
{
tmp = (gpointer)g_strdup (key);
}
g_hash_table_insert (config->items, tmp, g_strdup (value));
}
/* save the current configuration */
static void
save_ (gpointer key, gpointer value, gpointer file)
{
fprintf ((FILE *)file, "%s=%s\n", (char *)key, (char *)value);
}
void
mix_config_save (const mix_config_t *config)
{
FILE *f;
g_return_if_fail (config != NULL);
f = fopen (config->filename, "w");
if (!f)
{
g_warning ("Unable to open config file %s (%s)",
config->filename, g_strerror (errno));
return;
}
g_hash_table_foreach (config->items, save_, (gpointer)f);
fclose (f);
}
/* set autosave on delete flag */
void
mix_config_set_autosave (mix_config_t *config, gboolean autosave)
{
mix_config_update (config, AUTOSAVE_KEY_,
autosave? AUTOSAVE_YES_ : AUTOSAVE_NO_);
config->autosave = autosave;
}
gboolean
mix_config_is_autosave (const mix_config_t *config)
{
g_return_val_if_fail (config != NULL, FALSE);
return config->autosave;
}
|