switched over to a new method of handling uppercase/lowercase mappings
[vlendec/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 /* load a po file into the tdb */
27 static BOOL load_po(const char *po_file)
28 {
29         char **lines;
30         int num_lines, i;
31         char *msgid, *msgstr;
32         TDB_DATA key, data;
33
34         lines = file_lines_load(po_file, &num_lines);
35
36         if (!lines) {
37                 return False;
38         }
39
40         if (tdb_lockall(tdb) != 0) return False;
41
42         /* wipe the db */
43         tdb_traverse(tdb, (tdb_traverse_func) tdb_delete, NULL);
44         
45         for (i=0;i<num_lines;i++) {
46                 if (strncmp(lines[i], "msgid \"", 7) == 0) {
47                         msgid = lines[i] + 7;
48                 }
49                 if (strncmp(lines[i], "msgstr \"", 8) == 0) {
50                         msgstr = lines[i] + 8;
51                         trim_string(msgid, NULL, "\"");
52                         trim_string(msgstr, NULL, "\"");
53                         if (*msgstr == 0) {
54                                 msgstr = msgid;
55                         }
56                         key.dptr = msgid;
57                         key.dsize = strlen(msgid)+1;
58                         data.dptr = msgstr;
59                         data.dsize = strlen(msgstr)+1;
60                         tdb_store(tdb, key, data, 0);
61                 }
62         }
63
64         file_lines_free(lines);
65         tdb_unlockall(tdb);
66
67         return True;
68 }
69
70
71 /* work out what language to use from locale variables */
72 static char *get_lang(void)
73 {
74         char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
75         int i;
76         char *p;
77
78         for (i=0; vars[i]; i++) {
79                 if ((p = getenv(vars[i]))) {
80                         return p;
81                 }
82         }
83
84         return NULL;
85 }
86
87 /* initialise the message translation subsystem */
88 void lang_tdb_init(void)
89 {
90         char *lang;
91         char *path = NULL;
92         struct stat st;
93         static int initialised;
94         time_t loadtime;
95
96         /* we only want to init once per process */
97         if (initialised) return;
98         initialised = 1;
99
100         lang = get_lang();
101
102         /* if no lang then we don't translate */
103         if (!lang) return;
104
105         asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
106
107         tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
108         if (!tdb) {
109                 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
110                 free(path);
111                 return;
112         }
113
114         free(path);
115
116         asprintf(&path, "%s.po", lock_path(lang));
117
118         loadtime = tdb_fetch_int(tdb, "/LOADTIME/");
119
120         if (stat(path, &st) == 0 && (loadtime == -1 || loadtime < st.st_mtime)) {
121                 load_po(path);
122                 tdb_store_int(tdb, "/LOADTIME/", (int)time(NULL));
123         }
124         free(path);
125 }
126
127 /* translate a msgid to a message string in the current language 
128    returns a string that must be freed by calling lang_msg_free()
129 */
130 const char *lang_msg(const char *msgid)
131 {
132         TDB_DATA key, data;
133
134         lang_tdb_init();
135
136         if (!tdb) return msgid;
137
138         key.dptr = (char *)msgid;
139         key.dsize = strlen(msgid)+1;
140         
141         data = tdb_fetch(tdb, key);
142
143         /* if the message isn't found then we still need to return a pointer
144            that can be freed. Pity. */
145         if (!data.dptr) return strdup(msgid);
146
147         return (const char *)data.dptr;
148 }
149
150
151 /* free up a string from lang_msg() */
152 void lang_msg_free(const char *msgstr)
153 {
154         if (!tdb) return;
155         free((void *)msgstr);
156 }
157
158
159 /*
160   when the _() translation macro is used there is no obvious place to free
161   the resulting string and there is no easy way to give a static pointer.
162   All we can do is rotate between some static buffers and hope a single d_printf() 
163   doesn't have more calls to _() than the number of buffers 
164 */
165 const char *lang_msg_rotate(const char *msgid)
166 {
167 #define NUM_LANG_BUFS 4
168         char *msgstr;
169         static pstring bufs[NUM_LANG_BUFS];
170         static int next;
171
172         msgstr = lang_msg(msgid);
173         if (!msgstr) return msgid;
174
175         pstrcpy(bufs[next], msgstr);
176         msgstr = bufs[next];
177
178         next = (next+1) % NUM_LANG_BUFS;
179         
180         return msgstr;
181 }