Example FairCom DB Unicode API Example
#include "ctreep.h"
typedef struct datrec { /* offset: description */
LONG delflg; /* 0: delete flag */
LONG sernum; /* 4: serial number */
TEXT idnum[8]; /* 8: ID number */
TEXT utf8str[64]; /* 16: UTF8 string */
TEXT zipcode[10]; /* 80: zipcode */
TEXT code[6]; /* 90: codes */
} DATREC;
ISEG iseg[5] =
{ /* offset, length, mode */
{80,10,REGSEG}, /* 0 */
{16, 8,REGSEG | UNCSEG}, /* 1 */
{16,24,REGSEG | UNCSEG}, /* 2 */
{80, 5,REGSEG}, /* 3 */
{ 4, 4,SRLSEG} /* 4 */
};
IIDX iidx[3] =
{
{22,0,1,0,0,2,iseg + 0},
{33,0,1,0,0,2,iseg + 2},
{ 4,0,0,0,0,1,iseg + 4}
};
IFIL ifil = {"datrec",-1,96,0,SHARED | TRNLOG,3,0, SHARED | TRNLOG,iidx};
main(int argc,char **argv)
{
ctKSEGDEF sd;
DATREC dr;
NINT rc = 0, hnd;
FILNO filno;
if ((rc = INTISAM(100,16,16)))
exit(rc);
sd.kseg_ssiz = 12; /* use up to 12 byte */
sd.kseg_type = ctKSEG_TYPE_UNICODE; /* ICU Unicode */
sd.kseg_styp = ctKSEG_STYP_UTF8; /* UTF8 source data */
sd.kseg_comp = ctKSEG_COMPU_S_TERTIARY |
ctKSEG_COMPU_N_DEFAULT;
strcpy(sd.kseg_desc,"ar"); /* arabic */
if ((hnd = PutXtdKeySegmentDef(ctKSEGapplic,0,&sd)) < 0) {
/* hnd holds the negative of the error code value */
CLISAM();
exit(-hnd);
}
/*
** else hnd holds the handle associated with the application-
** wide default for ICU Unicode extended key segments. Two of
** the segments specified in the ISEG array require ICU Unicode
** extended key segment definitions. If no other definitions
** are available upon the first use of the indexes containing
** the UNCSEG modifiers, then the application default
** definition will be used.
*/
if ((rc = CREIFIL(&ifil))) {
CLISAM();
exit(rc);
}
/*
** else the data and indexes have been successfully created. No
** extended key segment information has been added to either
** the data file or the index file at this point. If a call to
** PutXtdKeySegmentDef is now made that explicitly references the data
** file or index file (using ifil.tfilno or ifil.tfilno +
** keyno), then a special resource would be added upon
** successful completion of the PutXtdKeySegmentDef call.
*/
memset(&dr,0,sizeof(dr));
/*
** dr.delflg is now set to zero, and dr.sernum (now zero) will
** be filled-in by a call to ADDREC
*/
strcpy(dr.idnum,"1234567");
strcpy(dr.zipcode,"99999");
strcpy(dr.code,"YNM");
/*
** the ordinary ASCII fields are now set
*/
getUnicodeUTF8string(dr.utf8str,64);
/*
** this external routine will fill-in the multi-byte UTF8
** encoded Unicode string up to 64 bytes
*/
if (!TRANBEG(ctTRNLOG | ctENABLE) ||
(rc = ADDREC(ifil.tfilno,&dr)) ||
(rc = TRANEND(ctFREE))) {
/* could not successfully add data record */
CLISAM();
if (!rc)
rc = uerr_cod;
exit(rc);
}
/*
** else successfully added data record. This first use of the
** UNCSEGs will have caused the index file to be updated not
** only with the key values, but the application default
** extended key segment definition also will have been added to
** the index.
*/
CLISAM();
exit(0);
}