appletwordwrap.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletwordwrap.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include "appletmemory.h"
00008 #include "appletstrings.h"
00009 #include "appletwordwrap.h"
00010 
00011 
00012 
00013 boolean uppercasehandle (Handle htext) {
00014     
00015     unsigned long ctchars;
00016     ptrchar p;
00017     short i;
00018     char ch;
00019     
00020     ctchars = gethandlesize (htext);
00021     
00022     lockhandle (htext);
00023     
00024     p = *htext;
00025     
00026     for (i = 1; i <= ctchars; i++) {
00027         
00028         ch = *p;
00029         
00030         if ((ch >= 'a') && (ch <= 'z'))
00031             ch -= 32;
00032         
00033         *p = ch;
00034         
00035         p++;
00036         } /*for*/
00037         
00038     unlockhandle (htext);
00039     
00040     return (true);
00041     } /*uppercasehandle*/
00042     
00043 
00044 boolean wordwraphandle (Handle htext) {
00045     
00046     /*
00047     DW 12/3/94: rewrote the routine. it's now a two-pass thing.
00048     */
00049     
00050     unsigned long handlesize = gethandlesize (htext);
00051     unsigned long ix = 0;
00052     char ch, chnext, chprev = ' ';
00053     boolean fllinestart = true;
00054         
00055     while (true) { /*pass 1 -- remove leading white space on each line*/
00056     
00057         if (ix >= handlesize)
00058             break;
00059         
00060         ch = (*htext) [ix];
00061         
00062         if (fllinestart && ((ch == ' ') || (ch == chtab))) {
00063             
00064             moveleft (&(*htext) [ix + 1], &(*htext) [ix], handlesize - ix);
00065         
00066             handlesize--;
00067         
00068             sethandlesize (htext, handlesize);
00069             
00070             ix--;
00071             
00072             goto pass1nextchar;
00073             }
00074             
00075         if (ch == chreturn) {
00076             
00077             fllinestart = true;
00078             
00079             goto pass1nextchar;
00080             }
00081             
00082         fllinestart = false; 
00083         
00084         pass1nextchar:
00085         
00086         ix++;
00087         } /*while*/
00088         
00089     ix = 0; /*start at the beginning of the handle for pass 2*/
00090     
00091     while (true) { /*pass 2 -- strip out the extraneous hard returns*/
00092         
00093         if (ix >= handlesize)
00094             break;
00095         
00096         ch = (*htext) [ix];
00097         
00098         if (ch != chreturn) 
00099             goto pass2nextchar;
00100             
00101         if (ix == (handlesize - 1)) { /*last char is a return*/
00102             
00103             /*sethandlesize (htext, handlesize - 1);*/ /*disabled -- the text might not be the whole message*/
00104             
00105             break; /*finished processing the text*/
00106             }
00107         
00108         chnext = (*htext) [ix + 1];
00109         
00110         if ((chnext == chreturn) || (chprev == chreturn)) /*leave double or triple returns alone*/
00111             goto pass2nextchar;
00112             
00113         if ((chnext != ' ') && (chprev != ' ')) {
00114             
00115             (*htext) [ix] = ' '; /*replace it with a space*/
00116             
00117             goto pass2nextchar;
00118             }
00119             
00120         moveleft (&(*htext) [ix + 1], &(*htext) [ix], handlesize - ix);
00121         
00122         handlesize--;
00123         
00124         sethandlesize (htext, handlesize);
00125         
00126         pass2nextchar:
00127         
00128         chprev = ch;
00129         
00130         ix++;
00131         } /*while*/
00132         
00133     return (true);
00134     } /*wordwraphandle*/
00135                     
00136                 
00137 boolean daveNetMassager (short indentlen, short maxlinelen, Handle h, Handle *hmassaged) {
00138     
00139     unsigned long handlesize = gethandlesize (h);
00140     unsigned long ixhandle = 0;
00141     short linelen, wordlen;
00142     bigstring indent, dashes, word, cr, space;
00143     Handle hnew;
00144     short i;
00145     
00146     if (!getnewhandle (0, &hnew))
00147         return (false);
00148         
00149     filledstring (' ', indentlen, indent);
00150     
00151     setstringlength (dashes, 0);
00152     
00153     setstringlength (word, 0);
00154     
00155     setstringwithchar (chreturn, cr);
00156     
00157     setstringwithchar (' ', space);
00158     
00159     for (i = 1; i <= 16; i++)
00160         pushstring ("\p ---", dashes);
00161     
00162     if (!pushtexthandle (indent, hnew))
00163         goto error;
00164         
00165     linelen = indentlen;
00166     
00167     while (true) {
00168     
00169         if (ixhandle >= handlesize)
00170             break;
00171         
00172         if ((*h) [ixhandle] == chreturn) {
00173             
00174             if (equalstrings (word, "\p---")) {
00175                 
00176                 if (!pushtexthandle (dashes, hnew))
00177                     goto error;
00178                 
00179                 setstringlength (word, 0);
00180                 }
00181             
00182             if (!pushtexthandle (cr, hnew))
00183                 goto error;
00184                 
00185             if (!pushtexthandle (indent, hnew))
00186                 goto error;
00187                 
00188             linelen = indentlen;
00189                 
00190             ixhandle++;
00191             }
00192         else { /*first char is not a return*/
00193             
00194             while (true) { /*skip over leading blanks*/
00195                 
00196                 if ((*h) [ixhandle] != ' ')
00197                     break;
00198                     
00199                 ixhandle++;
00200                 
00201                 if (ixhandle >= handlesize)
00202                     goto exit;
00203                 } /*while*/
00204             
00205             setstringlength (word, 0);
00206             
00207             while (true) { /*pop off the first word*/
00208                 
00209                 char ch = (*h) [ixhandle];
00210                 
00211                 if ((ch == ' ') || (ch == chreturn)) 
00212                     break;
00213                 
00214                 pushchar (ch, word);
00215                 
00216                 ixhandle++;
00217                 
00218                 if (ixhandle >= handlesize)
00219                     break;
00220                 } /*while*/
00221                 
00222             while (true) { /*skip over leading blanks*/
00223                 
00224                 if (ixhandle >= handlesize)
00225                     break;
00226                 
00227                 if ((*h) [ixhandle] != ' ')
00228                     break;
00229                     
00230                 ixhandle++;
00231                 } /*while*/
00232             
00233             wordlen = stringlength (word);
00234             
00235             if ((linelen + wordlen) > maxlinelen) {
00236                 
00237                 if (!pushtexthandle (cr, hnew))
00238                     goto error;
00239                     
00240                 if (!pushtexthandle (indent, hnew))
00241                     goto error;
00242                     
00243                 if (!pushtexthandle (word, hnew))
00244                     goto error;
00245                     
00246                 if (!pushtexthandle (space, hnew))
00247                     goto error;
00248                     
00249                 linelen = indentlen + wordlen;
00250                 }
00251             else {
00252                 if (!pushtexthandle (word, hnew))
00253                     goto error;
00254                     
00255                 if (!pushtexthandle (space, hnew))
00256                     goto error;
00257                     
00258                 linelen += wordlen;
00259                 }
00260             }
00261         } /*while*/
00262     
00263     exit:
00264     
00265     *hmassaged = hnew;
00266     
00267     return (true);
00268     
00269     error:
00270     
00271     disposehandle (hnew);
00272     
00273     *hmassaged = nil;
00274     
00275     return (false);
00276     } /*daveNetMassager*/
00277     
00278     
00279     
00280     

Generated on Wed May 31 18:19:44 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6