[Remind-Fans] [PATCH] support for two-byte characters in calendar output

Wolfgang Kroener lists at azog.de
Sun Jun 27 09:33:12 EDT 2010


From: Wolfgang Kroener <wk at azog.de>

UTF-8 characters in the -c calendar output no longer break the layout.
---
 src/calendar.c |   33 +++++++++++++++++++++++++++++++--
 1 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/calendar.c b/src/calendar.c
index e678a09..5fe62e6 100644
--- a/src/calendar.c
+++ b/src/calendar.c
@@ -14,6 +14,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <wchar.h>
 #include <ctype.h>
 
 #include <stdlib.h>
@@ -520,6 +521,9 @@ static int WriteOneColLine(int col)
     char const *s;
     char const *space;
     int numwritten = 0;
+    int tmplen;
+    mbstate_t ps = {0};
+    int twobytestart = 1;
 
 /* Print as many characters as possible within the column */
     space = NULL;
@@ -552,7 +556,20 @@ static int WriteOneColLine(int col)
     if (!space) {
 	for (s = e->pos; s - e->pos < ColSpaces; s++) {
 	    if (!*s) break;
-	    numwritten++;
+            tmplen = mbrlen(s, 2, &ps);
+            if ((size_t)tmplen != (size_t)-1 && (size_t)tmplen != (size_t)-2) {
+              /* non-twobyte character found */
+              numwritten++;
+            } else {
+              /* twobyte character */
+              if (twobytestart == 1) {
+                numwritten++;
+                twobytestart = 0;
+              } else {
+                e->pos++;
+                twobytestart = 1;
+              }
+            }
 	    PutChar(*s);
 	}
 	e->pos = s;
@@ -561,7 +578,19 @@ static int WriteOneColLine(int col)
 /* We found a space - print everything before it. */
 	for (s = e->pos; s<space; s++) {
 	    if (!*s) break;
-	    numwritten++;
+            tmplen = mbrlen(s, 2, &ps);
+            if ((size_t)tmplen != (size_t)-1 && (size_t)tmplen != (size_t)-2) {
+              /* non-twobyte character found */
+              numwritten++;
+            } else {
+              /* twobyte character */
+              if (twobytestart == 1) {
+                numwritten++;
+                twobytestart = 0;
+              } else {
+                twobytestart = 1;
+              }
+            }
 	    PutChar(*s);
 	}
     }
-- 
1.7.1




More information about the Remind-fans mailing list