2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "cmdline_cirbuf.h"
35 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
37 cbuf->maxlen = maxlen;
47 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
51 if (!n || n > CIRBUF_GET_FREELEN(cbuf))
54 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
56 if (n < cbuf->start + e) {
57 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
58 memcpy(cbuf->buf + cbuf->start - n + e, c, n);
61 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0, cbuf->start + e);
62 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n + (cbuf->start + e), 0, n - (cbuf->start + e));
63 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
64 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c, n - (cbuf->start + e));
67 cbuf->start += (cbuf->maxlen - n + e);
68 cbuf->start %= cbuf->maxlen;
75 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
79 if (!n || n > CIRBUF_GET_FREELEN(cbuf))
82 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
84 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
85 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
86 memcpy(cbuf->buf + cbuf->end + !e, c, n);
89 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0, cbuf->maxlen - cbuf->end - 1 + e);
90 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 + e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
91 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen - cbuf->end - 1 + e);
92 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e, n - cbuf->maxlen + cbuf->end + 1 - e);
96 cbuf->end %= cbuf->maxlen;
103 __cirbuf_add_head(struct cirbuf * cbuf, char c)
105 if (!CIRBUF_IS_EMPTY(cbuf)) {
106 cbuf->start += (cbuf->maxlen - 1);
107 cbuf->start %= cbuf->maxlen;
109 cbuf->buf[cbuf->start] = c;
114 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
116 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
117 __cirbuf_add_head(cbuf, c);
124 cirbuf_add_head(struct cirbuf * cbuf, char c)
126 __cirbuf_add_head(cbuf, c);
132 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
134 if (!CIRBUF_IS_EMPTY(cbuf)) {
136 cbuf->end %= cbuf->maxlen;
138 cbuf->buf[cbuf->end] = c;
143 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
145 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
146 __cirbuf_add_tail(cbuf, c);
153 cirbuf_add_tail(struct cirbuf * cbuf, char c)
155 __cirbuf_add_tail(cbuf, c);
160 __cirbuf_shift_left(struct cirbuf *cbuf)
163 char tmp = cbuf->buf[cbuf->start];
165 for (i=0 ; i<cbuf->len ; i++) {
166 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
167 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
169 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
170 cbuf->start += (cbuf->maxlen - 1);
171 cbuf->start %= cbuf->maxlen;
172 cbuf->end += (cbuf->maxlen - 1);
173 cbuf->end %= cbuf->maxlen;
177 __cirbuf_shift_right(struct cirbuf *cbuf)
180 char tmp = cbuf->buf[cbuf->end];
182 for (i=0 ; i<cbuf->len ; i++) {
183 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
184 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
186 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
188 cbuf->start %= cbuf->maxlen;
190 cbuf->end %= cbuf->maxlen;
193 /* XXX we could do a better algorithm here... */
194 void cirbuf_align_left(struct cirbuf * cbuf)
196 if (cbuf->start < cbuf->maxlen/2) {
197 while (cbuf->start != 0) {
198 __cirbuf_shift_left(cbuf);
202 while (cbuf->start != 0) {
203 __cirbuf_shift_right(cbuf);
208 /* XXX we could do a better algorithm here... */
209 void cirbuf_align_right(struct cirbuf * cbuf)
211 if (cbuf->start >= cbuf->maxlen/2) {
212 while (cbuf->end != cbuf->maxlen-1) {
213 __cirbuf_shift_left(cbuf);
217 while (cbuf->start != cbuf->maxlen-1) {
218 __cirbuf_shift_right(cbuf);
226 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
228 if (!size || size > CIRBUF_GET_LEN(cbuf))
232 if (CIRBUF_IS_EMPTY(cbuf)) {
233 cbuf->start += size - 1;
234 cbuf->start %= cbuf->maxlen;
238 cbuf->start %= cbuf->maxlen;
246 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
248 if (!size || size > CIRBUF_GET_LEN(cbuf))
252 if (CIRBUF_IS_EMPTY(cbuf)) {
253 cbuf->end += (cbuf->maxlen - size + 1);
254 cbuf->end %= cbuf->maxlen;
257 cbuf->end += (cbuf->maxlen - size);
258 cbuf->end %= cbuf->maxlen;
266 __cirbuf_del_head(struct cirbuf * cbuf)
269 if (!CIRBUF_IS_EMPTY(cbuf)) {
271 cbuf->start %= cbuf->maxlen;
276 cirbuf_del_head_safe(struct cirbuf * cbuf)
278 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
279 __cirbuf_del_head(cbuf);
286 cirbuf_del_head(struct cirbuf * cbuf)
288 __cirbuf_del_head(cbuf);
294 __cirbuf_del_tail(struct cirbuf * cbuf)
297 if (!CIRBUF_IS_EMPTY(cbuf)) {
298 cbuf->end += (cbuf->maxlen - 1);
299 cbuf->end %= cbuf->maxlen;
304 cirbuf_del_tail_safe(struct cirbuf * cbuf)
306 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
307 __cirbuf_del_tail(cbuf);
314 cirbuf_del_tail(struct cirbuf * cbuf)
316 __cirbuf_del_tail(cbuf);
319 /* convert to buffer */
322 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
324 unsigned int n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
329 if (cbuf->start <= cbuf->end) {
330 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
331 memcpy(c, cbuf->buf + cbuf->start , n);
334 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, cbuf->maxlen - cbuf->start);
335 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, n - cbuf->maxlen + cbuf->start);
336 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
337 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, n - cbuf->maxlen + cbuf->start);
342 /* convert to buffer */
345 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
347 unsigned int n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
352 if (cbuf->start <= cbuf->end) {
353 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
354 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
357 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, cbuf->end + 1);
358 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
360 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, cbuf->end + 1);
361 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, n - cbuf->end - 1);
366 /* get head or get tail */
369 cirbuf_get_head(struct cirbuf * cbuf)
371 return cbuf->buf[cbuf->start];
374 /* get head or get tail */
377 cirbuf_get_tail(struct cirbuf * cbuf)
379 return cbuf->buf[cbuf->end];