Portability: We can get away with the older and better-supported

wctomb() here; we don't need wcrtomb().  In particular, this fixes
libarchive building on FreeBSD 4.
This commit is contained in:
Tim Kientzle 2008-06-15 05:12:47 +00:00
parent 8dd058b03d
commit a64ca54afc
2 changed files with 24 additions and 33 deletions

View File

@ -41,14 +41,6 @@ __FBSDID("$FreeBSD$");
#include <wchar.h>
#endif
#ifdef __sgi
/*
* The following prototype is missing on IRXI,
* even though the function is implemented in libc.
*/
size_t wcrtomb(char *, wchar_t, mbstate_t *);
#endif
#include "archive_private.h"
#include "archive_string.h"
@ -172,14 +164,13 @@ __archive_strappend_int(struct archive_string *as, int d, int base)
}
/*
* Home-grown wcrtomb for UTF-8.
* Home-grown wctomb for UTF-8.
*/
static size_t
my_wcrtomb_utf8(char *p, wchar_t wc, mbstate_t *s)
static int
my_wctomb_utf8(char *p, wchar_t wc)
{
(void)s; /* UNUSED */
if (p == NULL)
/* UTF-8 doesn't use shift states. */
return (0);
if (wc <= 0x7f) {
p[0] = (char)wc;
@ -213,19 +204,20 @@ my_wcrtomb_utf8(char *p, wchar_t wc, mbstate_t *s)
static int
my_wcstombs(struct archive_string *as, const wchar_t *w,
size_t (*func)(char *, wchar_t, mbstate_t *))
int (*func)(char *, wchar_t))
{
size_t n;
char *p;
mbstate_t shift_state;
char buff[256];
/* Clear the shift state before starting. */
(*func)(NULL, L'\0');
/*
* Convert one wide char at a time into 'buff', whenever that
* fills, append it to the string.
*/
p = buff;
wcrtomb(NULL, L'\0', &shift_state);
while (*w != L'\0') {
/* Flush the buffer when we have <=16 bytes free. */
/* (No encoding has a single character >16 bytes.) */
@ -234,7 +226,7 @@ my_wcstombs(struct archive_string *as, const wchar_t *w,
archive_strcat(as, buff);
p = buff;
}
n = (*func)(p, *w++, &shift_state);
n = (*func)(p, *w++);
if (n == (size_t)-1)
return (-1);
p += n;
@ -251,7 +243,7 @@ my_wcstombs(struct archive_string *as, const wchar_t *w,
struct archive_string *
__archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
{
if (my_wcstombs(as, w, my_wcrtomb_utf8))
if (my_wcstombs(as, w, my_wctomb_utf8))
return (NULL);
return (as);
}
@ -260,36 +252,34 @@ __archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
* Translates a wide character string into current locale character set
* and appends to the archive_string. Note: returns NULL if conversion
* fails.
*
* TODO: use my_wcrtomb_utf8 if !HAVE_WCRTOMB (add configure logic first!)
*/
struct archive_string *
__archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
{
if (my_wcstombs(as, w, wcrtomb))
#if HAVE_WCTOMB
if (my_wcstombs(as, w, wctomb))
return (NULL);
#else
/* TODO: Can we do better than this? Are there platforms
* that have locale support but don't have wctomb()? */
if (my_wcstombs(as, w, my_wctomb_utf8))
return (NULL);
#endif
return (as);
}
/*
* Home-grown mbrtowc for UTF-8. Some systems lack UTF-8
* (or even lack mbrtowc()) and we need UTF-8 support for pax
* Home-grown mbtowc for UTF-8. Some systems lack UTF-8
* (or even lack mbtowc()) and we need UTF-8 support for pax
* format. So please don't replace this with a call to the
* standard mbrtowc() function!
* standard mbtowc() function!
*/
static size_t
my_mbrtowc_utf8(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
my_mbtowc_utf8(wchar_t *pwc, const char *s, size_t n)
{
int ch;
/*
* This argument is here to make the prototype identical to the
* standard mbrtowc(), so I can build generic string processors
* that just accept a pointer to a suitable mbrtowc() function.
*/
(void)ps; /* UNUSED */
/* Standard behavior: a NULL value for 's' just resets shift state. */
if (s == NULL)
return (0);
@ -360,7 +350,7 @@ __archive_string_utf8_w(struct archive_string *as)
dest = ws;
src = as->s;
while (*src != '\0') {
n = my_mbrtowc_utf8(dest, src, 8, NULL);
n = my_mbtowc_utf8(dest, src, 8);
if (n == 0)
break;
if (n == (size_t)-1 || n == (size_t)-2) {

View File

@ -108,6 +108,7 @@
#define HAVE_WCHAR_H 1
#define HAVE_WCSCPY 1
#define HAVE_WCSLEN 1
#define HAVE_WCTOMB 1
#define HAVE_WMEMCMP 1
#define HAVE_WMEMCPY 1
#define HAVE_ZLIB_H 1