locnx2utf8

Converts a string from the local code page to UTF-8, providing the caller with full control of the mapping and handling of unmappable characters.

Library:LibC
Service:Characters and Strings

Syntax

  #include <unilib.h> 
   
  int locnx2utf8 (
     UniRuleTable_t        table,
     char                 *dest,
     size_t               *destLen,
     const char           *src,
     size_t                srcLen,
     Loc2Utf8NoMapFunc_t   noMapFunc,
     void                 *noMapFuncParm,
     int                   noMapFlag);
  

Parameters

table

(IN) Specifies the table to use in the conversion. To use the host's default code page, specify UNI_LOCAL_DEFAULT. Otherwise, specify the value returned from the UniGetTable function.

dest

(OUT) Points to the converted string.

destLen

(IN/OUT) Points to the maximum number of bytes that the converted string can contain. When the function returns, it contains the number of bytes in the converted string.

src

(IN) Points to the source string in local code page format.

srcLen

(IN) Specifies the number of bytes in the source string.

noMapFunc

(IN) Specifies the function to call when unmappable characters are found.

noMapFuncParm

(IN) Points to an optional value for the noMapFunc function, which you can use in your noMapFunc function to distinguish between uses of the function. Whatever value you pass here, that value is passed to the no-map function when the locnx2utf8 function encounters a character that is not in its tables.

noMapFlag

(IN) Specifies whether to replace an unmappable character or to simply return an error. It uses one of the following flags, but only the UNI_MAP_CHAR flag enables calling the noMapFunc function.

Flag

Value

Description

UNI_MAP_NO_CHAR

0

Returns the UNI_ERR_UNMAPPABLE_CHAR error code as soon as a character is found to be unmappable. The result is null-terminated at the offending position in the string.

UNI_MAP_BY_FUNC

1

Passes a pointer to the current locations in the source and destination strings to the noMapFunc function if the noMapFunc parameter is not nil.

UNI_MAP_SELF

2

Uses the source character. In many cases this does not result in anything useful.

Return Values

If successful, returns 0. Otherwise, returns a negative error code:

Decimal

Name

Description

-496

UNI_ERR_BAD_HANDLE

The table parameter specifies a nonexistent rule table.

-506

UNI_ERR_PREMATURE_END

The string ended prematurely. This error can occur only in translating from a string in the local code page into UTF-8.

-532

UNI_ERR_UNMAPPABLE_CHAR

A character in the source is unmappable.

Remarks

The function you provide for the noMapFunc parameter must conform to the following syntax:

  typedef int (*Loc2Utf8NoMapFunc_t)
     ( char         **dest,
       size_t         remaining,
       const char   **src,
       void          *userParm );
  
  
dest

(IN/OUT) Points to a pointer to the current position in the destination string. If the translation by the function results in a character output to this string, this pointer must be updated to point one character beyond it.

remaining

(IN/OUT) Specifies the number of characters in the source string that remain to be translated.

src

(IN/OUT) Points to a pointer to the current position in the source string.

  • If your noMapFunc consumes the character at this position, do not increment the pointer beyond the present position for locnx2uni does this.

  • If your noMapFunc consumes n characters, increment the value by n-1.

userParm

(IN) Specifies an optional parameter for the noMapFunc function. This is the value passed to locnx2utf8 in the noMapFuncParm parameter.

After your function is called, you can use it to convert the unmappable character and allow locnx2utf8 to continue converting the string, or you can use your function to convert the remaining characters.

See Also