xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/vstring.h (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: vstring.h,v 1.1.1.1 2009/06/23 10:09:01 tron Exp $	*/
2 
3 #ifndef _VSTRING_H_INCLUDED_
4 #define _VSTRING_H_INCLUDED_
5 
6 /*++
7 /* NAME
8 /*	vstring 3h
9 /* SUMMARY
10 /*	arbitrary-length string manager
11 /* SYNOPSIS
12 /*	#include "vstring.h"
13 /* DESCRIPTION
14 /* .nf
15 
16  /*
17   * System library.
18   */
19 #include <stdarg.h>
20 
21  /*
22   * Utility library.
23   */
24 #include <vbuf.h>
25 
26  /*
27   * We can't allow bare VBUFs in the interface, because VSTRINGs have a
28   * specific initialization and destruction sequence.
29   */
30 typedef struct VSTRING {
31     VBUF    vbuf;
32     ssize_t  maxlen;
33 } VSTRING;
34 
35 extern VSTRING *vstring_alloc(ssize_t);
36 extern void vstring_ctl(VSTRING *,...);
37 extern VSTRING *vstring_truncate(VSTRING *, ssize_t);
38 extern VSTRING *vstring_free(VSTRING *);
39 extern VSTRING *vstring_strcpy(VSTRING *, const char *);
40 extern VSTRING *vstring_strncpy(VSTRING *, const char *, ssize_t);
41 extern VSTRING *vstring_strcat(VSTRING *, const char *);
42 extern VSTRING *vstring_strncat(VSTRING *, const char *, ssize_t);
43 extern VSTRING *vstring_memcpy(VSTRING *, const char *, ssize_t);
44 extern VSTRING *vstring_memcat(VSTRING *, const char *, ssize_t);
45 extern char *vstring_memchr(VSTRING *, int);
46 extern VSTRING *vstring_insert(VSTRING *, ssize_t, const char *, ssize_t);
47 extern VSTRING *vstring_prepend(VSTRING *, const char *, ssize_t);
48 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf(VSTRING *, const char *,...);
49 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_append(VSTRING *, const char *,...);
50 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_prepend(VSTRING *, const char *, ...);
51 extern char *vstring_export(VSTRING *);
52 extern VSTRING *vstring_import(char *);
53 
54 #define VSTRING_CTL_MAXLEN	1
55 #define VSTRING_CTL_END		0
56 
57  /*
58   * Macros. Unsafe macros have UPPERCASE names.
59   */
60 #define VSTRING_SPACE(vp, len)	((vp)->vbuf.space(&(vp)->vbuf, len))
61 #define vstring_str(vp)		((char *) (vp)->vbuf.data)
62 #define VSTRING_LEN(vp)		((ssize_t) ((vp)->vbuf.ptr - (vp)->vbuf.data))
63 #define vstring_end(vp)		((char *) (vp)->vbuf.ptr)
64 #define VSTRING_TERMINATE(vp)	do { \
65 				    if ((vp)->vbuf.cnt <= 0) \
66 					VSTRING_SPACE((vp),1); \
67 				    *(vp)->vbuf.ptr = 0; \
68 				} while (0)
69 #define VSTRING_RESET(vp)	do { \
70 				    (vp)->vbuf.ptr = (vp)->vbuf.data; \
71 				    (vp)->vbuf.cnt = (vp)->vbuf.len; \
72 				} while (0)
73 #define	VSTRING_ADDCH(vp, ch)	VBUF_PUT(&(vp)->vbuf, ch)
74 #define VSTRING_SKIP(vp)	do { \
75 				    while ((vp)->vbuf.cnt > 0 && *(vp)->vbuf.ptr) \
76 				        (vp)->vbuf.ptr++, (vp)->vbuf.cnt--; \
77 				} while (0)
78 #define vstring_avail(vp)	((vp)->vbuf.cnt)
79 
80  /*
81   * The following macro is not part of the public interface, because it can
82   * really screw up a buffer by positioning past allocated memory.
83   */
84 #define VSTRING_AT_OFFSET(vp, offset) do { \
85 	(vp)->vbuf.ptr = (vp)->vbuf.data + (offset); \
86 	(vp)->vbuf.cnt = (vp)->vbuf.len - (offset); \
87     } while (0)
88 
89 extern VSTRING *vstring_vsprintf(VSTRING *, const char *, va_list);
90 extern VSTRING *vstring_vsprintf_append(VSTRING *, const char *, va_list);
91 
92 /* BUGS
93 /*	Auto-resizing may change the address of the string data in
94 /*	a vstring structure. Beware of dangling pointers.
95 /* HISTORY
96 /* .ad
97 /* .fi
98 /*	A vstring module appears in the UNPROTO software by Wietse Venema.
99 /* LICENSE
100 /* .ad
101 /* .fi
102 /*	The Secure Mailer license must be distributed with this software.
103 /* AUTHOR(S)
104 /*	Wietse Venema
105 /*	IBM T.J. Watson Research
106 /*	P.O. Box 704
107 /*	Yorktown Heights, NY 10598, USA
108 /*--*/
109 
110 #endif
111