home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume26 / unproto / part01 / vstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-07  |  2.3 KB  |  92 lines

  1. /*++
  2. /* NAME
  3. /*    vs_alloc(), VS_ADDCH()
  4. /* SUMMARY
  5. /*    auto-resizing string library
  6. /* PACKAGE
  7. /*    vstring
  8. /* SYNOPSIS
  9. /*    #include "vstring.h"
  10. /*
  11. /*    struct vstring *vs_alloc(len)
  12. /*    int len;
  13. /*
  14. /*    int VS_ADDCH(vs, wp, ch)
  15. /*    struct vstring *vs;
  16. /*    char *wp;
  17. /*    int ch;
  18. /* DESCRIPTION
  19. /*    These functions and macros implement a small library for
  20. /*    arbitrary-length strings that grow automatically when
  21. /*    they fill up. The allocation strategy is such that there
  22. /*    will always be place for the terminating null character.
  23. /*
  24. /*    vs_alloc() allocates storage for a variable-length string.
  25. /*
  26. /*    VS_ADDCH() adds a character to a variable-length string
  27. /*    and automagically extends the string if fills up.
  28. /*    \fIvs\fP is a pointer to a vstring structure; \fIwp\fP
  29. /*    the current write position in the corresponding character
  30. /*    array; \fIch\fP the character value to be written.
  31. /*    Note that VS_ADDCH() is a macro that evaluates some
  32. /*    arguments more than once.
  33. /* DIAGNOSTICS
  34. /*    VS_ADDCH() returns zero if it was unable to dynamically
  35. /*    resize a string.
  36. /*
  37. /*    vs_alloc() returns a null pointer in case of problems.
  38. /* BUGS
  39. /*    Auto-resizing may change the address of the string data in
  40. /*    a vstring structure. Beware of dangling pointers.
  41. /* AUTHOR(S)
  42. /*    Wietse Venema
  43. /*    Eindhoven University of Technology
  44. /*    Department of Mathematics and Computer Science
  45. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  46. /* LAST MODIFICATION
  47. /*    91/09/22 21:21:38
  48. /* VERSION/RELEASE
  49. /*    1.2
  50. /*--*/
  51.  
  52. static char vstring_sccsid[] = "@(#) vstring.c 1.2 91/09/22 21:21:38";
  53.  
  54. /* C library */
  55.  
  56. extern char *malloc();
  57. extern char *realloc();
  58.  
  59. /* Application-specific stuff */
  60.  
  61. #include "vstring.h"
  62.  
  63. /* vs_alloc - initial string allocation */
  64.  
  65. struct vstring *vs_alloc(len)
  66. int     len;
  67. {
  68.     register struct vstring *vp;
  69.  
  70.     if (len < 1 
  71.     || (vp = (struct vstring *) malloc(sizeof(struct vstring))) == 0
  72.     || (vp->str = malloc(len)) == 0)
  73.     return (0);
  74.     vp->last = vp->str + len - 1;
  75.     return (vp);
  76. }
  77.  
  78. /* vs_realloc - extend string, update write pointer */
  79.  
  80. char   *vs_realloc(vp, cp)
  81. register struct vstring *vp;
  82. char   *cp;
  83. {
  84.     int     where = cp - vp->str;
  85.     int     len = vp->last - vp->str + 1;
  86.  
  87.     if ((vp->str = realloc(vp->str, len *= 2)) == 0)
  88.     return (0);
  89.     vp->last = vp->str + len - 1;
  90.     return (vp->str + where);
  91. }
  92.