[Winpt-commits] r368 - trunk/Src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 6 14:40:05 CET 2011


Author: twoaday
Date: 2011-12-06 14:40:04 +0100 (Tue, 06 Dec 2011)
New Revision: 368

Modified:
   trunk/Src/ChangeLog
   trunk/Src/StringBuffer.cpp
   trunk/Src/StringBuffer.h
   trunk/Src/wptNLS.cpp
   trunk/Src/wptPassphraseDlg.cpp
   trunk/Src/wptW32API.cpp
Log:
2011-12-06  Timo Schulz  <twoaday at gmx.net>

        * wptNLS.cpp (get_user_langid): Removed.
        Use gettext_get_langid() instead. Changed all callers.
        (gettext_set_file): Removed.
        (w32_stpcpy): Likewise.
        * StringBuffer.cpp: Overload '=' operator for 'char*'.



Modified: trunk/Src/ChangeLog
===================================================================
--- trunk/Src/ChangeLog	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/ChangeLog	2011-12-06 13:40:04 UTC (rev 368)
@@ -1,5 +1,13 @@
 2011-12-06  Timo Schulz  <twoaday at gmx.net>
 
+	* wptNLS.cpp (get_user_langid): Removed.
+	Use gettext_get_langid() instead. Changed all callers.
+	(gettext_set_file): Removed.
+	(w32_stpcpy): Likewise.
+	* StringBuffer.cpp: Overload '=' operator for 'char*'.
+
+2011-12-06  Timo Schulz  <twoaday at gmx.net>
+
 	* wptKeyPropsDlg.cpp (on_init_dialog): Extra hint in
 	the title that the key has been revoked.
 	* wptKeyManagerDlg.cpp (keymanger_dlg_proc): Redraw

Modified: trunk/Src/StringBuffer.cpp
===================================================================
--- trunk/Src/StringBuffer.cpp	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/StringBuffer.cpp	2011-12-06 13:40:04 UTC (rev 368)
@@ -239,3 +239,10 @@
     addCharacters (val, strlen (val));
     return *this;
 }
+
+StringBuffer& StringBuffer::operator=(char *val)
+{
+    reset ();
+    addCharacters (val, strlen (val));
+    return *this;
+}

Modified: trunk/Src/StringBuffer.h
===================================================================
--- trunk/Src/StringBuffer.h	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/StringBuffer.h	2011-12-06 13:40:04 UTC (rev 368)
@@ -89,6 +89,8 @@
     StringBuffer& operator +=(char val);
 
     StringBuffer& operator=(const char *val);
+    
+    StringBuffer& operator=(char *val);
 };
 
 

Modified: trunk/Src/wptNLS.cpp
===================================================================
--- trunk/Src/wptNLS.cpp	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/wptNLS.cpp	2011-12-06 13:40:04 UTC (rev 368)
@@ -90,6 +90,18 @@
 static gettext_domain_t the_domain;
 
 
+/* Return TRUE if the native user language is not English 
+   and thus, gettext support is available */
+static bool
+gettext_is_required (void)
+{
+    LANGID lang = GetUserDefaultLangID ();
+    if (PRIMARYLANGID (lang) == LANG_ENGLISH)
+	return false;
+    return true;
+}
+
+
 static DWORD
 do_swap_u32 (DWORD i)
 {
@@ -120,17 +132,6 @@
 }
 
 
-/* Missing W32 functions. */
-static char*
-w32_stpcpy (char *a, const char *b)
-{
-    while (*b)
-	*a++ = *b++;
-    *a = 0;
-    return (char*)a;
-}
-
-
 static gettext_domain_t
 load_domain (const char *filename)
 {
@@ -163,7 +164,7 @@
 	size_t nb = fread (read_ptr, 1, to_read, fp);
 	if (nb < to_read) {
 	    fclose (fp);
-	    free(data);	
+	    free (data);	
 	    return NULL; /* read error */		
 	}
 	read_ptr += nb;	
@@ -205,7 +206,7 @@
     }
 
     /* allocate an array to keep track of code page mappings */
-    domain->mapped = (char *)calloc( 1, domain->nstrings );
+    domain->mapped = (char *)calloc (1, domain->nstrings);
     if (!domain->mapped) 
 	BUG (0);
     
@@ -215,22 +216,6 @@
 }
 
 
-/* Returns a string representation of the users system language
- * if we support it, 'en' otherwise.
- */
-static const char*
-get_user_langid (void)
-{
-    int lang = GetUserDefaultLangID() & 511;
-    
-    for (int i=0; lang_list[i].id != NULL; i++) {
-	if (lang == lang_list[i].langid)
-	    return lang_list[i].id;
-    }
-    return "en";
-}
-
-
 /* To avoid problems that the current directory is not the
  * same folder the WinPT.exe is located in, we extract the
  * module directory.
@@ -238,12 +223,10 @@
 char*
 get_module_dir (char *buf, DWORD buflen)
 {
-    int pos;
-    
     if (buflen < 1 || !GetModuleFileName (glob_hinst, buf, buflen-1))
 	return NULL;
 
-    pos = strlen (buf);
+    size_t pos = strlen (buf);
     while (pos > 0 && buf[--pos] != '\\')
 	;
     buf[pos+1] = '\0';
@@ -252,13 +235,12 @@
 
 
 /* Returns a localisation name for a specific resource (file).
- * For instance, lang=DE and file=winpt.chm will become
- * winpt.DE.chm
+ * For instance, lang=DE and file=winpt.chm will become winpt.DE.chm
  */
 char*
 get_locale_name (const char *file)
 {
-    const char *lang = get_user_langid();
+    const char *lang = gettext_get_langid ();
     char *name, *ext;
     char *p, buf[MAX_PATH+1];
     
@@ -268,8 +250,8 @@
 	sprintf (p, "%s.%s", file, lang);
 	return p;
     }
-    name = substr (file, 0, p-file);
-    ext = substr (file, p-file+1, strlen(file));
+    name = substr (file, 0, p - file);
+    ext = substr (file, p - file + 1, strlen(file));
     p = get_module_dir (buf, MAX_PATH);
     if (!p)
 	BUG (NULL);
@@ -296,18 +278,23 @@
 }
 
 
+/* Replace and release the old domain and assign a new domain then */
+static void
+gettext_update_domain (gettext_domain_t new_dom)
+{
+    gettext_free_current_domain ();
+    the_domain = new_dom;
+}
+
+
 char* get_reg_entry_mo (void);
 
-/* Fallback code which is used when no new NLS file 
- * were found. Then we read the MODir registry key
- * and check if a file named 'winpt.mo' exists in this
- * directory.
- */
+/* Fallback code which is used when no new NLS file were found.
+   Then we read the MODir registry key and check if a file named 
+   'winpt.mo' exists in this directory. */
 static gettext_domain_t
 load_modir_domain (void)
-{
-    gettext_domain_t domain = NULL;
-    
+{        
     char *modir = get_reg_entry_mo ();
     if (!modir)
 	return NULL;
@@ -320,6 +307,7 @@
 	s += "\\";
     s += "winpt.mo";
     
+    gettext_domain_t domain = NULL;
     const char *fname = s.getBuffer();
     if (!file_exist_check (fname))
 	domain = load_domain (fname);
@@ -328,9 +316,14 @@
 }
 
 
+/* Try to load the user domain, either from
+   the WinPT.exe directory or from the registry */
 int
 gettext_set_user_domain (void)
 {
+    if (!gettext_is_required ())
+	return 0;
+    
     gettext_domain_t domain = NULL;
     char *file = get_locale_name ("winpt.mo");
     if (file && !file_exist_check (file))
@@ -338,8 +331,7 @@
     else
 	domain = load_modir_domain ();
     free_if_alloc (file);
-    gettext_free_current_domain ();    
-    the_domain = domain;
+    gettext_update_domain (domain);
     return 0;
 }
 
@@ -359,39 +351,6 @@
     return 0;
 }
 
-/* Set the file used for translations. Pass a NULL to disable translation.
-   A new filename may be set at anytime. */
-int
-gettext_set_file (const char *filename, const char *nls_dir)
-{
-    gettext_domain_t domain = NULL;
-
-    if (filename && *filename) {
-	if (filename[1] == ':' && filename[2] == '\\')
-	    domain = load_domain (filename); /* absolute path - use it as is */
-	else { /* relative path - append ".mo" and get dir from the environment */
-	    char *buf, *dir;
-		
-	    dir = strdup (nls_dir);
-	    if (!dir)
-		BUG (0);
-	    buf= (char *)malloc (strlen (dir) + strlen (filename)+1+3+1);
-	    strcpy (w32_stpcpy (w32_stpcpy (
-		    w32_stpcpy (buf, dir), "\\"), filename),".mo");
-	    domain = load_domain(buf);
-	    free (buf);
-	    free (dir);
-	}
-	if (!domain)
-	    return -1;
-    }
-
-    gettext_free_current_domain ();
-    the_domain = domain;
-    return 0;
-}
-
-
 static const char*
 get_string (gettext_domain_t domain, DWORD idx)
 {
@@ -399,6 +358,9 @@
 				    domain->trans_tab[idx].offset);
     if (!domain->mapped[idx])
 	domain->mapped[idx] = 1;
+    /* FIXME: all text is in UTF-8 and need to be converted to
+       the locale charset. But the caller does not free the data and
+       thus we will get a leak */
     return (const char *)p;
 }
 
@@ -471,20 +433,21 @@
 }
 
 
+
 /* Map the user specific language ID to a gettext conform language string.
+   English "en" is defined as the fallback.
    Example: LANG_GERMAN -> "de" */
 const char*
 gettext_get_langid (void)
 {
     LANGID lang = GetUserDefaultLangID ();
-    if (PRIMARYLANGID (lang) == LANG_ENGLISH)
-	return NULL;
-
+    
     for (int i=0; lang_list[i].id; i++) {
 	if (PRIMARYLANGID (lang) == lang_list[i].langid)
 	    return lang_list[i].id;
     }
-    return NULL;
+    
+    return "en";
 }
 
 

Modified: trunk/Src/wptPassphraseDlg.cpp
===================================================================
--- trunk/Src/wptPassphraseDlg.cpp	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/wptPassphraseDlg.cpp	2011-12-06 13:40:04 UTC (rev 368)
@@ -79,10 +79,9 @@
 static void
 set_passphrase_hint (HWND dlg, gpgme_key_t key)
 {
-    StringBuffer p;
-    char *uid;
-
-    uid = utf8_to_native (key->uids->name);
+    char *uid = utf8_to_native (key->uids->name);
+    
+    StringBuffer p;    
     p = p + uid + " (" + get_keyinfo (key) + ")";
 
     SendDlgItemMessage (dlg, IDC_PASSWD_KEYINF,

Modified: trunk/Src/wptW32API.cpp
===================================================================
--- trunk/Src/wptW32API.cpp	2011-12-06 12:50:14 UTC (rev 367)
+++ trunk/Src/wptW32API.cpp	2011-12-06 13:40:04 UTC (rev 368)
@@ -634,10 +634,8 @@
     memset (&pi, 0, sizeof (pi));
     //hh.exe -mapid 40 "winpt.$lang.chm"
     
-    _snprintf (path, 2*MAX_PATH, "hh.exe -mapid %d winpt.%s.chm",
-	       map_id, gettext_get_langid ());
-    CreateProcess (NULL, path, &sec_attr, &sec_attr, 
-		   FALSE, 0, NULL, NULL, &si, &pi);
+    _snprintf (path, 2*MAX_PATH, "hh.exe -mapid %d winpt.%s.chm", map_id, gettext_get_langid ());
+    CreateProcess (NULL, path, &sec_attr, &sec_attr, FALSE, 0, NULL, NULL, &si, &pi);
     CloseHandle (pi.hProcess);
     CloseHandle (pi.hThread);
     return 0;



More information about the Winpt-commits mailing list