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