Vi18nConvert


Call the Vi18nConvert function to convert the specified input buffer from the source character set to the destination character set.


Syntax

Vi18nError Vi18nConvert(
   void *pContext,
   char *srcCSet,
   void *inBuf,
   int inBufBytes,
   int *convertedBytes,
   char *desCSet,
   void *outBuf,
   int outBufBytes,
   int *outputBytes,
   int unicodeEndian)

Arguments

 


pContext

void*   The library context handle set by the Vi18nInit function.

srcCSet

char*   A pointer to the source character set name.

inBuf

void*   A pointer to the input buffer whose contents are to be converted.

inBufBytes

int   The length, in 8-bit bytes, of the input buffer.

convertedBytes

int*   On completion, the actual number of 8-bit bytes converted from the input buffer for conversions between UTF8 and UNICODE; for other conversions, the value is the minimum of inBufBytes and outBufBytes.

desCSet

char*   A pointer to the destination character set name.

outBuf

void*   A pointer to the output buffer, which upon completion, contains the converted characters.

outBufBytes

int   The length, in 8-bit bytes, of the output buffer.

outputBytes

int*   On completion, if greater than or equal to 0, the actual number of 8-bit bytes converted; if less than 0, the negation of the number of 8-bit bytes that overran the buffer.

unicodeEndian

int   The UNICODE endian format, which is one of the following values:

0 for little endian 1 for big endian


Returns

This function returns one of the following error codes:

Vi18nSuccess for success

 

Vi18nError_* for an error code identified in Error Codes

 

Vi18nFail for a non-specific error

 


Discussion

The Vi18nConvert function converts the input buffer (inBuf argument) from the source character set (srcCSet argument) to the destination character set (desCSet argument) and places the resulting characters in the output buffer (outBuf argument). If a character in the input buffer cannot be converted, the character is replaced by an question mark (?) in the output buffer.

You can only use the Vi18nConvert function for character set conversion within the same language. For example, you can convert 8859-1 to 850, eucjp to sjis, and vice versa; however, you cannot convert GB to BIG5, and vice versa.

Character set names are case insensitive and common variations are allowed; for example, you can use 8859-1 for iso8859-1 or ISO-8859-1. (For the list of character set names, see Charset Name Mapping Table.)

You must specify an endian format in the unicodeEndian argument when either the source character set or the destination character set is UNICODE.


Example

VdkError
vgw_createToken(VgwAppSession pVSes, VgwAppStream pVStrm,
VdkToken *ppToken, VdkTokenType type,
VdkCString szName, VdkBuffer szBuffer,
VdkInt4 nSize, VdkUint4 flags)
{
VdkError error = VdkSuccess;
VdkUint4 n = 0;
VdkCString outBuf = NULL, outNam = NULL;
VdkToken pToken = NULL;

/*****************************************
* convert field name/values charset here
*****************************************/
if (szBuffer) {
if (VdkSuccess != (error =
vgw_convRtoICharset(pVSes, (VdkCString)szBuffer,
nSize>0 ? nSize : VGW_StrLen(szBuffer), &outBuf, &n)))
goto abort;
if (outBuf) {
szBuffer = (VdkBuffer)outBuf;
nSize = n;
}
}
...
  if (outBuf) HeapFree(outBuf);
}

VdkError
vgw_convRtoICharset(VgwAppSession pVSes, VdkCString szIn, VdkUint4 nIn, VdkCString* ppszOut, VdkUint4* pnOut)
{ /*caller needs to call HeapFree on *ppszOut */
#define VRTY_UNICODE_CS "unicode"
VdkError error = VdkSuccess;
Vi18nError err;
int nAlloc, nConv = 0, nOut = 0, nEndian;
assert(pVSes);
assert(pVSes->vi18nContext);
  assert(ppszOut);
*ppszOut = NULL;
if (pnOut)
*pnOut = 0;
if( !pVSes->nReposCharSet ||
(VGW_StrEq(pVSes->pszReposCharSet, pVSes->pszIntCharSet)) )
return error;
if (!nIn)
return error;

/* unicode endian 'unicodeEndian', 0 for little endian, 1 for */
  /* big endian. Required when either the source or the         */
  /* destination charset is UNICODE.                            */
if (Vi18nGetCharsetEnum((char*)VRTY_UNICODE_CS) ==
Vi18nGetCharsetEnum((char*)pVSes->pszIntCharSet))
#ifdef _WINDOWS
nEndian = 0;
#else
nEndian = 1;
#endif
else if ((VdkUint2)Vi18nGetCharsetEnum(VRTY_UNICODE_CS) ==
pVSes->nReposCharSet)
nEndian = pVSes->nEndian;
else
#ifdef _WINDOWS
nEndian = 0;
#else
nEndian = 1;
#endif
  assert(pVSes->nRtoIFactor > 0);
  nAlloc = pVSes->nRtoIFactor * (nIn + 1);
if( !(*ppszOut = (VdkCString)HeapAlloc(nAlloc)) ){
error = VdkError_OutOfMemory;
goto abort;
}
if (Vi18nSuccess != (err =
Vi18nConvert((void*)pVSes->vi18nContext,
                 (char*)pVSes->pszReposCharSet,
             (void*)szIn,
                 (int)  nIn,
                        &nConv,
                 (char*)pVSes->pszIntCharSet,
             (void*)*ppszOut,
                        nAlloc,
                        &nOut,
                        nEndian))) {
error = VdkError_FatalError;
vgw_errPrint(pVSes, VdkMessageType_Error,
                       (VdkCString)Vi18nErrorMsg(err));
goto abort;
}
if (pnOut)
*pnOut = nOut;

abort:
return error;
}