home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / wordwrap.cc < prev   
C/C++ Source or Header  |  1998-08-08  |  1KB  |  75 lines

  1. /* $Id: wordwrap.cc,v 1.5 1997/03/23 13:52:15 dps Exp $ */
  2. /* Wordwrap function for library */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include "tblock.h"
  7. #define __EXLCUDE_READER_CLASSES
  8. #include "lib.h"
  9.  
  10.  
  11. /* Word wrap text into lines of length room */
  12. tblock *word_wrap(const char *txt, const char *nl, const char *expl_nl,
  13.           const int room, const int ilen=0)
  14. {
  15.     struct tblock *ans;
  16.     const char *wptr, *sc;
  17.     int wlen, croom, flg;
  18.     int nl_len;            // Performance hack
  19.  
  20.     ans=new(tblock);
  21.  
  22.     wlen=0;
  23.     wptr=sc=txt;
  24.     croom=room-ilen;
  25.     nl_len=strlen(nl);
  26.     flg=0;
  27.     
  28.     while (1)
  29.     {
  30.     /* FIXME: huge words might cause an oversize line */
  31.     /* (this is not a typesetting program like *roff) */
  32.     //
  33.     // isspace is broken FUCK
  34.     // thinks german umlaute are spaces
  35.     // original: if (isspace(*sc) || *sc=='\n' || *sc=='\0')
  36.     // (jk)
  37.     if ( *sc==' ' || *sc=='\n' || *sc=='\0' )
  38.     {
  39.         if (wlen+flg>croom)
  40.         {
  41.         ans->add(nl,nl_len);
  42.         croom=room;
  43.         flg=0;
  44.         }
  45.         if (wlen>0)
  46.         {
  47.         if (flg)
  48.         {
  49.             ans->add(' '); // debug
  50.             croom--;
  51.         }
  52.         ans->add(wptr, wlen);
  53.         croom-=wlen;
  54.         flg=1;
  55.         }
  56.         if (*sc=='\n')
  57.         {
  58.         ans->add(expl_nl);
  59.         croom=room;
  60.         flg=0;
  61.         }
  62.         wlen=0;
  63.     }
  64.     else
  65.     {
  66.         if (wlen==0)
  67.         wptr=sc;
  68.         wlen++;
  69.     }
  70.     if (*sc=='\0') break;    // Stop condition
  71.     sc++;
  72.     }
  73.     return ans;
  74. }        
  75.