author | Medo <smaxein@googlemail.com> |
Mon, 25 Jun 2012 00:42:07 +0200 | |
changeset 7271 | 5608ac657362 |
parent 7234 | 613998625a3c |
child 7275 | 15f722e0b96f |
permissions | -rw-r--r-- |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
#ifndef BUFFER_H_ |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
#define BUFFER_H_ |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
#include <stdint.h> |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
#include <stddef.h> |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
/** |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
* A simple struct to hold both the pointer to an array and its size, |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
* for e.g. conveniently returning it from a function. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
* |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
* Convention: Size is zero iff data is a NULL pointer. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
*/ |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
typedef struct { |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
void *data; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
size_t size; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
} flib_buffer; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
/** |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
* Just like flib_buffer, but the contents are not supposed to be modified. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
*/ |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
typedef struct { |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
const void *data; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
size_t size; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
} flib_constbuffer; |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
/** |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
27 |
* Simple variable-capacity data structure that can be efficiently appended to. |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
29 |
typedef struct _flib_vector flib_vector; |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
/** |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
* Create a new vector. Needs to be destroyed again later with flib_vector_destroy. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
* May return NULL if memory runs out. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
35 |
flib_vector *flib_vector_create(); |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
/** |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
38 |
* Free the memory of this vector |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
*/ |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
void flib_vector_destroy(flib_vector *vec); |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
/** |
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
43 |
* Resize the vector. This changes the size, and ensures the capacity is large enough to |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
44 |
* for the new size. Can also free memory if the new size is smaller. There is no guarantee |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
45 |
* about the contents of extra memory. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
46 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
47 |
int flib_vector_resize(flib_vector *vec, size_t newSize); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
48 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
49 |
/** |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
* Append the provided data to the end of the vector, enlarging it as required. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
* Returns the ammount of data appended, which is either len (success) or 0 (out of memory). |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
52 |
* The vector remains unchanged if appending fails. |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
53 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
54 |
int flib_vector_append(flib_vector *vec, const void *data, size_t len); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
55 |
|
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
56 |
/** |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
57 |
* Append data from a format string to the buffer (without trailing 0) |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
58 |
* Returns 0 on success. |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
59 |
*/ |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
60 |
int flib_vector_appendf(flib_vector *vec, const char *template, ...); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
61 |
|
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
62 |
/** |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
63 |
* Return a pointer to the current data buffer of the vector. This pointer can |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
64 |
* become invalid if the vector size or capacity is changed. |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
65 |
*/ |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
66 |
void *flib_vector_data(flib_vector *vec); |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
67 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
/** |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
69 |
* Return the current size of the vector. |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
70 |
*/ |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
71 |
size_t flib_vector_size(flib_vector *vec); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
72 |
|
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
73 |
/** |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
74 |
* Return a buffer pointing to the current contents of the vector. |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
75 |
* These will become invalid if the vector size or capacity is changed. |
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
77 |
flib_buffer flib_vector_as_buffer(flib_vector *vec); |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
78 |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
79 |
/** |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
80 |
* Return a constbuffer pointing to the current contents of the vector. |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
81 |
* These will become invalid if the vector size or capacity is changed. |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
82 |
*/ |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
83 |
flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec); |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
84 |
|
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
85 |
#endif |