1
/* $Id: xfce-rc-simple.c 21094 2006-04-19 19:19:03Z benny $ */
1
/* $Id: xfce-rc-simple.c 24966 2007-02-13 10:16:28Z benny $ */
3
* Copyright (c) 2003-2005 Benedikt Meurer <benny@xfce.org>
3
* Copyright (c) 2003-2007 Benedikt Meurer <benny@xfce.org>
4
4
* All rights reserved.
6
6
* This library is free software; you can redistribute it and/or
57
59
typedef struct _LEntry LEntry;
58
60
typedef struct _Group Group;
64
static Group* simple_add_group (XfceRcSimple *simple,
66
static Entry* simple_add_entry (XfceRcSimple *simple,
70
static gboolean simple_parse_line (gchar *line,
75
static gchar* simple_escape (gchar *buffer,
78
static gboolean simple_write (XfceRcSimple *simple,
79
const gchar *filename);
80
static void simple_entry_free (Entry *entry);
81
static void simple_group_free (Group *group);
60
85
struct _XfceRcSimple
64
89
gboolean shared_chunks;
65
GMemChunk *entry_chunk;
66
GMemChunk *lentry_chunk;
67
GMemChunk *group_chunk;
68
90
GStringChunk *string_chunk;
109
/* local prototypes */
110
static Group* simple_add_group (XfceRcSimple *simple,
112
static Entry* simple_add_entry (XfceRcSimple *simple,
115
const gchar *locale);
116
static gboolean simple_parse_line (gchar *line,
121
static gchar* simple_escape (gchar *buffer,
123
const gchar *string);
124
static gboolean simple_write (XfceRcSimple *simple,
125
const gchar *filename);
129
simple_add_group (XfceRcSimple *simple, const gchar *name)
132
simple_add_group (XfceRcSimple *simple,
134
138
if (strcmp (group->name, name) == 0)
137
group = g_chunk_new (Group, simple->group_chunk);
141
group = _xfce_slice_new (Group);
138
142
group->name = g_string_chunk_insert (simple->string_chunk, name);
139
143
group->efirst = NULL;
140
144
group->elast = NULL;
197
202
/* why you annoying macro, will you stop borking libxfceutil? thanks. DO NOT DO THAT: g_return_val_if_fail (locale == NULL, NULL); */
199
entry = g_chunk_new (Entry, simple->entry_chunk);
204
entry = _xfce_slice_new (Entry);
200
205
entry->key = g_string_chunk_insert (simple->string_chunk, key);
201
206
entry->value = g_string_chunk_insert (simple->string_chunk, value);
202
207
entry->lfirst = NULL;
223
228
/* overwrite existing value */
224
229
if (G_LIKELY (strcmp (entry->value, value) != 0))
225
entry->value = g_string_chunk_insert (simple->string_chunk, value);
230
entry->value = g_string_chunk_insert (simple->string_chunk, value);
253
258
if (G_LIKELY (lentry == NULL))
255
260
/* create new localized entry */
256
lentry = g_chunk_new (LEntry, simple->lentry_chunk);
261
lentry = _xfce_slice_new (LEntry);
257
262
lentry->locale = g_string_chunk_insert (simple->string_chunk, locale);
258
263
lentry->value = g_string_chunk_insert (simple->string_chunk, value);
498
506
fp = fopen (filename, "w");
499
507
if (G_UNLIKELY (fp == NULL))
501
g_critical ("Unable to open file %s for writing: %s",
509
g_critical ("Unable to open file %s for writing: %s", filename, g_strerror (errno));
509
515
/* don't store empty groups */
510
516
if (group->efirst == NULL)
513
519
/* NULL_GROUP has no header */
514
520
if (strcmp (group->name, NULL_GROUP) != 0)
515
fprintf (fp, "[%s]\n", group->name);
521
fprintf (fp, "[%s]\n", group->name);
517
523
for (entry = group->efirst; entry != NULL; entry = entry->next)
519
525
simple_escape (buffer, LINE_MAX, entry->value);
520
fprintf (fp, "%s=%s\n", entry->key, buffer);
526
fprintf (fp, "%s=%s\n", entry->key, buffer);
522
for (lentry = entry->lfirst; lentry != NULL; lentry = lentry->next)
528
for (lentry = entry->lfirst; lentry != NULL; lentry = lentry->next)
524
530
simple_escape (buffer, LINE_MAX, lentry->value);
525
fprintf (fp, "%s[%s]=%s\n", entry->key, lentry->locale, buffer);
531
fprintf (fp, "%s[%s]=%s\n", entry->key, lentry->locale, buffer);
529
535
fprintf (fp, "\n");
534
g_critical ("Unable to write to file %s: Unexpected internal error",
540
g_critical ("Unable to write to file %s: Unexpected internal error", filename);
537
542
unlink (filename);
553
simple_entry_free (Entry *entry)
558
/* release all lentries */
559
for (lentry = entry->lfirst; lentry != NULL; lentry = next)
561
/* determine the next lentry */
564
/* release this lentry */
565
_xfce_slice_free (LEntry, lentry);
568
/* release the entry */
569
_xfce_slice_free (Entry, entry);
575
simple_group_free (Group *group)
580
/* release all entries */
581
for (entry = group->efirst; entry != NULL; entry = next)
583
/* determine the next entry */
586
/* release this entry */
587
simple_entry_free (entry);
590
/* release the group */
591
_xfce_slice_free (Group, group);
547
597
_xfce_rc_simple_new (XfceRcSimple *shared,
548
const gchar *filename,
598
const gchar *filename,
551
601
XfceRcSimple *simple;
578
628
if (shared != NULL)
580
630
simple->shared_chunks = TRUE;
581
simple->entry_chunk = shared->entry_chunk;
582
simple->lentry_chunk = shared->lentry_chunk;
583
simple->group_chunk = shared->group_chunk;
584
631
simple->string_chunk = shared->string_chunk;
588
635
simple->shared_chunks = FALSE;
589
simple->entry_chunk = g_mem_chunk_create (Entry, 128, G_ALLOC_ONLY);
590
simple->lentry_chunk = g_mem_chunk_create (LEntry, 256, G_ALLOC_ONLY);
591
simple->group_chunk = g_mem_chunk_create (Group, 32, G_ALLOC_ONLY);
592
636
simple->string_chunk = g_string_chunk_new (4096);
616
g_return_val_if_fail (simple != NULL, FALSE);
617
g_return_val_if_fail (simple->filename != NULL, FALSE);
661
_xfce_return_val_if_fail (simple != NULL, FALSE);
662
_xfce_return_val_if_fail (simple->filename != NULL, FALSE);
619
664
readonly = xfce_rc_is_readonly (XFCE_RC (simple));
625
670
while (fgets (line, LINE_MAX, fp) != NULL)
627
672
if (!simple_parse_line (line, §ion, &key, &value, &locale))
630
675
if (section != NULL)
632
simple->group = simple_add_group (simple, section);
677
simple->group = simple_add_group (simple, section);
636
681
if (locale == NULL)
638
simple_add_entry (simple, key, value, NULL);
683
simple_add_entry (simple, key, value, NULL);
642
687
if (XFCE_RC (simple)->locale == NULL)
645
690
if (xfce_locale_match (XFCE_RC (simple)->locale, locale) > XFCE_LOCALE_NO_MATCH
659
705
_xfce_rc_simple_close (XfceRc *rc)
661
707
XfceRcSimple *simple = XFCE_RC_SIMPLE (rc);
711
/* release all memory allocated to the groups */
712
for (group = simple->gfirst; group != NULL; group = group_next)
714
/* determine the next group */
715
group_next = group->next;
717
/* release this group */
718
simple_group_free (group);
721
/* release the string chunk */
663
722
if (!simple->shared_chunks)
665
g_mem_chunk_destroy (simple->entry_chunk);
666
g_mem_chunk_destroy (simple->lentry_chunk);
667
g_mem_chunk_destroy (simple->group_chunk);
668
g_string_chunk_free (simple->string_chunk);
723
g_string_chunk_free (simple->string_chunk);
674
729
_xfce_rc_simple_flush (XfceRc *rc)
748
808
for (group = simple->gfirst; group != NULL; group = group->next)
753
result = g_realloc (result, (size + 1) * sizeof (*result));
813
result = g_realloc (result, (size + 1) * sizeof (*result));
755
815
result[pos] = g_strdup (group->name);
788
850
for (entry = group->efirst; entry != NULL; entry = entry->next)
793
result = g_realloc (result, (size + 1) * sizeof (*result));
855
result = g_realloc (result, (size + 1) * sizeof (*result));
795
857
result[pos] = g_strdup (entry->key);
806
_xfce_rc_simple_delete_group (XfceRc *rc, const gchar *name, gboolean global)
869
_xfce_rc_simple_delete_group (XfceRc *rc,
808
873
XfceRcSimple *simple = XFCE_RC_SIMPLE (rc);
811
878
if (name == NULL)
812
879
name = NULL_GROUP;
818
885
if (simple->group == group || strcmp (name, NULL_GROUP) == 0)
820
887
/* don't delete current group or the default group, just clear them */
888
for (entry = group->efirst; entry != NULL; entry = next)
891
simple_entry_free (entry);
821
893
group->efirst = group->elast = NULL;
887
_xfce_rc_simple_delete_entry (XfceRc *rc, const gchar *key, gboolean global)
968
_xfce_rc_simple_delete_entry (XfceRc *rc,
889
972
XfceRcSimple *simple = XFCE_RC_SIMPLE (rc);
946
1034
/* check for localized entry (best fit!) */
947
1035
if (G_LIKELY (translated && rc->locale != NULL))
949
best_match = XFCE_LOCALE_NO_MATCH;
952
for (lentry = entry->lfirst; lentry != NULL; lentry = lentry->next)
954
match = xfce_locale_match (rc->locale, lentry->locale);
955
if (match == XFCE_LOCALE_FULL_MATCH)
958
return lentry->value;
960
else if (match > best_match)
963
best_value = lentry->value;
967
if (best_value != NULL)
1037
best_match = XFCE_LOCALE_NO_MATCH;
1040
for (lentry = entry->lfirst; lentry != NULL; lentry = lentry->next)
1042
match = xfce_locale_match (rc->locale, lentry->locale);
1043
if (match == XFCE_LOCALE_FULL_MATCH)
1046
return lentry->value;
1048
else if (match > best_match)
1051
best_value = lentry->value;
1055
if (best_value != NULL)
973
1061
return entry->value;