sqlite3-xconv/xconv.c
2017-03-12 16:49:47 +00:00

50 lines
969 B
C

/*
* xconv.c -- An SQLite3 C extension that uses libxconv to convert from
* ISO 6937 to UTF-8.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3ext.h>
#include <xconv.h>
SQLITE_EXTENSION_INIT1
void
sqlite_xconv(sqlite3_context *c, int argc, sqlite3_value **argv)
{
char *src, *dst;
size_t dlen;
if (argc != 1 || !(src = (char *)sqlite3_value_text(argv[0])))
{
sqlite3_result_error(c, "xconv requires 1 parameter", -1);
return;
}
dlen = strlen(src) + 20;
if (!(dst = (char *)malloc(dlen)))
{
sqlite3_result_error(c, "malloc() failed", -1);
return;
}
if (!xconv(src, dst, dlen))
{
free(dst);
dst = strdup(src);
}
sqlite3_result_text(c, dst, -1, free);
}
int sqlite3_qxconv_init(sqlite3 *db, char **err,
const sqlite3_api_routines *api)
{
SQLITE_EXTENSION_INIT2(api);
return sqlite3_create_function(db, "xconv", 1,
SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
sqlite_xconv, NULL, NULL);
}