home *** CD-ROM | disk | FTP | other *** search
- /*********
- *
- * LINES.C
- *
- * by Tom Rettig
- * modified by Leonard Zerman
- *
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: LINES( <expC>, <line len> )
- * Return: <expN> number of lines of length <line len> in <expC>.
- * Zero if <expC> is null, if <line len> is <= 0,
- * or if <line len> is too small to handle an unbroken word.
- *********/
-
- #include "trlib.h"
-
- TRTYPE lines()
- {
- char *instr;
- int i, j, len, maxline, ret;
-
- if ( PCOUNT==2 && ISCHAR(1) && ISNUM(2) )
- {
- instr = _parc(1);
- maxline = _parni(2);
- len = _tr_strlen(instr);
-
- /* Return error if negative or zero line length or an empty string */
- if ( maxline <= 0 || len < 1 )
- _retni( ERROR );
- else
- {
- /* Repeat until all full lines have been counted */
- for ( i=maxline-1, j=0, ret=1; i+maxline <= len; i+=maxline )
- {
- /* Back up to first space if in middle of a word */
- while ( instr[i] != SPACEC && instr[i+1] != SPACEC )
- i--;
-
- if ( i >= j )
- {
- /* move head pointer and increment line counter */
- j = i + 1;
- ret++;
- }
- else
- {
- /* error if word is too long to fit in maxline */
- _retni( ERROR );
- return;
- }
- }
- /* add one for last line if any characters left over */
- if ( (i+1) < len )
- ret ++;
-
- /* return the number of lines */
- _retni( ret );
- }
- }
- else
- _retni( ERROR );
- }
-