/* * xconv.c -- An SQLite3 C extension that uses libxconv to convert from * ISO 6937 to UTF-8. */ #include #include #include #include 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) * 2 + 14; 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); } void sqlite_utf8conv(sqlite3_context *c, int argc, sqlite3_value **argv) { char *src, *dst; if (argc != 1 || !(src = (char *)sqlite3_value_text(argv[0]))) { sqlite3_result_error(c, "utf8conv requires 1 parameter", -1); return; } if (src[0] == 0x10) { if (src[1] == 'i' && src[2] == '7') { size_t dlen; src += 3; dlen = strlen(src) * 2 + 14; if (!(dst = (char *)malloc(dlen))) { sqlite3_result_error(c, "malloc() failed", -1); return; } if (!xconv(src, dst, dlen)) { free(dst); dst = strdup(src); } } else dst = strdup(""); } else { if (*src < 0x20) src += *src == 0x1F ? 2 : 1; dst = strdup(src); } sqlite3_result_text(c, dst, -1, free); } #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_xconv_init(sqlite3 *db, char **err, const sqlite3_api_routines *api) { int rc1, rc2; SQLITE_EXTENSION_INIT2(api); rc1 = sqlite3_create_function(db, "xconv", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, sqlite_xconv, NULL, NULL); rc2 = sqlite3_create_function(db, "utf8conv", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, sqlite_utf8conv, NULL, NULL); return rc1 != SQLITE_OK && rc2 != SQLITE_OK ? rc1 : SQLITE_OK; }