Pristine Ack-5.5
[Ack-5.5.git] / util / ack / grows.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  */
6
7 /**************************************************************************/
8 /*                                                                        */
9 /*                 Bookkeeping for growing strings                        */
10 /*                                                                        */
11 /**************************************************************************/
12
13 #include "ack.h"
14 #include "grows.h"
15
16 #ifndef NORCSID
17 static char rcs_id[] = "$Id: grows.c,v 2.4 1994/06/24 10:12:40 ceriel Exp $" ;
18 static char rcs_grows[] = RCS_GROWS ;
19 #endif
20
21 gr_add(id,c) register growstring *id ; char c ; {
22         if ( id->gr_size==id->gr_max) {
23                 if ( id->gr_size==0 ) { /* The first time */
24                         id->gr_max= 2*GR_MORE ;
25                         id->gr_string= getcore(id->gr_max) ;
26                 } else {
27                         id->gr_max += GR_MORE ;
28                         id->gr_string= changecore(id->gr_string,id->gr_max ) ;
29                 }
30         }
31         *(id->gr_string+id->gr_size++)= c ;
32 }
33
34 gr_cat(id,string) growstring *id ; char *string ; {
35         register char *ptr ;
36
37 #ifdef DEBUG
38         if ( id->gr_size && *(id->gr_string+id->gr_size-1) ) {
39                 vprint("Non-zero terminated %*s\n",
40                         id->gr_size, id->gr_string ) ;
41         }
42 #endif
43         if ( id->gr_size ) id->gr_size-- ;
44         ptr=string ;
45         for (;;) {
46                 gr_add(id,*ptr) ;
47                 if ( *ptr++ ) continue ;
48                 break ;
49         }
50 }
51
52 gr_throw(id) register growstring *id ; {
53         /* Throw the string away */
54         if ( id->gr_max==0 ) return ;
55         freecore(id->gr_string) ;
56         id->gr_string = 0 ;
57         id->gr_max=0 ;
58         id->gr_size=0 ;
59 }
60
61 gr_init(id) growstring *id ; {
62         id->gr_size=0 ; id->gr_max=0 ;
63 }
64
65 char *gr_final(id) growstring *id ; {
66         /* Throw away the bookkeeping, adjust the string to its final
67            length and return a pointer to a string to be get rid of with
68            throws
69         */
70         register char *retval ;
71         retval= keeps(gr_start(*id)) ;
72         gr_throw(id) ;
73         return retval ;
74 }