You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1011 B

/*
* xconv.c -- A Jim C extension that uses libxconv to convert from
* ISO 6937 to UTF-8.
*/
#include <stdio.h>
#include <string.h>
#include <jim.h>
#include <jim-config.h>
#include <utf8.h>
#include <xconv.h>
static int
Xconv_Cmd(Jim_Interp *j, int argc, Jim_Obj *const argv[])
{
const char *src, *dst;
size_t dstlen, len;
Jim_Obj *ret;
if (argc != 2)
{
Jim_WrongNumArgs(j, 1, argv, "<ISO 6937 string>");
return JIM_ERR;
}
src = Jim_GetString(argv[1], NULL);
dstlen = strlen(src) * 2;
dst = (char *)Jim_Alloc(dstlen + 1);
if ((len = xconv(src, dst, dstlen)))
{
#ifdef JIM_UTF8
int chars;
chars = utf8_strlen(dst, len);
ret = Jim_NewStringObjUtf8(j, dst, chars);
Jim_Free(dst);
#else
ret = Jim_NewStringObjNoAlloc(j, dst, len);
#endif
Jim_SetResult(j, ret);
}
else
{
Jim_Free(dst);
Jim_SetResult(j, argv[1]); // Increments ref count.
}
return JIM_OK;
}
int
Jim_xconvInit(Jim_Interp *j)
{
Jim_CreateCommand(j, "xconv", Xconv_Cmd, NULL, NULL);
return JIM_OK;
}