Patch Attached....

On Wed, May 23, 2001 at 05:28:12PM +1000, [EMAIL PROTECTED] wrote:
> We probably need somewhere a table that takes an array of
> fallback encoding names.  Most of the entries probably only
> need one fallback name but the Japanese Shift JIS ones
> might need more.  Look in the encodings.def file in the
> libiconv source...
> 

Does this patch meet the requirement? 
-- 
Best regard
hashao
Index: xap_EncodingManager.cpp
===================================================================
RCS file: /cvsroot/abi/src/af/xap/xp/xap_EncodingManager.cpp,v
retrieving revision 1.32
diff -u -r1.32 xap_EncodingManager.cpp
--- xap_EncodingManager.cpp     2001/04/27 06:01:32     1.32
+++ xap_EncodingManager.cpp     2001/05/23 10:14:39
@@ -27,6 +27,41 @@
 #include <stdio.h>
 #include <string.h>
 
+/*!
+  This macros allow the use of an iconv fallback name if needed.
+  The fallbackname is an arrary of strings. NULL marks the end.
+  If no valid fallbackname is found, set the destination to 'name'.
+  Or else, what can we do?
+ */
+#define CPNAME_OR_FALLBACK(destination,name,fallbackname) \
+{  \
+       static char* fallback_macro_cpname = NULL; \
+       if (!fallback_macro_cpname)    \
+       {       \
+               iconv_t macro_cd = iconv_open(name,name);     \
+               if (macro_cd == (iconv_t)-1) \
+               { \
+                       const char** macro_cur = fallbackname;  \
+                       for(;*macro_cur; macro_cur++)   \
+                       {       \
+                           macro_cd = iconv_open(*macro_cur, *macro_cur); \
+                           if(macro_cd != (iconv_t)-1) \
+                           {   \
+                               fallback_macro_cpname = *macro_cur;     \
+                               iconv_close(macro_cd);  \
+                               break;  \
+                           }   \
+                       }       \
+                       fallback_macro_cpname = name ;  \
+               } \
+               else \
+               { \
+                       fallback_macro_cpname = name;  \
+                       iconv_close(macro_cd); \
+               } \
+       } \
+       destination = fallback_macro_cpname;  \
+}
 
 static         iconv_t iconv_handle_N2U = NULL, iconv_handle_U2N = NULL,
        iconv_handle_U2Latin1 = NULL,
@@ -375,6 +410,26 @@
        return search_map_with_opt_suffix(m,fallback_key,fallback_key_final);
 };
 
+struct _map_to_array
+{
+       const char* key;//it can't be NULL for non-special entries like last or first
+       const char** values;//NULL-teminated array of strings
+};
+
+/*
+ * Search a map from char* to an array of char*. Return the found char** if
+ * key is found in the mapping, else return NULL. 
+ */
+static const char** search_map_to_array(const _map_to_array* m,const char* key)
+{
+       const _map_to_array* cur = m+1; 
+       for(;cur->key;++cur) 
+       {
+           if (!UT_stricmp(cur->key,key))
+               return cur->values;                     
+       }
+       return NULL;
+};
 /* ************************* here begin tables *************************/
 
 /* this array describes mapping form current encoding to Tex's encoding name.
@@ -431,7 +486,7 @@
   Tested with GB2312 only.  
 */
 static const char* wincharsetcode_zh_GB2312[]= /* chinese*/
-{ "zh_CN.GB2312", "zh_TW.GB2312", NULL };
+{ "zh_CN.GBK", "zh_CN.GB2312", "zh_TW.GB2312", NULL };
 
 static const char* wincharsetcode_zh_BIG5[]= /* chinese*/
 { "zh_CN.BIG5", "zh_TW.BIG5", NULL };
@@ -496,12 +551,14 @@
  This table is useful since iconv implementations don't know some cpNNNN 
  charsets but under some different name.
 */
-static const _map MSCodepagename_to_charset_name_map[]=
+static const char* GB_encode[] = {"GBK", "GB2312", NULL};
+static const char* BIG5_encode[] = {"BIG5", NULL};
+static const _map_to_array MSCodepagename_to_charset_name_map[]=
 {
 /*key, value*/
     {NULL,NULL},
-    {"CP936","GB2312"},
-    {"CP950","BIG5"},  
+    {"CP936",GB_encode},
+    {"CP950",BIG5_encode},
     {NULL,NULL}
 };
 
@@ -514,6 +571,7 @@
 {
 /*key,value*/
     {NULL,NULL},
+    {"GBK", "CP936"},
     {"GB2312","CP936"},
     {"BIG5","CP950"},
     {NULL,NULL}
@@ -526,6 +584,7 @@
     {NULL},
    {"zh_CN.BIG5",      "0x404"},  
    {"zh_CN.GB2312",    "0x804"},     
+   {"zh_CN.GBK",       "0x804"},
    {"zh_TW.BIG5",      "0x404"},  
    {"zh_TW.GB2312",    "0x804"}, 
     {NULL}
@@ -879,9 +938,11 @@
     static char buf[100];
     sprintf(buf,"CP%d",lid);    
     char* cpname = buf;
-    bool is_default;
-    const char* ret = 
search_map(MSCodepagename_to_charset_name_map,cpname,&is_default);
-    return is_default ? cpname : ret;
+    char* destinate_charset = cpname;
+    const char** ret = search_map_to_array(MSCodepagename_to_charset_name_map,cpname);
+    if(ret)
+       CPNAME_OR_FALLBACK(destinate_charset, cpname, ret);
+    return !ret ? cpname : destinate_charset;
 };
 
 const char* XAP_EncodingManager::CodepageFromCharset(char *charset) const
@@ -895,9 +956,11 @@
 const char* XAP_EncodingManager::WindowsCharsetName() const
 {
     char* cpname = wvLIDToCodePageConverter(getWinLanguageCode());
-    bool is_default;
-    const char* ret = 
search_map(MSCodepagename_to_charset_name_map,cpname,&is_default);
-    return is_default ? cpname : ret;
+    char* destinate_charset = cpname;
+    const char** ret = search_map_to_array(MSCodepagename_to_charset_name_map,cpname);
+    if(ret)
+       CPNAME_OR_FALLBACK(destinate_charset, cpname, ret);
+    return !ret ? cpname : destinate_charset;
 };
 
 UT_uint32  XAP_EncodingManager::getWinLanguageCode() const

Reply via email to