-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfi-builder-keyfile.c
287 lines (231 loc) · 7.47 KB
/
dfi-builder-keyfile.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "dfi-builder-keyfile.h"
#include <string.h>
typedef struct
{
gchar *key;
gchar *locale;
gchar *value;
} DesktopFileIndexKeyfileItem;
typedef struct
{
gchar *name;
guint start;
} DesktopFileIndexKeyfileGroup;
struct _DesktopFileIndexKeyfile
{
GPtrArray *groups;
GPtrArray *items;
};
static void
desktop_file_index_keyfile_group_free (gpointer data)
{
DesktopFileIndexKeyfileGroup *group = data;
g_free (group->name);
g_slice_free (DesktopFileIndexKeyfileGroup, group);
}
static void
desktop_file_index_keyfile_item_free (gpointer data)
{
DesktopFileIndexKeyfileItem *item = data;
g_free (item->key);
g_free (item->locale);
g_free (item->value);
g_slice_free (DesktopFileIndexKeyfileItem, item);
}
void
desktop_file_index_keyfile_free (DesktopFileIndexKeyfile *keyfile)
{
g_ptr_array_free (keyfile->groups, TRUE);
g_ptr_array_free (keyfile->items, TRUE);
g_slice_free (DesktopFileIndexKeyfile, keyfile);
}
guint
desktop_file_index_keyfile_get_n_groups (DesktopFileIndexKeyfile *keyfile)
{
return keyfile->groups->len;
}
guint
desktop_file_index_keyfile_get_n_items (DesktopFileIndexKeyfile *keyfile)
{
return keyfile->items->len;
}
const gchar *
desktop_file_index_keyfile_get_group_name (DesktopFileIndexKeyfile *keyfile,
guint group)
{
DesktopFileIndexKeyfileGroup *kfg;
kfg = keyfile->groups->pdata[group];
return kfg->name;
}
void
desktop_file_index_keyfile_get_group_range (DesktopFileIndexKeyfile *keyfile,
guint group,
guint *start,
guint *end)
{
DesktopFileIndexKeyfileGroup *kfg;
kfg = keyfile->groups->pdata[group];
*start = kfg->start;
if (end)
{
if (group == keyfile->groups->len - 1)
*end = keyfile->items->len;
else
{
kfg = keyfile->groups->pdata[group + 1];
*end = kfg->start;
}
}
}
void
desktop_file_index_keyfile_get_item (DesktopFileIndexKeyfile *keyfile,
guint item,
const gchar **key,
const gchar **locale,
const gchar **value)
{
DesktopFileIndexKeyfileItem *kfi;
kfi = keyfile->items->pdata[item];
*key = kfi->key;
*locale = kfi->locale;
*value = kfi->value;
}
const gchar *
desktop_file_index_keyfile_get_value (DesktopFileIndexKeyfile *keyfile,
const gchar * const *locale_variants,
const gchar *group_name,
const gchar *key)
{
gint start = 0, end = 0;
gint i;
/* Find group... */
for (i = 0; i < keyfile->groups->len; i++)
{
DesktopFileIndexKeyfileGroup *group = keyfile->groups->pdata[i];
if (g_str_equal (group->name, group_name))
{
start = group->start;
if (i < keyfile->groups->len - 1)
{
DesktopFileIndexKeyfileGroup *next_group;
next_group = keyfile->groups->pdata[i + 1];
end = next_group->start;
}
else
end = keyfile->items->len;
}
}
/* For each locale variant... */
for (i = 0; locale_variants[i]; i++)
{
gint j;
for (j = start; j < end; j++)
{
DesktopFileIndexKeyfileItem *item = keyfile->items->pdata[j];
/* There are more unique locales than there are keys, so check
* those first.
*/
if (item->locale && g_str_equal (item->locale, locale_variants[i]) && g_str_equal (item->key, key))
return item->value;
}
}
/* Try the NULL locale as a fallback */
for (i = start; i < end; i++)
{
DesktopFileIndexKeyfileItem *item = keyfile->items->pdata[i];
if (item->locale[0] == '\0' && g_str_equal (item->key, key))
return item->value;
}
return NULL;
}
DesktopFileIndexKeyfile *
desktop_file_index_keyfile_new (const gchar *filename,
GError **error)
{
DesktopFileIndexKeyfile *kf;
gchar *contents;
const gchar *c;
gsize length;
gint line = 1;
if (!g_file_get_contents (filename, &contents, &length, error))
return NULL;
kf = g_slice_new (DesktopFileIndexKeyfile);
kf->groups = g_ptr_array_new_with_free_func (desktop_file_index_keyfile_group_free);
kf->items = g_ptr_array_new_with_free_func (desktop_file_index_keyfile_item_free);
c = contents;
while (*c)
{
gint line_length;
line_length = strcspn (c, "\n");
if (line_length == 0 || c[0] == '#')
/* looks like a comment */
;
else if (c[0] == '[')
{
DesktopFileIndexKeyfileGroup *kfg;
gint group_size;
group_size = strcspn (c + 1, "]");
if (group_size != line_length - 2)
{
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
"%s:%d: Invalid group line: ']' must be last character on line", filename, line);
goto err;
}
kfg = g_slice_new (DesktopFileIndexKeyfileGroup);
kfg->name = g_strndup (c + 1, group_size);
kfg->start = kf->items->len;
g_ptr_array_add (kf->groups, kfg);
}
else
{
DesktopFileIndexKeyfileItem *kfi;
gsize key_size;
const gchar *locale;
gsize locale_size;
const gchar *value;
gsize value_size;
key_size = strspn (c, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
if (key_size && c[key_size] == '[')
{
locale = c + key_size + 1;
locale_size = strspn (locale, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@._");
if (locale_size == 0 || locale[locale_size] != ']' || locale[locale_size + 1] != '=')
{
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
"%s:%d: Keys containing '[' must then have a locale name, then ']='", filename, line);
goto err;
}
value = locale + locale_size + 2;
value_size = line_length - locale_size - key_size - 3; /* [ ] = */
}
else if (key_size && c[key_size] == '=')
{
locale = "";
locale_size = 0;
value = c + key_size + 1;
value_size = line_length - key_size - 1; /* = */
}
else
{
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
"%s:%d: Lines must either be empty, comments, groups or assignments", filename, line);
goto err;
}
kfi = g_slice_new (DesktopFileIndexKeyfileItem);
kfi->key = g_strndup (c, key_size);
kfi->locale = g_strndup (locale, locale_size);
kfi->value = g_strndup (value, value_size);
g_ptr_array_add (kf->items, kfi);
}
c += line_length;
/* May have unterminated lines... */
if (*c == '\n')
c++;
line++;
}
return kf;
err:
g_ptr_array_free (kf->groups, TRUE);
g_ptr_array_free (kf->items, TRUE);
return NULL;
}