r5296: - only include the tdb headers where they are needed
[Samba.git] / source / intl / lang_tdb.c
blob660323682db141ec3282963ab5f79dca91a7e8d0
1 /*
2 Unix SMB/CIFS implementation.
3 tdb based replacement for gettext
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "lib/tdb/include/tdbutil.h"
23 #include "system/time.h"
25 static TDB_CONTEXT *tdb;
27 /* the currently selected language */
28 static char *current_lang;
31 /* load a msg file into the tdb */
32 static BOOL load_msg(const char *msg_file)
34 char **lines;
35 int num_lines, i;
36 char *msgid, *msgstr;
37 TDB_DATA key, data;
39 lines = file_lines_load(msg_file, &num_lines);
41 if (!lines) {
42 return False;
45 if (tdb_lockall(tdb) != 0) return False;
47 /* wipe the db */
48 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
50 msgid = NULL;
52 for (i=0;i<num_lines;i++) {
53 if (strncmp(lines[i], "msgid \"", 7) == 0) {
54 msgid = lines[i] + 7;
56 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
57 msgstr = lines[i] + 8;
58 trim_string(msgid, NULL, "\"");
59 trim_string(msgstr, NULL, "\"");
60 if (*msgstr == 0) {
61 msgstr = msgid;
63 key.dptr = msgid;
64 key.dsize = strlen(msgid)+1;
65 data.dptr = msgstr;
66 data.dsize = strlen(msgstr)+1;
67 tdb_store(tdb, key, data, 0);
68 msgid = NULL;
72 file_lines_free(lines);
73 tdb_unlockall(tdb);
75 return True;
79 /* work out what language to use from locale variables */
80 static const char *get_lang(void)
82 const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
83 int i;
84 char *p;
86 for (i=0; vars[i]; i++) {
87 if ((p = getenv(vars[i]))) {
88 return p;
92 return NULL;
95 /* initialise the message translation subsystem. If the "lang" argument
96 is NULL then get the language from the normal environment variables */
97 BOOL lang_tdb_init(const char *lang)
99 char *path = NULL;
100 char *msg_path = NULL;
101 struct stat st;
102 static int initialised;
103 time_t loadtime;
104 TALLOC_CTX *mem_ctx;
106 /* we only want to init once per process, unless given
107 an override */
108 if (initialised && !lang) return True;
110 if (initialised) {
111 /* we are re-initialising, free up any old init */
112 if (tdb) {
113 tdb_close(tdb);
114 tdb = NULL;
116 SAFE_FREE(current_lang);
119 initialised = 1;
121 if (!lang) {
122 /* no lang given, use environment */
123 lang = get_lang();
126 /* if no lang then we don't translate */
127 if (!lang) return True;
129 mem_ctx = talloc_init("lang_tdb_init");
130 if (!mem_ctx) {
131 return False;
133 asprintf(&msg_path, "%s.msg", lib_path(mem_ctx, (const char *)lang));
134 if (stat(msg_path, &st) != 0) {
135 /* the msg file isn't available */
136 free(msg_path);
137 talloc_free(mem_ctx);
138 return False;
142 asprintf(&path, "%s%s.tdb", lock_path(mem_ctx, "lang_"), lang);
144 tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
145 if (!tdb) {
146 tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDONLY, 0);
147 free(path);
148 free(msg_path);
149 talloc_free(mem_ctx);
150 if (!tdb) return False;
151 current_lang = strdup(lang);
152 return True;
155 free(path);
156 talloc_free(mem_ctx);
158 loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
160 if (loadtime == -1 || loadtime < st.st_mtime) {
161 load_msg(msg_path);
162 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
164 free(msg_path);
166 current_lang = strdup(lang);
168 return True;
171 /* translate a msgid to a message string in the current language
172 returns a string that must be freed by calling lang_msg_free()
174 char *lang_msg(const char *msgid)
176 TDB_DATA key, data;
178 lang_tdb_init(NULL);
180 if (!tdb) return strdup(msgid);
182 key.dptr = strdup(msgid);
183 key.dsize = strlen(msgid)+1;
185 data = tdb_fetch(tdb, key);
187 free(key.dptr);
189 /* if the message isn't found then we still need to return a pointer
190 that can be freed. Pity. */
191 if (!data.dptr)
192 return strdup(msgid);
194 return data.dptr;
198 /* free up a string from lang_msg() */
199 void lang_msg_free(char *msgstr)
201 free(msgstr);
206 when the _() translation macro is used there is no obvious place to free
207 the resulting string and there is no easy way to give a static pointer.
208 All we can do is rotate between some static buffers and hope a single d_printf()
209 doesn't have more calls to _() than the number of buffers
211 const char *lang_msg_rotate(const char *msgid)
213 #define NUM_LANG_BUFS 4
214 const char *msgstr;
215 static pstring bufs[NUM_LANG_BUFS];
216 static int next;
218 msgstr = lang_msg(msgid);
219 if (!msgstr) return msgid;
221 pstrcpy(bufs[next], msgstr);
222 msgstr = bufs[next];
224 next = (next+1) % NUM_LANG_BUFS;
226 return msgstr;
231 return the current language - needed for language file mappings
233 char *lang_tdb_current(void)
235 return current_lang;