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
|
/* -*-c-*- -------------- mixgtk_config.c :
* Implementation of the functions declared in mixgtk_config.h
* ------------------------------------------------------------------
* Last change: Time-stamp: "2001-04-29 14:53:06 jao"
* ------------------------------------------------------------------
* 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 "mixgtk.h"
#include "mixgtk_config.h"
#define CONFIG_FILENAME "config"
#define COMMENT_PREFIX '#'
#define AUTOSAVE_FLAG "Autosave"
static GHashTable *items_;
static gchar *config_ = NULL;
static gboolean autosave_ = FALSE;
/* load configuration */
gboolean
mixgtk_config_load (void)
{
const gchar *autosave;
FILE *f;
gchar *mixdir = g_strdup_printf ("%s/%s", g_get_home_dir (), MIXGTK_FILES_DIR);
int k = mkdir (mixdir, S_IRWXU | S_IRWXG | S_IRWXO);
if (!k) g_message ("Configuration directory %s created\n", mixdir);
config_ = g_strdup_printf ("%s/%s", mixdir, CONFIG_FILENAME);
items_ = g_hash_table_new (g_str_hash, g_str_equal);
f = fopen (config_, "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 (items_,
(gpointer)g_strdup (g_strstrip (vals[0])),
(gpointer)g_strdup (g_strstrip (vals[1])));
g_strfreev (vals);
}
}
fclose (f);
}
autosave = mixgtk_config_get (AUTOSAVE_FLAG);
autosave_ = autosave && !g_strcasecmp (autosave, "True");
g_free (mixdir);
return TRUE;
}
/* autosave state */
gboolean
mixgtk_config_is_autosave (void)
{
return autosave_;
}
void
mixgtk_config_set_autosave (gboolean autosave)
{
if (autosave != autosave_)
{
mixgtk_config_update (AUTOSAVE_FLAG, autosave? "True" : "False");
autosave_ = autosave;
}
}
/* update config item */
void
mixgtk_config_update (const gchar *key, const gchar *value)
{
gpointer tmp = NULL;
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
tmp = g_hash_table_lookup (items_, key);
if (tmp)
{
g_free (tmp);
tmp = (gpointer)key;
}
else
{
tmp = (gpointer)g_strdup (key);
}
g_hash_table_insert (items_, tmp, g_strdup (value));
}
/* get config item */
const gchar *
mixgtk_config_get (const gchar *key)
{
return (const gchar*)g_hash_table_lookup (items_, key);
}
/* save configuration */
static void
save_ (gpointer key, gpointer value, gpointer file)
{
fprintf ((FILE *)file, "%s=%s\n", (char *)key, (char *)value);
}
void
mixgtk_config_save (void)
{
FILE *f = fopen (config_, "w");
if (!f)
{
g_warning ("Unable to open config file %s (%s)",
config_, g_strerror (errno));
return;
}
g_hash_table_foreach (items_, save_, (gpointer)f);
fclose (f);
}
|