### mbrlen -- Calculate Length of Multibyte Character

Format

>     #include <wchar.h>
>     size_t mbrlen(const char *string, size_t n,
>                   mbstate_t *state);

Language Level: ANSI 93
mbrlen is a restartable version of mblen and performs the same function. It determines the length in bytes of the multibyte character pointed to by string. A maximum of n bytes are examined.

With mbrlen, you can begin processing a string, and then another. Once you have begun processing the second string, you may switch back and continue with the first string. On platforms that support shift states, state represents the initial shift state of the string (0). If you read in only part of the string, mbrlen sets state to the string's shift state at the point you stopped. You can then call mbrlen again for that string and pass in the updated state value to continue reading where you left off.

Note: Because the OS/2 and Windows code pages do not have shift states, the state parameter is provided only for compatibility with other ANSI/ISO platforms. IBM C and C++ Compilers ignores the value passed for state.

The behavior of mbrlen is affected by the LC_CTYPE category of the current locale.

Return Value
If string is a null pointer, mbrlen returns 0.

If string is not a null pointer, mbrlen returns the first of the following that applies:

0
  If string points to the null character.
positive
  If the next n or fewer bytes complete the valid multibyte character; the value returned is the number of bytes that complete the multibyte character.
-2
  If the next n bytes form an incomplete (but potentially valid) multibyte character, and all n bytes have been processed.
-1
  If an encoding error occurs (when the next n or fewer bytes do not form a complete and valid multibyte character). mbrlen sets errno to EILSEQ.

Example
This example uses mbrlen to determine the length of the strings mbs1 and mbs2.

    #include <wchar.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <locale.h>

    #if (1 == __TOS_OS2__)
       #define LOCNAME "ja_jp.ibm-932"
    #else
       #define LOCNAME "ja_jp.ibm-943"
    #endif

    int main(void)
    {
       char       mbs1[] = "abc";
       char       mbs2[] = "\x81\x41" "a" "\x81\xc1";
       mbstate_t  ss = 0;
       size_t     charCount, length, rc;

       if (NULL == setlocale(LC_ALL, LOCNAME)) {
          printf("Locale \"%s\" could not be loaded\n", LOCNAME);
          exit(1);
       }

       charCount = length = 0;
       for (; ; ) {
          rc =  mbrlen(mbs1+length, MB_CUR_MAX, &ss);
          if (rc>0) {
             length += rc;
             ++charCount;
          } else {
             break;
          }
       }
       printf("Length of the first string is %d bytes (%d characters)\n",
               length, charCount);

       charCount = length = 0;
       for (; ; ) {
          rc =  mbrlen(mbs2+length, MB_CUR_MAX, &ss);
          if (rc>0) {
             length += rc;
             ++charCount;
          } else {
             break;
          }
       }
       printf("Length of the second string is %d bytes (%d characters)\n",
               length, charCount);

       return 0;

       /********************************************************************
          The output should be similiar to:

          Length of the first string is 3 bytes (3 characters)
          Length of the second string is 5 bytes (3 characters)
       ********************************************************************/
    }

mblen -- Determine Length of Multibyte Character
mbtowc -- Convert Multibyte Character to Wide Character
mbrtowc -- Convert Multibyte Character to Wide Character
mbsrtowcs -- Convert Multibyte String to Wide-Character String
setlocale -- Set Locale
wcrtomb -- Convert Wide Character to Multibyte Character
wcsrtombs -- Convert Wide-Character String to Multibyte String
<locale.h>
<wchar.h>