get rid of compiler warnings (casts and delete unused variables)
[sfrench/samba-autobuild/.git] / source3 / intl / lang_tdb.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    tdb based replacement for gettext 
5    Copyright (C) Andrew Tridgell 2001
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 static TDB_CONTEXT *tdb;
25
26 /* the currently selected language */
27 static char *current_lang;
28
29
30 /* load a msg file into the tdb */
31 static BOOL load_msg(const char *msg_file)
32 {
33         char **lines;
34         int num_lines, i;
35         char *msgid, *msgstr;
36         TDB_DATA key, data;
37
38         lines = file_lines_load(msg_file, &num_lines);
39
40         if (!lines) {
41                 return False;
42         }
43
44         if (tdb_lockall(tdb) != 0) return False;
45
46         /* wipe the db */
47         tdb_traverse(tdb, (tdb_traverse_func) tdb_delete, NULL);
48
49         msgid = NULL;
50         
51         for (i=0;i<num_lines;i++) {
52                 if (strncmp(lines[i], "msgid \"", 7) == 0) {
53                         msgid = lines[i] + 7;
54                 }
55                 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
56                         msgstr = lines[i] + 8;
57                         trim_string(msgid, NULL, "\"");
58                         trim_string(msgstr, NULL, "\"");
59                         if (*msgstr == 0) {
60                                 msgstr = msgid;
61                         }
62                         key.dptr = msgid;
63                         key.dsize = strlen(msgid)+1;
64                         data.dptr = msgstr;
65                         data.dsize = strlen(msgstr)+1;
66                         tdb_store(tdb, key, data, 0);
67                         msgid = NULL;
68                 }
69         }
70
71         file_lines_free(lines);
72         tdb_unlockall(tdb);
73
74         return True;
75 }
76
77
78 /* work out what language to use from locale variables */
79 static char *get_lang(void)
80 {
81         char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
82         int i;
83         char *p;
84
85         for (i=0; vars[i]; i++) {
86                 if ((p = getenv(vars[i]))) {
87                         return p;
88                 }
89         }
90
91         return NULL;
92 }
93
94 /* initialise the message translation subsystem. If the "lang" argument
95    is NULL then get the language from the normal environment variables */
96 BOOL lang_tdb_init(const char *lang)
97 {
98         char *path = NULL;
99         char *msg_path = NULL;
100         struct stat st;
101         static int initialised;
102         time_t loadtime;
103
104         /* we only want to init once per process, unless given
105            an override */
106         if (initialised && !lang) return True;
107
108         if (initialised) {
109                 /* we are re-initialising, free up any old init */
110                 tdb_close(tdb);
111                 tdb = NULL;
112                 SAFE_FREE(current_lang);
113         }
114
115         initialised = 1;
116
117         if (!lang) {
118                 /* no lang given, use environment */
119                 lang = get_lang();
120         }
121
122         /* if no lang then we don't translate */
123         if (!lang) return True;
124
125         asprintf(&msg_path, "%s.msg", lib_path((char *)lang));
126         if (stat(msg_path, &st) != 0) {
127                 /* the msg file isn't available */
128                 free(msg_path);
129                 return False;
130         }
131         
132
133         asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
134
135         tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
136         if (!tdb) {
137                 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
138                 free(path);
139                 free(msg_path);
140                 if (!tdb) return False;
141                 current_lang = strdup(lang);
142                 return True;
143         }
144
145         free(path);
146
147         loadtime = tdb_fetch_int(tdb, "/LOADTIME/");
148
149         if (loadtime == -1 || loadtime < st.st_mtime) {
150                 load_msg(msg_path);
151                 tdb_store_int(tdb, "/LOADTIME/", (int)time(NULL));
152         }
153         free(msg_path);
154
155         current_lang = strdup(lang);
156
157         return True;
158 }
159
160 /* translate a msgid to a message string in the current language 
161    returns a string that must be freed by calling lang_msg_free()
162 */
163 const char *lang_msg(const char *msgid)
164 {
165         TDB_DATA key, data;
166
167         lang_tdb_init(NULL);
168
169         if (!tdb) return msgid;
170
171         key.dptr = (char *)msgid;
172         key.dsize = strlen(msgid)+1;
173         
174         data = tdb_fetch(tdb, key);
175
176         /* if the message isn't found then we still need to return a pointer
177            that can be freed. Pity. */
178         if (!data.dptr) return strdup(msgid);
179
180         return (const char *)data.dptr;
181 }
182
183
184 /* free up a string from lang_msg() */
185 void lang_msg_free(const char *msgstr)
186 {
187         if (!tdb) return;
188         free((void *)msgstr);
189 }
190
191
192 /*
193   when the _() translation macro is used there is no obvious place to free
194   the resulting string and there is no easy way to give a static pointer.
195   All we can do is rotate between some static buffers and hope a single d_printf() 
196   doesn't have more calls to _() than the number of buffers 
197 */
198 const char *lang_msg_rotate(const char *msgid)
199 {
200 #define NUM_LANG_BUFS 4
201         char *msgstr;
202         static pstring bufs[NUM_LANG_BUFS];
203         static int next;
204
205         msgstr = (char *)lang_msg(msgid);
206         if (!msgstr) return msgid;
207
208         pstrcpy(bufs[next], msgstr);
209         msgstr = bufs[next];
210
211         next = (next+1) % NUM_LANG_BUFS;
212         
213         return msgstr;
214 }
215
216
217 /* 
218    return the current language - needed for language file mappings 
219 */
220 char *lang_tdb_current(void)
221 {
222         return current_lang;
223 }