Postfix3.3.1
vstring.h
[詳解]
1 #ifndef _VSTRING_H_INCLUDED_
2 #define _VSTRING_H_INCLUDED_
3 
4 /*++
5 /* NAME
6 /* vstring 3h
7 /* SUMMARY
8 /* arbitrary-length string manager
9 /* SYNOPSIS
10 /* #include "vstring.h"
11 /* DESCRIPTION
12 /* .nf
13 
14  /*
15  * System library.
16  */
17 #include <stdarg.h>
18 
19  /*
20  * Utility library.
21  */
22 #include <vbuf.h>
23 #include <check_arg.h>
24 
25  /*
26  * We can't allow bare VBUFs in the interface, because VSTRINGs have a
27  * specific initialization and destruction sequence.
28  */
29 typedef struct VSTRING {
31  ssize_t maxlen;
32 } VSTRING;
33 
34 extern VSTRING *vstring_alloc(ssize_t);
35 extern void vstring_ctl(VSTRING *,...);
36 extern VSTRING *vstring_truncate(VSTRING *, ssize_t);
37 extern VSTRING *vstring_free(VSTRING *);
38 extern VSTRING *vstring_strcpy(VSTRING *, const char *);
39 extern VSTRING *vstring_strncpy(VSTRING *, const char *, ssize_t);
40 extern VSTRING *vstring_strcat(VSTRING *, const char *);
41 extern VSTRING *vstring_strncat(VSTRING *, const char *, ssize_t);
42 extern VSTRING *vstring_memcpy(VSTRING *, const char *, ssize_t);
43 extern VSTRING *vstring_memcat(VSTRING *, const char *, ssize_t);
44 extern char *vstring_memchr(VSTRING *, int);
45 extern VSTRING *vstring_insert(VSTRING *, ssize_t, const char *, ssize_t);
46 extern VSTRING *vstring_prepend(VSTRING *, const char *, ssize_t);
47 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf(VSTRING *, const char *,...);
48 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_append(VSTRING *, const char *,...);
49 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_prepend(VSTRING *, const char *,...);
50 extern char *vstring_export(VSTRING *);
51 extern VSTRING *vstring_import(char *);
52 
53 /* Legacy API: constant plus type-unchecked argument. */
54 #define VSTRING_CTL_MAXLEN 1
55 #define VSTRING_CTL_EXACT 2
56 #define VSTRING_CTL_END 0
57 
58 /* Safer API: type-checked arguments. */
59 #define CA_VSTRING_CTL_END VSTRING_CTL_END
60 #define CA_VSTRING_CTL_EXACT VSTRING_CTL_EXACT
61 #define CA_VSTRING_CTL_MAXLEN(val) VSTRING_CTL_MAXLEN, CHECK_VAL(VSTRING_CTL, ssize_t, (val))
62 
63 CHECK_VAL_HELPER_DCL(VSTRING_CTL, ssize_t);
64 
65 #define VSTRING_FLAG_EXACT (1<<8) /* exact allocation for tests */
66 
67  /*
68  * Macros. Unsafe macros have UPPERCASE names.
69  */
70 #define VSTRING_SPACE(vp, len) ((vp)->vbuf.space(&(vp)->vbuf, (len)))
71 #define vstring_str(vp) ((char *) (vp)->vbuf.data)
72 #define VSTRING_LEN(vp) ((ssize_t) ((vp)->vbuf.ptr - (vp)->vbuf.data))
73 #define vstring_end(vp) ((char *) (vp)->vbuf.ptr)
74 #define VSTRING_TERMINATE(vp) do { \
75  *(vp)->vbuf.ptr = 0; \
76  } while (0)
77 #define VSTRING_RESET(vp) do { \
78  (vp)->vbuf.ptr = (vp)->vbuf.data; \
79  (vp)->vbuf.cnt = (vp)->vbuf.len; \
80  } while (0)
81 #define VSTRING_ADDCH(vp, ch) VBUF_PUT(&(vp)->vbuf, ch)
82 #define VSTRING_SKIP(vp) do { \
83  while ((vp)->vbuf.cnt > 0 && *(vp)->vbuf.ptr) \
84  (vp)->vbuf.ptr++, (vp)->vbuf.cnt--; \
85  } while (0)
86 #define vstring_avail(vp) ((vp)->vbuf.cnt)
87 
88  /*
89  * The following macro is not part of the public interface, because it can
90  * really screw up a buffer by positioning past allocated memory.
91  */
92 #define VSTRING_AT_OFFSET(vp, offset) do { \
93  (vp)->vbuf.ptr = (vp)->vbuf.data + (offset); \
94  (vp)->vbuf.cnt = (vp)->vbuf.len - (offset); \
95  } while (0)
96 
97 extern VSTRING *vstring_vsprintf(VSTRING *, const char *, va_list);
98 extern VSTRING *vstring_vsprintf_append(VSTRING *, const char *, va_list);
99 
100 /* BUGS
101 /* Auto-resizing may change the address of the string data in
102 /* a vstring structure. Beware of dangling pointers.
103 /* HISTORY
104 /* .ad
105 /* .fi
106 /* A vstring module appears in the UNPROTO software by Wietse Venema.
107 /* LICENSE
108 /* .ad
109 /* .fi
110 /* The Secure Mailer license must be distributed with this software.
111 /* AUTHOR(S)
112 /* Wietse Venema
113 /* IBM T.J. Watson Research
114 /* P.O. Box 704
115 /* Yorktown Heights, NY 10598, USA
116 /*
117 /* Wietse Venema
118 /* Google, Inc.
119 /* 111 8th Avenue
120 /* New York, NY 10011, USA
121 /*--*/
122 
123 #endif
VSTRING * vstring_sprintf_prepend(VSTRING *vp, const char *format,...)
Definition: vstring.c:645
VSTRING * vstring_insert(VSTRING *, ssize_t, const char *, ssize_t)
Definition: vstring.c:518
VSTRING * vstring_strcat(VSTRING *, const char *)
Definition: vstring.c:459
VSTRING const char VSTRING const char VSTRING const char char * vstring_export(VSTRING *)
Definition: vstring.c:569
VSTRING * vstring_prepend(VSTRING *, const char *, ssize_t)
Definition: vstring.c:545
VSTRING * vstring_strncat(VSTRING *, const char *, ssize_t)
Definition: vstring.c:471
VSTRING * vstring_vsprintf_append(VSTRING *, const char *, va_list)
Definition: vstring.c:636
char * vstring_memchr(VSTRING *, int)
Definition: vstring.c:506
VSTRING * vstring_truncate(VSTRING *, ssize_t)
Definition: vstring.c:415
VSTRING * vstring_sprintf_append(VSTRING *vp, const char *format,...)
Definition: vstring.c:624
VSTRING * vstring_vsprintf(VSTRING *, const char *, va_list)
Definition: vstring.c:614
CHECK_VAL_HELPER_DCL(VSTRING_CTL, ssize_t)
VSTRING * vstring_free(VSTRING *)
Definition: vstring.c:380
Definition: vbuf.h:37
VSTRING * vstring_alloc(ssize_t)
Definition: vstring.c:353
ssize_t maxlen
Definition: vstring.h:31
VBUF vbuf
Definition: vstring.h:30
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
Definition: vstring.c:602
struct VSTRING VSTRING
VSTRING * vstring_strcpy(VSTRING *, const char *)
Definition: vstring.c:431
VSTRING * PRINTFLIKE(2, 3) vstring_sprintf(VSTRING *
VSTRING * vstring_memcpy(VSTRING *, const char *, ssize_t)
Definition: vstring.c:483
VSTRING * vstring_strncpy(VSTRING *, const char *, ssize_t)
Definition: vstring.c:445
VSTRING * vstring_import(char *)
Definition: vstring.c:581
void vstring_ctl(VSTRING *,...)
Definition: vstring.c:390
VSTRING * vstring_memcat(VSTRING *, const char *, ssize_t)
Definition: vstring.c:495