Example: NLS APIs
This example illustrates using NLS APIs.
/* National Language Support Code Snippet */
/* Used to demonstrate how the APIs would be run. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CWBNL.H"
#include "CWBNLCNV.H"
#include "CWBSV.H"
cwbSV_ErrHandle errhandle;
/* Return the message text associated with the top-level */
/* error identified by the error handle provided. Since */
/* all APIs that fail use the error handle, this was moved */
/* into a separate routine. */
void resolveErr(cwbSV_ErrHandle errhandle)
{
static unsigned char buf[ BUFSIZ ];
unsigned long retlen;
unsigned int rc;
if ((rc = cwbSV_GetErrText(errhandle, (char*)buf, (unsigned long) BUFSIZ, &retlen)) != CWB_OK)
printf("cwbSV_GetErrText() Service API failed with return code 0x%x.\n", rc);
else
printf("%s\n", (char *) buf);
}
void main(void){
/* define some variables
-------------------- */
int SVrc = 0;
int NLrc = 0;
char *myloadpath = "";
char *resultPtr;
char *mylang;
unsigned short resultlen;
unsigned short reqlen;
unsigned long searchhandle;
unsigned long codepage;
unsigned long trgtpage;
char *srcbuf = "Change this string";
char *trgtbuf;
unsigned long srclen;
unsigned long trgtlen;
unsigned long nmbrerrs;
unsigned long posoferr;
unsigned long rqdlen;
unsigned long ccsid;
/* Create an error message object and return a handle to */
/* it. This error handle can be passed to APIs that */
/* support it. If an error occurs, the error handle can */
/* be used to retrieve the message text associated with */
/* the API error. */
SVrc = cwbSV_CreateErrHandle(&errhandle);
if (SVrc != CWB_OK) {
printf("cwbSV_CreateErrHandle failed with return code %d.\n", SVrc);
}
/* Retreive the current language setting. */
resultlen = CWBNL_MAX_LANG_SIZE+1;
resultPtr = (char *) malloc(resultlen * sizeof(char));
NLrc = cwbNL_GetLang(myloadpath, resultPtr, resultlen, &reqlen, errhandle);
if (NLrc != CWB_OK) {
if (NLrc == CWB_BUFFER_OVERFLOW)
printf("GetLang buffer too small, recommended size %d.\n", reqlen);
resolveErr(errhandle);
}
printf("GetLang API returned %s.\n", resultPtr);
mylang = (char *) malloc(resultlen * sizeof(char));
strcpy(mylang, resultPtr);
/* Retrieve the descriptive name of a language setting. */
resultlen = CWBNL_MAX_NAME_SIZE+1;
resultPtr = (char *) realloc(resultPtr, resultlen * sizeof(char));
NLrc = cwbNL_GetLangName(mylang, resultPtr, resultlen, &reqlen, errhandle);
if (NLrc != CWB_OK) {
if (NLrc == CWB_BUFFER_OVERFLOW)
printf("GetLangName buffer too small, recommended size %d.\n", reqlen);
resolveErr(errhandle);
}
printf("GetLangName API returned %s.\n", resultPtr);
/* Return the complete path for language files. */
resultlen = CWBNL_MAX_PATH_SIZE+1;
resultPtr = (char *) realloc(resultPtr, resultlen * sizeof(char));
NLrc = cwbNL_GetLangPath(myloadpath, resultPtr, resultlen, &reqlen, errhandle);
if (NLrc != CWB_OK) {
if (NLrc == CWB_BUFFER_OVERFLOW)
printf("GetLangPath buffer too small, recommended size %d.\n", reqlen);
resolveErr(errhandle);
}
printf("GetLangPath API returned %s.\n", resultPtr);
/* Get the code page of the current process. */
NLrc = cwbNL_GetCodePage(&codepage, errhandle);
if (NLrc != CWB_OK) {
resolveErr(errhandle);
}
printf("GetCodePage API returned %u.\n", codepage);
/* Convert strings from one code page to another. This */
/* API combines three converter APIs for the default */
/* conversion. The three converter APIs it combines are: */
/* cwbNL_CreateConverterEx */
/* cwbNL_Convert */
/* cwbNL_DeleteConverter */
srclen = strlen(srcbuf) + 1;
trgtlen = srclen;
trgtpage = 437;
trgtbuf = (char *) malloc(trgtlen * sizeof(char));
printf("String to convert is %s.\n",srcbuf);
NLrc = cwbNL_ConvertCodePagesEx(codepage, trgtpage, srclen,
trgtlen, srcbuf, trgtbuf, &nmbrerrs, &posoferr, &rqdlen,
errhandle);
if (NLrc != CWB_OK) {
resolveErr(errhandle);
printf("number of errors detected is %u.\n", nmbrerrs);
printf("location of first error is %u.\n", posoferr);
}
printf("ConvertCodePagesEx API returned %s.\n", trgtbuf);
/* Map a code page to the corresponding CCSID. */
NLrc = cwbNL_CodePageToCCSID(codepage, &ccsid, errhandle);
if (NLrc != CWB_OK) {
resolveErr(errhandle);
}
printf("CodePageToCCSID returned %u.\n", ccsid);
cwbSV_DeleteErrHandle(errhandle);
}