1 /* This file is part of the YAZ toolkit. 2 * Copyright (C) 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 28 /** 29 * \file wrbuf.h 30 * \brief Header for WRBUF (growing buffer) 31 */ 32 33 #ifndef WRBUF_H 34 #define WRBUF_H 35 36 #include <yaz/xmalloc.h> 37 #include <yaz/yaz-iconv.h> 38 39 YAZ_BEGIN_CDECL 40 41 /** \brief string buffer */ 42 typedef struct wrbuf 43 { 44 char *buf; 45 size_t pos; 46 size_t size; 47 } wrbuf, *WRBUF; 48 49 /** \brief construct WRBUF 50 \returns WRBUF 51 */ 52 YAZ_EXPORT WRBUF wrbuf_alloc(void); 53 54 /** \brief destroy WRBUF and its buffer 55 \param b WRBUF 56 57 For YAZ 4.0.2 WRBUF b may be NULL. 58 */ 59 YAZ_EXPORT void wrbuf_destroy(WRBUF b); 60 61 /** \brief empty WRBUF content (length of buffer set to 0) 62 \param b WRBUF 63 */ 64 YAZ_EXPORT void wrbuf_rewind(WRBUF b); 65 66 /** \brief append constant size buffer to WRBUF 67 \param b WRBUF 68 \param buf buffer 69 \param size size of buffer 70 */ 71 YAZ_EXPORT void wrbuf_write(WRBUF b, const char *buf, size_t size); 72 73 /** \brief inserts buffer into WRBUF at some position 74 \param b WRBUF 75 \param pos position (0=beginning) 76 \param buf buffer 77 \param size size of buffer 78 */ 79 YAZ_EXPORT void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size); 80 81 /** \brief appends C-string to WRBUF 82 \param b WRBUF 83 \param buf C-string (0-terminated) 84 */ 85 YAZ_EXPORT void wrbuf_puts(WRBUF b, const char *buf); 86 87 /** \brief appends C-string to WRBUF - void pointer variant 88 \param buf C-string 89 \param client_data assumed WRBUF 90 */ 91 YAZ_EXPORT void wrbuf_vp_puts(const char *buf, void *client_data); 92 93 /** \brief writes buffer of certain size to WRBUF and XML encode (as CDATA) 94 \param b WRBUF 95 \param cp CDATA 96 \param size size of CDATA 97 */ 98 YAZ_EXPORT void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size); 99 100 /** \brief writes C-String to WRBUF and XML encode (as CDATA) 101 \param b WRBUF 102 \param cp CDATA buffer (0-terminated) 103 */ 104 YAZ_EXPORT void wrbuf_xmlputs(WRBUF b, const char *cp); 105 106 /** \brief puts buf to WRBUF and replaces a single char 107 \param b WRBUF 108 \param buf buffer to append (C-string) 109 \param from character "from" 110 \param to charcter "to" 111 */ 112 YAZ_EXPORT void wrbuf_puts_replace_char(WRBUF b, const char *buf, 113 const char from, const char to); 114 115 /** \brief puts buf to WRBUF and replaces a string with another 116 \param b WRBUF 117 \param buf buffer to append (C-string) 118 \param from pattern string 119 \param to replacement string 120 */ 121 YAZ_EXPORT void wrbuf_puts_replace_str(WRBUF b, const char *buf, 122 const char *from, const char *to); 123 124 /** \brief writes C-string to WRBUF and escape non-ASCII characters 125 \param b WRBUF 126 \param str C-string 127 128 Non-ASCII characters will be presented as \\xDD . 129 */ 130 YAZ_EXPORT void wrbuf_puts_escaped(WRBUF b, const char *str); 131 132 /** \brief writes buffer to WRBUF and escape non-ASCII characters 133 \param b WRBUF 134 \param buf buffer 135 \param len size of buffer 136 137 Non-ASCII characters will be presented as \\xDD . 138 */ 139 YAZ_EXPORT void wrbuf_write_escaped(WRBUF b, const char *buf, size_t len); 140 141 /** \brief writes printf result to WRBUF 142 \param b WRBUF 143 \param fmt printf-like format 144 */ 145 YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...) 146 #ifdef __GNUC__ 147 __attribute__ ((format (printf, 2, 3))) 148 #endif 149 ; 150 151 /** \brief General writer of string using iconv and cdata 152 \param b WRBUF 153 \param cd iconv handle (0 for no conversion) 154 \param buf buffer 155 \param size size of buffer 156 \param wfunc write handler (that takes WRBUF only) 157 \returns -1 if invalid sequence was encountered (truncation in effect) 158 \returns 0 if buffer could be converted and written 159 */ 160 int wrbuf_iconv_write2(WRBUF b, yaz_iconv_t cd, const char *buf, 161 size_t size, 162 void (*wfunc)(WRBUF, const char *, size_t)); 163 164 /** \brief writer of string using iconv and cdata 165 166 Obsolete: use wrbuf_iconv_write2 instead. 167 \param b WRBUF 168 \param cd iconv handle (0 for no conversion) 169 \param buf buffer 170 \param size size of buffer 171 \param cdata non-zero for CDATA; 0 for cdata 172 \returns -1 if invalid sequence was encountered (truncation in effect) 173 \returns 0 if buffer could be converted and written 174 */ 175 int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, 176 size_t size, int cdata) 177 #ifdef __GNUC__ 178 __attribute__ ((deprecated)) 179 #endif 180 ; 181 182 /** \brief Converts buffer using iconv and appends to WRBUF 183 \param b WRBUF 184 \param cd iconv handle 185 \param buf buffer 186 \param size size of buffer 187 */ 188 YAZ_EXPORT void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, 189 size_t size); 190 191 /** \brief Converts buffer using iconv and appends to WRBUF as XML CDATA 192 \param b WRBUF 193 \param cd iconv handle 194 \param buf buffer 195 \param size size of buffer 196 */ 197 YAZ_EXPORT void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, 198 const char *buf, size_t size); 199 200 /** \brief iconv converts C-string and appends to WRBUF 201 \param b WRBUF 202 \param cd iconv handle 203 \param str C-string 204 */ 205 YAZ_EXPORT void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *str); 206 207 /** \brief iconv converts C-string and appends to WRBUF as XML CDATA 208 \param b WRBUF 209 \param cd iconv handle 210 \param str C-string 211 */ 212 YAZ_EXPORT void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, 213 const char *str); 214 215 /** \brief iconv converts character and appends to WRBUF 216 \param b WRBUF 217 \param cd iconv handle 218 \param ch character 219 */ 220 YAZ_EXPORT void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch); 221 222 /** \brief iconv reset(flush) to WRBUF 223 \param b 224 \param cd iconv handle 225 226 This function calls iconv(cd, 0, 0, ..) to make it 227 flush any remaining content. 228 */ 229 YAZ_EXPORT void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd); 230 231 /** \brief chips traling blanks away from WRBUF 232 \param b WRBUF 233 */ 234 YAZ_EXPORT void wrbuf_chop_right(WRBUF b); 235 236 /** \brief cut size of WRBUF 237 \param b WRBUF 238 \param no_to_remove number of bytes to remove 239 */ 240 YAZ_EXPORT void wrbuf_cut_right(WRBUF b, size_t no_to_remove); 241 242 /** \brief grow WRBUF larger 243 \param b WRBUF 244 \param minsize make WRBUF at least this size 245 246 This function is normally not used by applications 247 */ 248 YAZ_EXPORT int wrbuf_grow(WRBUF b, size_t minsize); 249 250 #define wrbuf_len(b) ((b)->pos) 251 #define wrbuf_buf(b) ((b)->buf) 252 253 /** \brief returns WRBUF content as C-string 254 \param b WRBUF (may not be NULL) 255 \returns C-string 256 */ 257 YAZ_EXPORT const char *wrbuf_cstr(WRBUF b); 258 259 /** \brief returns WRBUF content as C-string or NULL 260 \param b WRBUF 261 \returns C-string or NULL 262 263 This function returns NULL if either b is NULL or length of buffer is 0 264 */ 265 YAZ_EXPORT 266 const char *wrbuf_cstr_null(WRBUF b); 267 268 #define wrbuf_putc(b, c) \ 269 ((void) ((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0), \ 270 (b)->buf[(b)->pos++] = (c)) 271 272 273 /** \brief writes JSON text to WRBUF with escaping 274 \param b result 275 \param str input string to be encoded 276 */ 277 YAZ_EXPORT 278 void wrbuf_json_puts(WRBUF b, const char *str); 279 280 /** \brief writes JSON text to WRBUF with escaping 281 \param b result 282 \param cp char buffer 283 \param sz size of char buffer 284 */ 285 YAZ_EXPORT 286 void wrbuf_json_write(WRBUF b, const char *cp, size_t sz); 287 288 YAZ_EXPORT 289 void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd, 290 const char *buf, size_t size); 291 292 YAZ_EXPORT 293 void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz); 294 295 /** \brief writes SHA1 text to WRBUF 296 \param b result 297 \param cp char buffer 298 \param sz size of char buffer 299 \param hexit 1=hex mode; 0=binary 300 \returns 0 if successful 301 \returns -1 on error 302 */ 303 YAZ_EXPORT 304 int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit); 305 306 /** \brief writes SHA1 text to WRBUF 307 \param b result 308 \param cp C-string 309 \param hexit 1=hex mode; 0=binary 310 \returns 0 if successful 311 \returns -1 on error 312 */ 313 YAZ_EXPORT 314 int wrbuf_sha1_puts(WRBUF b, const char *cp, int hexit); 315 316 YAZ_END_CDECL 317 318 #endif 319 /* 320 * Local variables: 321 * c-basic-offset: 4 322 * c-file-style: "Stroustrup" 323 * indent-tabs-mode: nil 324 * End: 325 * vim: shiftwidth=4 tabstop=8 expandtab 326 */ 327 328