cocos2d-x 3.0rc1 使用iconv库 解决UTF8乱码问题
多国语言要用到开源字符转换 iconv
先贴出自己的使用代码 你能够做成头文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "..\cocos2d\iconv\include\iconv.h" #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #include "..\cocos2d\external\win32-specific\icon\include\iconv.h" #endif #pragma comment(lib,"libiconv.lib") #include "string" using namespace std; //#ifndef A2U //#define A2U static int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen) { iconv_t cd; const char *temp = inbuf; const char **pin = &temp; char **pout = &outbuf; memset(outbuf,0,outlen); cd = iconv_open(to_charset,from_charset); if(cd==0) return -1; if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1; iconv_close(cd); return 0; } /*UTF8תΪGB2312*/ static std::string u2a(const char *inbuf) { size_t inlen = strlen(inbuf); char * outbuf = new char[inlen * 2 + 2]; std::string strRet; if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0) { strRet = outbuf; } delete [] outbuf; return strRet; } /*GB2312תΪUTF8*/ static std::string a2u(const char *inbuf) { size_t inlen = strlen(inbuf); char * outbuf = new char[inlen * 2 + 2]; std::string strRet; if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0) { strRet = outbuf; } delete [] outbuf; return strRet; } //#endif
注意设置安卓版本号的路径和win32版本号的路径
win32版本号是rc1版本号里面有的自带iocnv.h头文件和lib
可是Android版本号须要自己下载
1.下载
在windows上用iconv.dll。在IOS上系统有提供这个库。导入就能够,在android上要自己去下一个。
http://ftp.gnu.org/pub/gnu/libiconv/
这个是官网地址,眼下iconv最新版本号是1.14,2011.8.7版本号的。大小4.8M
官方地址:
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
2.完成下载,解压放到E:\MyCocos\cocos2dx-workspace\hello\cocos2d文件夹下(这里是我自己的文件夹),改名为iconv,和cocos。extensions,external等同文件夹
3.在iconv文件夹下添加Android.mk文件,写入
LOCAL_PATH:= $(call my-dir) #libiconv.so include $(CLEAR_VARS) LOCAL_MODULE := iconv_static LOCAL_MODULE_FILENAME := libiconv LOCAL_CFLAGS := \ -Wno-multichar \ -DAndroid \ -DLIBDIR="c" \ -DBUILDING_LIBICONV \ -DIN_LIBRARY LOCAL_SRC_FILES := \ libcharset/lib/localcharset.c \ lib/iconv.c \ lib/relocatable.c LOCAL_C_INCLUDES += \ $(LOCAL_PATH)/include \ $(LOCAL_PATH)/libcharset \ $(LOCAL_PATH)/lib \ $(LOCAL_PATH)/libcharset/include \ $(LOCAL_PATH)/srclib include $(BUILD_STATIC_LIBRARY)
4. 在E:\MyCocos\cocos2dx-workspace\hello\proj.android\jniproject的Android.mk里改动,增加这个lib和include文件夹(以下的路径要依据项目实际情况改动)
$(LOCAL_PATH)/../../cocos2d/iconv/include \ $(LOCAL_PATH)/../../cocos2d/iconv/libcharset \ $(LOCAL_PATH)/../../cocos2d/iconv/lib \ $(LOCAL_PATH)/../../cocos2d/iconv/libcharset/include \ $(LOCAL_PATH)/../../cocos2d/iconv/srclib \ $(LOCAL_PATH)/../../cocos2d/iconv \ LOCAL_WHOLE_STATIC_LIBRARIES += iconv_static $(call import-module,iconv)下面是我自己的Android.mk文件里的代码 以后假设版本号变动能够參考这个代码来改
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \ $(LOCAL_PATH)/../../cocos2d/iconv/include \ $(LOCAL_PATH)/../../cocos2d/iconv/libcharset \ $(LOCAL_PATH)/../../cocos2d/iconv/lib \ $(LOCAL_PATH)/../../cocos2d/iconv/libcharset/include \ $(LOCAL_PATH)/../../cocos2d/iconv/srclib \ $(LOCAL_PATH)/../../cocos2d/iconv \ LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static LOCAL_WHOLE_STATIC_LIBRARIES += iconv_static include $(BUILD_SHARED_LIBRARY) $(call import-module,2d) $(call import-module,audio/android) $(call import-module,Box2D) $(call import-module,iconv)
5。
网上非常多教程都讲到这里。但实际上,假设这个时候进行编译。问题还一大堆。
1)iconv.h不存在,在ivonv/include/有 iconv.h.in, iconv.h.build.in,就是没看到iconv.h
把iconv.h.in改名成iconv.h。发现编译非常多错误
主要错误是类型不匹配,还有非常多@符号。
因此须要进行改动。主要改法是不存在的改成存在的。@符号去掉
以下是我自己改完之后的iconv.h文件。
编译OK.
/* Copyright (C) 1999-2003, 2005-2006, 2008-2011 Free Software Foundation, Inc. This file is part of the GNU LIBICONV Library. The GNU LIBICONV Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU LIBICONV Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU LIBICONV Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* When installed, this file is called "iconv.h". */ #ifndef _LIBICONV_H #define _LIBICONV_H #define _LIBICONV_VERSION 0x010E /* version number: (major<<8) + minor */ extern int _libiconv_version; /* Likewise */ /* We would like to #include any system header file which could define iconv_t, 1. in order to eliminate the risk that the user gets compilation errors because some other system header file includes /usr/include/iconv.h which defines iconv_t or declares iconv after this file, 2. when compiling for LIBICONV_PLUG, we need the proper iconv_t type in order to produce binary compatible code. But gcc's #include_next is not portable. Thus, once libiconv's iconv.h has been installed in /usr/local/include, there is no way any more to include the original /usr/include/iconv.h. We simply have to get away without it. Ad 1. The risk that a system header file does #include "iconv.h" or #include_next "iconv.h" is small. They all do #include <iconv.h>. Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It has to be a scalar type because (iconv_t)(-1) is a possible return value from iconv_open().) */ /* Define iconv_t ourselves. */ #undef iconv_t #define iconv_t libiconv_t typedef void* iconv_t; /* Get size_t declaration. Get wchar_t declaration if it exists. */ #include <stddef.h> /* Get errno declaration and values. */ #include <errno.h> /* Some systems, like SunOS 4, don't have EILSEQ. Some systems, like BSD/OS, have EILSEQ in a different header. On these systems, define EILSEQ ourselves. */ #ifndef EILSEQ #define EILSEQ 84 #endif #ifdef __cplusplus extern "C" { #endif /* Allocates descriptor for code conversion from encoding ‘fromcode’ to encoding ‘tocode’. */ #ifndef LIBICONV_PLUG #define iconv_open libiconv_open #endif extern iconv_t iconv_open (const char* tocode, const char* fromcode); /* Converts, using conversion descriptor ‘cd’, at most ‘*inbytesleft’ bytes starting at ‘*inbuf’, writing at most ‘*outbytesleft’ bytes starting at ‘*outbuf’. Decrements ‘*inbytesleft’ and increments ‘*inbuf’ by the same amount. Decrements ‘*outbytesleft’ and increments ‘*outbuf’ by the same amount. */ #ifndef LIBICONV_PLUG #define iconv libiconv #endif extern size_t iconv (iconv_t cd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft);//iconv.c要对应改动 /* Frees resources allocated for conversion descriptor ‘cd’. */ #ifndef LIBICONV_PLUG #define iconv_close libiconv_close #endif extern int iconv_close (iconv_t cd); #ifdef __cplusplus } #endif #ifndef LIBICONV_PLUG /* Nonstandard extensions. */ #if USE_MBSTATE_T #if BROKEN_WCHAR_H /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before <wchar.h>. BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included before <wchar.h>. */ #include <stddef.h> #include <stdio.h> #include <time.h> #endif #include <wchar.h> #endif #ifdef __cplusplus extern "C" { #endif /* A type that holds all memory needed by a conversion descriptor. A pointer to such an object can be used as an iconv_t. */ typedef struct { void* dummy1[28]; #if USE_MBSTATE_T mbstate_t dummy2; #endif } iconv_allocation_t; /* Allocates descriptor for code conversion from encoding ‘fromcode’ to encoding ‘tocode’ into preallocated memory. Returns an error indicator (0 or -1 with errno set). */ #define iconv_open_into libiconv_open_into extern int iconv_open_into (const char* tocode, const char* fromcode, iconv_allocation_t* resultp); /* Control of attributes. */ #define iconvctl libiconvctl extern int iconvctl (iconv_t cd, int request, void* argument); /* Hook performed after every successful conversion of a Unicode character. */ typedef void (*iconv_unicode_char_hook) (unsigned int uc, void* data); /* Hook performed after every successful conversion of a wide character. */ typedef void (*iconv_wide_char_hook) (wchar_t wc, void* data); /* Set of hooks. */ struct iconv_hooks { iconv_unicode_char_hook uc_hook; iconv_wide_char_hook wc_hook; void* data; }; /* Fallback function. Invoked when a small number of bytes could not be converted to a Unicode character. This function should process all bytes from inbuf and may produce replacement Unicode characters by calling the write_replacement callback repeatedly. */ typedef void (*iconv_unicode_mb_to_uc_fallback) (const char* inbuf, size_t inbufsize, void (*write_replacement) (const unsigned int *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); /* Fallback function. Invoked when a Unicode character could not be converted to the target encoding. This function should process the character and may produce replacement bytes (in the target encoding) by calling the write_replacement callback repeatedly. */ typedef void (*iconv_unicode_uc_to_mb_fallback) (unsigned int code, void (*write_replacement) (const char *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); #if HAVE_WCHAR_T /* Fallback function. Invoked when a number of bytes could not be converted to a wide character. This function should process all bytes from inbuf and may produce replacement wide characters by calling the write_replacement callback repeatedly. */ typedef void (*iconv_wchar_mb_to_wc_fallback) (const char* inbuf, size_t inbufsize, void (*write_replacement) (const wchar_t *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); /* Fallback function. Invoked when a wide character could not be converted to the target encoding. This function should process the character and may produce replacement bytes (in the target encoding) by calling the write_replacement callback repeatedly. */ typedef void (*iconv_wchar_wc_to_mb_fallback) (wchar_t code, void (*write_replacement) (const char *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); #else /* If the wchar_t type does not exist, these two fallback functions are never invoked. Their argument list therefore does not matter. */ typedef void (*iconv_wchar_mb_to_wc_fallback) (); typedef void (*iconv_wchar_wc_to_mb_fallback) (); #endif /* Set of fallbacks. */ struct iconv_fallbacks { iconv_unicode_mb_to_uc_fallback mb_to_uc_fallback; iconv_unicode_uc_to_mb_fallback uc_to_mb_fallback; iconv_wchar_mb_to_wc_fallback mb_to_wc_fallback; iconv_wchar_wc_to_mb_fallback wc_to_mb_fallback; void* data; }; /* Requests for iconvctl. */ #define ICONV_TRIVIALP 0 /* int *argument */ #define ICONV_GET_TRANSLITERATE 1 /* int *argument */ #define ICONV_SET_TRANSLITERATE 2 /* const int *argument */ #define ICONV_GET_DISCARD_ILSEQ 3 /* int *argument */ #define ICONV_SET_DISCARD_ILSEQ 4 /* const int *argument */ #define ICONV_SET_HOOKS 5 /* const struct iconv_hooks *argument */ #define ICONV_SET_FALLBACKS 6 /* const struct iconv_fallbacks *argument */ /* Listing of locale independent encodings. */ #define iconvlist libiconvlist extern void iconvlist (int (*do_one) (unsigned int namescount, const char * const * names, void* data), void* data); /* Canonicalize an encoding name. The result is either a canonical encoding name, or name itself. */ extern const char * iconv_canonicalize (const char * name); /* Support for relocatable packages. */ /* Sets the original and the current installation prefix of the package. Relocation simply replaces a pathname starting with the original prefix by the corresponding pathname with the current prefix instead. Both prefixes should be directory names without trailing slash (i.e. use "" instead of "/"). */ extern void libiconv_set_relocation_prefix (const char *orig_prefix, const char *curr_prefix); #ifdef __cplusplus } #endif #endif #endif /* _LIBICONV_H */
还须要改其它2个文件
E:\MyCocos\cocos2dx-workspace\hello\cocos2d\iconv\lib\iconv.c
/* * Copyright (C) 1999-2008, 2011 Free Software Foundation, Inc. * This file is part of the GNU LIBICONV Library. * * The GNU LIBICONV Library is free software; you can redistribute it * and/or modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * The GNU LIBICONV Library is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with the GNU LIBICONV Library; see the file COPYING.LIB. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street, * Fifth Floor, Boston, MA 02110-1301, USA. */ #include <iconv.h> #include <stdlib.h> #include <string.h> #include "config.h" #include "localcharset.h" #ifdef __CYGWIN__ #include <cygwin/version.h> #endif #if ENABLE_EXTRA /* * Consider all system dependent encodings, for any system, * and the extra encodings. */ #define USE_AIX #define USE_OSF1 #define USE_DOS #define USE_EXTRA #else /* * Consider those system dependent encodings that are needed for the * current system. */ #ifdef _AIX #define USE_AIX #endif #if defined(__osf__) || defined(VMS) #define USE_OSF1 #endif #if defined(__DJGPP__) || (defined(_WIN32) && (defined(_MSC_VER) || defined(__MINGW32__))) #define USE_DOS #endif #endif /* * Data type for general conversion loop. */ struct loop_funcs { size_t (*loop_convert) (iconv_t icd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); size_t (*loop_reset) (iconv_t icd, char* * outbuf, size_t *outbytesleft); }; /* * Converters. */ #include "converters.h" /* * Transliteration tables. */ #include "cjk_variants.h" #include "translit.h" /* * Table of all supported encodings. */ struct encoding { struct mbtowc_funcs ifuncs; /* conversion multibyte -> unicode */ struct wctomb_funcs ofuncs; /* conversion unicode -> multibyte */ int oflags; /* flags for unicode -> multibyte conversion */ }; #define DEFALIAS(xxx_alias,xxx) /* nothing */ enum { #define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \ ei_##xxx , #include "encodings.def" #ifdef USE_AIX # include "encodings_aix.def" #endif #ifdef USE_OSF1 # include "encodings_osf1.def" #endif #ifdef USE_DOS # include "encodings_dos.def" #endif #ifdef USE_EXTRA # include "encodings_extra.def" #endif #include "encodings_local.def" #undef DEFENCODING ei_for_broken_compilers_that_dont_like_trailing_commas }; #include "flags.h" static struct encoding const all_encodings[] = { #define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \ { xxx_ifuncs1,xxx_ifuncs2, xxx_ofuncs1,xxx_ofuncs2, ei_##xxx##_oflags }, #include "encodings.def" #ifdef USE_AIX # include "encodings_aix.def" #endif #ifdef USE_OSF1 # include "encodings_osf1.def" #endif #ifdef USE_DOS # include "encodings_dos.def" #endif #ifdef USE_EXTRA # include "encodings_extra.def" #endif #undef DEFENCODING #define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \ { xxx_ifuncs1,xxx_ifuncs2, xxx_ofuncs1,xxx_ofuncs2, 0 }, #include "encodings_local.def" #undef DEFENCODING }; #undef DEFALIAS /* * Conversion loops. */ #include "loops.h" /* * Alias lookup function. * Defines * struct alias { int name; unsigned int encoding_index; }; * const struct alias * aliases_lookup (const char *str, unsigned int len); * #define MAX_WORD_LENGTH ... */ #if defined _AIX # include "aliases_sysaix.h" #elif defined hpux || defined __hpux # include "aliases_syshpux.h" #elif defined __osf__ # include "aliases_sysosf1.h" #elif defined __sun # include "aliases_syssolaris.h" #else # include "aliases.h" #endif /* * System dependent alias lookup function. * Defines * const struct alias * aliases2_lookup (const char *str); */ #if defined(USE_AIX) || defined(USE_OSF1) || defined(USE_DOS) || defined(USE_EXTRA) /* || ... */ struct stringpool2_t { #define S(tag,name,encoding_index) char stringpool_##tag[sizeof(name)]; #include "aliases2.h" #undef S }; static const struct stringpool2_t stringpool2_contents = { #define S(tag,name,encoding_index) name, #include "aliases2.h" #undef S }; #define stringpool2 ((const char *) &stringpool2_contents) static const struct alias sysdep_aliases[] = { #define S(tag,name,encoding_index) { (int)(long)&((struct stringpool2_t *)0)->stringpool_##tag, encoding_index }, #include "aliases2.h" #undef S }; #ifdef __GNUC__ __inline #endif const struct alias * aliases2_lookup (register const char *str) { const struct alias * ptr; unsigned int count; for (ptr = sysdep_aliases, count = sizeof(sysdep_aliases)/sizeof(sysdep_aliases[0]); count > 0; ptr++, count--) if (!strcmp(str, stringpool2 + ptr->name)) return ptr; return NULL; } #else #define aliases2_lookup(str) NULL #define stringpool2 NULL #endif #if 0 /* Like !strcasecmp, except that the both strings can be assumed to be ASCII and the first string can be assumed to be in uppercase. */ static int strequal (const char* str1, const char* str2) { unsigned char c1; unsigned char c2; for (;;) { c1 = * (unsigned char *) str1++; c2 = * (unsigned char *) str2++; if (c1 == 0) break; if (c2 >= 'a' && c2 <= 'z') c2 -= 'a'-'A'; if (c1 != c2) break; } return (c1 == c2); } #endif iconv_t iconv_open (const char* tocode, const char* fromcode) { struct conv_struct * cd; unsigned int from_index; int from_wchar; unsigned int to_index; int to_wchar; int transliterate; int discard_ilseq; #include "iconv_open1.h" cd = (struct conv_struct *) malloc(from_wchar != to_wchar ?sizeof(struct wchar_conv_struct) : sizeof(struct conv_struct)); if (cd == NULL) { errno = ENOMEM; return (iconv_t)(-1); } #include "iconv_open2.h" return (iconv_t)cd; invalid: errno = EINVAL; return (iconv_t)(-1); } size_t iconv (iconv_t icd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft) { conv_t cd = (conv_t) icd; if (inbuf == NULL || *inbuf == NULL) return cd->lfuncs.loop_reset(icd,outbuf,outbytesleft); else return cd->lfuncs.loop_convert(icd, (const char* *)inbuf,inbytesleft, outbuf,outbytesleft); } int iconv_close (iconv_t icd) { conv_t cd = (conv_t) icd; free(cd); return 0; } #ifndef LIBICONV_PLUG /* * Verify that a 'struct conv_struct' and a 'struct wchar_conv_struct' each * fit in an iconv_allocation_t. * If this verification fails, iconv_allocation_t must be made larger and * the major version in LIBICONV_VERSION_INFO must be bumped. * Currently 'struct conv_struct' has 21 integer/pointer fields, and * 'struct wchar_conv_struct' additionally has an 'mbstate_t' field. */ typedef int verify_size_1[2 * (sizeof (struct conv_struct) <= sizeof (iconv_allocation_t)) - 1]; typedef int verify_size_2[2 * (sizeof (struct wchar_conv_struct) <= sizeof (iconv_allocation_t)) - 1]; int iconv_open_into (const char* tocode, const char* fromcode, iconv_allocation_t* resultp) { struct conv_struct * cd; unsigned int from_index; int from_wchar; unsigned int to_index; int to_wchar; int transliterate; int discard_ilseq; #include "iconv_open1.h" cd = (struct conv_struct *) resultp; #include "iconv_open2.h" return 0; invalid: errno = EINVAL; return -1; } int iconvctl (iconv_t icd, int request, void* argument) { conv_t cd = (conv_t) icd; switch (request) { case ICONV_TRIVIALP: *(int *)argument = ((cd->lfuncs.loop_convert == unicode_loop_convert && cd->iindex == cd->oindex) || cd->lfuncs.loop_convert == wchar_id_loop_convert ? 1 : 0); return 0; case ICONV_GET_TRANSLITERATE: *(int *)argument = cd->transliterate; return 0; case ICONV_SET_TRANSLITERATE: cd->transliterate = (*(const int *)argument ? 1 : 0); return 0; case ICONV_GET_DISCARD_ILSEQ: *(int *)argument = cd->discard_ilseq; return 0; case ICONV_SET_DISCARD_ILSEQ: cd->discard_ilseq = (*(const int *)argument ?
1 : 0); return 0; case ICONV_SET_HOOKS: if (argument != NULL) { cd->hooks = *(const struct iconv_hooks *)argument; } else { cd->hooks.uc_hook = NULL; cd->hooks.wc_hook = NULL; cd->hooks.data = NULL; } return 0; case ICONV_SET_FALLBACKS: if (argument != NULL) { cd->fallbacks = *(const struct iconv_fallbacks *)argument; } else { cd->fallbacks.mb_to_uc_fallback = NULL; cd->fallbacks.uc_to_mb_fallback = NULL; cd->fallbacks.mb_to_wc_fallback = NULL; cd->fallbacks.wc_to_mb_fallback = NULL; cd->fallbacks.data = NULL; } return 0; default: errno = EINVAL; return -1; } } /* An alias after its name has been converted from 'int' to 'const char*'. */ struct nalias { const char* name; unsigned int encoding_index; }; static int compare_by_index (const void * arg1, const void * arg2) { const struct nalias * alias1 = (const struct nalias *) arg1; const struct nalias * alias2 = (const struct nalias *) arg2; return (int)alias1->encoding_index - (int)alias2->encoding_index; } static int compare_by_name (const void * arg1, const void * arg2) { const char * name1 = *(const char **)arg1; const char * name2 = *(const char **)arg2; /* Compare alphabetically, but put "CS" names at the end. */ int sign = strcmp(name1,name2); if (sign != 0) { sign = ((name1[0]=='C' && name1[1]=='S') - (name2[0]=='C' && name2[1]=='S')) * 4 + (sign >= 0 ? 1 : -1); } return sign; } void iconvlist (int (*do_one) (unsigned int namescount, const char * const * names, void* data), void* data) { #define aliascount1 sizeof(aliases)/sizeof(aliases[0]) #ifndef aliases2_lookup #define aliascount2 sizeof(sysdep_aliases)/sizeof(sysdep_aliases[0]) #else #define aliascount2 0 #endif #define aliascount (aliascount1+aliascount2) struct nalias aliasbuf[aliascount]; const char * namesbuf[aliascount]; size_t num_aliases; { /* Put all existing aliases into a buffer. */ size_t i; size_t j; j = 0; for (i = 0; i < aliascount1; i++) { const struct alias * p = &aliases[i]; if (p->name >= 0 && p->encoding_index != ei_local_char && p->encoding_index != ei_local_wchar_t) { aliasbuf[j].name = stringpool + p->name; aliasbuf[j].encoding_index = p->encoding_index; j++; } } #ifndef aliases2_lookup for (i = 0; i < aliascount2; i++) { aliasbuf[j].name = stringpool2 + sysdep_aliases[i].name; aliasbuf[j].encoding_index = sysdep_aliases[i].encoding_index; j++; } #endif num_aliases = j; } /* Sort by encoding_index. */ if (num_aliases > 1) qsort(aliasbuf, num_aliases, sizeof(struct nalias), compare_by_index); { /* Process all aliases with the same encoding_index together. */ size_t j; j = 0; while (j < num_aliases) { unsigned int ei = aliasbuf[j].encoding_index; size_t i = 0; do namesbuf[i++] = aliasbuf[j++].name; while (j < num_aliases && aliasbuf[j].encoding_index == ei); if (i > 1) qsort(namesbuf, i, sizeof(const char *), compare_by_name); /* Call the callback. */ if (do_one(i,namesbuf,data)) break; } } #undef aliascount #undef aliascount2 #undef aliascount1 } /* * Table of canonical names of encodings. * Instead of strings, it contains offsets into stringpool and stringpool2. */ static const unsigned short all_canonical[] = { #if defined _AIX # include "canonical_sysaix.h" #elif defined hpux || defined __hpux # include "canonical_syshpux.h" #elif defined __osf__ # include "canonical_sysosf1.h" #elif defined __sun # include "canonical_syssolaris.h" #else # include "canonical.h" #endif #ifdef USE_AIX # if defined _AIX # include "canonical_aix_sysaix.h" # else # include "canonical_aix.h" # endif #endif #ifdef USE_OSF1 # if defined __osf__ # include "canonical_osf1_sysosf1.h" # else # include "canonical_osf1.h" # endif #endif #ifdef USE_DOS # include "canonical_dos.h" #endif #ifdef USE_EXTRA # include "canonical_extra.h" #endif #if defined _AIX # include "canonical_local_sysaix.h" #elif defined hpux || defined __hpux # include "canonical_local_syshpux.h" #elif defined __osf__ # include "canonical_local_sysosf1.h" #elif defined __sun # include "canonical_local_syssolaris.h" #else # include "canonical_local.h" #endif }; const char * iconv_canonicalize (const char * name) { const char* code; char buf[MAX_WORD_LENGTH+10+1]; const char* cp; char* bp; const struct alias * ap; unsigned int count; unsigned int index; const char* pool; /* Before calling aliases_lookup, convert the input string to upper case, * and check whether it's entirely ASCII (we call gperf with option "-7" * to achieve a smaller table) and non-empty. If it's not entirely ASCII, * or if it's too long, it is not a valid encoding name. */ for (code = name;;) { /* Search code in the table. */ for (cp = code, bp = buf, count = MAX_WORD_LENGTH+10+1; ; cp++, bp++) { unsigned char c = * (unsigned char *) cp; if (c >= 0x80) goto invalid; if (c >= 'a' && c <= 'z') c -= 'a'-'A'; *bp = c; if (c == '\0') break; if (--count == 0) goto invalid; } for (;;) { if (bp-buf >= 10 && memcmp(bp-10,"//TRANSLIT",10)==0) { bp -= 10; *bp = '\0'; continue; } if (bp-buf >= 8 && memcmp(bp-8,"//IGNORE",8)==0) { bp -= 8; *bp = '\0'; continue; } break; } if (buf[0] == '\0') { code = locale_charset(); /* Avoid an endless loop that could occur when using an older version of localcharset.c. */ if (code[0] == '\0') goto invalid; continue; } pool = stringpool; ap = aliases_lookup(buf,bp-buf); if (ap == NULL) { pool = stringpool2; ap = aliases2_lookup(buf); if (ap == NULL) goto invalid; } if (ap->encoding_index == ei_local_char) { code = locale_charset(); /* Avoid an endless loop that could occur when using an older version of localcharset.c. */ if (code[0] == '\0') goto invalid; continue; } if (ap->encoding_index == ei_local_wchar_t) { /* On systems which define __STDC_ISO_10646__, wchar_t is Unicode. This is also the case on native Woe32 systems and Cygwin >= 1.7, where we know that it is UTF-16. */ #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || (defined __CYGWIN__ && CYGWIN_VERSION_DLL_MAJOR >= 1007) if (sizeof(wchar_t) == 4) { index = ei_ucs4internal; break; } if (sizeof(wchar_t) == 2) { # if WORDS_LITTLEENDIAN index = ei_utf16le; # else index = ei_utf16be; # endif break; } #elif __STDC_ISO_10646__ if (sizeof(wchar_t) == 4) { index = ei_ucs4internal; break; } if (sizeof(wchar_t) == 2) { index = ei_ucs2internal; break; } if (sizeof(wchar_t) == 1) { index = ei_iso8859_1; break; } #endif } index = ap->encoding_index; break; } return all_canonical[index] + pool; invalid: return name; } int _libiconv_version = _LIBICONV_VERSION; #if defined __FreeBSD__ && !defined __gnu_freebsd__ /* GNU libiconv is the native FreeBSD iconv implementation since 2002. It wants to define the symbols 'iconv_open', 'iconv', 'iconv_close'. */ #define strong_alias(name, aliasname) _strong_alias(name, aliasname) #define _strong_alias(name, aliasname) \ extern __typeof (name) aliasname __attribute__ ((alias (#name))); #undef iconv_open #undef iconv #undef iconv_close strong_alias (libiconv_open, iconv_open) strong_alias (libiconv, iconv) strong_alias (libiconv_close, iconv_close) #endif #endif
和
E:\MyCocos\cocos2dx-workspace\hello\cocos2d\iconv\libcharset\lib\localcharset.c
/* Determine a canonical name for the current locale's character encoding. Copyright (C) 2000-2006, 2008-2010 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Written by Bruno Haible <bruno@clisp.org>. */ #include <config.h> /* Specification. */ #include "localcharset.h" #include <fcntl.h> #include <stddef.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #if defined __APPLE__ && defined __MACH__ && HAVE_LANGINFO_CODESET # define DARWIN7 /* Darwin 7 or newer, i.e. MacOS X 10.3 or newer */ #endif #if defined _WIN32 || defined __WIN32__ # define WIN32_NATIVE #endif #if defined __EMX__ /* Assume EMX program runs on OS/2, even if compiled under DOS. */ # ifndef OS2 # define OS2 # endif #endif #if !defined WIN32_NATIVE # include <unistd.h> # if HAVE_LANGINFO_CODESET # include <langinfo.h> # else # if 0 /* see comment below */ # include <locale.h> # endif # endif # ifdef __CYGWIN__ # define WIN32_LEAN_AND_MEAN # include <windows.h> # endif #elif defined WIN32_NATIVE # define WIN32_LEAN_AND_MEAN # include <windows.h> #endif #if defined OS2 # define INCL_DOS # include <os2.h> #endif #if ENABLE_RELOCATABLE # include "relocatable.h" #else # define relocate(pathname) (pathname) #endif /* Get LIBDIR. */ #ifndef LIBDIR # include "configmake.h" #endif /* Define O_NOFOLLOW to 0 on platforms where it does not exist. */ #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 #endif #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ /* Win32, Cygwin, OS/2, DOS */ # define ISSLASH(C) ((C) == '/' || (C) == '\\') #endif #ifndef DIRECTORY_SEPARATOR # define DIRECTORY_SEPARATOR '/' #endif #ifndef ISSLASH # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR) #endif #if HAVE_DECL_GETC_UNLOCKED # undef getc # define getc getc_unlocked #endif /* The following static variable is declared 'volatile' to avoid a possible multithread problem in the function get_charset_aliases. If we are running in a threaded environment, and if two threads initialize 'charset_aliases' simultaneously, both will produce the same value, and everything will be ok if the two assignments to 'charset_aliases' are atomic. But I don't know what will happen if the two assignments mix. */ #if __STDC__ != 1 # define volatile /* empty */ #endif /* Pointer to the contents of the charset.alias file, if it has already been read, else NULL. Its format is: ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */ static const char * volatile charset_aliases; /* Return a pointer to the contents of the charset.alias file. */ static const char * get_charset_aliases (void) { const char *cp; cp = charset_aliases; if (cp == NULL) { #if !(defined DARWIN7 || defined VMS || defined WIN32_NATIVE || defined __CYGWIN__) const char *dir; const char *base = "charset.alias"; char *file_name; /* Make it possible to override the charset.alias location. This is necessary for running the testsuite before "make install". */ dir = getenv ("CHARSETALIASDIR"); if (dir == NULL || dir[0] == '\0') dir = relocate ("c");//chenhd 2014.02 modify (LIBDIR); /* Concatenate dir and base into freshly allocated file_name. */ { size_t dir_len = strlen (dir); size_t base_len = strlen (base); int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1])); file_name = (char *) malloc (dir_len + add_slash + base_len + 1); if (file_name != NULL) { memcpy (file_name, dir, dir_len); if (add_slash) file_name[dir_len] = DIRECTORY_SEPARATOR; memcpy (file_name + dir_len + add_slash, base, base_len + 1); } } if (file_name == NULL) /* Out of memory. Treat the file as empty. */ cp = ""; else { int fd; /* Open the file. Reject symbolic links on platforms that support O_NOFOLLOW. This is a security feature. Without it, an attacker could retrieve parts of the contents (namely, the tail of the first line that starts with "* ") of an arbitrary file by placing a symbolic link to that file under the name "charset.alias" in some writable directory and defining the environment variable CHARSETALIASDIR to point to that directory. */ fd = open (file_name, O_RDONLY | (1 ?O_NOFOLLOW : 0));//(HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0)); if (fd < 0) /* File not found. Treat it as empty. */ cp = ""; else { FILE *fp; fp = fdopen (fd, "r"); if (fp == NULL) { /* Out of memory. Treat the file as empty. */ close (fd); cp = ""; } else { /* Parse the file's contents. */ char *res_ptr = NULL; size_t res_size = 0; for (;;) { int c; char buf1[50+1]; char buf2[50+1]; size_t l1, l2; char *old_res_ptr; c = getc (fp); if (c == EOF) break; if (c == '\n' || c == ' ' || c == '\t') continue; if (c == '#') { /* Skip comment, to end of line. */ do c = getc (fp); while (!(c == EOF || c == '\n')); if (c == EOF) break; continue; } ungetc (c, fp); if (fscanf (fp, "%50s %50s", buf1, buf2) < 2) break; l1 = strlen (buf1); l2 = strlen (buf2); old_res_ptr = res_ptr; if (res_size == 0) { res_size = l1 + 1 + l2 + 1; res_ptr = (char *) malloc (res_size + 1); } else { res_size += l1 + 1 + l2 + 1; res_ptr = (char *) realloc (res_ptr, res_size + 1); } if (res_ptr == NULL) { /* Out of memory. */ res_size = 0; free (old_res_ptr); break; } strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1); strcpy (res_ptr + res_size - (l2 + 1), buf2); } fclose (fp); if (res_size == 0) cp = ""; else { *(res_ptr + res_size) = '\0'; cp = res_ptr; } } } free (file_name); } #else # if defined DARWIN7 /* To avoid the trouble of installing a file that is shared by many GNU packages -- many packaging systems have problems with this --, simply inline the aliases here. */ cp = "ISO8859-1" "\0" "ISO-8859-1" "\0" "ISO8859-2" "\0" "ISO-8859-2" "\0" "ISO8859-4" "\0" "ISO-8859-4" "\0" "ISO8859-5" "\0" "ISO-8859-5" "\0" "ISO8859-7" "\0" "ISO-8859-7" "\0" "ISO8859-9" "\0" "ISO-8859-9" "\0" "ISO8859-13" "\0" "ISO-8859-13" "\0" "ISO8859-15" "\0" "ISO-8859-15" "\0" "KOI8-R" "\0" "KOI8-R" "\0" "KOI8-U" "\0" "KOI8-U" "\0" "CP866" "\0" "CP866" "\0" "CP949" "\0" "CP949" "\0" "CP1131" "\0" "CP1131" "\0" "CP1251" "\0" "CP1251" "\0" "eucCN" "\0" "GB2312" "\0" "GB2312" "\0" "GB2312" "\0" "eucJP" "\0" "EUC-JP" "\0" "eucKR" "\0" "EUC-KR" "\0" "Big5" "\0" "BIG5" "\0" "Big5HKSCS" "\0" "BIG5-HKSCS" "\0" "GBK" "\0" "GBK" "\0" "GB18030" "\0" "GB18030" "\0" "SJIS" "\0" "SHIFT_JIS" "\0" "ARMSCII-8" "\0" "ARMSCII-8" "\0" "PT154" "\0" "PT154" "\0" /*"ISCII-DEV" "\0" "?" "\0"*/ "*" "\0" "UTF-8" "\0"; # endif # if defined VMS /* To avoid the troubles of an extra file charset.alias_vms in the sources of many GNU packages, simply inline the aliases here. */ /* The list of encodings is taken from the OpenVMS 7.3-1 documentation "Compaq C Run-Time Library Reference Manual for OpenVMS systems" section 10.7 "Handling Different Character Sets". */ cp = "ISO8859-1" "\0" "ISO-8859-1" "\0" "ISO8859-2" "\0" "ISO-8859-2" "\0" "ISO8859-5" "\0" "ISO-8859-5" "\0" "ISO8859-7" "\0" "ISO-8859-7" "\0" "ISO8859-8" "\0" "ISO-8859-8" "\0" "ISO8859-9" "\0" "ISO-8859-9" "\0" /* Japanese */ "eucJP" "\0" "EUC-JP" "\0" "SJIS" "\0" "SHIFT_JIS" "\0" "DECKANJI" "\0" "DEC-KANJI" "\0" "SDECKANJI" "\0" "EUC-JP" "\0" /* Chinese */ "eucTW" "\0" "EUC-TW" "\0" "DECHANYU" "\0" "DEC-HANYU" "\0" "DECHANZI" "\0" "GB2312" "\0" /* Korean */ "DECKOREAN" "\0" "EUC-KR" "\0"; # endif # if defined WIN32_NATIVE || defined __CYGWIN__ /* To avoid the troubles of installing a separate file in the same directory as the DLL and of retrieving the DLL's directory at runtime, simply inline the aliases here. */ cp = "CP936" "\0" "GBK" "\0" "CP1361" "\0" "JOHAB" "\0" "CP20127" "\0" "ASCII" "\0" "CP20866" "\0" "KOI8-R" "\0" "CP20936" "\0" "GB2312" "\0" "CP21866" "\0" "KOI8-RU" "\0" "CP28591" "\0" "ISO-8859-1" "\0" "CP28592" "\0" "ISO-8859-2" "\0" "CP28593" "\0" "ISO-8859-3" "\0" "CP28594" "\0" "ISO-8859-4" "\0" "CP28595" "\0" "ISO-8859-5" "\0" "CP28596" "\0" "ISO-8859-6" "\0" "CP28597" "\0" "ISO-8859-7" "\0" "CP28598" "\0" "ISO-8859-8" "\0" "CP28599" "\0" "ISO-8859-9" "\0" "CP28605" "\0" "ISO-8859-15" "\0" "CP38598" "\0" "ISO-8859-8" "\0" "CP51932" "\0" "EUC-JP" "\0" "CP51936" "\0" "GB2312" "\0" "CP51949" "\0" "EUC-KR" "\0" "CP51950" "\0" "EUC-TW" "\0" "CP54936" "\0" "GB18030" "\0" "CP65001" "\0" "UTF-8" "\0"; # endif #endif charset_aliases = cp; } return cp; } /* Determine the current locale's character encoding, and canonicalize it into one of the canonical names listed in config.charset. The result must not be freed; it is statically allocated. If the canonical name cannot be determined, the result is a non-canonical name. */ #ifdef STATIC STATIC #endif const char * locale_charset (void) { const char *codeset; const char *aliases; #if !(defined WIN32_NATIVE || defined OS2) # if HAVE_LANGINFO_CODESET /* Most systems support nl_langinfo (CODESET) nowadays. */ codeset = nl_langinfo (CODESET); # ifdef __CYGWIN__ /* Cygwin < 1.7 does not have locales. nl_langinfo (CODESET) always returns "US-ASCII". Return the suffix of the locale name from the environment variables (if present) or the codepage as a number. */ if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0) { const char *locale; static char buf[2 + 10 + 1]; locale = getenv ("LC_ALL"); if (locale == NULL || locale[0] == '\0') { locale = getenv ("LC_CTYPE"); if (locale == NULL || locale[0] == '\0') locale = getenv ("LANG"); } if (locale != NULL && locale[0] != '\0') { /* If the locale name contains an encoding after the dot, return it. */ const char *dot = strchr (locale, '.'); if (dot != NULL) { const char *modifier; dot++; /* Look for the possible @... trailer and remove it, if any. */ modifier = strchr (dot, '@'); if (modifier == NULL) return dot; if (modifier - dot < sizeof (buf)) { memcpy (buf, dot, modifier - dot); buf [modifier - dot] = '\0'; return buf; } } } /* Woe32 has a function returning the locale's codepage as a number: GetACP(). This encoding is used by Cygwin, unless the user has set the environment variable CYGWIN=codepage:oem (which very few people do). Output directed to console windows needs to be converted (to GetOEMCP() if the console is using a raster font, or to GetConsoleOutputCP() if it is using a TrueType font). Cygwin does this conversion transparently (see winsup/cygwin/fhandler_console.cc), converting to GetConsoleOutputCP(). This leads to correct results, except when SetConsoleOutputCP has been called and a raster font is in use. */ sprintf (buf, "CP%u", GetACP ()); codeset = buf; } # endif # else /* On old systems which lack it, use setlocale or getenv. */ const char *locale = NULL; /* But most old systems don't have a complete set of locales. Some (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't use setlocale here; it would return "C" when it doesn't support the locale name the user has set. */ # if 0 locale = setlocale (LC_CTYPE, NULL); # endif if (locale == NULL || locale[0] == '\0') { locale = getenv ("LC_ALL"); if (locale == NULL || locale[0] == '\0') { locale = getenv ("LC_CTYPE"); if (locale == NULL || locale[0] == '\0') locale = getenv ("LANG"); } } /* On some old systems, one used to set locale = "iso8859_1". On others, you set it to "language_COUNTRY.charset". In any case, we resolve it through the charset.alias file. */ codeset = locale; # endif #elif defined WIN32_NATIVE static char buf[2 + 10 + 1]; /* Woe32 has a function returning the locale's codepage as a number: GetACP(). When the output goes to a console window, it needs to be provided in GetOEMCP() encoding if the console is using a raster font, or in GetConsoleOutputCP() encoding if it is using a TrueType font. But in GUI programs and for output sent to files and pipes, GetACP() encoding is the best bet. */ sprintf (buf, "CP%u", GetACP ()); codeset = buf; #elif defined OS2 const char *locale; static char buf[2 + 10 + 1]; ULONG cp[3]; ULONG cplen; /* Allow user to override the codeset, as set in the operating system, with standard language environment variables. */ locale = getenv ("LC_ALL"); if (locale == NULL || locale[0] == '\0') { locale = getenv ("LC_CTYPE"); if (locale == NULL || locale[0] == '\0') locale = getenv ("LANG"); } if (locale != NULL && locale[0] != '\0') { /* If the locale name contains an encoding after the dot, return it. */ const char *dot = strchr (locale, '.'); if (dot != NULL) { const char *modifier; dot++; /* Look for the possible @... trailer and remove it, if any. */ modifier = strchr (dot, '@'); if (modifier == NULL) return dot; if (modifier - dot < sizeof (buf)) { memcpy (buf, dot, modifier - dot); buf [modifier - dot] = '\0'; return buf; } } /* Resolve through the charset.alias file. */ codeset = locale; } else { /* OS/2 has a function returning the locale's codepage as a number. */ if (DosQueryCp (sizeof (cp), cp, &cplen)) codeset = ""; else { sprintf (buf, "CP%u", cp[0]); codeset = buf; } } #endif if (codeset == NULL) /* The canonical name cannot be determined. */ codeset = ""; /* Resolve alias. */ for (aliases = get_charset_aliases (); *aliases != '\0'; aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1) if (strcmp (codeset, aliases) == 0 || (aliases[0] == '*' && aliases[1] == '\0')) { codeset = aliases + strlen (aliases) + 1; break; } /* Don't return an empty string. GNU libc and GNU libiconv interpret the empty string as denoting "the locale's character encoding", thus GNU libiconv would call this function a second time. */ if (codeset[0] == '\0') codeset = "ASCII"; return codeset; }
copy完这3个文件
交叉编译时另一个问题
config.h找不到
原因是库里面所以的config.h文件名称都是config.h.in
在iocnv以下搜索config.h
将3个文件的.in去掉
交叉编译成功
cocos2d-x-3.0rc1 完美执行iocnv显示中文字符串
这里我就不截图了
这里也留个记录 供以后使用
參考了原文地址:http://blog.sina.com.cn/s/blog_a17b071c0101lm91.html
原文给出的东西非常实用 只是使用起来还是有一些错误 只是里面详细改了那些位置 他用蓝色标记注明了 还是非常细致的
我的这个版本号 代码就是直接copy