]> git.meshlink.io Git - catta/blob - avahi-utils/stdb.c
* add new flags parameter to avahi_client_new()
[catta] / avahi-utils / stdb.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <gdbm.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <locale.h>
26 #include <stdio.h>
27
28 #include <avahi-common/malloc.h>
29
30 #include "stdb.h"
31
32 static GDBM_FILE gdbm_file = NULL;
33 static char *buffer = NULL;
34
35 static int init(void) {
36
37     if (gdbm_file)
38         return 0;
39
40     if (!(gdbm_file = gdbm_open((char*) DATABASE_FILE, 0, GDBM_READER, 0, NULL)))
41         return -1;
42
43     return 0;
44 }
45
46 const char* stdb_lookup(const char *name) {
47     datum key, data;
48     const char *loc;
49
50     if (init() < 0)
51         goto fail;
52
53     data.dptr = NULL;
54     data.dsize = 0;
55     
56     if ((loc = setlocale(LC_MESSAGES, NULL))) {
57         char k[256];
58         
59         snprintf(k, sizeof(k), "%s[%s]", name, loc);
60         key.dptr = k;
61         key.dsize = strlen(k);
62         data = gdbm_fetch(gdbm_file, key);
63
64         if (!data.dptr) {
65             char l[32], *e;
66             snprintf(l, sizeof(l), "%s", loc);
67             
68             if ((e = strchr(l, '@'))) {
69                 *e = 0;
70                 snprintf(k, sizeof(k), "%s[%s]", name, l);
71                 key.dptr = k;
72                 key.dsize = strlen(k);
73                 data = gdbm_fetch(gdbm_file, key);
74             }
75
76             if (!data.dptr) {
77                 if ((e = strchr(l, '_'))) {
78                     *e = 0;
79                     snprintf(k, sizeof(k), "%s[%s]", name, l);
80                     key.dptr = k;
81                     key.dsize = strlen(k);
82                     data = gdbm_fetch(gdbm_file, key);
83                 }
84             }
85         }
86     }
87
88     if (!data.dptr) {
89         key.dptr = (char*) name;
90         key.dsize = strlen(name);
91         data = gdbm_fetch(gdbm_file, key);
92     }
93
94     if (!data.dptr)
95         goto fail;
96
97     avahi_free(buffer);
98     buffer = avahi_strndup(data.dptr, data.dsize);
99     free(data.dptr);
100     
101     return buffer;
102     
103 fail:
104
105     return name;
106 }
107
108 void stdb_shutdown(void) {
109     if (gdbm_file)
110         gdbm_close(gdbm_file);
111
112     avahi_free(buffer);
113 }