grows.c 1.8 KB

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