File: buffer.h

package info (click to toggle)
gensio 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,776 kB
  • sloc: ansic: 45,195; python: 2,718; cpp: 1,129; sh: 586; makefile: 420
file content (76 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 *  gensio - A library for abstracting stream I/O
 *  Copyright (C) 2018  Corey Minyard <minyard@acm.org>
 *
 *  SPDX-License-Identifier: LGPL-2.1-only
 */

#ifndef _SER2NET_BUFFER_H
#define _SER2NET_BUFFER_H

struct sbuf {
    unsigned char *buf;
    unsigned int maxsize;
    unsigned int cursize;
    unsigned int pos;
};

typedef int (*buffer_do_write)(void *cbdata, void *buf, unsigned int buflen,
			       unsigned int *written);

/*
 * Call do_write() with all the data in the buffer.  This may take
 * multiple calls to do_write() if the data wraps.
 * If do_write() returns an error, buffer_write will exit immediately.
 * It may have written some data.
 */
int buffer_write(buffer_do_write do_write, void *cb_data, struct sbuf *buf);

/*
 * Add the data to the buffer.  If there is not enough room for the data,
 * part of the data is added.  The number of bytes added is returned.
 */
unsigned int buffer_output(struct sbuf *buf, const unsigned char *data,
			   unsigned int len);

/*
 * Add a single character to the buffer.  Returns the number of
 * characters added.
 */
unsigned int buffer_outchar(struct sbuf *buf, unsigned char data);

/*
 * Initialize the buffer.  If data is NULL, it is allocated.
 */
int buffer_init(struct sbuf *buf, unsigned char *data, unsigned int datalen);

/*
 * Number of bytes left in the buffer.
 */
#define buffer_left(buf) ((buf)->maxsize - (buf)->cursize)

/*
 * Number of bytes currently held in the buffer.
 */
#define buffer_cursize(buf) ((buf)->cursize)

/*
 * Consume the given number of bytes in the buffer.
 */
#define buffer_advance(bufp, count) \
    do { \
	(bufp)->pos += (count);			\
	while ((bufp)->pos >= (bufp)->maxsize)	\
	    (bufp)->pos -= (bufp)->maxsize;	\
    }

/*
 * Set the buffer to have no data.
 */
#define buffer_reset(buf) \
    do {			\
	(buf)->cursize = 0;	\
	(buf)->pos = 0;		\
    } while(0)

#endif /* _SER2NET_BUFFER_H */