r541 - trunk

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Mon Mar 3 02:24:11 EST 2008


Author: icculus
Date: 2008-03-03 02:24:10 -0500 (Mon, 03 Mar 2008)
New Revision: 541

Modified:
   trunk/gui.h
   trunk/gui_ncurses.c
   trunk/gui_stdio.c
   trunk/mojosetup.c
   trunk/universal.h
Log:
Moved splitText() and strchars() out of ncurses UI so the stdio UI (etc) can
 use them.


Modified: trunk/gui.h
===================================================================
--- trunk/gui.h	2008-03-03 07:01:54 UTC (rev 540)
+++ trunk/gui.h	2008-03-03 07:24:10 UTC (rev 541)
@@ -254,9 +254,19 @@
 #endif
 #define utf8codepoint(x) entry->utf8codepoint(x)
 
+#ifdef utf8len
+#undef utf8len
 #endif
+#define utf8len(x) entry->utf8len(x)
 
+#ifdef splitText
+#undef splitText
+#endif
+#define splitText(w,x,y,z) entry->splitText(w,x,y,z)
 
+#endif
+
+
 /*
  * make some decisions about which GUI plugins to build...
  *  We list them all here, but some are built, some aren't. Some are DLLs,

Modified: trunk/gui_ncurses.c
===================================================================
--- trunk/gui_ncurses.c	2008-03-03 07:01:54 UTC (rev 540)
+++ trunk/gui_ncurses.c	2008-03-03 07:24:10 UTC (rev 541)
@@ -62,15 +62,6 @@
 } MojoColor;
 
 
-static int strchars(const char *str)
-{
-    int retval = 0;
-    while (utf8codepoint(&str))
-        retval++;
-    return retval;
-} // strchars
-
-
 typedef struct
 {
     WINDOW *mainwin;
@@ -98,89 +89,6 @@
 static MojoBox *progressBox = NULL;
 
 
-// !!! FIXME: cut and pasted in gui_stdio.c, too...fix bugs in both copies!
-// !!! FIXME:  (or move this somewhere else...)
-// !!! FIXME: this handles Unicode, but assumes each glyph takes one column.
-static char **splitText(char *text, int *_count, int *_w)
-{
-    int i;
-    int scrw, scrh;
-    char **retval = NULL;
-    getmaxyx(stdscr, scrh, scrw);
-    int count = 0;
-    int w = 0;
-
-    *_count = *_w = 0;
-    while (*text)
-    {
-        const char *utf8text = text;
-        uint32 ch = 0;
-        int pos = 0;
-        int furthest = 0;
-
-        for (i = 0; ((ch = utf8codepoint(&utf8text)) && (i < (scrw-4))); i++)
-        {
-            if ((ch == '\r') || (ch == '\n'))
-            {
-                char *endptr = (char *) utf8text;
-                const char nextbyte = *utf8text;
-                count++;
-                retval = (char **) xrealloc(retval, count * sizeof (char *));
-                *endptr = '\0';
-                retval[count-1] = xstrdup(text);
-                *endptr = nextbyte;
-                if ((ch == '\r') && (nextbyte == '\n'))  // DOS endlines!
-                    utf8text++; // skip it.
-                text = (char *) utf8text;  // update to start of new line.
-
-                if (i > w)
-                    w = i;
-                i = -1;  // will be zero on next iteration...
-            } // if
-            else if ((ch == ' ') || (ch == '\t'))
-            {
-                furthest = i;
-            } // else if
-        } // for
-
-        // line overflow or end of stream...
-        pos = (ch) ? furthest : i;
-        if ((ch) && (furthest == 0))  // uhoh, no split at all...hack it.
-        {
-            pos = strchars(text);
-            if (pos > scrw-4)  // too big, have to chop a string in the middle.
-                pos = scrw-4;
-        } // if
-
-        if (pos > 0)
-        {
-            char *endptr = NULL;
-            int j = 0;
-            char tmpch = 0;
-
-            utf8text = text;  // adjust pointer by redecoding from start...
-            for (j = 0; j < pos; j++)
-                utf8codepoint(&utf8text);
-
-            endptr = (char *) utf8text;
-            tmpch = *utf8text;
-            count++;
-            retval = (char **) xrealloc(retval, count * sizeof (char*));
-            *endptr = '\0';
-            retval[count-1] = xstrdup(text);
-            text = (char *) utf8text;
-            *endptr = tmpch;
-            if (pos > w)
-                w = pos;
-        } // if
-    } // while
-
-    *_count = count;
-    *_w = w;
-    return retval;
-} // splitText
-
-
 static void drawButton(MojoBox *mojobox, int button)
 {
     const boolean hover = (mojobox->hoverover == button);
@@ -276,7 +184,7 @@
         else
             break;  // we're good, get out.
 
-        len = strchars(msg);
+        len = utf8len(msg);
         y = scrh / 2;
         x = ((scrw - len) / 2);
 
@@ -333,9 +241,10 @@
     for (i = 0; i < bcount; i++)
         retval->buttontext[i] = xstrdup(buttons[i]);
 
-    retval->textlines = splitText(retval->text, &retval->textlinecount, &w);
+    retval->textlines = splitText(retval->text, scrw-4,
+                                  &retval->textlinecount, &w);
 
-    len = strchars(title);
+    len = utf8len(title);
     if (len > scrw-4)
     {
         len = scrw-4;
@@ -348,7 +257,7 @@
     if (bcount > 0)
     {
         for (i = 0; i < bcount; i++)
-            buttonsw += strchars(buttons[i]) + 5;  // '<', ' ', ' ', '>', ' '
+            buttonsw += utf8len(buttons[i]) + 5;  // '<', ' ', ' ', '>', ' '
         if (buttonsw > w)
             w = buttonsw;
         // !!! FIXME: what if these overflow the screen?
@@ -390,7 +299,7 @@
     wmove(win, h-1, w-1);
     waddch(win, ACS_LRCORNER | COLOR_PAIR(MOJOCOLOR_BORDERBOTTOM));
 
-    len = strchars(retval->title);
+    len = utf8len(retval->title);
     wmove(win, 0, ((w-len)/2)-1);
     wattron(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
     waddch(win, ' ');
@@ -407,7 +316,7 @@
         whline(win, ACS_HLINE | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP), w-2);
         for (i = 0; i < bcount; i++)
         {
-            len = strchars(buttons[i]) + 4;
+            len = utf8len(buttons[i]) + 4;
             buttonx -= len+1;
             win = retval->buttons[i] = newwin(1, len, buttony, buttonx);
             keypad(win, TRUE);
@@ -1463,7 +1372,7 @@
         {
             int cells = (int) ( ((double) w) * (((double) percent) / 100.0) );
             snprintf(buf, w+1, "%d%%", percent);
-            mvwaddstr(win, h-3, ((w+2) - strchars(buf)) / 2, buf);
+            mvwaddstr(win, h-3, ((w+2) - utf8len(buf)) / 2, buf);
             mvwchgat(win, h-3, 1, cells, A_BOLD, MOJOCOLOR_DONE, NULL);
             mvwchgat(win, h-3, 1+cells, w-cells, A_BOLD, MOJOCOLOR_TODO, NULL);
         } // else
@@ -1473,7 +1382,7 @@
         if (snprintf(buf, w+1, "%s", item) > (w-4))
             strcpy((buf+w)-4, "...");  // !!! FIXME: Unicode problem.
         mvwhline(win, h-2, 1, ' ', w);
-        mvwaddstr(win, h-2, ((w+2) - strchars(buf)) / 2, buf);
+        mvwaddstr(win, h-2, ((w+2) - utf8len(buf)) / 2, buf);
 
         free(buf);
         wrefresh(win);

Modified: trunk/gui_stdio.c
===================================================================
--- trunk/gui_stdio.c	2008-03-03 07:01:54 UTC (rev 540)
+++ trunk/gui_stdio.c	2008-03-03 07:24:10 UTC (rev 541)
@@ -234,81 +234,6 @@
 } // MojoGui_stdio_stop
 
 
-// !!! FIXME: cut and pasted in gui_ncurses.c, too...fix bugs in both copies!
-// !!! FIXME:  (or move this somewhere else...)
-// !!! FIXME: this is not really Unicode friendly...
-static char **splitText(const char *_text, int *_count, int *_w)
-{
-    char *ptr = xstrdup(_text);
-    char *text = ptr;
-    int i;
-    int scrw = 80;
-    char **retval = NULL;
-    int count = 0;
-    int w = 0;
-
-    *_count = *_w = 0;
-    while (*text)
-    {
-        int pos = 0;
-        int furthest = 0;
-
-        for (i = 0; (text[i]) && (i < (scrw-4)); i++)
-        {
-            const int ch = text[i];
-            if ((ch == '\r') || (ch == '\n'))
-            {
-                count++;
-                retval = (char **) xrealloc(retval, count * sizeof (char *));
-                text[i] = '\0';
-                retval[count-1] = xstrdup(text);
-                text += i;
-                *text = ch;
-                if ((ch == '\r') && (text[1] == '\n'))
-                    text++;
-                text++;
-
-                if (i > w)
-                    w = i;
-                i = -1;  // will be zero on next iteration...
-            } // if
-            else if (isspace(ch))
-            {
-                furthest = i;
-            } // else if
-        } // for
-
-        // line overflow or end of stream...
-        pos = (text[i]) ? furthest : i;
-        if ((text[i]) && (furthest == 0))  // uhoh, no split at all...hack it.
-        {
-            // !!! FIXME: might be chopping in the middle of a UTF-8 seq.
-            pos = strlen(text);
-            if (pos > scrw-4)
-                pos = scrw-4;
-        } // if
-
-        if (pos > 0)
-        {
-            char ch = text[pos];
-            count++;
-            retval = (char **) xrealloc(retval, count * sizeof (char*));
-            text[pos] = '\0';
-            retval[count-1] = xstrdup(text);
-            text += pos;
-            *text = ch;
-            if (pos > w)
-                w = pos;
-        } // if
-    } // while
-
-    free(ptr);
-    *_count = count;
-    *_w = w;
-    return retval;
-} // splitText
-
-
 static void dumb_pager(const char *name, const char *data, size_t datalen)
 {
     const int MAX_PAGE_LINES = 21;
@@ -317,7 +242,7 @@
     int w = 0;
     int linecount = 0;
     boolean getout = false;
-    char **lines = splitText(data, &linecount, &w);
+    char **lines = splitText(data, 80, &linecount, &w);
 
     assert(linecount >= 0);
 

Modified: trunk/mojosetup.c
===================================================================
--- trunk/mojosetup.c	2008-03-03 07:01:54 UTC (rev 540)
+++ trunk/mojosetup.c	2008-03-03 07:24:10 UTC (rev 541)
@@ -40,6 +40,8 @@
     numstr,
     MojoPlatform_ticks,
     utf8codepoint,
+    utf8len,
+    splitText,
 };
 
 int GArgc = 0;
@@ -777,6 +779,92 @@
 } // utf8codepoint
 
 
+int utf8len(const char *str)
+{
+    int retval = 0;
+    while (utf8codepoint(&str))
+        retval++;
+    return retval;
+} // utf8len
+
+
+static char *strfrombuf(const char *text, int len)
+{
+    char *retval = xmalloc(len + 1);
+    memcpy(retval, text, len);
+    retval[len] = '\0';
+    return retval;
+} // strfrombuf
+
+
+char **splitText(const char *text, int scrw, int *_count, int *_w)
+{
+    int i = 0;
+    int j = 0;
+    char **retval = NULL;
+    int count = 0;
+    int w = 0;
+
+    *_count = *_w = 0;
+    while (*text)
+    {
+        const char *utf8text = text;
+        uint32 ch = 0;
+        int pos = 0;
+        int furthest = 0;
+
+        for (i = 0; ((ch = utf8codepoint(&utf8text))) && (i < scrw); i++)
+        {
+            if ((ch == '\r') || (ch == '\n'))
+            {
+                const char nextbyte = *utf8text;
+                count++;
+                retval = (char **) xrealloc(retval, count * sizeof (char *));
+                retval[count-1] = strfrombuf(text, utf8text - text);
+                if ((ch == '\r') && (nextbyte == '\n'))  // DOS endlines!
+                    utf8text++; // skip it.
+                text = (char *) utf8text;  // update to start of new line.
+
+                if (i > w)
+                    w = i;
+                i = -1;  // will be zero on next iteration...
+            } // if
+            else if ((ch == ' ') || (ch == '\t'))
+            {
+                furthest = i;
+            } // else if
+        } // for
+
+        // line overflow or end of stream...
+        pos = (ch) ? furthest : i;
+        if ((ch) && (furthest == 0))  // uhoh, no split at all...hack it.
+        {
+            pos = utf8len(text);
+            if (pos > scrw)  // too big, have to chop a string in the middle.
+                pos = scrw;
+        } // if
+
+        if (pos > 0)
+        {
+            utf8text = text;  // adjust pointer by redecoding from start...
+            for (j = 0; j < pos; j++)
+                utf8codepoint(&utf8text);
+
+            count++;
+            retval = (char **) xrealloc(retval, count * sizeof (char*));
+            retval[count-1] = strfrombuf(text, utf8text - text);
+            text = (char *) utf8text;
+            if (pos > w)
+                w = pos;
+        } // if
+    } // while
+
+    *_count = count;
+    *_w = w;
+    return retval;
+} // splitText
+
+
 static void outOfMemory(void)
 {
     // Try to translate "out of memory", but not if it causes recursion.

Modified: trunk/universal.h
===================================================================
--- trunk/universal.h	2008-03-03 07:01:54 UTC (rev 540)
+++ trunk/universal.h	2008-03-03 07:24:10 UTC (rev 541)
@@ -102,7 +102,13 @@
 // !!! FIXME: document me!
 uint32 utf8codepoint(const char **str);
 
+// !!! FIXME: document me!
+int utf8len(const char *str);
 
+// !!! FIXME: document me!
+char **splitText(const char *text, int scrw, int *_count, int *_w);
+
+
 // Format a string, sort of (but not exactly!) like sprintf().
 //  The only formatters accepted are %0 through %9 (and %%), which do not
 //  have to appear in order in the string, but match the varargs passed to the
@@ -355,6 +361,8 @@
     const char *(*numstr)(int val);
     uint32 (*ticks)(void);
     uint32 (*utf8codepoint)(const char **_str);
+    int (*utf8len)(const char *str);
+    char **(*splitText)(const char *text, int scrw, int *_count, int *_w);
 } MojoSetupEntryPoints;
 extern MojoSetupEntryPoints GEntryPoints;
 




More information about the mojosetup-commits mailing list