home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
xbase
/
library
/
clipper
/
rettig
/
source
/
center.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-21
|
2KB
|
64 lines
/*********
* CENTER.C
*
* by Tom Rettig
*
* Placed in the public domain by Tom Rettig Associates, 10/22/1990.
*
* Syntax: CENTER( <expC>, <expN> )
* Return: <expC> preceded with <expN>/2 spaces.
* Unchanged <expC> if <expN> is <= LEN(<expC>).
* Note : Does not trim leading or trailing spaces from <expC>.
*********/
#include "trlib.h"
TRTYPE center()
{
static char funcname[] = { "center" };
char *instr, *ret;
int i, j, spaces, len, size;
if ( PCOUNT==2 && ISCHAR(1) && ISNUM(2) )
{
instr = _parc(1);
spaces = _parni(2);
len = _tr_strlen(instr);
size = len + 2 + (spaces/2);
ret = _tr_allocmem( (unsigned)size);
if (spaces <= 0) /* if spaces is a negative number */
{
_retc(instr);
_tr_freemem( ret,(unsigned)size);
return;
}
if ( ret )
{
if ( spaces>len )
{
for ( i=0; i<=(spaces-len)/2; i++ )
ret[i] = SPACEC;
for ( j=0; instr[j]; i++, j++ )
ret[i] = instr[j];
ret[i] = NULLC;
_retc( ret );
_tr_freemem( ret,(unsigned)size);
}
else /* return unchanged if spaces < len of string */
{
_retc( instr );
_tr_freemem( ret,(unsigned)size);
}
}
else
_retc( _tr_errmsgs(funcname,E_ALLOC) );
}
else
_retc( _tr_errmsgs(funcname,E_SYNTAX) );
}