From Alexander Bokovoy
[ira/wip.git] / source3 / intl / lang_tdb.c
index b98e5734cbf82ac6ad0873aed84aaebad50b0831..bb780c5feda6afd23e74cd6531b83d39006653f3 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -27,20 +26,23 @@ static char *current_lang;
 
 
 /* load a msg file into the tdb */
-static BOOL load_msg(const char *msg_file)
+static bool load_msg(const char *msg_file)
 {
        char **lines;
        int num_lines, i;
        char *msgid, *msgstr;
-       TDB_DATA key, data;
+       TDB_DATA data;
 
-       lines = file_lines_load(msg_file, &num_lines);
+       lines = file_lines_load(msg_file, &num_lines,0);
 
        if (!lines) {
                return False;
        }
 
-       if (tdb_lockall(tdb) != 0) return False;
+       if (tdb_lockall(tdb) != 0) {
+               file_lines_free(lines);
+               return False;
+       }
 
        /* wipe the db */
        tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
@@ -60,11 +62,8 @@ static BOOL load_msg(const char *msg_file)
                        }
                        all_string_sub(msgid, "\\n", "\n", 0);
                        all_string_sub(msgstr, "\\n", "\n", 0);
-                       key.dptr = msgid;
-                       key.dsize = strlen(msgid)+1;
-                       data.dptr = msgstr;
-                       data.dsize = strlen(msgstr)+1;
-                       tdb_store(tdb, key, data, 0);
+                       data = string_term_tdb_data(msgstr);
+                       tdb_store_bystring(tdb, msgid, data, 0);
                        msgid = NULL;
                }
        }
@@ -94,14 +93,14 @@ static const char *get_lang(void)
 
 /* initialise the message translation subsystem. If the "lang" argument
    is NULL then get the language from the normal environment variables */
-BOOL lang_tdb_init(const char *lang)
+bool lang_tdb_init(const char *lang)
 {
        char *path = NULL;
        char *msg_path = NULL;
        struct stat st;
        static int initialised;
        time_t loadtime;
-       BOOL result = False;
+       bool result = False;
 
        /* we only want to init once per process, unless given
           an override */
@@ -128,7 +127,7 @@ BOOL lang_tdb_init(const char *lang)
        if (!lang) 
                return True;
 
-       asprintf(&msg_path, "%s.msg", lib_path((const char *)lang));
+       asprintf(&msg_path, "%s.msg", data_path((const char *)lang));
        if (stat(msg_path, &st) != 0) {
                /* the msg file isn't available */
                DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path, 
@@ -148,7 +147,7 @@ BOOL lang_tdb_init(const char *lang)
                                   strerror(errno)));
                        goto done;
                }
-               current_lang = strdup(lang);
+               current_lang = SMB_STRDUP(lang);
                result = True;
                goto done;
        }
@@ -160,7 +159,7 @@ BOOL lang_tdb_init(const char *lang)
                tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
        }
 
-       current_lang = strdup(lang);
+       current_lang = SMB_STRDUP(lang);
        result = True;
 
  done:
@@ -175,8 +174,9 @@ BOOL lang_tdb_init(const char *lang)
 */
 const char *lang_msg(const char *msgid)
 {
-       TDB_DATA key, data;
-       char *p, *q, *msgid_quoted;
+       TDB_DATA data;
+       const char *p;
+       char *q, *msgid_quoted;
        int count;
 
        lang_tdb_init(NULL);
@@ -193,7 +193,7 @@ const char *lang_msg(const char *msgid)
                        count++;
        }
 
-       if (!(msgid_quoted = malloc(strlen(msgid) + count + 1)))
+       if (!(msgid_quoted = (char *)SMB_MALLOC(strlen(msgid) + count + 1)))
                return msgid;
 
        /* string_sub() is unsuitable here as it replaces some punctuation
@@ -210,17 +210,14 @@ const char *lang_msg(const char *msgid)
 
        *q = 0;
 
-       key.dptr = (char *)msgid_quoted;
-       key.dsize = strlen(msgid_quoted)+1;
-       
-       data = tdb_fetch(tdb, key);
+       data = tdb_fetch_bystring(tdb, msgid_quoted);
 
        free(msgid_quoted);
 
        /* if the message isn't found then we still need to return a pointer
           that can be freed. Pity. */
        if (!data.dptr)
-               return strdup(msgid);
+               return SMB_STRDUP(msgid);
 
        return (const char *)data.dptr;
 }
@@ -233,32 +230,6 @@ void lang_msg_free(const char *msgstr)
        free((void *)msgstr);
 }
 
-
-/*
-  when the _() translation macro is used there is no obvious place to free
-  the resulting string and there is no easy way to give a static pointer.
-  All we can do is rotate between some static buffers and hope a single d_printf() 
-  doesn't have more calls to _() than the number of buffers 
-*/
-const char *lang_msg_rotate(const char *msgid)
-{
-#define NUM_LANG_BUFS 16
-       char *msgstr;
-       static pstring bufs[NUM_LANG_BUFS];
-       static int next;
-
-       msgstr = (char *)lang_msg(msgid);
-       if (!msgstr) return msgid;
-
-       pstrcpy(bufs[next], msgstr);
-       msgstr = bufs[next];
-
-       next = (next+1) % NUM_LANG_BUFS;
-       
-       return msgstr;
-}
-
-
 /* 
    return the current language - needed for language file mappings 
 */