wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / intl / lang_tdb.c
index fe2ad5b2fce48ab811600c2f282f6efd7f127607..ecdfe1dc911db693ec6673954dd9d4d87a1a3839 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,
    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"
+#include "system/filesys.h"
+#include "intl/lang_tdb.h"
+#include "util_tdb.h"
+#include "lib/util/util_paths.h"
 
 static TDB_CONTEXT *tdb;
 
@@ -27,23 +30,26 @@ 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, NULL);
 
        if (!lines) {
                return False;
        }
 
-       if (tdb_lockall(tdb) != 0) return False;
+       if (tdb_lockall(tdb) != 0) {
+               TALLOC_FREE(lines);
+               return False;
+       }
 
        /* wipe the db */
-       tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
+       tdb_wipe_all(tdb);
 
        msgid = NULL;
        
@@ -60,16 +66,13 @@ 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;
                }
        }
 
-       file_lines_free(lines);
+       TALLOC_FREE(lines);
        tdb_unlockall(tdb);
 
        return True;
@@ -94,14 +97,16 @@ 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;
+       char *dpath = NULL;
+       char *lpath = NULL;
 
        /* we only want to init once per process, unless given
           an override */
@@ -128,15 +133,31 @@ BOOL lang_tdb_init(const char *lang)
        if (!lang) 
                return True;
 
-       asprintf(&msg_path, "%s.msg", lib_path((const char *)lang));
+       dpath = data_path(talloc_tos(), (const char *)lang);
+       if (dpath == NULL) {
+               goto done;
+       }
+
+       if (asprintf(&msg_path, "%s.msg", dpath) == -1) {
+               DEBUG(0, ("asprintf failed\n"));
+               goto done;
+       }
        if (stat(msg_path, &st) != 0) {
                /* the msg file isn't available */
                DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path, 
                           strerror(errno)));
                goto done;
        }
-       
-       asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
+
+       lpath = lock_path("lang_");
+       if (lpath == NULL) {
+               goto done;
+       }
+
+       if (asprintf(&path, "%s%s.tdb", lpath, lang) == -1) {
+               DEBUG(0, ("asprintf failed\n"));
+               goto done;
+       }
 
        DEBUG(10, ("lang_tdb_init: loading %s\n", path));
 
@@ -148,7 +169,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,12 +181,14 @@ 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:
        SAFE_FREE(msg_path);
        SAFE_FREE(path);
+       TALLOC_FREE(lpath);
+       TALLOC_FREE(dpath);
 
        return result;
 }
@@ -175,7 +198,7 @@ BOOL lang_tdb_init(const char *lang)
 */
 const char *lang_msg(const char *msgid)
 {
-       TDB_DATA key, data;
+       TDB_DATA data;
        const char *p;
        char *q, *msgid_quoted;
        int count;
@@ -194,7 +217,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
@@ -211,17 +234,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;
 }
@@ -231,35 +251,9 @@ const char *lang_msg(const char *msgid)
 void lang_msg_free(const char *msgstr)
 {
        if (!tdb) return;
-       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;
+       free(discard_const_p(void, msgstr));
 }
 
-
 /* 
    return the current language - needed for language file mappings 
 */