@@@ bench wip
[mLib] / struct / buf-dstr.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * Buffers and dynamic strings
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#include <string.h>
31
32#include "buf.h"
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @{,d}buf_getdstr{8,{16,24,32,64}{,l,b},z} --- *
37 *
38 * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block
39 * @dstr *d@ = where to put the result
40 *
41 * Returns: Zero if it worked, nonzero if there wasn't enough space.
42 *
43 * Use: Gets a block of data from a buffer, and writes its contents
44 * to a string.
45 */
46
47#define BUF_GETDSTR_(n, W, w) \
48 int buf_getdstr##w(buf *b, dstr *d) \
49 { \
50 void *p; \
51 size_t sz; \
52 \
53 if ((p = buf_getmem##w(b, &sz)) == 0) \
54 return (-1); \
55 DPUTM(d, p, sz); \
56 return (0); \
57 } \
58 int (dbuf_getdstr##w)(dbuf *db, dstr *d) \
59 { return (dbuf_getdstr##w(db, d)); }
60BUF_DOSUFFIXES(BUF_GETDSTR_)
61
62/* --- @{,d}buf_putdstr{8,{16,24,32,64}{,l,b},z} --- *
63 *
64 * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block
65 * @dstr *d@ = string to write
66 *
67 * Returns: Zero if it worked, nonzero if there wasn't enough space.
68 *
69 * Use: Puts a dynamic string to a buffer.
70 */
71
72#define BUF_PUTDSTR_(n, W, w) \
73 int buf_putdstr##w(buf *b, dstr *d) \
74 { return (buf_putmem##w(b, d->buf, d->len)); } \
75 int (dbuf_putdstr##w)(dbuf *db, dstr *d) \
76 { return (dbuf_putdstr##w(db, d)); }
77BUF_DOSUFFIXES(BUF_PUTDSTR_)
78
79/*----- That's all, folks -------------------------------------------------*/