]> git.sur5r.net Git - openldap/blob - build/unproto/vstring.c
ITS#1991 fix + use struct berval
[openldap] / build / unproto / vstring.c
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 /*
19 /*      char *vs_strcpy(vp, dst, src)
20 /*      struct vstring *vp;
21 /*      char *dst;
22 /*      char *src;
23 /* DESCRIPTION
24 /*      These functions and macros implement a small library for
25 /*      arbitrary-length strings that grow automatically when
26 /*      they fill up. The allocation strategy is such that there
27 /*      will always be place for the terminating null character.
28 /*
29 /*      vs_alloc() allocates storage for a variable-length string
30 /*      of at least "len" bytes.
31 /*
32 /*      VS_ADDCH() adds a character to a variable-length string
33 /*      and automagically extends the string if fills up.
34 /*      \fIvs\fP is a pointer to a vstring structure; \fIwp\fP
35 /*      the current write position in the corresponding character
36 /*      array; \fIch\fP the character value to be written.
37 /*      Note that VS_ADDCH() is a macro that evaluates some
38 /*      arguments more than once.
39 /*
40 /*      vs_strcpy() appends a null-terminated string to a variable-length
41 /*      string. \fIsrc\fP provides the data to be copied; \fIvp\fP is the
42 /*      target, and \fIdst\fP the current write position within the target.
43 /*      The result is null-terminated. The return value is the new write
44 /*      position.
45 /* DIAGNOSTICS
46 /*      VS_ADDCH() returns zero if it was unable to dynamically
47 /*      resize a string.
48 /*
49 /*      vs_alloc() returns a null pointer in case of problems.
50 /*
51 /*      vs_strcpy() returns a null pointer if the request failed.
52 /* BUGS
53 /*      Auto-resizing may change the address of the string data in
54 /*      a vstring structure. Beware of dangling pointers.
55 /* AUTHOR(S)
56 /*      Wietse Venema
57 /*      Eindhoven University of Technology
58 /*      Department of Mathematics and Computer Science
59 /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
60 /* LAST MODIFICATION
61 /*      92/01/15 21:53:06
62 /* VERSION/RELEASE
63 /*      1.3
64 /*--*/
65
66 static char vstring_sccsid[] = "@(#) vstring.c 1.3 92/01/15 21:53:06";
67
68 /* C library */
69
70 extern char *malloc();
71 extern char *realloc();
72
73 /* Application-specific stuff */
74
75 #include "vstring.h"
76
77 /* vs_alloc - initial string allocation */
78
79 struct vstring *vs_alloc(len)
80 int     len;
81 {
82     register struct vstring *vp;
83
84     if (len < 1 
85         || (vp = (struct vstring *) malloc(sizeof(struct vstring))) == 0
86         || (vp->str = malloc(len)) == 0)
87         return (0);
88     vp->last = vp->str + len - 1;
89     return (vp);
90 }
91
92 /* vs_realloc - extend string, update write pointer */
93
94 char   *vs_realloc(vp, cp)
95 register struct vstring *vp;
96 char   *cp;
97 {
98     int     where = cp - vp->str;
99     int     len = vp->last - vp->str + 1;
100
101     if ((vp->str = realloc(vp->str, len *= 2)) == 0)
102         return (0);
103     vp->last = vp->str + len - 1;
104     return (vp->str + where);
105 }
106
107 /* vs_strcpy - copy string */
108
109 char   *vs_strcpy(vp, dst, src)
110 register struct vstring *vp;
111 register char *dst;
112 register char *src;
113 {
114     while (*src) {
115         if (VS_ADDCH(vp, dst, *src) == 0)
116             return (0);
117         src++;
118     }
119     *dst = '\0';
120     return (dst);
121 }
122