On Sat, 2002-07-20 at 22:26, Karl Fogel wrote:
> Hmmm, I re-read your definition and think I now understand what you
> mean by "the ASCII encoding" above, and that it effectively is the
> definition I was expecting.
>
> [karl makes a try...] Care to patch?
I never managed to find time to build svn myself. I'm honestly
frustated that there is not one single repository where all the needed
sources can be get and a simple configure script to get the build
started. So all I can do is provide code which you'll have to bend to
fit in. is_ascii_text is the main function. You should call it before
accepting the bytes of the text as ASCII. I don't know how to get the
current locale's coset name in your environment (on Unix systems that'd
be a nl_langinfo(CODESET) call) so please fill this in.
The code (completely untested but it should work):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static const char *
normalize_codeset (const char *name)
{
name = strdup (name);
if (name != NULL)
{
char *rp = name;
char *wp = name;
while (*rp != '\0')
{
if (isalnum (*rp))
*wp++ = *rp;
++rp;
}
*wp = '\0';
}
return name;
}
/* Keep sorted according to strcmp. */
static const char *ascii_safe_names[] =
{
"ascii", "iso88591", "iso88592"
};
const size_t nascii_safe_names = sizeof (ascii_safe_names) / sizeof
(ascii_safe_names[0]);
static int
xstrcmp (const void *a, const void *b)
{
return strcmp (*(const char **) a, *(const char **) b);
}
bool
is_ascii_text (const char *buf, size_t len)
{
const char *codeset;
size_t cnt;
bool result = false;
/* Get the codeset for the current locale. */
// XXX fill in
codeset = get_current_codeset ();
if (codeset == NULL)
/* Out of memory. */
return false;
/* Normalize. This creates a copy of the string. */
codeset = normalize_codeset (codeset);
/* Check whether this is the name of a ASCII-save codeset. */
if (bsearch (&codeset, ascii_safe_names, nascii_safe_names,
sizeof (ascii_safe_names[0]), xstrcmp) == NULL)
/* No. We cannot use the text. */
goto out;
/* Check whether the buffer contains any non-ASCII characters. */
while (len-- > 0)
{
if (*buf < 0x20 || *buf >= 0x7f)
goto out;
++buf;
}
/* All went well. */
result = true;
out:
free (codeset);
return result;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
---------------. ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Red Hat `--' drepper at redhat.com `------------------------
Received on Sun Jul 21 09:14:48 2002