]> code.citadel.org Git - citadel.git/commitdiff
* rewrite memfmout() so it doesn't split overlong lines; It now will try to output...
authorWilfried Göesgens <willi@citadel.org>
Sat, 2 Jan 2010 23:24:54 +0000 (23:24 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sat, 2 Jan 2010 23:24:54 +0000 (23:24 +0000)
citadel/msgbase.c

index 70b00bdd0281227b34f78e25b6289886532d87fe..33cda64e5d159bfe833ee4807b9126ef829361ca 100644 (file)
@@ -929,61 +929,84 @@ void memfmout(
        char *mptr,             /* where are we going to get our text from? */
        const char *nl)         /* string to terminate lines with */
 {
-       int b, c;
-       int real = 0;
-       int old = 0;
-       cit_uint8_t ch;
-       char aaa[140];
-       char buffer[SIZ];
-       static int width = 80;
-
-       strcpy(aaa, "");
-       old = 255;
-       strcpy(buffer, "");
-       c = 1;                  /* c is the current pos */
-
+       StrBuf *OutBuf;
+       char *LineStart;
+       char *LastBlank;
+       size_t len;
+       size_t NLLen;
+       char *eptr;
+       int NLFound, NLFoundLastTime;
+       int Found;
+
+       len = strlen (mptr);
+       NLLen = strlen (nl);
+       eptr = mptr + len;
+
+       OutBuf = NewStrBufPlain(NULL, 200);
+       
+       NLFound = NLFoundLastTime = 0;
        do {
-               ch = *mptr++;
-
-               old = real;
-               real = ch;
-
-               if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10)) {
-                       ch = 32;
-               }
-               if (((old == 13) || (old == 10)) && (isspace(real))) {
-                       cprintf("%s", nl);
-                       c = 1;
-               }
-               if (ch > 32) {
-                       if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
-                               cprintf("%s%s", nl, aaa);
-                               c = strlen(aaa);
-                               aaa[0] = 0;
-                       }
-                       b = strlen(aaa);
-                       aaa[b] = ch;
-                       aaa[b + 1] = 0;
-               }
-               if (ch == 32) {
-                       if ((strlen(aaa) + c) > (width - 5)) {
-                               cprintf("%s", nl);
-                               c = 1;
-                       }
-                       cprintf("%s ", aaa);
-                       ++c;
-                       c = c + strlen(aaa);
-                       strcpy(aaa, "");
+               size_t i;
+               LineStart = LastBlank = mptr;
+               Found = 'x';
+
+               for (i = 0; Found == 'x'; i++)
+               {
+                       if (LineStart[i] == '\n')
+                               Found = '\n';
+                       else if (LineStart[i] == '\r')
+                               Found = '\r';
+                       else if (LineStart[i] == ' ')
+                               LastBlank = &LineStart[i];
+                       else if ((i > 80) && (LineStart != LastBlank))
+                               Found = ' ';
+                       else if (LineStart[i] == '\0')
+                               Found = '\0';
                }
-               if ((ch == 13) || (ch == 10)) {
-                       cprintf("%s%s", aaa, nl);
-                       c = 1;
-                       strcpy(aaa, "");
+               switch (Found)
+               {
+               case '\n':
+                       if (LineStart[i + 1] == '\r')
+                               mptr = &LineStart[i + 1];
+                       else 
+                               mptr = &LineStart[i];
+                       i--;
+                       NLFound = 1;
+                       break;
+               case '\r':
+                       if (LineStart[i + 1] == '\n')
+                               mptr = &LineStart[i + 1];
+                       else 
+                               mptr = &LineStart[i];
+                       i--;
+                       NLFound = 1;
+                       break;
+               case '\0':
+                       mptr = &LineStart[i];
+                       i--;
+                       NLFound = 0;
+                       break;
+               case ' ':
+                       mptr = LastBlank + 1;
+                       i = LastBlank - LineStart;
+                       NLFound = 0;
+                       break;
+               case 'x':
+                       /* WHUT? */
+                       break;
                }
-
-       } while (ch > 0);
-
-       cprintf("%s%s", aaa, nl);
+               if (NLFoundLastTime)
+                       StrBufPlain(OutBuf, HKEY(" "));
+               else
+                       FlushStrBuf(OutBuf);
+               StrBufAppendBufPlain(OutBuf, LineStart, i, 0);
+               StrBufAppendBufPlain(OutBuf, nl, NLLen, 0);
+
+               cputbuf(OutBuf);
+               NLFoundLastTime = NLFound;
+       } while (*mptr != '\0');
+
+       FreeStrBuf(&OutBuf);
 }