本文整理汇总了C++中iconv函数的典型用法代码示例。如果您正苦于以下问题:C++ iconv函数的具体用法?C++ iconv怎么用?C++ iconv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了iconv函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: vmi_convert_str_encoding
status_t
vmi_convert_str_encoding(
const unicode_string_t *in,
unicode_string_t *out,
const char *outencoding)
{
iconv_t cd = 0;
size_t iconv_val = 0;
if (!in || !out)
return VMI_FAILURE;
size_t inlen = in->length;
size_t outlen = 2 * (inlen + 1);
char *incurr = (char*)in->contents;
memset(out, 0, sizeof(*out));
out->contents = safe_malloc(outlen);
memset(out->contents, 0, outlen);
char *outstart = (char*)out->contents;
char *outcurr = (char*)out->contents;
out->encoding = outencoding;
cd = iconv_open(out->encoding, in->encoding); // outset, inset
if ((iconv_t) (-1) == cd) { // init failure
if (EINVAL == errno) {
dbprint(VMI_DEBUG_READ, "%s: conversion from '%s' to '%s' not supported\n",
__FUNCTION__, in->encoding, out->encoding);
} else {
dbprint(VMI_DEBUG_READ, "%s: Initializiation failure: %s\n", __FUNCTION__,
strerror(errno));
} // if-else
goto fail;
} // if
// init success
iconv_val = iconv(cd, &incurr, &inlen, &outcurr, &outlen);
if ((size_t) - 1 == iconv_val) {
dbprint(VMI_DEBUG_READ, "%s: iconv failed, in string '%s' length %zu, "
"out string '%s' length %zu\n", __FUNCTION__,
in->contents, in->length, out->contents, outlen);
switch (errno) {
case EILSEQ:
dbprint(VMI_DEBUG_READ, "invalid multibyte sequence");
break;
case EINVAL:
dbprint(VMI_DEBUG_READ, "incomplete multibyte sequence");
break;
case E2BIG:
dbprint(VMI_DEBUG_READ, "no more room");
break;
default:
dbprint(VMI_DEBUG_READ, "error: %s\n", strerror(errno));
break;
} // switch
goto fail;
} // if failure
// conversion success
out->length = (size_t) (outcurr - outstart);
(void) iconv_close(cd);
return VMI_SUCCESS;
fail:
if (out->contents) {
free(out->contents);
}
// make failure really obvious
memset(out, 0, sizeof(*out));
if ((iconv_t) (-1) != cd) { // init succeeded
(void) iconv_close(cd);
} // if
return VMI_FAILURE;
}
开发者ID:bentau,项目名称:libvmi,代码行数:80,代码来源:convenience.c
示例2: encoding
/**************************************************************************
Convert string from local encoding (8 bit char) to
display encoding (16 bit unicode) and resut put in pToUniString.
if pToUniString == NULL then resulting string will be allocate automaticaly.
'ulength' give real sizeof 'pToUniString' array.
Function return (Uint16 *) pointer to (new) pToUniString.
**************************************************************************/
Uint16 *convertcopy_to_utf16(Uint16 *pToUniString, size_t ulength,
const char *pFromString)
{
/* Start Parametrs */
const char *pTocode = get_display_encoding();
const char *pFromcode = get_internal_encoding();
const char *pStart = pFromString;
size_t length = strlen(pFromString) + 1;
Uint16 *pResult = pToUniString;
/* ===== */
iconv_t cd = iconv_open(pTocode, pFromcode);
if (cd == (iconv_t) (-1)) {
if (errno != EINVAL) {
return pToUniString;
}
}
if (!pResult) {
/* From 8 bit code to UTF-16 (16 bit code) */
ulength = length * 2;
pResult = fc_calloc(1, ulength);
}
iconv(cd, NULL, NULL, NULL, NULL); /* return to the initial state */
/* Do the conversion for real. */
{
const char *pInptr = pStart;
size_t Insize = length;
char *pOutptr = (char *)pResult;
size_t Outsize = ulength;
while (Insize > 0 && Outsize > 0) {
size_t Res =
iconv(cd, (ICONV_CONST char **) &pInptr, &Insize, &pOutptr, &Outsize);
if (Res == (size_t) (-1)) {
if (errno == EINVAL) {
break;
} else {
int saved_errno = errno;
iconv_close(cd);
errno = saved_errno;
if (!pToUniString) {
FC_FREE(pResult);
}
return pToUniString;
}
}
}
{
size_t Res = iconv(cd, NULL, NULL, &pOutptr, &Outsize);
if (Res == (size_t) (-1)) {
int saved_errno = errno;
iconv_close(cd);
errno = saved_errno;
if (!pToUniString) {
FC_FREE(pResult);
}
return pToUniString;
}
}
}
iconv_close(cd);
return (Uint16 *) pResult;
}
开发者ID:longturn,项目名称:freeciv-S2_5,代码行数:84,代码来源:gui_iconv.c
示例3: iconv_convert
/**
* iconv_convert : convert a string, using the current codeset
* return: a malloc'd string with the converted result
*/
char *
iconv_convert (const char *input)
{
//#if HAVE_ICONV
#if 0 // ASUS EXT
size_t inputsize = strlen (input) + 1;
size_t dummy = 0;
size_t length = 0;
char *result;
char *inptr, *outptr;
size_t insize, outsize;
/* conversion not necessary. save our time. */
if (!cd)
return strdup (input);
/* Determine the length we need. */
iconv (cd, NULL, NULL, NULL, &dummy);
{
static char tmpbuf[BUFSIZ];
inptr = (char*) input;
insize = inputsize;
while (insize > 0)
{
outptr = tmpbuf;
outsize = BUFSIZ;
if (iconv (cd, &inptr, &insize, &outptr, &outsize) == (size_t) (-1))
{
/**
* if error is EINVAL or EILSEQ, conversion must be stoped,
* but if it is E2BIG (not enough space in buffer), we just loop again
*/
if( errno != E2BIG)
{
perror ("error iconv");
return NULL;
}
}
length += outptr - tmpbuf;
}
outptr = tmpbuf;
outsize = BUFSIZ;
if (iconv (cd, NULL, NULL, &outptr, &outsize) == (size_t) (-1))
{
perror ("error iconv");
return NULL;
}
length += outptr - tmpbuf;
}
/* length determined, allocate result space */
if ((result = (char*) malloc (length * sizeof (char))) == NULL)
{
perror ("error malloc");
return NULL;
}
/* Do the conversion for real. */
iconv (cd, NULL, NULL, NULL, &dummy);
{
inptr = (char*) input;
insize = inputsize;
outptr = result;
outsize = length;
while (insize > 0)
{
if (iconv (cd, &inptr, &insize, &outptr, &outsize) == (size_t) (-1))
{
if (errno != E2BIG)
{
perror ("error iconv");
free (result);
return NULL;
}
}
}
if (iconv (cd, NULL, NULL, &outptr, &outsize) == (size_t) (-1))
{
perror ("error iconv");
free (result);
return NULL;
}
if (outsize != 0)
abort ();
}
return result;
#else
return strdup (input);
#endif
}
开发者ID:GunioRobot,项目名称:rtn56u,代码行数:97,代码来源:util_iconv.c
示例4: eina_str_convert
EAPI char *
eina_str_convert(const char *enc_from, const char *enc_to, const char *text)
{
iconv_t ic;
char *new_txt, *outp;
const char *inp;
size_t inb, outb, outlen, tob, outalloc;
if (!text)
return NULL;
ic = iconv_open(enc_to, enc_from);
if (ic == (iconv_t)(-1))
return NULL;
new_txt = malloc(64);
inb = strlen(text);
outb = 64;
inp = text;
outp = new_txt;
outalloc = 64;
outlen = 0;
for (;; )
{
size_t count;
tob = outb;
#ifdef __FreeBSD__
count = iconv(ic, &inp, &inb, &outp, &outb);
#else
count = iconv(ic, (char **)&inp, &inb, &outp, &outb);
#endif
outlen += tob - outb;
if (count == (size_t)(-1))
{
if (errno == E2BIG)
{
new_txt = realloc(new_txt, outalloc + 64);
outp = new_txt + outlen;
outalloc += 64;
outb += 64;
}
else
{
if (new_txt)
free(new_txt);
new_txt = NULL;
break;
}
}
if (inb == 0)
{
if (outalloc == outlen)
new_txt = realloc(new_txt, outalloc + 1);
new_txt[outlen] = 0;
break;
}
}
iconv_close(ic);
return new_txt;
}
开发者ID:FlorentRevest,项目名称:EFL,代码行数:65,代码来源:eina_str.c
示例5: while
bool CharsetConverter::Convert(const std::string& sSource, std::string& sResult)
{
sResult.erase();
#ifndef WITH_ICONV
return false;
#else
if (m_pIconv == reinterpret_cast<iconv_t>(-1))
{
return false;
}
const static size_t nOutBuffSize = 256;
char szOutBuff[nOutBuffSize];
size_t nOut = nOutBuffSize;
#if defined _LIBICONV_VERSION && _LIBICONV_VERSION >= 0x010D
const char* szSrc = sSource.data();
const char* szSrcCurr = NULL;
#else
char* szSrc = const_cast<char*>(sSource.data());
char* szSrcCurr = NULL;
#endif
size_t nSrcSize = sSource.size();
size_t nSrcPos = 0;
size_t nSrcCurrSize = 0;
size_t nSrcCurrSizeRead = 0;
char* szOutTmp = NULL;
size_t nRet = static_cast<size_t>(-1);
while (nSrcPos != nSrcSize)
{
nSrcCurrSize = Min(nOutBuffSize / 4, nSrcSize - nSrcPos);
nSrcCurrSizeRead = nSrcCurrSize;
szSrcCurr = szSrc + nSrcPos;
szOutTmp = szOutBuff;
#ifndef sun
nRet = iconv(m_pIconv, &szSrcCurr, &nSrcCurrSizeRead, &szOutTmp, &nOut);
#else
nRet = iconv(m_pIconv, const_cast<const char **>(&szSrcCurr), &nSrcCurrSizeRead, &szOutTmp, &nOut);
#endif
if (nRet == static_cast<size_t>(-1))
{
switch (errno)
{
case EINVAL:
break;
case EILSEQ:
--nSrcCurrSizeRead;
break;
default:
LogError() << "Error iconv: " << Error::GetLastErrorStr();
iconv_close(m_pIconv);
return false;
}
}
sResult.append(szOutBuff, nOutBuffSize - nOut);
nOut = nOutBuffSize;
nSrcPos += nSrcCurrSize - nSrcCurrSizeRead;
}
return true;
#endif
}
开发者ID:AmesianX,项目名称:staff,代码行数:66,代码来源:CharsetConverter.cpp
示例6: mdb_ascii2unicode
/*
* This function is used in writing text data to an MDB table.
* If slen is 0, strlen will be used to calculate src's length.
*/
int
mdb_ascii2unicode(MdbHandle *mdb, char *src, size_t slen, char *dest, size_t dlen)
{
size_t len_in, len_out;
char *in_ptr, *out_ptr;
if ((!src) || (!dest) || (!dlen))
return 0;
in_ptr = src;
out_ptr = dest;
len_in = (slen) ? slen : strlen(in_ptr);
len_out = dlen;
#ifdef HAVE_ICONV
iconv(mdb->iconv_out, &in_ptr, &len_in, &out_ptr, &len_out);
//printf("len_in %d len_out %d\n", len_in, len_out);
dlen -= len_out;
#else
if (IS_JET3(mdb)) {
dlen = MIN(len_in, len_out);
strncpy(out_ptr, in_ptr, dlen);
} else {
unsigned int i;
slen = MIN(len_in, len_out/2);
dlen = slen*2;
for (i=0; i<slen; i++) {
out_ptr[i*2] = in_ptr[i];
out_ptr[i*2+1] = 0;
}
}
#endif
/* Unicode Compression */
if(IS_JET4(mdb) && (dlen>4)) {
unsigned char *tmp = g_malloc(dlen);
unsigned int tptr = 0, dptr = 0;
int comp = 1;
tmp[tptr++] = 0xff;
tmp[tptr++] = 0xfe;
while((dptr < dlen) && (tptr < dlen)) {
if (((dest[dptr+1]==0) && (comp==0))
|| ((dest[dptr+1]!=0) && (comp==1))) {
/* switch encoding mode */
tmp[tptr++] = 0;
comp = (comp) ? 0 : 1;
} else if (dest[dptr]==0) {
/* this string cannot be compressed */
tptr = dlen;
} else if (comp==1) {
/* encode compressed character */
tmp[tptr++] = dest[dptr];
dptr += 2;
} else if (tptr+1 < dlen) {
/* encode uncompressed character */
tmp[tptr++] = dest[dptr];
tmp[tptr++] = dest[dptr+1];
dptr += 2;
} else {
/* could not encode uncompressed character
* into single byte */
tptr = dlen;
}
}
if (tptr < dlen) {
memcpy(dest, tmp, tptr);
dlen = tptr;
}
g_free(tmp);
}
return dlen;
}
开发者ID:Byclosure,项目名称:mdbtools,代码行数:78,代码来源:iconv.c
示例7: pinentry_local_to_utf8
/* Convert TEXT which is encoded according to LC_CTYPE to UTF-8. With
SECURE set to true, use secure memory for the returned buffer.
Return NULL on error. */
char *
pinentry_local_to_utf8 (char *lc_ctype, char *text, int secure)
{
char *old_ctype;
char *source_encoding;
iconv_t cd;
const char *input = text;
size_t input_len = strlen (text) + 1;
char *output;
size_t output_len;
char *output_buf;
size_t processed;
/* If no locale setting could be determined, simply copy the
string. */
if (!lc_ctype)
{
fprintf (stderr, "%s: no LC_CTYPE known - assuming UTF-8\n",
this_pgmname);
output_buf = secure? secmem_malloc (input_len) : malloc (input_len);
if (output_buf)
strcpy (output_buf, input);
return output_buf;
}
old_ctype = strdup (setlocale (LC_CTYPE, NULL));
if (!old_ctype)
return NULL;
setlocale (LC_CTYPE, lc_ctype);
source_encoding = nl_langinfo (CODESET);
setlocale (LC_CTYPE, old_ctype);
free (old_ctype);
/* This is overkill, but simplifies the iconv invocation greatly. */
output_len = input_len * MB_LEN_MAX;
output_buf = output = secure? secmem_malloc (output_len):malloc (output_len);
if (!output)
return NULL;
cd = iconv_open ("UTF-8", source_encoding);
if (cd == (iconv_t) -1)
{
fprintf (stderr, "%s: can't convert from %s to UTF-8: %s\n",
this_pgmname, source_encoding? source_encoding : "?",
strerror (errno));
if (secure)
secmem_free (output_buf);
else
free (output_buf);
return NULL;
}
processed = iconv (cd, &input, &input_len, &output, &output_len);
iconv_close (cd);
if (processed == (size_t) -1 || input_len)
{
fprintf (stderr, "%s: error converting from %s to UTF-8: %s\n",
this_pgmname, source_encoding? source_encoding : "?",
strerror (errno));
if (secure)
secmem_free (output_buf);
else
free (output_buf);
return NULL;
}
return output_buf;
}
开发者ID:JThramer,项目名称:pinentry-mac,代码行数:69,代码来源:pinentry.c
示例8: file_write_using_iconv
static intptr_t
file_write_using_iconv(struct OMRPortLibrary *portLibrary, intptr_t fd, const char *buf, intptr_t nbytes)
{
intptr_t result = 0;
char stackBuf[512];
char *bufStart = NULL;
uintptr_t outBufLen = sizeof(stackBuf);
iconv_t converter = J9VM_INVALID_ICONV_DESCRIPTOR;
size_t inbytesleft = 0;
size_t outbytesleft = 0;
char *inbuf = NULL;
char *outbuf = NULL;
intptr_t bytesToWrite = 0;
#ifdef J9ZOS390
/* LIR 1280 (z/OS only) - every failed call to iconv_open() is recorded on the operator console, so don't retry */
if (FALSE == PPG_file_text_iconv_open_failed) {
/* iconv_get is not an a2e function, so we need to pass it honest-to-goodness EBCDIC strings */
#pragma convlit(suspend)
#endif
#ifndef OMRZTPF
converter = iconv_get(portLibrary, J9FILETEXT_ICONV_DESCRIPTOR, nl_langinfo(CODESET), "UTF-8");
#else
converter = iconv_get(portLibrary, J9FILETEXT_ICONV_DESCRIPTOR, "IBM1047", "ISO8859-1" );
#endif
#ifdef J9ZOS390
#pragma convlit(resume)
if (J9VM_INVALID_ICONV_DESCRIPTOR == converter) {
PPG_file_text_iconv_open_failed = TRUE;
}
}
#endif
if (J9VM_INVALID_ICONV_DESCRIPTOR == converter) {
/* no converter available for this code set. Just dump the UTF-8 chars */
result = portLibrary->file_write(portLibrary, fd, (void *)buf, nbytes);
return (result == nbytes) ? 0 : result;
}
inbuf = (char *)buf; /* for some reason this argument isn't const */
outbuf = bufStart = stackBuf;
inbytesleft = nbytes;
outbytesleft = sizeof(stackBuf);
while ((size_t)-1 == iconv(converter, &inbuf, &inbytesleft, &outbuf, &outbytesleft)) {
int tmp_errno = errno;
if (inbytesleft == 0) {
break;
}
if ((outbytesleft == 0) || (tmp_errno == E2BIG)) {
/* input conversion stopped due to lack of space in the output buffer */
if (growBuffer(portLibrary, stackBuf, &bufStart, &outbuf, &outbytesleft, &outBufLen) < 0) {
/* failed to grow buffer, just output what we've got so far */
break;
}
} else if (tmp_errno == EILSEQ) {
/* input conversion stopped due to an input byte that does not belong to the input code set */
const char *unicodeFormat = "\\u%04x";
#define J9FILETEXT_ESCAPE_STR_SIZE 6 /* max size of unicode format */
char escapedStr[J9FILETEXT_ESCAPE_STR_SIZE];
char *escapedStrStart = escapedStr;
uint16_t unicodeC = 0;
size_t escapedLength = 0;
size_t utf8Length = decodeUTF8CharN((const uint8_t *)inbuf, &unicodeC, inbytesleft);
if (utf8Length == 0) {
/* invalid encoding, including 4-byte UTF-8 */
utf8Length = 1;
escapedLength = 1;
escapedStr[0] = '?';
} else {
escapedLength = portLibrary->str_printf(portLibrary, escapedStr, J9FILETEXT_ESCAPE_STR_SIZE, unicodeFormat, (uintptr_t)unicodeC);
}
inbytesleft -= utf8Length;
inbuf += utf8Length;
if ((size_t)-1 == iconv(converter, &escapedStrStart, &escapedLength, &outbuf, &outbytesleft)) {
/* not handling EILSEQ here because:
* 1. we can't do much if iconv() fails to convert ascii.
* 2. inbuf and inbytesleft have been explicitly updated so the while loop will get terminated after converting the rest of the characters.
*/
tmp_errno = errno;
/* if the remaining outbuf is too small, then grow it before storing Unicode string representation */
if (tmp_errno == E2BIG) {
if (growBuffer(portLibrary, stackBuf, &bufStart, &outbuf, &outbytesleft, &outBufLen) < 0) {
/* failed to grow buffer, just output what we've got so far */
break;
}
//.........这里部分代码省略.........
开发者ID:LinHu2016,项目名称:omr,代码行数:101,代码来源:omrfiletext.c
示例9: setlocale
char *mdx_make_sjis_to_syscharset(char* str) {
#if 0
iconv_t fd = 0;
size_t len = 0;
char* result = NULL;
char* rr = NULL;
size_t remain=0, oremain=0;
char* cur = NULL;
const char* loc = (const char *)"UTF-8";
unsigned char src[3];
unsigned char dst[7];
int ptr = 0;
size_t sl=0;
size_t dl=0;
cur = setlocale(LC_CTYPE, "");
if (!cur) {
goto error_end;
}
if (strstr(cur, "eucJP")) {
loc = (const char *)"EUC-JP";
}
fd = iconv_open(loc, "SHIFT-JIS");
if (fd<0) {
goto error_end;
}
/* enough for store utf8 */
remain = strlen(str);
if (remain==0) {
goto error_end;
}
oremain = remain*6;
result = (char *)malloc(sizeof(char)*oremain);
if (!result) {
goto error_end;
}
memset((void *)result, 0, oremain);
rr = result;
ptr=0;
/* to omit X68k specific chars, process charconv one by one */
do {
char *sp, *dp;
unsigned char c = 0;
memset((void *)src, 0, sizeof(src));
memset((void *)dst, 0, sizeof(dst));
oremain = 0;
c = src[0] = (unsigned char)str[ptr++];
sl=1;
if (!c) break;
if (c==0x80 || (c>=0x81 && c<=0x84) || (c>=0x88 && c<=0x9f) ||
(c>=0xe0 && c<=0xea) || (c>=0xf0 && c<=0xf3) || c==0xf6) {
src[1] = (unsigned char)str[ptr++];
if (!src[1]) {
strcat(result, ".");
break;
}
sl++;
}
sp = (char *)src;
dp = (char *)dst;
dl = 7;
len = iconv(fd, (char **)&sp, &sl, (char **)&dp, &dl);
if (len==(size_t)(-1)) {
strcat(result, ".");
} else {
strcat(result, (char *)dst);
}
} while (1);
iconv_close(fd);
return result;
error_end:
if (result) {
free(result);
}
if (fd>=0) {
iconv_close(fd);
}
#endif
return strdup(str);
}
开发者ID:Kinglions,项目名称:modizer,代码行数:89,代码来源:mdxfile.c
示例10: UTF8ToUCS4
int UTF8ToUCS4(char*inbuf,unsigned int len_src,char*outbuf,unsigned int lenout){iconv_t conv;conv=iconv_open("UCS-4LE","UTF-8");iconv(conv,NULL,NULL,NULL,NULL);int ret;ret=iconv(conv,(const char**)&inbuf,&len_src,&outbuf,&lenout);iconv_close(conv);return ret;}int Draw_string_line(char*_filename,int _font_size,int _y_offset){char*ttf_path="brlcd.ttf";unsigned int ucode_array[50];int array_counter=0;int max_font_height=0;int max_font_width=0;static int _DoinitLib=1;FT_UInt glyph_index;int x_offset=0;int y_offset=_y_offset;const int oled_width=128;const int oled_height=64;char bufout[512];unsigned int len_src=strlen(_filename);unsigned int*out_text;
开发者ID:joaonunesk,项目名称:PR30,代码行数:1,代码来源:OLED_DRAW_API.c
示例11: htp_transcode_bstr
/**
* Transcode one bstr.
*
* @param[in] cd
* @param[in] input
* @param[in] output
*/
int htp_transcode_bstr(iconv_t cd, bstr *input, bstr **output) {
// Reset conversion state for every new string
iconv(cd, NULL, 0, NULL, 0);
bstr_builder_t *bb = NULL;
const size_t buflen = 10;
unsigned char *buf = malloc(buflen);
if (buf == NULL) {
return HTP_ERROR;
}
const char *inbuf = (const char *)bstr_ptr(input);
size_t inleft = bstr_len(input);
char *outbuf = (char *)buf;
size_t outleft = buflen;
int loop = 1;
while (loop) {
loop = 0;
if (iconv(cd, (ICONV_CONST char **)&inbuf, &inleft, (char **)&outbuf, &outleft) == (size_t) - 1) {
if (errno == E2BIG) {
// Create bstr builder on-demand
if (bb == NULL) {
bb = bstr_builder_create();
if (bb == NULL) {
free(buf);
return HTP_ERROR;
}
}
// The output buffer is full
bstr_builder_append_mem(bb, buf, buflen - outleft);
outbuf = (char *)buf;
outleft = buflen;
// Continue in the loop, as there's more work to do
loop = 1;
} else {
// Error
if (bb != NULL) bstr_builder_destroy(bb);
free(buf);
return HTP_ERROR;
}
}
}
if (bb != NULL) {
bstr_builder_append_mem(bb, buf, buflen - outleft);
*output = bstr_builder_to_str(bb);
bstr_builder_destroy(bb);
if (*output == NULL) {
free(buf);
return HTP_ERROR;
}
} else {
*output = bstr_dup_mem(buf, buflen - outleft);
if (*output == NULL) {
free(buf);
return HTP_ERROR;
}
}
free(buf);
return HTP_OK;
}
开发者ID:yuecailing,项目名称:rep_test,代码行数:76,代码来源:htp_transcoder.c
示例12: Curl_convert_from_utf8
/*
* Curl_convert_from_utf8() is an internal function for performing UTF-8
* conversions on non-ASCII platforms.
*/
CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
char *buffer, size_t length)
{
CURLcode rc;
if(data->set.convfromutf8) {
/* use translation callback */
rc = data->set.convfromutf8(buffer, length);
if(rc != CURLE_OK) {
failf(data,
"CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
(int)rc, curl_easy_strerror(rc));
}
return rc;
}
else {
#ifdef HAVE_ICONV
/* do the translation ourselves */
const char *input_ptr;
char *output_ptr;
size_t in_bytes, out_bytes, rc;
int error;
/* open an iconv conversion descriptor if necessary */
if(data->utf8_cd == (iconv_t)-1) {
data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
CURL_ICONV_CODESET_FOR_UTF8);
if(data->utf8_cd == (iconv_t)-1) {
error = ERRNO;
failf(data,
"The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
CURL_ICONV_CODESET_OF_HOST,
CURL_ICONV_CODESET_FOR_UTF8,
error, strerror(error));
return CURLE_CONV_FAILED;
}
}
/* call iconv */
input_ptr = output_ptr = buffer;
in_bytes = out_bytes = length;
rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
&output_ptr, &out_bytes);
if((rc == ICONV_ERROR) || (in_bytes != 0)) {
error = ERRNO;
failf(data,
"The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
error, strerror(error));
return CURLE_CONV_FAILED;
}
if(output_ptr < input_ptr) {
/* null terminate the now shorter output string */
*output_ptr = 0x00;
}
#else
failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
return CURLE_CONV_REQD;
#endif /* HAVE_ICONV */
}
return CURLE_OK;
}
开发者ID:mcodegeeks,项目名称:OpenKODE-Framework,代码行数:65,代码来源:non-ascii.c
示例13: main
int
main (void)
{
char name[] = "/tmp/widetext.out.XXXXXX";
char mbbuf[SIZE];
char mb2buf[SIZE];
wchar_t wcbuf[SIZE];
wchar_t wc2buf[SIZE];
size_t mbsize;
size_t wcsize;
int fd;
FILE *fp;
size_t n;
int res;
int status = 0;
wchar_t *wcp;
setlocale (LC_ALL, "de_DE.UTF-8");
printf ("locale used: %s\n\n", setlocale (LC_ALL, NULL));
/* Read the file into memory. */
mbsize = fread (mbbuf, 1, SIZE, stdin);
if (mbsize == 0)
{
printf ("%Zd: cannot read input file from standard input: %m\n",
__LINE__);
exit (1);
}
printf ("INFO: input file has %Zd bytes\n", mbsize);
/* First convert the text to wide characters. We use iconv here. */
{
iconv_t cd;
char *inbuf = mbbuf;
size_t inleft = mbsize;
char *outbuf = (char *) wcbuf;
size_t outleft = sizeof (wcbuf);
size_t nonr;
cd = iconv_open ("WCHAR_T", "UTF-8");
if (cd == (iconv_t) -1)
{
printf ("%Zd: cannot get iconv descriptor for conversion to UCS4\n",
__LINE__);
exit (1);
}
/* We must need only one call and there must be no losses. */
nonr = iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
if (nonr != 0 && nonr != (size_t) -1)
{
printf ("%Zd: iconv performed %Zd nonreversible conversions\n",
__LINE__, nonr);
exit (1);
}
if ((size_t) nonr == -1 )
{
printf ("\
%Zd: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
__LINE__, nonr, inleft, outleft);
exit (1);
}
if (inleft != 0)
{
printf ("%Zd: iconv didn't convert all input\n", __LINE__);
exit (1);
}
iconv_close (cd);
if ((sizeof (wcbuf) - outleft) % sizeof (wchar_t) != 0)
{
printf ("%Zd: iconv converted not complete wchar_t\n", __LINE__);
exit (1);
}
wcsize = (sizeof (wcbuf) - outleft) / sizeof (wchar_t);
assert (wcsize + 1 <= SIZE);
}
开发者ID:BackupTheBerlios,项目名称:wl530g-svn,代码行数:82,代码来源:tst-widetext.c
示例14: ngx_http_do_iconv
static ngx_int_t
ngx_http_do_iconv(ngx_http_request_t *r, ngx_chain_t **c, void *data,
size_t len, u_char *from, u_char *to, size_t *conved_bytes,
size_t *rest_bytes)
{
iconv_t cd;
ngx_chain_t *cl, *chain, **ll;
ngx_buf_t *b;
size_t cv, rest, rv;
cv = 0;
dd("iconv from=%s, to=%s", from, to);
cd = iconv_open((const char *) to, (const char *) from);
if (cd == (iconv_t) -1) {
dd("iconv open error");
return NGX_ERROR;
}
dd("len=%zu, iconv_buf_size=%zu", len, iconv_buf_size);
ll = &chain;
conv_begin:
while (len) {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
iconv_close(cd);
return NGX_ERROR;
}
/* --- b->temporary--- */
b = ngx_create_temp_buf(r->pool, iconv_buf_size);
if (b == NULL) {
iconv_close(cd);
return NGX_ERROR;
}
cl->buf = b;
rest = iconv_buf_size;
do {
rv = iconv(cd, (void *) &data, &len, (void *) &b->last, &rest);
if (rv == (size_t) -1) {
if (errno == EINVAL) {
cv += iconv_buf_size - rest;
dd("iconv error:EINVAL,len=%d cv=%d rest=%d", (int) len,
(int) cv, (int) rest);
goto conv_done;
}
if (errno == E2BIG) {
dd("E2BIG");
/* E2BIG error is not considered*/
break;
}
if (errno == EILSEQ) {
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
"iconv sees invalid character sequence "
"(EILSEQ)");
if (len >= 1) {
if (rest == 0) {
dd("EILSEQ:rest=0");
cv += iconv_buf_size - rest;
*ll = cl;
ll = &cl->next;
goto conv_begin;
} else {
dd("EILSEQ:rest=%d", (int)rest);
len--;
data = (u_char *)data + 1;
/* replace illegal character to '?' */
*b->last = '?';
b->last = (u_char *)b->last + 1;
rest--;
}
} else {
goto conv_done;
}
}
}
} while (rv == (size_t) -1 && errno == EILSEQ);
/* E2BIG error is not considered*/
/* this code can work but stops when meet illegal encoding */
/*
if (rv == (size_t) -1) {
if (errno == EILSEQ) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"iconv error:EILSEQ");
iconv_close(cd);
return NGX_ERROR;
}
//.........这里部分代码省略.........
开发者ID:carriercomm,项目名称:jip-auth,代码行数:101,代码来源:ngx_http_iconv_module.c
示例15: str_cd_iconv
char *
str_cd_iconv (const char *src, iconv_t cd)
{
/* For most encodings, a trailing NUL byte in the input will be converted
to a trailing NUL byte in the output. But not for UTF-7. So that this
function is usable for UTF-7, we have to exclude the NUL byte from the
conversion and add it by hand afterwards. */
# if !defined _LIBICONV_VERSION && !defined __GLIBC__
/* Irix iconv() inserts a NUL byte if it cannot convert.
NetBSD iconv() inserts a question mark if it cannot convert.
Only GNU libiconv and GNU libc are known to prefer to fail rather
than doing a lossy conversion. For other iconv() implementations,
we have to look at the number of irreversible conversions returned;
but this information is lost when iconv() returns for an E2BIG reason.
Therefore we cannot use the second, faster algorithm. */
char *result = NULL;
size_t length = 0;
int retval = mem_cd_iconv (src, strlen (src), cd, &result, &length);
char *final_result;
if (retval < 0)
{
if (result != NULL)
abort ();
return NULL;
}
/* Add the terminating NUL byte. */
final_result =
(result != NULL ? realloc (result, length + 1) : malloc (length + 1));
if (final_result == NULL)
{
if (result != NULL)
free (result);
errno = ENOMEM;
return NULL;
}
final_result[length] = '\0';
return final_result;
# else
/* This algorithm is likely faster than the one above. But it may produce
iconv() returns for an E2BIG reason, when the output size guess is too
small. Therefore it can only be used when we don't need the number of
irreversible conversions performed. */
char *result;
size_t result_size;
size_t length;
const char *inptr = src;
size_t inbytes_remaining = strlen (src);
/* Make a guess for the worst-case output size, in order to avoid a
realloc. It's OK if the guess is wrong as long as it is not zero and
doesn't lead to an integer overflow. */
result_size = inbytes_remaining;
{
size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
if (result_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
result_size *= MB_LEN_MAX;
}
result_size += 1; /* for the terminating NUL */
result = (char *) malloc (result_size);
if (result == NULL)
{
errno = ENOMEM;
return NULL;
}
/* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
# if defined _LIBICONV_VERSION \
|| !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
/* Set to the initial state. */
iconv (cd, NULL, NULL, NULL, NULL);
# endif
/* Do the conversion. */
{
char *outptr = result;
size_t outbytes_remaining = result_size - 1;
for (;;)
{
/* Here inptr + inbytes_remaining = src + strlen (src),
outptr + outbytes_remaining = result + result_size - 1. */
size_t res = iconv (cd,
(ICONV_CONST char **) &inptr, &inbytes_remaining,
&outptr, &outbytes_remaining);
if (res == (size_t)(-1))
{
if (errno == EINVAL)
break;
else if (errno == E2BIG)
{
size_t used = outptr - result;
size_t newsize = result_size * 2;
char *newresult;
//.........这里部分代码省略.........
开发者ID:4solo,项目名称:cs35,代码行数:101,代码来源:striconv.c
示例16: mem_cd_iconv
int
mem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
char **resultp, size_t *lengthp)
{
# define tmpbufsize 4096
size_t length;
char *result;
/* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
# if defined _LIBICONV_VERSION \
|| !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
/* Set to the initial state. */
iconv (cd, NULL, NULL, NULL, NULL);
# endif
/* Determine the length we need. */
{
size_t count = 0;
/* The alignment is needed when converting e.g. to glibc's WCHAR_T or
libiconv's UCS-4-INTERNAL encoding. */
union { unsigned int align; char buf[tmpbufsize]; } tmp;
# define tmpbuf tmp.buf
const char *inptr = src;
size_t insize = srclen;
while (insize > 0)
{
char *outptr = tmpbuf;
size_t outsize = tmpbufsize;
size_t res = iconv (cd,
(ICONV_CONST char **) &inptr, &insize,
&outptr, &outsize);
if (res == (size_t)(-1))
{
if (errno == E2BIG)
;
else if (errno == EINVAL)
break;
else
return -1;
}
# if !defined _LIBICONV_VERSION && !defined __GLIBC__
/* Irix iconv() inserts a NUL byte if it cannot convert.
NetBSD iconv() inserts a question mark if it cannot convert.
Only GNU libiconv and GNU libc are known to prefer to fail rather
than doing a lossy conversion. */
else if (res > 0)
{
errno = EILSEQ;
return -1;
}
# endif
count += outptr - tmpbuf;
}
/* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
# if defined _LIBICONV_VERSION \
|| !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
{
char *outptr = tmpbuf;
size_t outsize = tmpbufsize;
size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
if (res == (size_t)(-1))
return -1;
count += outptr - tmpbuf;
}
# endif
length = count;
# undef tmpbuf
}
if (length == 0)
{
*lengthp = 0;
return 0;
}
if (*resultp != NULL && *lengthp >= length)
result = *resultp;
else
{
result = (char *) malloc (length);
if (result == NULL)
{
errno = ENOMEM;
return -1;
}
}
/* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
# if defined _LIBICONV_VERSION \
|| !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
/* Return to the initial state. */
iconv (cd, NULL, NULL, NULL, NULL);
# endif
/* Do the conversion for real. */
{
const char *inptr = src;
size_t insize = srclen;
//.........这里部分代码省略.........
开发者ID:4solo,项目名称:cs35,代码行数:101,代码来源:striconv.c
示例17: main
int
main ()
{
#if HAVE_ICONV
/* Assume that iconv() supports at least the encoding UTF-8. */
/* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]". */
/* Test conversion from UTF-8 to UTF-16BE with no errors. */
{
static const char input[] =
"Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
static const char expected[] =
"\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
iconv_t cd;
char buf[100];
const char *inptr;
size_t inbytesleft;
char *outptr;
size_t outbytesleft;
size_t res;
cd = iconv_open ("UTF-16BE", "UTF-8");
ASSERT (cd != (iconv_t)(-1));
inptr = input;
inbytesleft = sizeof (input) - 1;
outptr = buf;
outbytesleft = sizeof (buf);
res = iconv (cd,
(ICONV_CONST char **) &inptr, &inbytesleft,
&outptr, &outbytesleft);
ASSERT (res == 0 && inbytesleft == 0);
ASSERT (outptr == buf + (sizeof (expected) - 1));
ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
ASSERT (iconv_close (cd) == 0);
}
/* Test conversion from UTF-8 to UTF-16LE with no errors. */
{
static const char input[] =
"Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
static const char expected[] =
"J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
iconv_t cd;
char buf[100];
const char *inptr;
size_t inbytesleft;
char *outptr;
size_t outbytesleft;
size_t res;
cd = iconv_open ("UTF-16LE", "UTF-8");
ASSERT (cd != (iconv_t)(-1));
inptr = input;
inbytesleft = sizeof (input) - 1;
outptr = buf;
outbytesleft = sizeof (buf);
res = iconv (cd,
(ICONV_CONST char **) &inptr, &inbytesleft,
&outptr, &outbytesleft);
ASSERT (res == 0 && inbytesleft == 0);
ASSERT (outptr == buf + (sizeof (expected) - 1));
ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
ASSERT (iconv_close (cd) == 0);
}
/* Test conversion from UTF-8 to UTF-32BE with no errors. */
{
static const char input[] =
"Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
static const char expected[] =
"\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
iconv_t cd;
char buf[100];
const char *inptr;
size_t inbytesleft;
char *outptr;
size_t outbytesleft;
size_t res;
cd = iconv_open ("UTF-32BE", "UTF-8");
ASSERT (cd != (iconv_t)(-1));
inptr = input;
inbytesleft = sizeof (input) - 1;
outptr = buf;
outbytesleft = sizeof (buf);
res = iconv (cd,
(ICONV_CONST char **) &inptr, &inbytesleft,
&outptr, &outbytesleft);
ASSERT (res == 0 && inbytesleft == 0);
ASSERT (outptr == buf + (sizeof (expected) - 1));
ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
ASSERT (iconv_close (cd) == 0);
}
//.........这里部分代码省略.........
开发者ID:Distrotech,项目名称:gnulib,代码行数:101,代码来源:test-iconv-utf.c
示例18: mdb_unicode2ascii
/*
* This function is used in reading text data from an MDB table.
*/
int
mdb_unicode2ascii(MdbHandle *mdb, char *src
|
请发表评论