_mbbtype

De RAD Studio
Aller à : navigation, rechercher

Remonter à string.h - Index


Header File

mbstring.h

Category

Classification Routines

Prototype

int _mbbtype(unsigned char ch, int mode);

Description

The _mbbtype function inspects the multibyte argument, character ch, to determine whether it is a single-byte character, or whether ch is the leadbyte or trailing byte in a multibyte character. The _mbbtype function can determine whether ch is an invalid character.

Return Value

The return value depends on the value of ch and the test which you want performed on ch.

The value that _mbbtype returns is one of the following manifest constants, defined in mbctype.h:


Value of mode Value of ch Test performed Return Value
mode != 1

Single byte

Valid single or lead byte

_MBC_SINGLE
mode != 1

Leadbyte

Valid single of lead byte

_MBC_LEAD
mode = 1

Trailbyte

Valid single or trail byte

_MBC_TRAIL

Any value

Any value

Valid character

_MBC_ILLEGAL


Example

#include <mbstring.h>
#include <mbctype.h>
int count_chars(char* input)
{
  int last_type = 0;
  int chars = 0;

  while (*input)
  {
    /* Obtain the type of the current char based on the type of the last one */
    last_type = _mbbtype(*input, last_type);

    /* Consider single and MBCS chars as whole chars. For illegal chars exit with -1 */
    switch (last_type) 
    {
      case _MBC_LEAD:
      case _MBC_SINGLE:
        chars++;
        break;
      case _MBC_ILLEGAL:
        return -1;
    }

    input++;
  }

  /* The number of chars calculated */
  return chars;
}