File: gconf-locale.c

package info (click to toggle)
gconf 3.2.6-6
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 15,120 kB
  • sloc: ansic: 39,709; sh: 11,189; python: 1,107; makefile: 445; xml: 332
file content (403 lines) | stat: -rw-r--r-- 8,815 bytes parent folder | download | duplicates (6)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* GConf
 * Copyright (C) 1999, 2000 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include "gconf-locale.h"
#include "gconf-internals.h"
#include <sys/time.h>
#include <time.h>
#include <string.h>

static void
gconf_locale_cache_add (GConfLocaleCache* cache,
                        const gchar* locale);

static GConfLocaleList*
gconf_locale_list_new (const gchar* locale);

typedef struct _GConfLocaleListPrivate GConfLocaleListPrivate;

struct _GConfLocaleListPrivate {
  gchar** list;
  guint refcount;
};

typedef struct _Entry Entry;

struct _Entry {
  gchar* locale;
  GConfLocaleList* list;
  GTime mod_time;
};

struct _GConfLocaleCache {
  GHashTable* hash;
};

GConfLocaleCache*
gconf_locale_cache_new (void)
{
  GConfLocaleCache* cache;

  cache = g_new(GConfLocaleCache, 1);

  cache->hash = g_hash_table_new(g_str_hash, g_str_equal);

  return cache;
}

void
gconf_locale_cache_free (GConfLocaleCache* cache)
{
  gconf_locale_cache_expire (cache, 0);

  g_assert(g_hash_table_size(cache->hash) == 0);

  g_hash_table_destroy(cache->hash);

  g_free(cache);
}

static void
gconf_locale_cache_add (GConfLocaleCache* cache,
                        const gchar* locale)
{
  Entry* e;

  e = g_new(Entry, 1);

  e->locale = g_strdup(locale);
  e->list = gconf_locale_list_new(locale);
  e->mod_time = time(NULL);
  
  g_hash_table_insert (cache->hash, e->locale, e);
}

typedef struct _ExpireData ExpireData;

struct _ExpireData {
  GTime now;
  guint max_age;
};

static gboolean
expire_foreach(const gchar* key,
               Entry* e, ExpireData* ed)
{
  GTime last_access = e->mod_time;

  /* MUST be >= so max_age of 0 will remove everything */
  if ((ed->now - last_access) >= ed->max_age)
    {
      gconf_locale_list_unref(e->list);
      g_free(e->locale);
      g_free(e);
      return TRUE;
    }
  else
    return FALSE;
}

void
gconf_locale_cache_expire (GConfLocaleCache* cache,
                           guint max_age_exclusive_in_seconds)
{
  ExpireData ed = { 0, 0 };
  ed.max_age = max_age_exclusive_in_seconds;

  ed.now = time(NULL);
  
  g_hash_table_foreach_remove(cache->hash, (GHRFunc)expire_foreach,
                              &ed);
}

void
gconf_locale_list_ref (GConfLocaleList* list)
{
  GConfLocaleListPrivate* priv;

  priv = (GConfLocaleListPrivate*) list;

  priv->refcount += 1;
}

void
gconf_locale_list_unref (GConfLocaleList* list)
{
  GConfLocaleListPrivate* priv;

  priv = (GConfLocaleListPrivate*) list;

  g_return_if_fail(priv->refcount > 0);

  priv->refcount -= 1;

  if (priv->refcount == 0)
    {
      g_strfreev(priv->list);
      g_free(list);
    }
}

GConfLocaleList*
gconf_locale_cache_get_list (GConfLocaleCache* cache,
                             const gchar* locale)
{
  Entry* e;

  if (locale == NULL)
    locale = "C";
  
  e = g_hash_table_lookup(cache->hash, locale);

  if (e != NULL)
    {
      gconf_locale_list_ref(e->list);
      return e->list;
    }
  else
    {
      gconf_locale_cache_add(cache, locale);

      e = g_hash_table_lookup(cache->hash, locale);

      g_assert(e != NULL);

      gconf_locale_list_ref(e->list);

      return e->list;
    }
}


/*
 * Big mess o' cut-and-pasted code
 */

/* --------------------------------------------------------------- */

/* Mask for components of locale spec. The ordering here is from
 * least significant to most significant
 */
enum
{
  COMPONENT_CODESET =   1 << 0,
  COMPONENT_TERRITORY = 1 << 1,
  COMPONENT_MODIFIER =  1 << 2
};

/* Break an X/Open style locale specification into components
 */
static guint
explode_locale (const gchar *locale,
		gchar **language, 
		gchar **territory, 
		gchar **codeset, 
		gchar **modifier)
{
  const gchar *uscore_pos;
  const gchar *at_pos;
  const gchar *dot_pos;

  guint mask = 0;

  uscore_pos = strchr (locale, '_');
  dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
  at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');

  if (at_pos)
    {
      mask |= COMPONENT_MODIFIER;
      *modifier = g_strdup (at_pos);
    }
  else
    at_pos = locale + strlen (locale);

  if (dot_pos)
    {
      mask |= COMPONENT_CODESET;
      *codeset = g_new (gchar, 1 + at_pos - dot_pos);
      strncpy (*codeset, dot_pos, at_pos - dot_pos);
      (*codeset)[at_pos - dot_pos] = '\0';
    }
  else
    dot_pos = at_pos;

  if (uscore_pos)
    {
      mask |= COMPONENT_TERRITORY;
      *territory = g_new (gchar, 1 + dot_pos - uscore_pos);
      strncpy (*territory, uscore_pos, dot_pos - uscore_pos);
      (*territory)[dot_pos - uscore_pos] = '\0';
    }
  else
    uscore_pos = dot_pos;

  *language = g_new (gchar, 1 + uscore_pos - locale);
  strncpy (*language, locale, uscore_pos - locale);
  (*language)[uscore_pos - locale] = '\0';

  return mask;
}

/*
 * Compute all interesting variants for a given locale name -
 * by stripping off different components of the value.
 *
 * For simplicity, we assume that the locale is in
 * X/Open format: language[_territory][.codeset][@modifier]
 *
 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
 *       as well. We could just copy the code from glibc wholesale
 *       but it is big, ugly, and complicated, so I'm reluctant
 *       to do so when this should handle 99% of the time...
 */
static GSList *
compute_locale_variants (const gchar *locale)
{
  GSList *retval = NULL;

  gchar *language = NULL;
  gchar *territory = NULL;
  gchar *codeset = NULL;
  gchar *modifier = NULL;

  guint mask;
  guint i;

  g_return_val_if_fail (locale != NULL, NULL);

  mask = explode_locale (locale, &language, &territory, &codeset, &modifier);

  /* Iterate through all possible combinations, from least attractive
   * to most attractive.
   */
  for (i=0; i<=mask; i++)
    if ((i & ~mask) == 0)
      {
	gchar *val = g_strconcat(language,
				 (i & COMPONENT_TERRITORY) ? territory : "",
				 (i & COMPONENT_CODESET) ? codeset : "",
				 (i & COMPONENT_MODIFIER) ? modifier : "",
				 NULL);
	retval = g_slist_prepend (retval, val);
      }

  g_free (language);
  if (mask & COMPONENT_CODESET)
    g_free (codeset);
  if (mask & COMPONENT_TERRITORY)
    g_free (territory);
  if (mask & COMPONENT_MODIFIER)
    g_free (modifier);

  return retval;
}

/* -------------------------------------------------------------- */

/*
 * End of big mess o' cut and pasted code
 */


static GConfLocaleList*
gconf_locale_list_new (const gchar* locale)
{
  GConfLocaleListPrivate* priv;
  
  priv = g_new(GConfLocaleListPrivate, 1);
  
  priv->refcount = 1;
  priv->list = gconf_split_locale(locale);
  
  return (GConfLocaleList*) priv;
}

gchar**
gconf_split_locale               (const gchar* locale)
{
  gchar** vector = NULL;
  GSList* list = NULL;
  gint c_locale_defined = FALSE;
  gchar *category_memory, *orig_category_memory;
  
  if (locale == NULL)
    locale = "C";

  /* For each locale name in the colon-separated string,
     extract all its variants */
  
  orig_category_memory = category_memory = g_malloc (strlen (locale)+1);
  
  while (locale[0] != '\0')
    {
      while (locale[0] != '\0' && locale[0] == ':')
        ++locale;
      
      if (locale[0] != '\0')
        {
          char *cp = category_memory;
          
          while (locale[0] != '\0' && locale[0] != ':')
            *category_memory++= *locale++;
          
          category_memory[0]= '\0'; 
          category_memory++;
          
          if (strcmp (cp, "C") == 0)
            c_locale_defined= TRUE;
          
          list = g_slist_concat (list, compute_locale_variants (cp));
        }
    }
  
  g_free (orig_category_memory);
  
  if (!c_locale_defined)
    list = g_slist_append (list, g_strdup ("C"));

  {
    /* Convert list to a string vector */
    guint n;
    guint i;
    GSList* tmp;
    
    n = g_slist_length(list);

    g_assert(n > 0);
    
    vector = g_new0(gchar*, n + 2);

    i = 0;
    tmp = list;

    while (tmp != NULL)
      {
        vector[i] = tmp->data;

        ++i;
        tmp = g_slist_next(tmp);
      }
    
    g_slist_free(list);
  }

  return vector;
}