Exemples
L'exemple suivant utilise la sous-routine mbtowc pour convertir un caractère en code à caractères multi-octets en code à caractères larges :
main()
{
char *s;
wchar_t wc;
int n;
(void)setlocale(LC_ALL,"");
/*
** s points to the character string that needs to be
** converted to a wide character to be stored in wc.
*/
n = mbtowc(&wc, s, MB_CUR_MAX);
if (n == -1){
/* Error handle */
}
if (n == 0){
/* case of name pointing to null */
}
/*
** wc contains the process code for the multibyte character
** pointed to by s.
*/
}
L'exemple suivant utilise la sous-routine wctombe pour convertir un caractère en code à caractères larges en code à caractères multi-octets :
main()
{
char *s;
wchar_t wc;
int n;
(void)setlocale(LC_ALL,"");
/*
** s points to the character string that needs to be
** converted to a wide character to be stored in wc.
*/
n = mbtowc(&wc, s, MB_CUR_MAX);
if (n == -1){
/* Error handle */
}
if (n == 0){
/* case of name pointing to null */
}
/*
** wc contains the process code for the multibyte character
** pointed to by s.
*/
}
L'exemple suivant utilise la sous-routine mblen pour rechercher la longueur en octets d'un caractère en code à caractères multi-octets :
#include <stdlib.h>
#include <locale.h>
main
{
char *name = "h";
int n;
(void)setlocale(LC_ALL,"");
n = mblen(name, MB_CUR_MAX);
/*
** The count returned in n is the multibyte length.
** It is always less than or equal to the value of
** MB_CUR_MAX in stdlib.h
*/
if(n == -1){
/* Error Handling */
}
}
L'exemple suivant obtient la position précédente d'un caractère dans une chaîne multi-octets. Si vous devez déterminer la position précédente du caractère, à partir de la position actuelle d'un caractère (pas une position d'octet aléatoire), parcourez la mémoire tampon en commençant par le début. Utilisez la sous-routine mblen jusqu'à ce que la position actuelle du caractère soit atteinte et enregistrez la position précédente du caractère pour obtenir la position de caractère requise.
char buf[]; /* contains the multibyte string */
char *cur, /* points to the current character position */
char *prev, /* points to previous multibyte character */
char *p; /* moving pointer */
/* initialize the buffer and pointers as needed */
/* loop through the buffer until the moving pointer reaches
** the current character position in the buffer, always
** saving the last character position in prev pointer */
p = prev = buf;
/* cur points to a valid character somewhere in buf */
while(p< cur){
prev = p;
if( (i=mblen(p, mbcurmax))<=0){
/* invalid multibyte character or null */
/* You can have a different error handling
** strategy */
p++; /* skip it */
}else {
p += i;
}
}
/* prev will point to the previous character position */
/* Note that if( prev == cur), then it means that there was
** no previous character. Also, if all bytes up to the
** current character are invalid, it will treat them as
** all valid single-byte characters and this may not be what
** you want. One may change this to handle another method of
** error recovery. */
L'exemple suivant utilise la sous-routine mbstowcs pour convertir une chaîne de caractères multi-octets en chaîne de caractères larges :
#include <stdlib.h>
#include <locale.h>
main()
{
char *s;
wchar_t *pwcs;
size_t retval, n;
(void)setlocale(LC_ALL, "");
n = strlen(s) + 1; /*string length + terminating null */
/* Allocate required wchar array */
pwcs = (wchar_t *)malloc(n * sizeof(wchar_t) );
retval = mbstowcs(pwcs, s, n);
if(retval == -1){
/* Error handle */
}
/*
** pwcs contains the wide character string.
*/
}
L'exemple suivant illustre les problèmes liés à l'utilisation de la sous-routine mbstowcs sur un bloc de données important pour la conversion en caractères larges. Lorsqu'elle rencontre des caractères multi-octets qui ne sont pas valides, la sous-routine mbstowcs renvoie la valeur -1, mais ne précise pas où l'erreur s'est produite. La sous-routine mbtowc doit donc être utilisée à plusieurs reprises pour convertir un caractère à la fois en code de caractères larges.
Lors de la conversion de jeux de codes à un octet, il n'y a pas de possibilité de multi-octets partiels. Cependant, lors de la conversion de jeux de codes multi-octets, des multi-octets partiels sont copiés dans une mémoire tampon de sauvegarde. Lors de l'appel suivant à la sous-routine read, le multioctet partiel est préfixé au reste de la séquence d'octets.
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
char *curp, *cure;
int bytesread, bytestoconvert, leftover;
int invalid_multibyte, mbcnt, wcnt;
wchar_t *pwcs;
wchar_t wbuf[BUFSIZ+1];
char buf[BUFSIZ+1];
char savebuf[MB_LEN_MAX];
size_t mb_cur_max;
int fd;
/*
** MB_LEN_MAX specifies the system wide constant for
** the maximum number of bytes in a multibyte character.
*/
(void)setlocale(LC_ALL, "");
mb_cur_max = MB_CUR_MAX;
fd = open(argv[1], 0);
if(fd < 0){
/* error handle */
}
leftover = 0;
if(mb_cur_max==1){ /* Single byte code sets case */
for(;;){
bytesread = read(fd, buf, BUSIZ);
if(bytesread <= 0)
break;
mbstowcs(wbuf, buf, bytesread+1);
/* Process using the wide character buffer */
}
/* File processed ... */
exit(0); /* End of program */
}else{ /* Multibyte code sets */
leftover = 0;
for(;;) {
if(leftover)
strncpy(buf, savebuf ,leftover);
bytesread=read(fd,buf+leftover, BUFSIZ-leftover);
if(bytesread <= 0)
break;
buf[leftover+bytesread] = '\0';
/* Null terminate string */
invalid_multibyte = 0;
bytestoconvert = leftover+bytesread;
cure= buf+bytestoconvert;
leftover=0;
pwcs = wbuf;
/* Stop processing when invalid mbyte found. */
curp= buf;
for(;curp<cure;){
mbcnt = mbtowc(pwcs,curp, mb_cur_max);
if(mbcnt>0){
curp += mbcnt;
pwcs++;
continue;
}else{
/* More data needed on next read*/
if ( cure-curp<mb_cur_max){
leftover=cure-curp;
strncpy(savebuf,curp,leftover);
/* Null terminate before partial mbyte */
*curp=0;
break;
}else{
/*Invalid multibyte found */
invalid_multibyte =1;
break;
}
}
}
if(invalid_multibyte){ /*error handle */
}
/* Process the wide char buffer */
}
}
}
L'exemple suivant utilise les sous-routines wcstombs et wcslen pour convertir une chaîne de caractères larges en forme multi-octets :
#include <stdlib.h>
#include <locale.h>
main()
{
wchar_t *pwcs; /* Source wide character string */
char *s; /* Destination multibyte character string */
size_t n;
size_t retval;
(void)setlocale(LC_ALL, "");
/*
** Calculate the maximum number of bytes needed to
** store the wide character buffer in multibyte form in the
** current code page and malloc() the appropriate storage,
** including the terminating null.
*/
s = (char *) malloc( wcslen(pwcs) * MB_CUR_MAX + 1 );
retval= wcstombs( s, pwcs, n);
if( retval == -1) {
/* Error handle */
/* s points to the multibyte character string. */
}