From 8831696c796ff5e160b0e845523a5bbc706405bb Mon Sep 17 00:00:00 2001 From: Marrub Date: Thu, 3 Nov 2016 13:50:09 -0400 Subject: [PATCH] This is terrible. --- iconv_lua.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 iconv_lua.c diff --git a/iconv_lua.c b/iconv_lua.c new file mode 100644 index 0000000..10d3cab --- /dev/null +++ b/iconv_lua.c @@ -0,0 +1,59 @@ +// Made by Graham Sanderson, no rights reserved. +// Link with iconv and Lua 5.1. + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + size_t len, srcsz, dstsz; + char *lstr, *out, *pout; + char const *str, *pstr; + lua_State *lst; + iconv_t icd; + + if(argc != 2) + { + printf("Invalid arguments. Usage: %s \n", argv[0]); + return 1; + } + + len = strlen(argv[1]); + lstr = calloc(len + 5, 1); + memcpy(lstr, "s=\"", 3); + memcpy(lstr + 3, argv[1], len); + lstr[len + 3] = '"'; + + lst = luaL_newstate(); + luaL_openlibs(lst); + luaL_dostring(lst, lstr); + lua_getglobal(lst, "s"); + str = lua_tostring(lst, 1); + + out = calloc(len * 6, 1); + pstr = str; + pout = out; + + srcsz = len; + dstsz = len * 6; + + icd = iconv_open("UTF-8", "SHIFT-JIS"); + iconv(icd, &pstr, &srcsz, &pout, &dstsz); + iconv_close(icd); + + *pout = '\0'; + + puts(out); + + free(lstr); + free(out); + + return 0; +} + +// EOF