grows.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. *
  4. * This product is part of the Amsterdam Compiler Kit.
  5. *
  6. * Permission to use, sell, duplicate or disclose this software must be
  7. * obtained in writing. Requests for such permissions may be sent to
  8. *
  9. * Dr. Andrew S. Tanenbaum
  10. * Wiskundig Seminarium
  11. * Vrije Universiteit
  12. * Postbox 7161
  13. * 1007 MC Amsterdam
  14. * The Netherlands
  15. *
  16. */
  17. /**************************************************************************/
  18. /* */
  19. /* Bookkeeping for growing strings */
  20. /* */
  21. /**************************************************************************/
  22. #include "ack.h"
  23. #include "grows.h"
  24. #ifndef NORCSID
  25. static char rcs_id[] = "$Header$" ;
  26. static char rcs_grows[] = RCS_GROWS ;
  27. #endif
  28. gr_add(id,c) register growstring *id ; char c ; {
  29. if ( id->gr_size==id->gr_max) {
  30. if ( id->gr_size==0 ) { /* The first time */
  31. id->gr_max= 2*GR_MORE ;
  32. id->gr_string= getcore(id->gr_max) ;
  33. } else {
  34. id->gr_max += GR_MORE ;
  35. id->gr_string= changecore(id->gr_string,id->gr_max ) ;
  36. }
  37. }
  38. *(id->gr_string+id->gr_size++)= c ;
  39. }
  40. gr_cat(id,string) growstring *id ; char *string ; {
  41. register char *ptr ;
  42. #ifdef DEBUG
  43. if ( id->gr_size && *(id->gr_string+id->gr_size-1) ) {
  44. vprint("Non-zero terminated %*s\n",
  45. id->gr_size, id->gr_string ) ;
  46. }
  47. #endif
  48. if ( id->gr_size ) id->gr_size-- ;
  49. ptr=string ;
  50. for (;;) {
  51. gr_add(id,*ptr) ;
  52. if ( *ptr++ ) continue ;
  53. break ;
  54. }
  55. }
  56. gr_throw(id) register growstring *id ; {
  57. /* Throw the string away */
  58. if ( id->gr_max==0 ) return ;
  59. freecore(id->gr_string) ;
  60. id->gr_max=0 ;
  61. id->gr_size=0 ;
  62. }
  63. gr_init(id) growstring *id ; {
  64. id->gr_size=0 ; id->gr_max=0 ;
  65. }
  66. char *gr_final(id) growstring *id ; {
  67. /* Throw away the bookkeeping, adjust the string to its final
  68. length and return a pointer to a string to be get rid of with
  69. throws
  70. */
  71. register char *retval ;
  72. retval= keeps(gr_start(*id)) ;
  73. gr_throw(id) ;
  74. return retval ;
  75. }