
/*--------------------------------------------------------------------------

   %name: unicode.h %
   %version: 9 %
   %date_modified: Fri Oct 15 11:27:32 1999 %
   $Copyright:

   Copyright (c) 1989-1995 Novell, Inc.  All Rights Reserved.

   THIS WORK IS  SUBJECT  TO  U.S.  AND  INTERNATIONAL  COPYRIGHT  LAWS  AND
   TREATIES.   NO  PART  OF  THIS  WORK MAY BE  USED,  PRACTICED,  PERFORMED
   COPIED, DISTRIBUTED, REVISED, MODIFIED, TRANSLATED,  ABRIDGED, CONDENSED,
   EXPANDED,  COLLECTED,  COMPILED,  LINKED,  RECAST, TRANSFORMED OR ADAPTED
   WITHOUT THE PRIOR WRITTEN CONSENT OF NOVELL, INC. ANY USE OR EXPLOITATION
   OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE PERPETRATOR TO
   CRIMINAL AND CIVIL LIABILITY.$

--------------------------------------------------------------------------*/


#if !defined( UNICODE_H )
#define UNICODE_H

#include <stddef.h>     /* size_t */

#if !defined( NTYPES_H )
#  include "ntypes.h"
#endif

typedef unsigned short unicode;       /* Unicode data must be 16 bits   */
typedef unicode N_FAR * punicode;
typedef unicode N_FAR * N_FAR * ppunicode;

/*
 *    Error codes
 */

#define UNI_ALREADY_LOADED   -489  /* Already loaded another country or code page */
#define UNI_FUTURE_OPCODE    -490  /* Rule table has unimplimented rules*/
#define UNI_NO_SUCH_FILE     -491  /* No such file or directory */
#define UNI_TOO_MANY_FILES   -492  /* Too many files already open */
#define UNI_NO_PERMISSION    -493  /* Permission denied on file open */
#define UNI_NO_MEMORY        -494  /* Not enough memory */
#define UNI_LOAD_FAILED      -495  /* NWLoadRuleTable failed, don't know why */
#define UNI_HANDLE_BAD       -496  /* Rule table handle was bad */
#define UNI_HANDLE_MISMATCH  -497  /* Rule table handle doesn't match operation*/
#define UNI_RULES_CORRUPT    -498  /* Rule table is corrupt */
#define UNI_NO_DEFAULT       -499  /* No default rule and no 'No map' character*/
#define UNI_INSUFFICIENT_BUFFER -500
#define UNI_OPEN_FAILED      -501  /* Open failed in NWLoadRuleTable */
#define UNI_NO_LOAD_DIR      -502  /* Load directory could not be determined */
#define UNI_BAD_FILE_HANDLE  -503  /* File handle was bad */
#define UNI_READ_FAILED      -504  /* File read of rule table failed */
#define UNI_TRANS_CORRUPT    -505  /* Translator is corrupt */

#define UNI_ILLEGAL_UTF8_CHARACTER -506 /* Illegal UTF-8 character encountered */

/*
 *    unicode.h functions with no counterpart in string.h
 */

#ifdef __cplusplus
extern "C" {
#endif


N_EXTERN_LIBRARY(nint) NWInitUnicodeTables
(
   nint countryCode,
   nint codePage
);

N_EXTERN_LIBRARY(NWRCODE) NWLSetPrimaryUnicodeSearchPath
(
   const nstr N_FAR *strSearchPath
);

N_EXTERN_LIBRARY(nint) NWFreeUnicodeTables
(
   void
);

N_EXTERN_LIBRARY(nint) NWLoadRuleTable
(
   pnstr ruleTableName,           /* Name of the rule table              */
   pnptr ruleHandle               /* Where to put the rule table handle  */
);

N_EXTERN_LIBRARY(nint) NWUnloadRuleTable
(
   nptr        ruleHandle         /* Rule table handle                   */
);


#if defined N_PLAT_NLM

N_EXTERN_LIBRARY(nint)
NWLocalToUnicode                    /* Convert local to Unicode            */
(
   nptr               ruleHandle,    /* Rule table handle                   */
   punicode           dest,          /* Buffer for resulting Unicode        */
   nuint32            maxLen,        /* Size of results buffer              */
   const void N_FAR * src,           /* Buffer with source local code       */
   unicode            noMap,         /* No map character                    */
   pnuint             len,           /* Number of unicode chars in output   */
   nuint32   allowNoMapFlag    /* Flag indicating default map is allowable */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeToLocal                    /* Convert Unicode to local code       */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   nptr                  dest,       /* Buffer for resulting local code     */
   nuint32               maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   nuint8                noMap,      /* No Map character                    */
   pnuint                len,        /* Number of bytes in output           */
   nuint32   allowNoMapFlag    /* Flag indicating default map is allowable */
);

#if !defined(EXCLUDE_UNICODE_NLM_COMPATIBILITY_MACROS)
#define NWLocalToUnicode(P1,P2,P3,P4,P5,P6) NWLocalToUnicode(P1,P2,P3,P4,P5,P6, 1)
#define NWUnicodeToLocal(P1,P2,P3,P4,P5,P6) NWUnicodeToLocal(P1,P2,P3,P4,P5,P6, 1)
#endif

/* If I could make size_t be nuint32 for N_PLAT_NLM all of the functions */
/* below here could be single sourced.                                   */
#if 0
N_EXTERN_LIBRARY(nint)
NWUnicodeToCollation                /* Convert Unicode to collation        */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   punicode              dest,       /* Buffer for resulting Unicode weights*/
   nuint32               maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   unicode               noMap,      /* No map character                    */
   pnuint32              len         /* Number of unicode chars in output   */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeCompare                    /* Compare two unicode characters      */
(
   nptr           ruleHandle,       /* Rule table handle                   */
   unicode        chr1,             /* 1st character                       */
   unicode        chr2              /* 2nd character                       */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeToMonocase                 /* Convert Unicode to collation        */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   punicode              dest,       /* Buffer for resulting Unicode weights*/
   nuint32               maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   pnuint32              len         /* Number of unicode chars in output   */
);

#endif

#else   /*  not N_PLAT_NLM  */

N_EXTERN_LIBRARY(nint)
NWLocalToUnicode                    /* Convert local to Unicode            */
(
   nptr                 ruleHandle, /* Rule table handle                   */
   punicode             dest,       /* Buffer for resulting Unicode        */
   size_t               maxLen,     /* Size of results buffer              */
   const nuint8 N_FAR * src,        /* Buffer with source local code       */
   unicode              noMap,      /* No map character                    */
   size_t       N_FAR * len         /* Number of unicode chars in output   */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeToLocal                    /* Convert Unicode to local code       */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   pnuint8               dest,       /* Buffer for resulting local code     */
   size_t                maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   unsigned char         noMap,      /* No Map character                    */
   size_t        N_FAR * len         /* Number of bytes in output           */
);

#endif    /* not N_PLAT_NLM */

N_EXTERN_LIBRARY(nint)
NWUnicodeToCollation                /* Convert Unicode to collation        */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   punicode              dest,       /* Buffer for resulting Unicode weights*/
   size_t                maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   unicode               noMap,      /* No map character                    */
   size_t        N_FAR * len         /* Number of unicode chars in output   */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeCompare                    /* Compare two unicode characters      */
(
   nptr           ruleHandle,       /* Rule table handle                   */
   unicode        chr1,             /* 1st character                       */
   unicode        chr2              /* 2nd character                       */
);

N_EXTERN_LIBRARY(nint)
NWUnicodeToMonocase                 /* Convert Unicode to collation        */
(
   nptr                  ruleHandle, /* Rule table handle                   */
   punicode              dest,       /* Buffer for resulting Unicode weights*/
   size_t                maxLen,     /* Size of results buffer              */
   const unicode N_FAR * src,        /* Buffer with source Unicode          */
   size_t        N_FAR * len         /* Number of unicode chars in output   */
);


/*
 *    Functions that work with XLate Tables
 */

#if defined N_PLAT_DOS && defined N_UNI_NEW_TABLES

#  define N_UNI_LOAD_MONOCASE     0x0001
#  define N_UNI_LOAD_COLLATION    0x0002

N_EXTERN_LIBRARY(nint) NWLInitXlateTables
(
   nint codePage,
   nflag8 flags
);

N_EXTERN_LIBRARY(nint) NWLFreeXlateTables
(
   void
);

N_EXTERN_LIBRARY(nint) NWLLoadXlateTable
(
   pnstr ruleTableName,           /* Name of the rule table              */
   pnptr ruleHandle               /* Where to put the rule table handle  */
);

N_EXTERN_LIBRARY(nint) NWLUnloadXlateTable
(
   const void N_FAR * ruleHandle         /* Rule table handle                   */
);

#  define NWInitUnicodeTables(CountryCode, CodePage)  \
      NWLInitXlateTables(                                               \
                           CodePage,                                    \
                           N_UNI_LOAD_MONOCASE | N_UNI_LOAD_COLLATION   \
                        )
#  define NWFreeUnicodeTables   NWLFreeXlateTables
#  define NWLoadRuleTable       NWLLoadXlateTable
#  define NWUnloadRuleTable     NWLUnloadXlateTable

#endif


N_EXTERN_LIBRARY(nint) NWGetUnicodeToLocalHandle
(
   pnptr handle
);

N_EXTERN_LIBRARY(nint) NWGetLocalToUnicodeHandle
(
   pnptr handle
);

N_EXTERN_LIBRARY(nint) NWGetMonocaseHandle
(
   pnptr handle
);

N_EXTERN_LIBRARY(nint) NWGetCollationHandle
(
   pnptr handle
);


/*
 *    Functions in unicode.h that work like those in string.h
 */

N_EXTERN_LIBRARY(punicode) unicat    /* Corresponds to strcat    */
(
   punicode              s1,       /* Original string                     */
   const unicode N_FAR * s2        /* String to be appended               */
);

N_EXTERN_LIBRARY(punicode) unichr    /* Corresponds to strchr    */
(
   const unicode N_FAR * s,        /* String to be scanned                */
   unicode               c         /* Character to be found               */
);

N_EXTERN_LIBRARY(punicode) unicpy    /* Corresponds to strcpy    */
(
   punicode              s1,       /* Destination string                  */
   const unicode N_FAR * s2        /* Source string                       */
);

N_EXTERN_LIBRARY(size_t) unicspn     /* Corresponds to strcspn   */
(
   const unicode N_FAR * s1,       /* String to be scanned                */
   const unicode N_FAR * s2        /* Character set                       */
);

N_EXTERN_LIBRARY(size_t) unilen      /* Corresponds to strlen    */
(
   const unicode N_FAR * s         /* String to determine length of       */
);

N_EXTERN_LIBRARY(punicode) unincat   /* Corresponds to strncat   */
(
   punicode              s1,       /* Original string                     */
   const unicode N_FAR * s2,       /* String to be appended               */
   size_t                n         /* Maximum characters to be appended   */
);

N_EXTERN_LIBRARY(punicode) unincpy   /* Corresponds to strncpy   */
(
   punicode              s1,       /* Destination string                  */
   const unicode N_FAR * s2,       /* Source string                       */
   size_t                n         /* Maximum length                      */
);

N_EXTERN_LIBRARY(punicode) uninset   /* Corresponds to strnset   */
(
   punicode s,            /* String to be modified               */
   unicode  c,            /* Fill character                      */
   size_t   n             /* Maximum length                      */
);

N_EXTERN_LIBRARY(punicode) unipbrk   /* Corresponds to strpbrk   */
(
   const unicode N_FAR * s1,       /* String to be scanned                */
   const unicode N_FAR * s2        /* Character set                       */
);

N_EXTERN_LIBRARY (punicode) unipcpy    /* Corresponds to strpcpy */
(
   punicode              s1,       /* Destination string                  */
   const unicode N_FAR * s2        /* Source string                       */
);

N_EXTERN_LIBRARY(punicode) unirchr   /* Corresponds to strrchr   */
(
   const unicode N_FAR * s,        /* String to be scanned                */
   unicode               c         /* Character to be found               */
);

N_EXTERN_LIBRARY(punicode) unirev    /* Corresponds to strrev    */
(
   punicode s             /* String to be reversed               */
);

N_EXTERN_LIBRARY(punicode) uniset    /* Corresponds to strset    */
(
   punicode s,            /* String to modified                  */
   unicode  c             /* Fill character                      */
);

N_EXTERN_LIBRARY(size_t) unispn      /* Corresponds to strspn    */
(
   const unicode N_FAR * s1,       /* String to be tested                 */
   const unicode N_FAR * s2        /* Character set                       */
);

N_EXTERN_LIBRARY(punicode) unistr    /* Corresponds to strstr    */
(
   const unicode N_FAR * s1,       /* String to be scanned                */
   const unicode N_FAR * s2        /* String to be located                */
);

N_EXTERN_LIBRARY(punicode) unitok    /* Corresponds to strtok    */
(
   punicode              s1,       /* String to be parsed                 */
   const unicode N_FAR * s2        /* Delimiter values                    */
);

N_EXTERN_LIBRARY(nint) uniicmp       /* Corresponds to stricmp   */
(
   const unicode N_FAR * s1,       /* 1st string to be compared           */
   const unicode N_FAR * s2        /* 2nd string to be compared           */
);

N_EXTERN_LIBRARY(nint) uninicmp      /* Corresponds to strnicmp  */
(
   const unicode N_FAR * s1,       /* 1st string to be compared           */
   const unicode N_FAR * s2,       /* 2nd string to be compared           */
   size_t                len       /* Maximum length                      */
);

N_EXTERN_LIBRARY(nint) unicmp        /* Unicode compare          */
(
   const unicode N_FAR * s1,
   const unicode N_FAR * s2
);

N_EXTERN_LIBRARY(nint) unincmp       /* Unicode length compare  */
(
   const unicode N_FAR * s1,
   const unicode N_FAR * s2,
   nuint                 len
);

N_EXTERN_LIBRARY(size_t) unisize     /* Corresponds to sizeof    */
(
   const unicode N_FAR * s
);

/*
 * UTF-8  <--> Unicode Conversion APIS
 */
N_EXTERN_LIBRARY(nint) NWLUnicodeToUTF8
(
   const unicode N_FAR * uniStr,
   nuint                 maxSize,
   pnuint8               utf8Str,
   pnuint                utf8Size
);

N_EXTERN_LIBRARY(nint) NWLUTF8ToUnicode
(
   const nuint8 N_FAR * utf8Str,
   nuint                maxSize,
   punicode             uniStr,
   pnuint               uniSize,
   ppnstr               badSequence
);

N_EXTERN_LIBRARY(nint) NWLUTF8ToUnicodeSize
(
   const nuint8 N_FAR * utf8Str,
   pnuint               size
);

N_EXTERN_LIBRARY(nuint) NWLUnicodeToUTF8Size
(
   const unicode N_FAR * uniStr
);

#ifdef __cplusplus
}
#endif

#endif


