diff options
Diffstat (limited to 'tests/iobuf.h')
-rw-r--r-- | tests/iobuf.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/iobuf.h b/tests/iobuf.h new file mode 100644 index 0000000..4e43338 --- /dev/null +++ b/tests/iobuf.h | |||
@@ -0,0 +1,141 @@ | |||
1 | /* This file is part of GNU Pies. | ||
2 | Copyright (C) 2019 Sergey Poznyakoff | ||
3 | |||
4 | GNU Pies is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Pies is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #define IO_BUF_SIZE 512 | ||
18 | |||
19 | struct iobuf | ||
20 | { | ||
21 | char buf[IO_BUF_SIZE]; /* Initial line buffer */ | ||
22 | size_t start; /* Start of data */ | ||
23 | size_t avail; /* Start of available space */ | ||
24 | }; | ||
25 | |||
26 | static inline void | ||
27 | iobuf_reset (struct iobuf *bp) | ||
28 | { | ||
29 | bp->start = bp->avail = 0; | ||
30 | } | ||
31 | |||
32 | /* Return the count of data bytes in the buffer */ | ||
33 | static inline size_t | ||
34 | iobuf_data_size (struct iobuf *bp) | ||
35 | { | ||
36 | return bp->avail - bp->start; | ||
37 | } | ||
38 | |||
39 | static inline char * | ||
40 | iobuf_data_ptr (struct iobuf *bp) | ||
41 | { | ||
42 | return bp->buf + bp->start; | ||
43 | } | ||
44 | |||
45 | static inline void | ||
46 | iobuf_data_advance (struct iobuf *bp, size_t n) | ||
47 | { | ||
48 | bp->start += n; | ||
49 | if (bp->start == bp->avail) | ||
50 | iobuf_reset (bp); | ||
51 | } | ||
52 | |||
53 | static inline int | ||
54 | iobuf_putback (struct iobuf *bp) | ||
55 | { | ||
56 | return bp->buf[--bp->start-1]; | ||
57 | } | ||
58 | |||
59 | static inline int | ||
60 | iobuf_getc (struct iobuf *bp) | ||
61 | { | ||
62 | int c = -1; | ||
63 | if (iobuf_data_size (bp) > 0) | ||
64 | { | ||
65 | c = *iobuf_data_ptr (bp); | ||
66 | iobuf_data_advance(bp, 1); | ||
67 | } | ||
68 | return c; | ||
69 | } | ||
70 | |||
71 | /* Return the count of available bytes in the buffer */ | ||
72 | static inline size_t | ||
73 | iobuf_avail_size (struct iobuf *bp) | ||
74 | { | ||
75 | return sizeof (bp->buf) - bp->avail; | ||
76 | } | ||
77 | |||
78 | static inline char * | ||
79 | iobuf_avail_ptr (struct iobuf *bp) | ||
80 | { | ||
81 | return bp->buf + bp->avail; | ||
82 | } | ||
83 | |||
84 | static inline void | ||
85 | iobuf_avail_advance (struct iobuf *bp, size_t n) | ||
86 | { | ||
87 | bp->avail += n; | ||
88 | if (iobuf_avail_size (bp) == 0 && iobuf_data_size (bp) == 0) | ||
89 | iobuf_reset (bp); | ||
90 | } | ||
91 | |||
92 | static inline int | ||
93 | iobuf_putc (struct iobuf *bp, int c) | ||
94 | { | ||
95 | if (iobuf_avail_size (bp) > 0) | ||
96 | { | ||
97 | *iobuf_avail_ptr (bp) = c; | ||
98 | iobuf_avail_advance (bp, 1); | ||
99 | return c; | ||
100 | } | ||
101 | return -1; | ||
102 | } | ||
103 | |||
104 | static inline ssize_t | ||
105 | iobuf_fill (struct iobuf *bp, int fd) | ||
106 | { | ||
107 | ssize_t n = read (fd, iobuf_avail_ptr (bp), iobuf_avail_size (bp)); | ||
108 | if (n >= 0) | ||
109 | iobuf_avail_advance (bp, n); | ||
110 | return n; | ||
111 | } | ||
112 | |||
113 | static inline ssize_t | ||
114 | iobuf_flush (struct iobuf *bp, int fd) | ||
115 | { | ||
116 | ssize_t n = write (fd, iobuf_data_ptr (bp), iobuf_data_size (bp)); | ||
117 | if (n >= 0) | ||
118 | iobuf_data_advance (bp, n); | ||
119 | return n; | ||
120 | } | ||
121 | |||
122 | static inline ssize_t | ||
123 | iobuf_copy (struct iobuf *dst, struct iobuf *src) | ||
124 | { | ||
125 | ssize_t n = iobuf_data_size (src); | ||
126 | if (n > 0) | ||
127 | { | ||
128 | ssize_t avail = iobuf_avail_size (dst); | ||
129 | if (avail < n) | ||
130 | n = avail; | ||
131 | if (n) | ||
132 | { | ||
133 | memcpy (iobuf_avail_ptr (dst), iobuf_data_ptr (src), n); | ||
134 | iobuf_avail_advance (dst, n); | ||
135 | iobuf_data_advance (src, n); | ||
136 | } | ||
137 | } | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | |||