提交 b1792612 编写于 作者: F freemine

unify calls and remove obsoletes

上级 23f49cca
......@@ -74,7 +74,6 @@ do {
if (Sqlstate) strncpy((char*)Sqlstate, (char*)obj->err.sql_state, n); \
if (NativeError) *NativeError = obj->err.err_no; \
snprintf((char*)MessageText, (size_t)BufferLength, "%s", obj->err.err_str); \
if (TextLength && obj->err.err_str) *TextLength = (SQLSMALLINT)strlen(obj->err.err_str); \
if (TextLength && obj->err.err_str) *TextLength = (SQLSMALLINT)utf8_chars(obj->err.err_str); \
} while (0)
......@@ -264,10 +263,6 @@ struct sql_s {
uint64_t refcount;
conn_t *conn;
iconv_t w2c; // wchar* -> char*
iconv_t u2c; // unicode -> char*
iconv_t u2w; // unicode -> wchar*
TAOS_STMT *stmt;
param_bind_t *params;
int n_params;
......@@ -297,14 +292,6 @@ static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static void init_routine(void);
static size_t do_field_display_size(TAOS_FIELD *field);
static iconv_t sql_get_w2c(sql_t *sql) {
if (sql->w2c == (iconv_t)-1) {
sql->w2c = iconv_open("UTF-8", "UCS-2LE");
}
return sql->w2c;
}
static tsdb_conv_t* tsdb_conn_client_to_server(conn_t *conn) {
if (!conn->client_to_server) {
......@@ -409,22 +396,6 @@ do {
} \
} while (0)
// static iconv_t sql_get_u2c(sql_t *sql) {
// if (sql->u2c == (iconv_t)-1) {
// sql->u2c = iconv_open("UTF-8", "UCS-4LE");
// }
//
// return sql->u2c;
// }
//
// static iconv_t sql_get_u2w(sql_t *sql) {
// if (sql->u2w == (iconv_t)-1) {
// sql->u2w = iconv_open("UCS-2LE", "UCS-4LE");
// }
//
// return sql->u2w;
// }
static SQLRETURN doSQLAllocEnv(SQLHENV *EnvironmentHandle)
{
pthread_once(&init_once, init_routine);
......@@ -666,10 +637,6 @@ static SQLRETURN doSQLAllocStmt(SQLHDBC ConnectionHandle,
break;
}
sql->w2c = (iconv_t)-1;
sql->u2c = (iconv_t)-1;
sql->u2w = (iconv_t)-1;
sql->conn = conn;
DASSERT(INC_REF(sql)>0);
......@@ -767,19 +734,6 @@ static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle,
FREE_ERROR(sql);
if (sql->w2c!=(iconv_t)-1) {
iconv_close(sql->w2c);
sql->w2c = (iconv_t)-1;
}
if (sql->u2c!=(iconv_t)-1) {
iconv_close(sql->u2c);
sql->u2c = (iconv_t)-1;
}
if (sql->u2w!=(iconv_t)-1) {
iconv_close(sql->u2w);
sql->u2w = (iconv_t)-1;
}
free(sql);
return SQL_SUCCESS;
......@@ -793,11 +747,22 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle,
return r;
}
static SQLRETURN do_exec_direct(sql_t *sql, TSDB_CONV_CODE code, const char *statement) {
if (code) CHK_CONV(1, code);
DASSERT(code==TSDB_CONV_OK);
SQLRETURN r = SQL_ERROR;
do {
sql->rs = taos_query(sql->conn->taos, statement);
CHK_RS(r, sql, "failed to execute");
} while (0);
return r;
}
static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle,
SQLCHAR *StatementText, SQLINTEGER TextLength)
{
stack_buffer_t buffer; buffer.next = 0;
sql_t *sql = (sql_t*)StatementHandle;
if (!sql) return SQL_INVALID_HANDLE;
......@@ -825,20 +790,14 @@ static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle,
}
sql->n_params = 0;
SQLRETURN r = SQL_SUCCESS;
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *client_to_server = tsdb_conn_client_to_server(conn);
const char *stxt = NULL;
SQLRETURN r = SQL_ERROR;
do {
tsdb_conv(client_to_server, &buffer, (const char*)StatementText, (size_t)TextLength, &stxt, NULL);
if (!stxt) {
SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, "");
break;
}
sql->rs = taos_query(sql->conn->taos, stxt);
CHK_RS(r, sql, "failed to execute");
TSDB_CONV_CODE code = tsdb_conv(client_to_server, &buffer, (const char*)StatementText, (size_t)TextLength, &stxt, NULL);
r = do_exec_direct(sql, code, stxt);
} while (0);
tsdb_conv_free(client_to_server, stxt, &buffer, (const char*)StatementText);
return r;
......@@ -860,6 +819,8 @@ static SQLRETURN doSQLExecDirectW(SQLHSTMT hstmt, SQLWCHAR *szSqlStr, SQLINTEGER
CHK_CONN(sql);
CHK_CONN_TAOS(sql);
conn_t *conn = sql->conn;
if (!szSqlStr) {
SET_ERROR(sql, "HY009", TSDB_CODE_ODBC_BAD_ARG, "szSqlStr [%p] not allowed", szSqlStr);
return SQL_ERROR;
......@@ -869,10 +830,17 @@ static SQLRETURN doSQLExecDirectW(SQLHSTMT hstmt, SQLWCHAR *szSqlStr, SQLINTEGER
return SQL_ERROR;
}
size_t bytes = 0;
SQLCHAR *utf8 = wchars_to_chars(szSqlStr, (size_t)cbSqlStr, &bytes);
SQLRETURN r = SQLExecDirect(hstmt, utf8, (SQLINTEGER)bytes);
if (utf8) free(utf8);
SQLRETURN r = SQL_SUCCESS;
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_server = tsdb_conn_utf16_to_server(conn);
const char *stxt = NULL;
do {
size_t slen = (size_t)cbSqlStr * sizeof(*szSqlStr);
TSDB_CONV_CODE code = tsdb_conv(utf16_to_server, &buffer, (const char*)szSqlStr, slen, &stxt, NULL);
r = do_exec_direct(sql, code, stxt);
} while (0);
tsdb_conv_free(utf16_to_server, stxt, &buffer, (const char*)szSqlStr);
return r;
}
......@@ -1572,7 +1540,11 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
CHK_CONV(1, tsdb_conv_chars_to_bit(client_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.b));
} break;
case SQL_C_WCHAR: {
CHK_CONV(1, tsdb_wchars_to_bit(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.b));
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_utf8 = tsdb_conn_utf16_to_utf8(conn);
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_bit(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.b));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1629,7 +1601,11 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
// CHK_CONV(1, tsdb_chars_to_tinyint((const char *)paramValue, (size_t)*soi, &bind->u.v1));
} break;
case SQL_C_WCHAR: {
CHK_CONV(1, tsdb_wchars_to_tinyint(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.v1));
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_utf8 = tsdb_conn_utf16_to_utf8(conn);
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_tinyint(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.v1));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1688,7 +1664,11 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
// CHK_CONV(1, tsdb_chars_to_smallint((const char*)paramValue, (size_t)*soi, &bind->u.v2));
} break;
case SQL_C_WCHAR: {
CHK_CONV(1, tsdb_wchars_to_smallint(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.v2));
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_utf8 = tsdb_conn_utf16_to_utf8(conn);
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_smallint(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.v2));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1747,7 +1727,11 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
// CHK_CONV(1, tsdb_chars_to_int((const char*)paramValue, (size_t)*soi, &bind->u.v4));
} break;
case SQL_C_WCHAR: {
CHK_CONV(1, tsdb_wchars_to_int(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.v4));
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_utf8 = tsdb_conn_utf16_to_utf8(conn);
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_int(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.v4));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1806,7 +1790,11 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
// CHK_CONV(1, tsdb_chars_to_bigint((const char*)paramValue, (size_t)*soi, &bind->u.v8));
} break;
case SQL_C_WCHAR: {
CHK_CONV(1, tsdb_wchars_to_bigint(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.v8));
stack_buffer_t buffer; buffer.next = 0;
tsdb_conv_t *utf16_to_utf8 = tsdb_conn_utf16_to_utf8(conn);
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_bigint(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.v8));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1868,7 +1856,6 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
size_t slen = (size_t)*soi;
if (slen==SQL_NTS) slen = strlen((const char*)paramValue);
CHK_CONV(1, tsdb_conv_chars_to_float(client_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.f4));
// CHK_CONV(1, tsdb_chars_to_float((const char*)paramValue, (size_t)*soi, &bind->u.f4));
} break;
case SQL_C_WCHAR: {
stack_buffer_t buffer; buffer.next = 0;
......@@ -1876,7 +1863,6 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_float(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.f4));
// CHK_CONV(1, tsdb_wchars_to_float(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.f4));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -1944,7 +1930,6 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin
size_t slen = (size_t)*soi;
DASSERT(slen != SQL_NTS);
CHK_CONV(1, tsdb_conv_chars_to_double(utf16_to_utf8, &buffer, (const char *)paramValue, slen, &bind->u.f8));
// CHK_CONV(1, tsdb_wchars_to_double(sql_get_w2c(sql), (const unsigned char*)paramValue, (size_t)*soi, &bind->u.f8));
} break;
case SQL_C_USHORT:
case SQL_C_ULONG:
......@@ -2817,11 +2802,6 @@ SQLRETURN SQL_API SQLSetStmtAttr(SQLHSTMT StatementHandle,
static void init_routine(void) {
if (0) {
string_conv(NULL, NULL, NULL, 0, NULL, 0, NULL, NULL);
utf8_to_ucs4le(NULL, NULL);
ucs4le_to_utf8(NULL, 0, NULL);
}
taos_init();
}
......
......@@ -23,35 +23,6 @@
#include <string.h>
#include <time.h>
typedef struct buf_s buf_t;
struct buf_s {
char buf[1024*16+1];
char *ptr;
};
static char* buf_init(buf_t *buf, size_t len);
static void buf_clean(buf_t *buf);
static char* buf_init(buf_t *buf, size_t len) {
if (len>sizeof(buf->buf)) {
buf->ptr = (char*)malloc(len);
} else if (len>0) {
buf->ptr = &buf->buf[0];
} else {
buf->ptr = NULL;
}
return buf->ptr;
}
static void buf_clean(buf_t *buf) {
if (buf->ptr && buf->ptr != buf->buf) {
free(buf->ptr);
buf->ptr = NULL;
}
}
const char* tsdb_conv_code_str(TSDB_CONV_CODE code) {
switch (code) {
case TSDB_CONV_OK: return "TSDB_CONV_OK";
......@@ -69,36 +40,6 @@ const char* tsdb_conv_code_str(TSDB_CONV_CODE code) {
};
}
TSDB_CONV_CODE tsdb_iconv_conv(iconv_t cnv, const unsigned char *src, size_t *slen, unsigned char *dst, size_t *dlen) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
char *s = (char*)src;
char *d = (char*)dst;
size_t sl = *slen;
size_t dl = *dlen;
size_t n = iconv(cnv, &s, &sl, &d, &dl);
int e = errno;
if (dl) *d = '\0'; // what if all consumed?
*slen = sl;
*dlen = dl;
if (e==0) {
if (n) return TSDB_CONV_BAD_CHAR;
return TSDB_CONV_OK;
}
iconv(cnv, NULL, NULL, NULL, NULL);
switch (e) {
case E2BIG: return TSDB_CONV_TRUNC;
case EILSEQ: return TSDB_CONV_BAD_CHAR;
case EINVAL: return TSDB_CONV_BAD_CHAR;
default: return TSDB_CONV_GENERAL;
}
}
// src: int
TSDB_CONV_CODE tsdb_int64_to_bit(int64_t src, int8_t *dst) {
*dst = (int8_t)src;
......@@ -425,148 +366,6 @@ TSDB_CONV_CODE tsdb_chars_to_char(const char *src, size_t smax, char *dst, size_
}
// src: wchars
TSDB_CONV_CODE tsdb_wchars_to_bit(iconv_t cnv, const unsigned char *src, size_t smax, int8_t *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_bit(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_tinyint(iconv_t cnv, const unsigned char *src, size_t smax, int8_t *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_tinyint(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_smallint(iconv_t cnv, const unsigned char *src, size_t smax, int16_t *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_smallint(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_int(iconv_t cnv, const unsigned char *src, size_t smax, int32_t *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_int(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_bigint(iconv_t cnv, const unsigned char *src, size_t smax, int64_t *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_bigint(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_ts(iconv_t cnv, const unsigned char *src, size_t smax, int64_t *dst) {
return tsdb_wchars_to_bigint(cnv, src, smax, dst);
}
TSDB_CONV_CODE tsdb_wchars_to_float(iconv_t cnv, const unsigned char *src, size_t smax, float *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_float(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_double(iconv_t cnv, const unsigned char *src, size_t smax, double *dst) {
if(cnv == (iconv_t)-1) return TSDB_CONV_GENERAL;
size_t len = smax * 2;
buf_t buf;
buf_init(&buf, len+1);
if (!buf.ptr) return TSDB_CONV_OOM;
size_t dmax = len + 1;
TSDB_CONV_CODE code = tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)buf.ptr, &dmax);
if (code==TSDB_CONV_OK) {
code = tsdb_chars_to_double(buf.ptr, len+1-dmax, dst);
}
buf_clean(&buf);
return code;
}
TSDB_CONV_CODE tsdb_wchars_to_char(iconv_t cnv, const unsigned char *src, size_t smax, char *dst, size_t dmax) {
return tsdb_iconv_conv(cnv, src, &smax, (unsigned char*)dst, &dmax);
}
char* stack_buffer_alloc(stack_buffer_t *buffer, size_t bytes) {
if (!buffer) return NULL;
// align-by-size_of-size_t-bytes
......
......@@ -70,10 +70,6 @@ TSDB_CONV_CODE tsdb_conv(tsdb_conv_t *cnv, stack_buffer_t *buffer, const char *s
void tsdb_conv_free(tsdb_conv_t *cnv, const char *ptr, stack_buffer_t *buffer, const char *src);
TSDB_CONV_CODE tsdb_iconv_conv(iconv_t cnv, const unsigned char *src, size_t *slen, unsigned char *dst, size_t *dlen);
TSDB_CONV_CODE tsdb_int64_to_bit(int64_t src, int8_t *dst);
TSDB_CONV_CODE tsdb_int64_to_tinyint(int64_t src, int8_t *dst);
TSDB_CONV_CODE tsdb_int64_to_smallint(int64_t src, int16_t *dst);
......@@ -105,15 +101,5 @@ TSDB_CONV_CODE tsdb_chars_to_double(const char *src, size_t smax, double *dst);
TSDB_CONV_CODE tsdb_chars_to_timestamp(const char *src, size_t smax, SQL_TIMESTAMP_STRUCT *dst);
TSDB_CONV_CODE tsdb_chars_to_char(const char *src, size_t smax, char *dst, size_t dmax);
TSDB_CONV_CODE tsdb_wchars_to_bit(iconv_t cnv, const unsigned char *src, size_t smax, int8_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_tinyint(iconv_t cnv, const unsigned char *src, size_t smax, int8_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_smallint(iconv_t cnv, const unsigned char *src, size_t smax, int16_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_int(iconv_t cnv, const unsigned char *src, size_t smax, int32_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_bigint(iconv_t cnv, const unsigned char *src, size_t smax, int64_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_ts(iconv_t cnv, const unsigned char *src, size_t smax, int64_t *dst);
TSDB_CONV_CODE tsdb_wchars_to_float(iconv_t cnv, const unsigned char *src, size_t smax, float *dst);
TSDB_CONV_CODE tsdb_wchars_to_double(iconv_t cnv, const unsigned char *src, size_t smax, double *dst);
TSDB_CONV_CODE tsdb_wchars_to_char(iconv_t cnv, const unsigned char *src, size_t smax, char *dst, size_t dmax);
#endif // _todbc_conv_h_
......@@ -107,39 +107,6 @@ int is_valid_sql_sql_type(int type) {
return 1;
}
int string_conv(const char *fromcode, const char *tocode,
const unsigned char *src, size_t sbytes,
unsigned char *dst, size_t dbytes,
size_t *consumed, size_t *generated)
{
if (consumed) *consumed = 0;
if (generated) *generated = 0;
if (dbytes <= 0) return -1;
dst[0] = '\0';
iconv_t conv = iconv_open(tocode, fromcode);
if (!conv) return -1;
size_t r = 0;
do {
char *s = (char*)src;
char *d = (char*)dst;
size_t sl = sbytes;
size_t dl = dbytes;
r = iconv(conv, &s, &sl, &d, &dl);
*d = '\0';
if (consumed) *consumed = sbytes - sl;
if (generated) *generated = dbytes - dl;
} while (0);
iconv_close(conv);
return (int)r;
}
int utf8_chars(const char *src)
{
const char *fromcode = "UTF-8";
......@@ -160,91 +127,3 @@ int utf8_chars(const char *src)
return (int)chars;
}
unsigned char* utf8_to_ucs4le(const char *utf8, size_t *chars)
{
const char *tocode = "UCS-4LE";
const char *fromcode = "UTF-8";
iconv_t conv = iconv_open(tocode, fromcode);
if (!conv) return NULL;
unsigned char *ucs4le = NULL;
do {
size_t slen = strlen(utf8);
size_t dlen = slen * 4;
ucs4le = (unsigned char*)malloc(dlen+1);
if (!ucs4le) break;
char *src = (char*)utf8;
char *dst = (char*)ucs4le;
size_t s = slen;
size_t d = dlen;
iconv(conv, &src, &s, &dst, &d);
dst[0] = '\0';
if (chars) *chars = (dlen - d) / 4;
} while (0);
iconv_close(conv);
return ucs4le;
}
char* ucs4le_to_utf8(const unsigned char *ucs4le, size_t slen, size_t *chars)
{
const char *fromcode = "UCS-4LE";
const char *tocode = "UTF-8";
iconv_t conv = iconv_open(tocode, fromcode);
if (!conv) return NULL;
char *utf8 = NULL;
do {
size_t dlen = slen;
utf8 = (char*)malloc(dlen+1);
if (!utf8) break;
char *dst = utf8;
char *src = (char*)ucs4le;
size_t s = slen;
size_t d = dlen;
iconv(conv, &src, &s, &dst, &d);
dst[0] = '\0';
if (chars) *chars = (slen - s) / 4;
} while (0);
iconv_close(conv);
return utf8;
}
SQLCHAR* wchars_to_chars(const SQLWCHAR *wchars, size_t chs, size_t *bytes)
{
size_t dlen = chs * 4;
SQLCHAR *dst = (SQLCHAR*)malloc(dlen + 1);
if (!dst) return NULL;
string_conv("UCS-2LE", "UTF-8", (const unsigned char*)wchars, chs * sizeof(*wchars), dst, dlen + 1, NULL, bytes);
return dst;
}
size_t wchars_to_chars2(const SQLWCHAR *src, size_t slen, SQLCHAR *dst, size_t dlen)
{
size_t consumed=0, generated=0;
int n = string_conv("UCS-2LE", "UTF-8", (const unsigned char*)src, slen, dst, dlen, &consumed, &generated);
if (n) return (size_t)-1;
return generated;
}
size_t chars_to_wchars2(const SQLCHAR *src, size_t slen, SQLWCHAR *dst, size_t dlen)
{
size_t consumed=0, generated=0;
int n = string_conv("UTF-8", "UCS-2LE", (const unsigned char*)src, slen, (unsigned char*)dst, dlen, &consumed, &generated);
if (n) return (size_t)-1;
return generated;
}
......@@ -27,18 +27,7 @@ const char* sql_c_type(int type);
int is_valid_sql_c_type(int type);
int is_valid_sql_sql_type(int type);
int string_conv(const char *fromcode, const char *tocode,
const unsigned char *src, size_t sbytes,
unsigned char *dst, size_t dbytes,
size_t *consumed, size_t *generated);
int utf8_chars(const char *src);
unsigned char* utf8_to_ucs4le(const char *utf8, size_t *chars);
char* ucs4le_to_utf8(const unsigned char *ucs4le, size_t slen, size_t *chars);
SQLCHAR* wchars_to_chars(const SQLWCHAR *wchars, size_t chs, size_t *bytes);
size_t wchars_to_chars2(const SQLWCHAR *src, size_t slen, SQLCHAR *dst, size_t dlen);
size_t chars_to_wchars2(const SQLCHAR *src, size_t slen, SQLWCHAR *dst, size_t dlen);
#endif // _TODBC_UTIL_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册