comparison C/buf.h @ 186:d4b8416e9ab1

Move C
author David Demelier <markand@malikania.fr>
date Sat, 23 Nov 2013 16:14:05 +0100
parents buf.h@7f214f26a4c0
children
comparison
equal deleted inserted replaced
185:523156bb3af5 186:d4b8416e9ab1
1 /*
2 * buf.h -- easy way to manipulate strings
3 *
4 * Copyright (c) 2011, 2012, David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _BUF_H_
20 #define _BUF_H_
21
22 #include <stdarg.h>
23
24 #ifdef __GNUC__
25 # define _buf_at_printf(i1, i2) __attribute__ ((format (printf, i1, i2)))
26 #else
27 # define _buf_at_printf(i1, i2)
28 #endif
29
30 enum buf_flags {
31 BUF_UNSAFE = (1 << 0), /* string may be truncated */
32 BUF_FIXED = (1 << 1), /* string has fixed length */
33 };
34
35 struct buf {
36 enum buf_flags flags; /* string flags */
37 char *text; /* string text */
38 size_t length; /* string length */
39 size_t alsize; /* allocated size */
40 int chksize; /* chunk size */
41 int maxsize; /* max fixed length size */
42
43 void * (*malloc)(size_t); /* alternate malloc */
44 void * (*realloc)(void *, size_t); /* alternate realloc */
45 };
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 int
52 buf_init(struct buf *, const char *);
53
54 void
55 buf_set(struct buf *, const char *, ...);
56
57 int
58 buf_ncat(struct buf *, const char *, size_t);
59
60 int
61 buf_cat(struct buf *, const char *);
62
63 int
64 buf_putc(struct buf *, int);
65
66 int
67 buf_vprintf(struct buf *, const char *, va_list);
68
69 int
70 buf_printf(struct buf *, const char *, ...) _buf_at_printf(2, 3);
71
72 int
73 buf_trim(struct buf *);
74
75 void
76 buf_cut(struct buf *, int);
77
78 void
79 buf_clear(struct buf *);
80
81 void
82 buf_free(struct buf *);
83
84 #ifdef __cplusplus
85 }
86 #endif
87
88 #endif /* _BUF_H_ */