libbasicobjects  0.6.1
simplebuffer.h
1 /*
2  Simple buffer
3 
4  Basic buffer manipulation routines. Taken from ELAPI code.
5 
6  Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef ELAPI_SIMPLEBUFFER_H
21 #define ELAPI_SIMPLEBUFFER_H
22 
23 #include <stdint.h>
24 
25 #ifndef EOK
26 #define EOK 0
27 #endif
28 
29 /* Generic data structure for the buffer */
30 struct simplebuffer {
31  unsigned char *buffer;
32  uint32_t size;
33  uint32_t length;
34 };
35 
36 /* Function to free data */
37 void simplebuffer_free(struct simplebuffer *data);
38 
39 /* Allocate data structure */
40 int simplebuffer_alloc(struct simplebuffer **data);
41 
42 /* Function to add memory to the buffer */
43 int simplebuffer_grow(struct simplebuffer *data,
44  uint32_t len,
45  uint32_t block);
46 
47 /* Function to add raw data to the end of the buffer.
48  * Terminating 0 is not counted in length but appended
49  * automatically.
50  */
51 int simplebuffer_add_raw(struct simplebuffer *data,
52  void *data_in,
53  uint32_t len,
54  uint32_t block);
55 
56 
57 /* Function to add string to the buffer.
58  * Same as above just uses string as an argument.
59  */
60 int simplebuffer_add_str(struct simplebuffer *data,
61  const char *str,
62  uint32_t len,
63  uint32_t block);
64 
65 /* Finction to add CR to the buffer */
66 int simplebuffer_add_cr(struct simplebuffer *data);
67 
68 
69 /* Function to write data synchroniusly */
70 int simplebuffer_write(int fd,
71  struct simplebuffer *data,
72  uint32_t *left);
73 
74 /* Get buffer */
75 const unsigned char *simplebuffer_get_buf(struct simplebuffer *data);
76 
77 /* Get buffer */
78 void *simplebuffer_get_vbuf(struct simplebuffer *data);
79 
80 
81 /* Get length */
82 uint32_t simplebuffer_get_len(struct simplebuffer *data);
83 
84 #endif