home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3952 / vstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-02  |  2.2 KB  |  88 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. /*    extern 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. /*    It returns a null pointer in case of problems.
  26. /*
  27. /*    VS_ADDCH() adds a character to a variable-length string
  28. /*    and automagically extends the string if fills up.
  29. /*    \fIvs\fp is a pointer to a vstring structure; \fIwp\fP
  30. /*    the current write position in the corresponding character
  31. /*    array; \fIch\fP the character value to be written.
  32. /*    Note that VS_ADDCH() is a macro that evaluates some
  33. /*    arguments more than once.
  34. /* BUGS
  35. /*    Auto-resizing may change the address of the string data in
  36. /*    a vstring structure. Beware of dangling pointers.
  37. /* AUTHOR(S)
  38. /*    Wietse Venema
  39. /*    Eindhoven University of Technology
  40. /*    Department of Mathematics and Computer Science
  41. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  42. /* LAST MODIFICATION
  43. /*    91/09/01 23:08:38
  44. /* VERSION/RELEASE
  45. /*    1.1
  46. /*--*/
  47.  
  48. static char vstring_sccsid[] = "@(#) vstring.c 1.1 91/09/01 23:08:38";
  49.  
  50. /* C library */
  51.  
  52. extern char *malloc();
  53. extern char *realloc();
  54.  
  55. /* Application-specific stuff */
  56.  
  57. #include "vstring.h"
  58.  
  59. /* vs_alloc - initial string allocation */
  60.  
  61. struct vstring *vs_alloc(len)
  62. int     len;
  63. {
  64.     register struct vstring *vp;
  65.  
  66.     if (len < 1 
  67.     || (vp = (struct vstring *) malloc(sizeof(struct vstring))) == 0
  68.     || (vp->str = malloc(len)) == 0)
  69.     return (0);
  70.     vp->last = vp->str + len - 1;
  71.     return (vp);
  72. }
  73.  
  74. /* vs_realloc - extend string, update write pointer */
  75.  
  76. char   *vs_realloc(vp, cp)
  77. register struct vstring *vp;
  78. char   *cp;
  79. {
  80.     int     where = cp - vp->str;
  81.     int     len = vp->last - vp->str + 1;
  82.  
  83.     if ((vp->str = realloc(vp->str, len *= 2)) == 0)
  84.     return (0);
  85.     vp->last = vp->str + len - 1;
  86.     return (vp->str + where);
  87. }
  88.