grows.c 1.8 KB

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