mbtowc recovery

K

kyuupi

Limiting ourselves to C90 pre-amendment 1 interfaces, is there a
guaranteed way to recover from an invalid character conversion
attempt? I'm unable to find anything. In other words, suppose we
have two strings, one starts with a bad character for the current
locale, and the other doesn't, what needs to be done below for the
assertion to pass?

char bad[] = "<something>";
char ok[] = "<something else>";

void foo (void)
{
wchar_t wc;

if (mbtowc (&wc, bad, sizeof bad) == -1)
{
** what can be done here? **
assert (mbtowc (&wc, ok, sizeof ok) >= 0);
}
}

If there is nothing standardized, doesn't that render mbtowc and
friends somewhat useless in practice?
 
B

Bart van Ingen Schenau

kyuupi said:
Limiting ourselves to C90 pre-amendment 1 interfaces,

TTBMK, mbtowc was only added to C with amendment 1 (in 1994).
So, limiting ourselves to interfaces that predate the amendment renders
the question moot.
is there a
guaranteed way to recover from an invalid character conversion
attempt? I'm unable to find anything. In other words, suppose we
have two strings, one starts with a bad character for the current
locale, and the other doesn't, what needs to be done below for the
assertion to pass?

According to the C99 standard (I don't have any other trustworthy
sources that mention the function), you can reset the conversion state
maintained by the mbtowc function with the call
mbtowc(NULL, NULL, 0)
The return value of this call indicates if the (current, locale
dependent) multi-byte character coding uses shift states or not.

I could not find any information that during the standardisation of C99
anything was changed in this area.
char bad[] = "<something>";
char ok[] = "<something else>";

void foo (void)
{
wchar_t wc;

if (mbtowc (&wc, bad, sizeof bad) == -1)
{
** what can be done here? **
assert (mbtowc (&wc, ok, sizeof ok) >= 0);
}
}

If there is nothing standardized, doesn't that render mbtowc and
friends somewhat useless in practice?

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top