home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
xbase
/
library
/
clipper
/
rettig
/
source
/
wpstrip.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-21
|
2KB
|
81 lines
/*********
*
* WPSTRIP.C
*
* by Tom Rettig
*
* Placed in the public domain by Tom Rettig Associates, 10/22/1990.
*
* Syntax: WPSTRIP( <expC> [,<expN>] )
* Return: <expC> with all occurrences of newline CHR(10),
* carriage-return CHR(13), and soft carriage-return
* CHR(141) removed. Tab-characters are replaced with
* <expN> blank spaces (default 1 if omitted).
* Ascii values over 127 (except 141) are unchanged.
* Error message if out of memory.
* Note : Maximum <expN> is 16, minimum is zero; no error returned.
*********/
#include "trlib.h"
TRTYPE wpstrip()
{
static char funcname[] = { "wpstrip" };
char *instr, *ret;
int i, j, k, tabs, spaces, was_space;
if ( (PCOUNT==2 && ISCHAR(1) && ISNUM(2)) ||
(PCOUNT==1 && ISCHAR(1)) )
{
instr = _parc(1);
if ( PCOUNT==1 )
{
spaces = 1;
tabs = 0;
}
else
{
spaces = _parni(2) % MAXEXPAND;
if ( spaces < 0 )
spaces = 0;
for ( i=tabs=0; instr[i]; i++ ) /* count tabs */
tabs += instr[i]=='\t';
}
ret = _tr_allocmem( (unsigned)(_tr_strlen(instr)+1+(tabs*(spaces-1))));
if ( ret )
{
for ( i=0, j=0; instr[j]; j++)
{
switch ( instr[j] )
{
case ( '\t' ):
for ( k=1; k<=spaces; k++ )
ret[i++] = SPACEC;
was_space = TRUE;
break;
case ( '\r' ):
case ( '\x8d' ): /* 'soft' carriage return */
if ( !was_space )
ret[i++] = SPACEC;
/* was_space not set so as to put a space for each cr */
break;
case ( '\n' ):
break;
default:
ret[i++] = instr[j];
was_space = ( instr[j] == SPACEC );
}
}
ret[i] = NULLC;
_retc( ret );
_tr_freemem( ret,(unsigned)(_tr_strlen(instr)+1+(tabs*(spaces-1))));
}
else
_retc( _tr_errmsgs(funcname,E_ALLOC) );
}
else
_retc( _tr_errmsgs(funcname,E_SYNTAX) );
}