forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.h
More file actions
264 lines (235 loc) · 7.45 KB
/
Copy pathconf.h
File metadata and controls
264 lines (235 loc) · 7.45 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
This file is part of darktable,
copyright (c) 2009--2010 johannes hanika.
darktable 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 3 of the License, or
(at your option) any later version.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DT_USER_CONFIG_H
#define DT_USER_CONFIG_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "common/darktable.h"
#include "common/file_location.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <glib.h>
#include <glib/gprintf.h>
// silly gconf replacement for all of us now. (: very sucky altogether.
#define DT_CONF_MAX_VARS 512
#define DT_CONF_MAX_VAR_BUF 512
typedef struct dt_conf_t
{
dt_pthread_mutex_t mutex;
char filename[1024];
int num;
char varname[DT_CONF_MAX_VARS][DT_CONF_MAX_VAR_BUF];
char varval [DT_CONF_MAX_VARS][DT_CONF_MAX_VAR_BUF];
}
dt_conf_t;
/** return slot for this variable or newly allocated slot. */
static inline int dt_conf_get_var_pos(const char *name)
{
for(int i=0; i<darktable.conf->num; i++)
{
if(!strncmp(name, darktable.conf->varname[i], DT_CONF_MAX_VAR_BUF)) return i;
}
// not found, give it a new slot:
int num = darktable.conf->num++;
snprintf(darktable.conf->varname[num], DT_CONF_MAX_VAR_BUF, "%s", name);
memset(darktable.conf->varval[num], 0, DT_CONF_MAX_VAR_BUF);
// and get the default value from default darktablerc:
char buf[1024], defaultrc[1024];
dt_loc_get_datadir(buf, 1024);
snprintf(defaultrc, 1024, "%s/darktablerc", buf);
FILE *f = fopen(defaultrc, "rb");
char line[1024];
int read = 0;
if(!f) return num;
while(!feof(f))
{
read = fscanf(f, "%[^\n]\n", line);
if(read > 0)
{
char *c = line;
while(*c != '=' && c < line + strlen(line)) c++;
if(*c == '=')
{
*c = '\0';
if(!strncmp(line, name, DT_CONF_MAX_VAR_BUF))
{
strncpy(darktable.conf->varval[num], c+1, DT_CONF_MAX_VAR_BUF);
break;
}
}
}
}
fclose(f);
return num;
}
static inline void dt_conf_set_int(const char *name, int val)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%d", val);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
}
static inline void dt_conf_set_float(const char *name, float val)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
g_ascii_dtostr(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, val);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
}
static inline void dt_conf_set_bool(const char *name, int val)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%s", val ? "TRUE" : "FALSE");
dt_pthread_mutex_unlock(&darktable.conf->mutex);
}
static inline void dt_conf_set_string(const char *name, const char *val)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
snprintf(darktable.conf->varval[num], DT_CONF_MAX_VAR_BUF, "%s", val);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
}
static inline int dt_conf_get_int(const char *name)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
const int val = atol(darktable.conf->varval[num]);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
return val;
}
static inline float dt_conf_get_float(const char *name)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
const float val = g_ascii_strtod(darktable.conf->varval[num], NULL);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
return val;
}
static inline int dt_conf_get_bool(const char *name)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
const int val = darktable.conf->varval[num][0] == 'T';
dt_pthread_mutex_unlock(&darktable.conf->mutex);
return val;
}
static inline gchar *dt_conf_get_string(const char *name)
{
dt_pthread_mutex_lock(&darktable.conf->mutex);
const int num = dt_conf_get_var_pos(name);
dt_pthread_mutex_unlock(&darktable.conf->mutex);
return g_strdup(darktable.conf->varval[num]);
}
typedef struct dt_conf_string_entry_t
{
char *key;
char *value;
} dt_conf_string_entry_t;
/** get all strings in */
static inline GSList *dt_conf_all_string_entries (const char *dir)
{
GSList *result = NULL;
dt_pthread_mutex_lock(&darktable.conf->mutex);
for (int i=0; i<DT_CONF_MAX_VARS; i++)
{
if (strcmp(darktable.conf->varname[i],dir)==0)
{
dt_conf_string_entry_t *nv = (dt_conf_string_entry_t*)g_malloc (sizeof(dt_conf_string_entry_t));
gchar *key = g_strdup (darktable.conf->varname[i]);
/* get the key name from path/key */
gchar *p = key+strlen (key);
while (*--p!='/');
nv->key = g_strdup (++p);
/* get the value */
nv->value = g_strdup(darktable.conf->varval[i]);
result = g_slist_append (result,nv);
}
}
dt_pthread_mutex_unlock(&darktable.conf->mutex);
return result;
}
static inline void dt_conf_init(dt_conf_t *cf, const char *filename)
{
dt_pthread_mutex_init(&darktable.conf->mutex, NULL);
memset(cf->varname,0, DT_CONF_MAX_VARS*DT_CONF_MAX_VAR_BUF);
memset(cf->varval, 0, DT_CONF_MAX_VARS*DT_CONF_MAX_VAR_BUF);
snprintf(darktable.conf->filename, 1024, "%s", filename);
darktable.conf->num = 0;
FILE *f = fopen(filename, "rb");
char line[1024];
int read = 0;
int defaults = 0;
if(!f)
{
char buf[1024], defaultrc[1024];
dt_loc_get_datadir(buf, 1024);
snprintf(defaultrc, 1024, "%s/darktablerc", buf);
f = fopen(defaultrc, "rb");
defaults = 1;
}
if(!f) return;
while(!feof(f))
{
read = fscanf(f, "%[^\n]\n", line);
if(read > 0)
{
char *c = line;
while(*c != '=' && c < line + strlen(line)) c++;
if(*c == '=')
{
*c = '\0';
dt_conf_set_string(line, c+1);
}
}
}
fclose(f);
if(defaults) dt_configure_defaults();
return;
}
static inline void dt_conf_cleanup(dt_conf_t *cf)
{
FILE *f = fopen(cf->filename, "wb");
if(!f) return;
for(int i=0; i<cf->num; i++)
{
fprintf(f, "%s=%s\n", cf->varname[i], cf->varval[i]);
}
fclose(f);
dt_pthread_mutex_destroy(&darktable.conf->mutex);
}
/** check if key exists, return 1 if lookup successed, 0 if failed..*/
static inline int dt_conf_key_exists (const char *key)
{
dt_pthread_mutex_lock (&darktable.conf->mutex);
int res = 0;
/* lookup in stringtable for match of key name */
for (int i=0; i<darktable.conf->num; i++)
{
if (!strncmp (key, darktable.conf->varname[i], DT_CONF_MAX_VAR_BUF))
{
res=1;
break;
}
}
dt_pthread_mutex_unlock (&darktable.conf->mutex);
return res;
}
#endif
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;