home *** CD-ROM | disk | FTP | other *** search
- /*********
- *
- * TABSTRIP.C
- *
- * by Tom Rettig
- *
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: TABSTRIP( <expC> [,<expN>] )
- * Return: <expC> with tab-characters replaced with
- * <expN> blank spaces (default 1 if omitted).
- * Unchanged <expC> if out of memory.
- * Note : Maximum <expN> is 16, minimum is zero; no error returned.
- *********/
-
- #include "trlib.h"
-
- TRTYPE tabstrip()
- {
- static char funcname[] = { "tabstrip" };
- char *instr, *ret;
- int i, j, k, tabs, spaces;
-
- 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++)
- {
- if ( instr[j] == '\t' )
- {
- for ( k=1; k<=spaces; k++ )
- ret[i++] = SPACEC;
- }
- else
- ret[i++] = instr[j];
- }
- 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) );
- }
-