home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
word2x0a.zip
/
source
/
wordwrap.cc
< prev
Wrap
C/C++ Source or Header
|
1998-08-08
|
1KB
|
75 lines
/* $Id: wordwrap.cc,v 1.5 1997/03/23 13:52:15 dps Exp $ */
/* Wordwrap function for library */
#include <stdio.h>
#include <stdlib.h>
#include <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=0)
{
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) */
//
// isspace is broken FUCK
// thinks german umlaute are spaces
// original: if (isspace(*sc) || *sc=='\n' || *sc=='\0')
// (jk)
if ( *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(' '); // debug
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;
}