ack/util/ack/grows.c
2015-06-24 23:41:46 +01:00

77 lines
1.8 KiB
C

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*
*/
/**************************************************************************/
/* */
/* Bookkeeping for growing strings */
/* */
/**************************************************************************/
#include <stdlib.h>
#include "ack.h"
#include "grows.h"
void gr_add(growstring *id, char c)
{
if ( id->gr_size==id->gr_max) {
if ( id->gr_size==0 ) { /* The first time */
id->gr_max= 2*GR_MORE ;
id->gr_string= getcore(id->gr_max) ;
} else {
id->gr_max += GR_MORE ;
id->gr_string= changecore(id->gr_string,id->gr_max ) ;
}
}
*(id->gr_string+id->gr_size++)= c ;
}
void gr_cat(growstring *id, char *string)
{
register char *ptr ;
#ifdef DEBUG
if ( id->gr_size && *(id->gr_string+id->gr_size-1) ) {
vprint("Non-zero terminated %*s\n",
id->gr_size, id->gr_string ) ;
}
#endif
if ( id->gr_size ) id->gr_size-- ;
ptr=string ;
for (;;) {
gr_add(id,*ptr) ;
if ( *ptr++ ) continue ;
break ;
}
}
void gr_throw(growstring *id)
{
/* Throw the string away */
if ( id->gr_max==0 ) return ;
freecore(id->gr_string) ;
id->gr_string = 0 ;
id->gr_max=0 ;
id->gr_size=0 ;
}
void gr_init(growstring *id)
{
id->gr_size=0 ; id->gr_max=0 ;
}
char *gr_final(growstring *id)
{
/* Throw away the bookkeeping, adjust the string to its final
length and return a pointer to a string to be get rid of with
throws
*/
register char *retval ;
retval= keeps(gr_start(*id)) ;
gr_throw(id) ;
return retval ;
}