]> granicus.if.org Git - neomutt/commitdiff
Replace wctomb and mbtowc with wcrtomb and mbrtowc. From TAKIZAWA
authorThomas Roessler <roessler@does-not-exist.org>
Mon, 24 Jul 2000 07:47:32 +0000 (07:47 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Mon, 24 Jul 2000 07:47:32 +0000 (07:47 +0000)
Takashi <taki@luna.email.ne.jp>, with small changes from Edmund
Grimley Evans.

curs_lib.c
hdrline.c
help.c
mbyte.c
mbyte.h
menu.c

index dcf1f06d83bacc519b07ad9fa21880b0d2fc5043..2e20535bd2c661efee1df2a9d1dc981a690a878e 100644 (file)
@@ -467,14 +467,16 @@ int mutt_multi_choice (char *prompt, char *letters)
 
 int mutt_addwch (wchar_t wc)
 {
-  char buf[MB_LEN_MAX];
-  int n;
-
-  n = wctomb (buf, wc);
-  if (n == -1)
-    return n;
+  char buf[MB_LEN_MAX*2];
+  mbstate_t mbstate;
+  size_t n1, n2;
+
+  memset (&mbstate, 0, sizeof (mbstate));
+  if ((n1 = wcrtomb (buf, wc, &mbstate)) == (size_t)(-1) ||
+      (n2 = wcrtomb (buf + n1, 0, &mbstate)) == (size_t)(-1))
+    return -1; /* ERR */
   else
-    return addnstr (buf, n);
+    return addstr (buf);
 }
 
 /*
@@ -491,23 +493,26 @@ void mutt_format_string (char *dest, size_t destlen,
 {
   char *p;
   wchar_t wc;
-  int w, k;
+  int w;
+  size_t k;
   char scratch[MB_LEN_MAX];
+  mbstate_t mbstate1, mbstate2;
 
+  memset(&mbstate1, 0, sizeof (mbstate1));
+  memset(&mbstate2, 0, sizeof (mbstate2));
   --destlen;
   p = dest;
-  while ((k = mbtowc (&wc, s, n)))
+  for (; n && (k = mbrtowc (&wc, s, n, &mbstate1)); s += k, n -= k)
   {
-    if (k == -1 && n > 0)
+    if (k == (size_t)(-1) || k == (size_t)(-2))
     {
       k = 1;
       wc = replacement_char ();
     }
-    s += k, n -= k;
     w = wc < M_TREE_MAX ? 1 : wcwidth (wc); /* hack */
     if (w >= 0)
     {
-      if (w > max_width || (k = wctomb (scratch, wc)) > destlen)
+      if (w > max_width || (k = wcrtomb (scratch, wc, &mbstate2)) > destlen)
        break;
       min_width -= w;
       max_width -= w;
@@ -516,40 +521,44 @@ void mutt_format_string (char *dest, size_t destlen,
       destlen -= k;
     }
   }
-  k = (int)destlen < min_width ? destlen : min_width;
-  if (k <= 0)
+  w = (int)destlen < min_width ? destlen : min_width;
+  if (w <= 0)
     *p = '\0';
   else if (right_justify)
   {
-    p[k] = '\0';
+    p[w] = '\0';
     while (--p >= dest)
-      p[k] = *p;
-    while (--k >= 0)
-      dest[k] = pad_char;
+      p[w] = *p;
+    while (--w >= 0)
+      dest[w] = pad_char;
   }
   else
   {
-    while (--k >= 0)
+    while (--w >= 0)
       *p++ = pad_char;
     *p = '\0';
   }
 }
 
 /*
- * mutt_paddstr (n, s) is equivalent to
+ * mutt_paddstr (n, s) is almost equivalent to
  * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big), addstr (bigbuf)
  */
 
 void mutt_paddstr (int n, const char *s)
 {
   wchar_t wc;
-  int k, w;
+  int w;
+  size_t k;
+  size_t len = mutt_strlen (s);
+  mbstate_t mbstate;
 
-  while ((k = mbtowc (&wc, s, -1)))
+  memset (&mbstate, 0, sizeof (mbstate));
+  while (len && (k = mbrtowc (&wc, s, len, &mbstate)))
   {
-    if (k == -1)
+    if (k == (size_t)(-1) || k == (size_t)(-2))
     {
-      ++s; /* skip ill-formed character */
+      ++s, --len; /* skip ill-formed character */
       continue;
     }
     if ((w = wcwidth (wc)) >= 0)
@@ -559,7 +568,7 @@ void mutt_paddstr (int n, const char *s)
       addnstr ((char *)s, k);
       n -= w;
     }
-    s += k;
+    s += k, len -= k;
   }
   while (n-- > 0)
     addch (' ');
index 2a0bf2d0888f72bdc82c05831f621e751d4e9a98..8447655c2d4df431743a1971a5040f2e2920f26f 100644 (file)
--- a/hdrline.c
+++ b/hdrline.c
@@ -251,7 +251,7 @@ static void hdr_format_s (char *dest,
   }
 
   mutt_format_string (dest, destlen, min_width, max_width,
-                     right_justify, ' ', s, -1);
+                     right_justify, ' ', s, mutt_strlen (s));
 }
 
 static const char *
diff --git a/help.c b/help.c
index 9c5a8eb8ad78bfcd849ac8892b734573b2dca7be..324eba394b0d96ea766fcc4f6f53b673d161a159 100644 (file)
--- a/help.c
+++ b/help.c
@@ -88,11 +88,16 @@ static int print_macro (FILE *f, int maxwidth, const char **macro)
 {
   int n = maxwidth;
   wchar_t wc;
-  int k, w;
-
-  for (;;)
+  int w;
+  size_t k;
+  size_t len = mutt_strlen (*macro);
+  mbstate_t mbstate1, mbstate2;
+
+  memset (&mbstate1, 0, sizeof (mbstate1));
+  memset (&mbstate2, 0, sizeof (mbstate2));
+  for (; (k = mbrtowc (&wc, *macro, len, &mbstate1)); *macro += k, len -= k)
   {
-    if ((k = mbtowc (&wc, *macro, -1)) <= 0)
+    if (k == (size_t)(-1) || k == (size_t)(-2))
       break;
     if ((w = wcwidth (wc)) >= 0)
     {
@@ -100,10 +105,11 @@ static int print_macro (FILE *f, int maxwidth, const char **macro)
        break;
       n -= w;
       {
-       char tb[7];
-       int m = wctomb (tb, wc);
-       if (0 < m && m < 7)
-         tb[m] = '\0', fprintf (f, "%s", tb);
+       char buf[MB_LEN_MAX*2];
+       size_t n1, n2;
+       if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t)(-1) &&
+           (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t)(-1))
+         fputs (buf, f);
       }
     }
     else if (wc < 0x20 || wc == 0x7f)
@@ -129,7 +135,6 @@ static int print_macro (FILE *f, int maxwidth, const char **macro)
       n -= 1;
       fprintf (f, "?");
     }
-    *macro += k;
   }
   return (maxwidth - n);
 }
diff --git a/mbyte.c b/mbyte.c
index 124febcbe5d7188fb4e91bea86c065d8f869e73a..ba098a14a18509ce0f1c4b931db649a845226c5f 100644 (file)
--- a/mbyte.c
+++ b/mbyte.c
@@ -20,10 +20,24 @@ void mutt_set_charset (char *charset)
 
 #ifndef HAVE_WC_FUNCS
 
-int wctomb (char *s, wchar_t wc)
+size_t wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
 {
+  static mbstate_t mbstate;
+
+  if (!ps)
+    ps = &mbstate;
+
   if (!s)
-    return 0;
+  {
+    memset (ps, 0, sizeof (*ps));
+    return 1;
+  }
+  if (!wc)
+  {
+    memset (ps, 0, sizeof (*ps));
+    *s = 0;
+    return 1;
+  }
   if (Charset_is_utf8)
     return mutt_wctoutf8 (s, wc);
   else if (wc < 0x100)
@@ -32,19 +46,10 @@ int wctomb (char *s, wchar_t wc)
     return 1;
   }
   else
-    return -1;
-}
-
-int mbtowc (wchar_t *pwc, const char *s, size_t n)
-{
-  mbstate_t state;
-  int result;
-  memset(&state, 0, sizeof(state));
-  result = mbrtowc (pwc, s, n, &state);
-  if (result >= 0)
-    return result;
-  else
-    return -1;
+  {
+    errno = EILSEQ;
+    return (size_t)(-1);
+  }
 }
 
 size_t mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
diff --git a/mbyte.h b/mbyte.h
index 1f25dc7159020909983bd4a2436051ff6adb70ad..475582e957d75d9edd75e5752723198ff355e04d 100644 (file)
--- a/mbyte.h
+++ b/mbyte.h
@@ -7,8 +7,7 @@ extern int Charset_is_utf8;
 size_t utf8rtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *_ps);
 
 #ifndef HAVE_WC_FUNCS
-int wctomb (char *s, wchar_t wc);
-int mbtowc (wchar_t *pwc, const char *s, size_t n);
+size_t wcrtomb (char *s, wchar_t wc, mbstate_t *ps);
 size_t mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps);
 int iswprint (wint_t wc);
 int wcwidth (wchar_t wc);
diff --git a/menu.c b/menu.c
index 67a8567e9f73e2ba9396dfbfaac7ca1bbe90d2c0..ee7dfdb492cbf61cfb28a2ee08ffdeb8c8bbb01d 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -33,8 +33,11 @@ extern int Charset_is_utf8; /* FIXME: bad modularisation */
 static void print_enriched_string (int attr, unsigned char *s, int do_color)
 {
   wchar_t wc;
-  int k;
+  size_t k;
+  size_t n = mutt_strlen ((char *)s);
+  mbstate_t mbstate;
 
+  memset (&mbstate, 0, sizeof (mbstate));
   while (*s)
   {
     if (*s < M_TREE_MAX)
@@ -98,14 +101,14 @@ static void print_enriched_string (int attr, unsigned char *s, int do_color)
            addch ('&');
            break;
        }
-       s++;
+       s++, n--;
       }
       if (do_color) attrset(attr);
     }
-    else if ((k = mbtowc (&wc, (char *)s, -1)) > 0)
+    else if ((k = mbrtowc (&wc, (char *)s, n, &mbstate)) > 0)
     {
       addnstr ((char *)s, k);
-      s += k;
+      s += k, n-= k;
     }
     else
       break;
@@ -125,9 +128,7 @@ static void menu_make_entry (char *s, int l, MUTTMENU *menu, int i)
 
 void menu_pad_string (char *s, size_t l)
 {
-#if !defined(HAVE_BKGDSET) && !defined (USE_SLANG_CURSES)
-  int n = mutt_strlen (s);
-#endif
+  size_t n = mutt_strlen (s);
   int shift = option (OPTARROWCURSOR) ? 3 : 0;
   
   l--; /* save room for the terminal \0 */
@@ -135,7 +136,7 @@ void menu_pad_string (char *s, size_t l)
     l = COLS - shift;
 
   /* Let's just pad the string anyway ... */
-  mutt_format_string (s, INT_MAX, l, l, 0, ' ', s, INT_MAX);
+  mutt_format_string (s, INT_MAX, l, l, 0, ' ', s, n);
   return;
 
 #if !defined (HAVE_BKGDSET) && !defined (USE_SLANG_CURSES)