byte_array.h
| 1 | #ifndef BYTE_ARRAY_H |
| 2 | #define BYTE_ARRAY_H |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | |
| 7 | #define BYTEARRAY_DEFAULT_SIZE 4 |
| 8 | |
| 9 | #define BT_FIXED 1 |
| 10 | #define BT_FREE_EL 2 |
| 11 | #define BT_FREE_ARRAY 4 |
| 12 | #define BT_FREE_STRUCT 8 |
| 13 | |
| 14 | typedef enum {b_false, b_true} bool_t; |
| 15 | |
| 16 | typedef struct Bytearray |
| 17 | { |
| 18 | char* items; |
| 19 | size_t capacity; |
| 20 | size_t length; |
| 21 | size_t element_size; |
| 22 | short flags; |
| 23 | } Bytearray; |
| 24 | |
| 25 | #define new_bytearray(element_size) bytearray_with_capacity(BYTEARRAY_DEFAULT_SIZE, element_size) |
| 26 | #define bytearray_with_capacity(capacity, element_size) new_bytearray_ext(capacity, element_size, NULL, NULL, BT_FREE_ARRAY | BT_FREE_STRUCT) |
| 27 | Bytearray* new_bytearray_ext(size_t capacity, size_t element_size, char* array_storage, Bytearray* struct_storage, short flags); |
| 28 | void delete_bytearray(Bytearray* bytearray); |
| 29 | void delete_bytearray_ext(Bytearray* bytearray, void(*rmv_el) (void*), void(*rmv_items) (void*), void(*rmv_struct) (void*)); |
| 30 | |
| 31 | void* bytearray_at(const Bytearray* bytearray, size_t index); |
| 32 | #define bytearray_pop(bytearray) bytearray_pop_at(bytearray, bytearray->length-1) |
| 33 | void* bytearray_pop_at(Bytearray* bytearray, size_t index); |
| 34 | |
| 35 | #define bytearray_push(bytearray, item) bytearray_insert(bytearray, bytearray->length, item) |
| 36 | bool_t bytearray_insert(Bytearray* bytearray, size_t index, const void* item); |
| 37 | |
| 38 | void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*)); |
| 39 | |
| 40 | bool_t bytearray_adjust_size(Bytearray* bytearray, size_t size); |
| 41 | bool_t bytearray_shrink(Bytearray* bytearray); |
| 42 | size_t* bytearray_find(const Bytearray* haystack, const void* needle, int (*cmp)(const void*, const void*)); |
| 43 | |
| 44 | |
| 45 | #endif /* BYTE_ARRAY_H */ |
| 46 | |
| 47 |