c69c3ad2cfb9898043d34ee81b3f159735e93da9
[yaz-moved-to-github.git] / include / yaz / wrbuf.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /* $Id: wrbuf.h,v 1.29 2008-01-30 19:58:24 adam Exp $ */
28
29 /**
30  * \file wrbuf.h
31  * \brief Header for WRBUF (growing buffer)
32  */
33
34 #ifndef WRBUF_H
35 #define WRBUF_H
36
37 #include <yaz/xmalloc.h>
38 #include <yaz/yaz-iconv.h>
39
40 YAZ_BEGIN_CDECL
41
42 /** \brief string buffer */
43 typedef struct wrbuf
44 {
45     char *buf;
46     size_t pos;
47     size_t size;
48 } wrbuf, *WRBUF;
49
50 /** \brief allocate / construct WRBUF */
51 YAZ_EXPORT WRBUF wrbuf_alloc(void);
52
53 /** \brief destroy WRBUF and its buffer */
54 YAZ_EXPORT void wrbuf_destroy(WRBUF b);
55
56 /** \brief empty WRBUF content */
57 YAZ_EXPORT void wrbuf_rewind(WRBUF b);
58
59 /** \brief writes (append) buffer to WRBUF */
60 YAZ_EXPORT int wrbuf_write(WRBUF b, const char *buf, int size);
61 /** \brief appends C-string to WRBUF */
62 YAZ_EXPORT int wrbuf_puts(WRBUF b, const char *buf);
63
64 /** \brief writes buffer to WRBUF and XML encode (as CDATA) */
65 YAZ_EXPORT int wrbuf_xmlputs_n(WRBUF b, const char *cp, int size);
66 /** \brief writes C-String to WRBUF and XML encode (as CDATA) */
67 YAZ_EXPORT int wrbuf_xmlputs(WRBUF b, const char *cp);
68
69 YAZ_EXPORT int wrbuf_puts_replace_char(WRBUF b, const char *buf, 
70                                        const char from, const char to);
71
72 /** \brief writes buffer to WRBUF and escape non-ASCII characters */
73 YAZ_EXPORT void wrbuf_puts_escaped(WRBUF b, const char *str);
74
75 /** \brief writes C-string to WRBUF and escape non-ASCII characters */
76 YAZ_EXPORT void wrbuf_write_escaped(WRBUF b, const char *buf, size_t len);
77
78 /** \brief writes printf result to WRBUF */
79 YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...)
80 #ifdef __GNUC__
81         __attribute__ ((format (printf, 2, 3)))
82 #endif
83         ;
84
85 YAZ_EXPORT int wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf,
86                                  int size);
87 YAZ_EXPORT int wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd,
88                                        const char *buf, int size);
89 YAZ_EXPORT int wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd,
90                                       const char *strz);
91
92 YAZ_EXPORT int wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz);
93
94 YAZ_EXPORT int wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch);
95
96 YAZ_EXPORT void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd);
97
98 YAZ_EXPORT void wrbuf_chop_right(WRBUF b);
99
100 /** \brief cut size of WRBUF */
101 YAZ_EXPORT void wrbuf_cut_right(WRBUF b, size_t no_to_remove);
102
103
104 /** \brief grow WRBUF larger 
105     This function is normally not used by applications
106 */
107 YAZ_EXPORT int wrbuf_grow(WRBUF b, int minsize);
108
109 #define wrbuf_len(b) ((b)->pos)
110 #define wrbuf_buf(b) ((b)->buf)
111
112 YAZ_EXPORT const char *wrbuf_cstr(WRBUF b);
113
114 #define wrbuf_putc(b, c) \
115     (((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0),  \
116     (b)->buf[(b)->pos++] = (c), 0)
117
118 YAZ_END_CDECL
119
120 #endif
121 /*
122  * Local variables:
123  * c-basic-offset: 4
124  * indent-tabs-mode: nil
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */
128