Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: buf.h,v 1.13 2011/07/06 15:36:52 nicm Exp $ */
2 74b37681 2019-02-07 stsp /*
3 74b37681 2019-02-07 stsp * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 74b37681 2019-02-07 stsp * All rights reserved.
5 74b37681 2019-02-07 stsp *
6 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
7 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
8 74b37681 2019-02-07 stsp * are met:
9 74b37681 2019-02-07 stsp *
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
11 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. The name of the author may not be used to endorse or promote products
13 74b37681 2019-02-07 stsp * derived from this software without specific prior written permission.
14 74b37681 2019-02-07 stsp *
15 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 74b37681 2019-02-07 stsp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 74b37681 2019-02-07 stsp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 74b37681 2019-02-07 stsp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 74b37681 2019-02-07 stsp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 74b37681 2019-02-07 stsp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 74b37681 2019-02-07 stsp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 74b37681 2019-02-07 stsp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 74b37681 2019-02-07 stsp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 74b37681 2019-02-07 stsp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 74b37681 2019-02-07 stsp *
26 74b37681 2019-02-07 stsp * Buffer management
27 74b37681 2019-02-07 stsp * -----------------
28 74b37681 2019-02-07 stsp *
29 74b37681 2019-02-07 stsp * This code provides an API to generic memory buffer management. All
30 74b37681 2019-02-07 stsp * operations are performed on a buf structure, which is kept opaque to the
31 74b37681 2019-02-07 stsp * API user in order to avoid corruption of the fields and make sure that only
32 74b37681 2019-02-07 stsp * the internals can modify the fields.
33 74b37681 2019-02-07 stsp *
34 74b37681 2019-02-07 stsp * The first step is to allocate a new buffer using the buf_alloc()
35 74b37681 2019-02-07 stsp * function, which returns a pointer to a new buffer.
36 74b37681 2019-02-07 stsp */
37 74b37681 2019-02-07 stsp
38 74b37681 2019-02-07 stsp #ifndef BUF_H
39 74b37681 2019-02-07 stsp #define BUF_H
40 74b37681 2019-02-07 stsp
41 74b37681 2019-02-07 stsp #include <sys/types.h>
42 74b37681 2019-02-07 stsp
43 74b37681 2019-02-07 stsp typedef struct buf BUF;
44 af45e626 2019-02-08 stsp struct wklhead;
45 74b37681 2019-02-07 stsp
46 74b37681 2019-02-07 stsp BUF *buf_alloc(size_t);
47 74b37681 2019-02-07 stsp BUF *buf_load(const char *);
48 74b37681 2019-02-07 stsp void buf_free(BUF *);
49 74b37681 2019-02-07 stsp void *buf_release(BUF *);
50 74b37681 2019-02-07 stsp u_char buf_getc(BUF *, size_t);
51 74b37681 2019-02-07 stsp void buf_empty(BUF *);
52 af45e626 2019-02-08 stsp const struct got_error *buf_append(size_t *, BUF *, const void *, size_t);
53 af45e626 2019-02-08 stsp const struct got_error *buf_putc(BUF *, int);
54 af45e626 2019-02-08 stsp const struct got_error *buf_puts(size_t *, BUF *b, const char *str);
55 74b37681 2019-02-07 stsp size_t buf_len(BUF *);
56 74b37681 2019-02-07 stsp int buf_write_fd(BUF *, int);
57 af45e626 2019-02-08 stsp const struct got_error *buf_write(BUF *, const char *, mode_t);
58 af45e626 2019-02-08 stsp const struct got_error *buf_write_stmp(BUF *, char *, struct wklhead *);
59 74b37681 2019-02-07 stsp u_char *buf_get(BUF *b);
60 74b37681 2019-02-07 stsp
61 74b37681 2019-02-07 stsp #endif /* BUF_H */