Compile iconv into lua interface, and iconv compile lua
The previous blog post explains how to use iconv transcoding in cocos2dx. In this section, we will use the transcoding function written in the previous section to create a lua interface and use it in the lua script.
You can download luaconv from the Internet, but errors are always reported during compilation, so you have written an interface yourself.
1. Add the lua interface file
// Luaiconv. h
#ifndef __LUA_ICONV_H__#define __LUA_ICONV_H__#include "tolua++.h"#include "tolua_event.h"#include "lauxlib.h"#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32#include "iconv.h"#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS#include <iconv.h>#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID#include "iconv.h"#endifint tolua_iconv_open(lua_State *L);#endif
// Luaiconv. cpp
# Include "luaiconv. h "# include <stdlib. h> # define MAX_STRING_SIZE 1024 # define TEST_STR ("Hello, this is a transcoding test string") bool iconv_convert (void * src, unsigned int src_len, char * src_charset, void * dest, unsigned int dest_len, char * dest_charset) {const char * in; char * out, * dest_ptr; size_t in_left, out_left, mutant, converted; in_left = src_len; out_left = dest_len; in = (char *) src; out = dest_ptr = (char *) dest; iconv_t o Conv = iconv_open (dest_charset, src_charset); if (oConv = (iconv_t) (-1) {printf ("xxxxxxxxxx error: unable to open libiconv. \ n "); return false;} mutant = iconv (oConv, & in, & in_left, & out, & out_left); iconv_close (oConv ); if (mutant = (size_t) (-1) {printf ("xxxxxxxxxx error: unable to convert anything. \ n "); return false;} converted = dest_len-out_left; dest_ptr [converted] = '\ 0'; printf (" XXXXXXXXXX src string: % s \ n ", Src); printf (" XXXXXXXXXX to convert % u characters, % u mutanted, % u converted \ n ", src_len, mutant, converted); printf (" XXXXXXXXXX dst string: % s \ n ", dest); return true;} void convertTest () {char inStr [] = TEST_STR; char outStr [MAX_STRING_SIZE]; iconv_convert (& inStr, sizeof (inStr), "GBK", & outStr, sizeof (outStr), "UTF-8"); printf ("XXXXXXXXXX in string: % s \ n", inStr ); printf ("XXXXXXXXXX out string: % s \ n", outS Tr);} TOLUA_API int luaiconv (lua_State * L) {char * inbuf = (char *) luaL_checkstring (L, 1); char * src_charset = (char *) luaL_checkstring (L, 2); char * dst_charset = (char *) luaL_checkstring (L, 3); size_t ibleft = lua_strlen (L, 1); size_t obleft = (ibleft> 256 )? Ibleft: 256; char * outbuf = (char *) malloc (obleft * sizeof (char); if (outbuf = NULL) {lua_pushstring (L ,""); return 1;} if (! Iconv_convert (inbuf, ibleft, src_charset, outbuf, obleft, dst_charset) {lua_pushstring (L, ""); return 1;} lua_pushstring (L, outbuf); free (outbuf ); return 1;} static luaL_Reg iconvlib [] = {"luaiconv", luaiconv}, {NULL, NULL}; // The function name must be luaopen_xxx, where xxx indicates the library name, the Lua Code require "xxx" needs to correspond to it. Int luaopen_iconv (lua_State * L) {const char * libName = "iconv"; luaL_register (L, libName, iconvlib); // call libName. function Name return 1;} int tolua_iconv_open (lua_State * L) {luaopen_iconv (L); return 1 ;}
In AppDelegate
#include "luaiconv.h"
Then, in AppDelegate: applicationDidFinishLaunching (), call:
CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);tolua_iconv_open(pEngine->getLuaStack()->getLuaState());
Use this in lua:
require "iconv"iconv.luaiconv("string to convert code","GBK","UTF-8")
2. Add luaiconv. cpp to the Android. mkLOCAL_SRC_FILES file of the project.
Remember to add spaces and line breaks at the end :"\"
Otherwise, an error may be reported:
Make: *** No rule to make target 'xxx. o', needed by 'xxx'. Stop