diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/word2x/wordwrap.cc |
Initial commit
Diffstat (limited to 'support/word2x/wordwrap.cc')
-rw-r--r-- | support/word2x/wordwrap.cc | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/support/word2x/wordwrap.cc b/support/word2x/wordwrap.cc new file mode 100644 index 0000000000..14fa764c77 --- /dev/null +++ b/support/word2x/wordwrap.cc @@ -0,0 +1,74 @@ +/* $Id: wordwrap.cc,v 1.5 1997/03/23 13:52:15 dps Exp $ */ +/* Wordwrap function for library */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ +#include <stdio.h> +#include <stdlib.h> +#ifdef HAVE_CTYPE_H +#include <ctype.h> +#endif /* HAVE_CTYPE_H */ +#include "tblock.h" +#define __EXLCUDE_READER_CLASSES +#include "lib.h" + + +/* Word wrap text into lines of length room */ +tblock *word_wrap(const char *txt, const char *nl, const char *expl_nl, + const int room, const int ilen) +{ + struct tblock *ans; + const char *wptr, *sc; + int wlen, croom, flg; + int nl_len; // Performance hack + + ans=new(tblock); + + wlen=0; + wptr=sc=txt; + croom=room-ilen; + nl_len=strlen(nl); + flg=0; + + while (1) + { + /* FIXME: huge words might cause an oversize line */ + /* (this is not a typesetting program like *roff) */ + if (isspace(*sc) || *sc=='\n' || *sc=='\0') + { + if (wlen+flg>croom) + { + ans->add(nl,nl_len); + croom=room; + flg=0; + } + if (wlen>0) + { + if (flg) + { + ans->add(' '); + croom--; + } + ans->add(wptr, wlen); + croom-=wlen; + flg=1; + } + if (*sc=='\n') + { + ans->add(expl_nl); + croom=room; + flg=0; + } + wlen=0; + } + else + { + if (wlen==0) + wptr=sc; + wlen++; + } + if (*sc=='\0') break; // Stop condition + sc++; + } + return ans; +} |