Determine when to allocate a destination buffer

Whether you can perform code-set conversion on multibyte characters in place depends on whether the number of bytes written to the destination buffer is the same as the number of bytes read from the source.

Determine when to allocate a destination buffer as follows:
  • If the ifx_gl_cv_outbuflen() function determines that the size of the source string and its code-set-converted value are exactly equal, you can perform code-set conversion in place.
  • If the size of the code-set-converted value of the source string is not the same as the size of the source string itself, you cannot perform code-set conversion in place.
If you cannot perform code-set conversion in place, you must allocate a separate destination buffer. To allocate a destination buffer, you need to have an estimate of the number of bytes that it needs to hold. You can use either of the following methods to determine the number of bytes that might be written to the destination buffer:
  • The ifx_gl_cv_outbuflen() function calculates either exactly the number of bytes that will be written to the destination buffer or a close over-approximation of the number.

    The third argument to ifx_gl_cv_outbuflen() is the number of bytes in the character source.

  • The IFX_GL_MB_MAX macro can be used in the following expression to calculate the maximum number of bytes that can be written to the destination buffer for any source value in any locale:
    src_bytesleft * IFX_GL_MB_MAX

    The src_bytesleft value is the number of bytes to convert. This expression value is always greater than or equal to the expression value that uses the ifx_gl_mb_loc_max() function.

Of the two options, the expression that uses the macro IFX_GL_MB_MAX is faster and can be used to initialize static buffers. The function ifx_gl_case_conv_outbuflen() is slower but more precise.

The following code fragment uses the ifx_gl_cv_outbuflen() function to determine the estimated size of a code-set-conversion destination buffer:
int dstbytes;
gl_mchar_t *dstmbs;
gl_cv_state_t state;

dstbytes = ifx_gl_cv_outbuflen("ujis", "sjis", srcbytes);
dstmbs = (gl_mchar_t *) malloc(dstbytes);

state.first_frag = 1;
state.last_frag = 1;
if (ifx_gl_cv_mconv(&state, &dstmbs, &dstbytes, "ujis"
    &srcmbs, &srcbytes, "sjis") == -1 )