grows.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /* Bookkeeping for growing strings */
  9. /* */
  10. /**************************************************************************/
  11. #include "ack.h"
  12. #include "grows.h"
  13. #ifndef NORCSID
  14. static char rcs_id[] = "$Id$" ;
  15. static char rcs_grows[] = RCS_GROWS ;
  16. #endif
  17. gr_add(id,c) register growstring *id ; char c ; {
  18. if ( id->gr_size==id->gr_max) {
  19. if ( id->gr_size==0 ) { /* The first time */
  20. id->gr_max= 2*GR_MORE ;
  21. id->gr_string= getcore(id->gr_max) ;
  22. } else {
  23. id->gr_max += GR_MORE ;
  24. id->gr_string= changecore(id->gr_string,id->gr_max ) ;
  25. }
  26. }
  27. *(id->gr_string+id->gr_size++)= c ;
  28. }
  29. gr_cat(id,string) growstring *id ; char *string ; {
  30. register char *ptr ;
  31. #ifdef DEBUG
  32. if ( id->gr_size && *(id->gr_string+id->gr_size-1) ) {
  33. vprint("Non-zero terminated %*s\n",
  34. id->gr_size, id->gr_string ) ;
  35. }
  36. #endif
  37. if ( id->gr_size ) id->gr_size-- ;
  38. ptr=string ;
  39. for (;;) {
  40. gr_add(id,*ptr) ;
  41. if ( *ptr++ ) continue ;
  42. break ;
  43. }
  44. }
  45. gr_throw(id) register growstring *id ; {
  46. /* Throw the string away */
  47. if ( id->gr_max==0 ) return ;
  48. freecore(id->gr_string) ;
  49. id->gr_string = 0 ;
  50. id->gr_max=0 ;
  51. id->gr_size=0 ;
  52. }
  53. gr_init(id) growstring *id ; {
  54. id->gr_size=0 ; id->gr_max=0 ;
  55. }
  56. char *gr_final(id) growstring *id ; {
  57. /* Throw away the bookkeeping, adjust the string to its final
  58. length and return a pointer to a string to be get rid of with
  59. throws
  60. */
  61. register char *retval ;
  62. retval= keeps(gr_start(*id)) ;
  63. gr_throw(id) ;
  64. return retval ;
  65. }