removed dependency on stdbool and stdint, bytearray reworked (if it should be stack allocated, vla's are a better choice anyways, added linked list header
Minclude/cutils/byte_array.h
| @@ -3,34 +3,25 @@ | |||
|---|---|---|---|
| 3 | 3 | #include <stdlib.h> | |
| 4 | 4 | #include <string.h> | |
| 5 | 5 | #include <errno.h> | |
| 6 | + | #include <cutils/common.h> | |
| 6 | 7 | ||
| 7 | 8 | #define BYTEARRAY_DEFAULT_SIZE 4 | |
| 8 | 9 | ||
| 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 | 10 | typedef struct Bytearray | |
| 17 | 11 | { | |
| 18 | 12 | char* items; | |
| 19 | 13 | size_t capacity; | |
| 20 | 14 | size_t length; | |
| 21 | 15 | size_t element_size; | |
| 22 | - | short flags; | |
| 23 | 16 | } Bytearray; | |
| 24 | 17 | ||
| 25 | 18 | #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*)); | |
| 19 | + | Bytearray* bytearray_with_capacity(size_t capacity, size_t element_size); | |
| 20 | + | void delete_bytearray(Bytearray* bytearray, void(*rmv_el) (void*)); | |
| 30 | 21 | ||
| 31 | 22 | 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); | |
| 23 | + | #define bytearray_pop(bytearray, retptr) bytearray_pop_at(bytearray, bytearray->length-1, retptr) | |
| 24 | + | void* bytearray_pop_at(Bytearray* bytearray, size_t index, void* retptr); | |
| 34 | 25 | ||
| 35 | 26 | #define bytearray_push(bytearray, item) bytearray_insert(bytearray, bytearray->length, item) | |
| 36 | 27 | bool_t bytearray_insert(Bytearray* bytearray, size_t index, const void* item); | |
Ainclude/cutils/common.h
| @@ -0,0 +1,11 @@ | |||
|---|---|---|---|
| 1 | + | #ifndef CUTILS_COMMON_H | |
| 2 | + | #define CUTILS_COMMON_H | |
| 3 | + | ||
| 4 | + | #include <stddef.h> | |
| 5 | + | #ifndef SIZE_MAX | |
| 6 | + | #define SIZE_MAX (size_t)-1; | |
| 7 | + | #endif | |
| 8 | + | typedef enum {b_false, b_true} bool_t; | |
| 9 | + | ||
| 10 | + | ||
| 11 | + | #endif /* CUTILS_COMMON_H */ | |
Minclude/cutils/cutils.h
| @@ -5,5 +5,6 @@ | |||
|---|---|---|---|
| 5 | 5 | #include <cutils/dyn_string.h> | |
| 6 | 6 | #include <cutils/misc.h> | |
| 7 | 7 | #include <cutils/byte_array.h> | |
| 8 | + | #include <cutils/linked-list.h> | |
| 8 | 9 | ||
| 9 | 10 | #endif /* CUTILS_H */ | |
Minclude/cutils/dyn_string.h
| @@ -2,10 +2,10 @@ | |||
|---|---|---|---|
| 2 | 2 | #define DYN_STRING_H | |
| 3 | 3 | ||
| 4 | 4 | #define STRING_DEFAULT_SIZE 8 | |
| 5 | - | #include <stdint.h> | |
| 5 | + | ||
| 6 | 6 | #include <stdlib.h> | |
| 7 | 7 | #include <string.h> | |
| 8 | - | #include <stdbool.h> | |
| 8 | + | #include <cutils/common.h> | |
| 9 | 9 | ||
| 10 | 10 | typedef struct String | |
| 11 | 11 | { | |
| @@ -22,15 +22,16 @@ char string_at(const String* string, size_t index); | |||
|---|---|---|---|
| 22 | 22 | #define string_pop(string) string_pop_at(string, string->length-1) | |
| 23 | 23 | char string_pop_at(String* string, size_t index); | |
| 24 | 24 | ||
| 25 | - | bool string_insert(String* string, size_t index, char character); | |
| 25 | + | bool_t string_insert(String* string, size_t index, char character); | |
| 26 | 26 | #define string_push(string, character) string_insert(string, string->length, character) | |
| 27 | 27 | ||
| 28 | 28 | void string_remove(String* string, size_t index); | |
| 29 | + | void string_remove_range(String* string, size_t index, size_t length); | |
| 29 | 30 | ||
| 30 | - | bool string_adjust_size(String* string, size_t size); | |
| 31 | + | bool_t string_adjust_size(String* string, size_t size); | |
| 31 | 32 | size_t* string_find_char(const String* haystack, const char needle); | |
| 32 | 33 | ||
| 33 | - | bool string_concat(String* string, const String* other); | |
| 34 | + | bool_t string_concat(String* string, const String* other); | |
| 34 | 35 | String* from_cstring(const char* cstring); | |
| 35 | 36 | String* from_cstring_del(char* cstring); | |
| 36 | 37 | char* to_cstring(const String* string); | |
Ainclude/cutils/linked-list.h
| @@ -0,0 +1,41 @@ | |||
|---|---|---|---|
| 1 | + | #ifndef LINKED_LIST_H | |
| 2 | + | #define LINKED_LIST_H | |
| 3 | + | ||
| 4 | + | typedef struct SLnode | |
| 5 | + | { | |
| 6 | + | void* item; | |
| 7 | + | struct SLnode* next; | |
| 8 | + | } SLnode; | |
| 9 | + | ||
| 10 | + | typedef struct DLnode | |
| 11 | + | { | |
| 12 | + | void* item; | |
| 13 | + | struct DLnode* next; | |
| 14 | + | struct DLnode* prev; | |
| 15 | + | } DLnode; | |
| 16 | + | ||
| 17 | + | typedef struct LinkedList | |
| 18 | + | { | |
| 19 | + | void* head; | |
| 20 | + | void* tail; | |
| 21 | + | size_t length; | |
| 22 | + | bool_t doubly_linked; | |
| 23 | + | } LinkedList; | |
| 24 | + | ||
| 25 | + | LinkedList* new_ll(bool_t doubly_linked); | |
| 26 | + | void delete_ll(LinkedList* ll, void(*rmv) (void*)); | |
| 27 | + | ||
| 28 | + | void* ll_at(const LinkedList* ll, size_t index); | |
| 29 | + | #define ll_pop(ll) ll_pop_at(ll, ll->length-1) | |
| 30 | + | void* ll_pop_at(LinkedList* ll, size_t index); | |
| 31 | + | ||
| 32 | + | bool_t ll_insert(LinkedList* ll, size_t index, void* item); | |
| 33 | + | #define ll_push(ll, item) ll_insert(ll, ll->length, item) | |
| 34 | + | ||
| 35 | + | void ll_remove(LinkedList* ll, size_t index, void (*rmv)(void*)); | |
| 36 | + | void ll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*)); | |
| 37 | + | ||
| 38 | + | size_t* ll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*)); | |
| 39 | + | ||
| 40 | + | ||
| 41 | + | #endif /* LINKED_LIST_H */ | |
Minclude/cutils/misc.h
| @@ -14,11 +14,12 @@ | |||
|---|---|---|---|
| 14 | 14 | #include <windows.h> | |
| 15 | 15 | #endif | |
| 16 | 16 | ||
| 17 | - | #include <stdint.h> | |
| 18 | 17 | #include <string.h> | |
| 19 | 18 | ||
| 20 | 19 | #if __STDC_VERSION__ >= 199901L | |
| 20 | + | #include <stdint.h> | |
| 21 | 21 | typedef uintmax_t max_uint_t; | |
| 22 | + | uint32_t ntoh32(uint32_t const net); | |
| 22 | 23 | #else | |
| 23 | 24 | typedef unsigned long max_uint_t; | |
| 24 | 25 | #endif | |
| @@ -29,7 +30,6 @@ | |||
|---|---|---|---|
| 29 | 30 | #endif | |
| 30 | 31 | ||
| 31 | 32 | void sleep_ms(unsigned int milliseconds); | |
| 32 | - | uint32_t ntoh32(uint32_t const net); | |
| 33 | 33 | ||
| 34 | 34 | #if __STDC_VERSION__ >= 201112L | |
| 35 | 35 | struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts); | |
Minclude/cutils/vector.h
| @@ -2,10 +2,10 @@ | |||
|---|---|---|---|
| 2 | 2 | #define VECTOR_H | |
| 3 | 3 | ||
| 4 | 4 | #define VECTOR_DEFAULT_SIZE 4 | |
| 5 | - | #include <stdint.h> | |
| 5 | + | ||
| 6 | 6 | #include <stdlib.h> | |
| 7 | 7 | #include <string.h> | |
| 8 | - | #include <stdbool.h> | |
| 8 | + | #include <cutils/common.h> | |
| 9 | 9 | ||
| 10 | 10 | typedef struct Vector | |
| 11 | 11 | { | |
| @@ -22,13 +22,14 @@ void* vector_at(const Vector* vector, size_t index); | |||
|---|---|---|---|
| 22 | 22 | #define vector_pop(vector) vector_pop_at(vector, vector->length-1) | |
| 23 | 23 | void* vector_pop_at(Vector* vector, size_t index); | |
| 24 | 24 | ||
| 25 | - | bool vector_insert(Vector* vector, size_t index, void* item); | |
| 25 | + | bool_t vector_insert(Vector* vector, size_t index, void* item); | |
| 26 | 26 | #define vector_push(vector, item) vector_insert(vector, vector->length, item) | |
| 27 | 27 | ||
| 28 | 28 | void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)); | |
| 29 | + | void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv)(void*)); | |
| 29 | 30 | ||
| 30 | - | bool vector_adjust_size(Vector* vector, size_t size); | |
| 31 | - | bool vector_shrink(Vector* vector); | |
| 31 | + | bool_t vector_adjust_size(Vector* vector, size_t size); | |
| 32 | + | bool_t vector_shrink(Vector* vector); | |
| 32 | 33 | size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*)); | |
| 33 | 34 | ||
| 34 | 35 | ||
Mmeson.build
| @@ -1,7 +1,7 @@ | |||
|---|---|---|---|
| 1 | 1 | project('cutils', 'c') | |
| 2 | 2 | ||
| 3 | 3 | WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter'] | |
| 4 | - | CFLAGS = ['-std=c11', '-fstrict-aliasing', '-fPIC'] + WARNINGS | |
| 4 | + | CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS | |
| 5 | 5 | ||
| 6 | 6 | subdir('include') | |
| 7 | 7 | subdir('src') | |
Msrc/byte_array.c
| @@ -1,47 +1,29 @@ | |||
|---|---|---|---|
| 1 | 1 | #include <cutils/byte_array.h> | |
| 2 | 2 | ||
| 3 | - | Bytearray* new_bytearray_ext(size_t capacity, size_t element_size, char* array_storage, Bytearray* struct_storage, short flags) | |
| 3 | + | Bytearray* bytearray_with_capacity(size_t capacity, size_t element_size) | |
| 4 | 4 | { | |
| 5 | 5 | Bytearray* bytearray; | |
| 6 | 6 | ||
| 7 | - | if(struct_storage) | |
| 8 | - | { | |
| 9 | - | bytearray = struct_storage; | |
| 10 | - | } else { | |
| 11 | - | bytearray = malloc(sizeof(*bytearray)); | |
| 12 | - | if(!bytearray) | |
| 13 | - | return NULL; | |
| 14 | - | } | |
| 7 | + | bytearray = malloc(sizeof(*bytearray)); | |
| 8 | + | if(!bytearray) | |
| 9 | + | return NULL; | |
| 15 | 10 | ||
| 16 | - | if(array_storage) | |
| 11 | + | bytearray->items = malloc(sizeof(*bytearray->items)*capacity*element_size); | |
| 12 | + | if(!bytearray->items) | |
| 17 | 13 | { | |
| 18 | - | bytearray->items = array_storage; | |
| 19 | - | } else { | |
| 20 | - | bytearray->items = malloc(sizeof(*bytearray->items)*capacity*element_size); | |
| 21 | - | if(!bytearray->items) | |
| 22 | - | { | |
| 23 | - | free(bytearray); | |
| 24 | - | return NULL; | |
| 25 | - | } | |
| 14 | + | free(bytearray); | |
| 15 | + | return NULL; | |
| 26 | 16 | } | |
| 17 | + | ||
| 27 | 18 | bytearray->length = 0; | |
| 28 | 19 | bytearray->capacity = capacity; | |
| 29 | 20 | bytearray->element_size = element_size; | |
| 30 | - | bytearray->flags = flags; | |
| 31 | 21 | ||
| 32 | 22 | return bytearray; | |
| 33 | 23 | } | |
| 34 | 24 | ||
| 35 | - | void delete_bytearray(Bytearray* bytearray) | |
| 36 | - | { | |
| 37 | - | delete_bytearray_ext(bytearray, | |
| 38 | - | (bytearray->flags & BT_FREE_EL) ? free : NULL, | |
| 39 | - | (bytearray->flags & BT_FREE_ARRAY) ? free : NULL, | |
| 40 | - | (bytearray->flags & BT_FREE_STRUCT) ? free : NULL); | |
| 41 | - | } | |
| 42 | - | ||
| 43 | 25 | ||
| 44 | - | void delete_bytearray_ext(Bytearray* bytearray, void(*rmv_el) (void*), void(*rmv_items) (void*), void(*rmv_struct) (void*)) | |
| 26 | + | void delete_bytearray(Bytearray* bytearray, void(*rmv_el) (void*)) | |
| 45 | 27 | { | |
| 46 | 28 | if(rmv_el) | |
| 47 | 29 | { | |
| @@ -51,10 +33,9 @@ void delete_bytearray_ext(Bytearray* bytearray, void(*rmv_el) (void*), void(*rmv | |||
|---|---|---|---|
| 51 | 33 | rmv_el(&bytearray->items[i*bytearray->element_size]); | |
| 52 | 34 | } | |
| 53 | 35 | } | |
| 54 | - | if(rmv_items) | |
| 55 | - | rmv_items(bytearray->items); | |
| 56 | - | if(rmv_struct) | |
| 57 | - | rmv_struct(bytearray); | |
| 36 | + | ||
| 37 | + | free(bytearray->items); | |
| 38 | + | free(bytearray); | |
| 58 | 39 | } | |
| 59 | 40 | ||
| 60 | 41 | void* bytearray_at(const Bytearray* bytearray, size_t index) | |
| @@ -65,27 +46,21 @@ void* bytearray_at(const Bytearray* bytearray, size_t index) | |||
|---|---|---|---|
| 65 | 46 | return bytearray->items+(index*bytearray->element_size); | |
| 66 | 47 | } | |
| 67 | 48 | ||
| 68 | - | void* bytearray_pop_at(Bytearray* bytearray, size_t index) | |
| 49 | + | void* bytearray_pop_at(Bytearray* bytearray, size_t index, void* retptr) | |
| 69 | 50 | { | |
| 70 | - | void *tmp, *ret; | |
| 51 | + | void *tmp; | |
| 71 | 52 | ||
| 72 | 53 | tmp = bytearray_at(bytearray, index); | |
| 73 | 54 | if(!tmp) | |
| 74 | - | { | |
| 75 | - | errno = 0; | |
| 76 | 55 | return NULL; | |
| 77 | - | } | |
| 78 | 56 | ||
| 79 | - | ret = malloc(bytearray->element_size); | |
| 80 | - | if(!ret) | |
| 81 | - | { | |
| 82 | - | errno = ENOMEM; | |
| 57 | + | retptr = retptr ? retptr : malloc(bytearray->element_size); | |
| 58 | + | if(!retptr) | |
| 83 | 59 | return NULL; | |
| 84 | - | } | |
| 85 | 60 | ||
| 86 | - | memcpy(ret, tmp, bytearray->element_size); | |
| 61 | + | memcpy(retptr, tmp, bytearray->element_size); | |
| 87 | 62 | bytearray_remove(bytearray, index, NULL); | |
| 88 | - | return ret; | |
| 63 | + | return retptr; | |
| 89 | 64 | } | |
| 90 | 65 | ||
| 91 | 66 | bool_t bytearray_insert(Bytearray* bytearray, size_t index, const void* item) | |
| @@ -118,14 +93,6 @@ void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*)) | |||
|---|---|---|---|
| 118 | 93 | ||
| 119 | 94 | bool_t bytearray_adjust_size(Bytearray* bytearray, size_t size) | |
| 120 | 95 | { | |
| 121 | - | if(bytearray->flags & BT_FIXED) | |
| 122 | - | { | |
| 123 | - | if(bytearray->capacity < size) | |
| 124 | - | return b_false; | |
| 125 | - | else | |
| 126 | - | return b_true; | |
| 127 | - | } | |
| 128 | - | ||
| 129 | 96 | while(bytearray->capacity < size) | |
| 130 | 97 | { | |
| 131 | 98 | size_t capacity = bytearray->capacity; | |
| @@ -144,9 +111,6 @@ bool_t bytearray_adjust_size(Bytearray* bytearray, size_t size) | |||
|---|---|---|---|
| 144 | 111 | ||
| 145 | 112 | bool_t bytearray_shrink(Bytearray* bytearray) | |
| 146 | 113 | { | |
| 147 | - | if(bytearray->flags & BT_FIXED) | |
| 148 | - | return b_false; | |
| 149 | - | ||
| 150 | 114 | while(bytearray->capacity > bytearray->length*2) | |
| 151 | 115 | { | |
| 152 | 116 | size_t capacity = bytearray->capacity; | |
Msrc/dyn_string.c
| @@ -26,15 +26,24 @@ void string_remove(String* string, size_t index) | |||
|---|---|---|---|
| 26 | 26 | } | |
| 27 | 27 | } | |
| 28 | 28 | ||
| 29 | - | bool string_insert(String* string, size_t index, char character) | |
| 29 | + | void string_remove_range(String* string, size_t index, size_t length) | |
| 30 | + | { | |
| 31 | + | size_t i; | |
| 32 | + | for(i = 0; i < length; i++) | |
| 33 | + | { | |
| 34 | + | string_remove(string, index+i); | |
| 35 | + | } | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | bool_t string_insert(String* string, size_t index, char character) | |
| 30 | 39 | { | |
| 31 | 40 | if(index > string->length || !string_adjust_size(string, string->length)) | |
| 32 | - | return false; | |
| 41 | + | return b_false; | |
| 33 | 42 | ||
| 34 | 43 | memmove(string->chars+index+1, string->chars+index, (string->length-index)*sizeof(*string->chars)); | |
| 35 | 44 | string->chars[index] = character; | |
| 36 | 45 | string->length++; | |
| 37 | - | return true; | |
| 46 | + | return b_true; | |
| 38 | 47 | } | |
| 39 | 48 | ||
| 40 | 49 | char string_at(const String* string, size_t index) | |
| @@ -57,13 +66,13 @@ char string_pop_at(String* string, size_t index) | |||
|---|---|---|---|
| 57 | 66 | } | |
| 58 | 67 | } | |
| 59 | 68 | ||
| 60 | - | bool string_concat(String* string, const String* other) | |
| 69 | + | bool_t string_concat(String* string, const String* other) | |
| 61 | 70 | { | |
| 62 | 71 | if(!string_adjust_size(string,string->length+other->length-1)) | |
| 63 | - | return false; | |
| 72 | + | return b_false; | |
| 64 | 73 | memcpy(string->chars+string->length,other->chars,other->length); | |
| 65 | 74 | string->length += other->length; | |
| 66 | - | return true; | |
| 75 | + | return b_true; | |
| 67 | 76 | } | |
| 68 | 77 | ||
| 69 | 78 | size_t* string_find_char(const String* haystack, const char needle) | |
| @@ -82,7 +91,7 @@ size_t* string_find_char(const String* haystack, const char needle) | |||
|---|---|---|---|
| 82 | 91 | return NULL; | |
| 83 | 92 | } | |
| 84 | 93 | ||
| 85 | - | bool string_adjust_size(String* string, size_t size) | |
| 94 | + | bool_t string_adjust_size(String* string, size_t size) | |
| 86 | 95 | { | |
| 87 | 96 | while(string->capacity < size) | |
| 88 | 97 | { | |
| @@ -91,11 +100,11 @@ bool string_adjust_size(String* string, size_t size) | |||
|---|---|---|---|
| 91 | 100 | if(!string->chars) | |
| 92 | 101 | { | |
| 93 | 102 | string->chars = tmp; | |
| 94 | - | return false; | |
| 103 | + | return b_false; | |
| 95 | 104 | } | |
| 96 | 105 | string->capacity *= 2; | |
| 97 | 106 | } | |
| 98 | - | return true; | |
| 107 | + | return b_true; | |
| 99 | 108 | } | |
| 100 | 109 | ||
| 101 | 110 | ||
Msrc/misc.c
| @@ -10,6 +10,7 @@ void sleep_ms(unsigned int milliseconds) | |||
|---|---|---|---|
| 10 | 10 | #endif | |
| 11 | 11 | } | |
| 12 | 12 | ||
| 13 | + | #if __STDC_VERSION__ >= 199901L | |
| 13 | 14 | uint32_t ntoh32(uint32_t const net) | |
| 14 | 15 | { | |
| 15 | 16 | uint8_t data[4]; | |
| @@ -20,6 +21,7 @@ uint32_t ntoh32(uint32_t const net) | |||
|---|---|---|---|
| 20 | 21 | | ((uint32_t) data[1] << 16) | |
| 21 | 22 | | ((uint32_t) data[0] << 24); | |
| 22 | 23 | } | |
| 24 | + | #endif | |
| 23 | 25 | ||
| 24 | 26 | #if __STDC_VERSION__ >= 201112L | |
| 25 | 27 | struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts) | |
Msrc/test.c
| @@ -108,12 +108,8 @@ static void test2(void) | |||
|---|---|---|---|
| 108 | 108 | static void test3(void) | |
| 109 | 109 | { | |
| 110 | 110 | Bytearray* bt; | |
| 111 | - | void* localarray; | |
| 112 | - | struct test my_struct; | |
| 113 | - | struct test* my_structptr; | |
| 114 | 111 | char* tmpchar; | |
| 115 | 112 | ||
| 116 | - | /*test pushing and popping with default constructor and simple values*/ | |
| 117 | 113 | bt = new_bytearray(1); | |
| 118 | 114 | bytearray_push(bt, "a"); | |
| 119 | 115 | bytearray_push(bt, "b"); | |
| @@ -123,70 +119,37 @@ static void test3(void) | |||
|---|---|---|---|
| 123 | 119 | ||
| 124 | 120 | assert(strcmp(bt->items, "abcd") == 0); | |
| 125 | 121 | ||
| 126 | - | tmpchar = bytearray_pop(bt); | |
| 122 | + | tmpchar = bytearray_pop(bt, NULL); | |
| 127 | 123 | assert(*tmpchar == '\0'); | |
| 128 | 124 | free(tmpchar); | |
| 129 | 125 | ||
| 130 | - | tmpchar = bytearray_pop(bt); | |
| 126 | + | tmpchar = bytearray_pop(bt, NULL); | |
| 131 | 127 | assert(*tmpchar == 'd'); | |
| 132 | 128 | free(tmpchar); | |
| 133 | 129 | ||
| 134 | - | tmpchar = bytearray_pop(bt); | |
| 130 | + | tmpchar = bytearray_pop(bt, NULL); | |
| 135 | 131 | assert(*tmpchar == 'c'); | |
| 136 | 132 | free(tmpchar); | |
| 137 | 133 | ||
| 138 | - | tmpchar = bytearray_pop(bt); | |
| 134 | + | tmpchar = malloc(1); | |
| 135 | + | bytearray_pop(bt, tmpchar); | |
| 139 | 136 | assert(*tmpchar == 'b'); | |
| 140 | - | free(tmpchar); | |
| 141 | 137 | ||
| 142 | - | tmpchar = bytearray_pop(bt); | |
| 138 | + | bytearray_pop(bt, tmpchar); | |
| 143 | 139 | assert(*tmpchar == 'a'); | |
| 144 | 140 | free(tmpchar); | |
| 145 | 141 | ||
| 146 | - | tmpchar = bytearray_pop(bt); | |
| 142 | + | tmpchar = bytearray_pop(bt, NULL); | |
| 147 | 143 | assert(tmpchar == NULL); | |
| 148 | - | free(tmpchar); | |
| 149 | 144 | ||
| 150 | 145 | bytearray_push(bt, "a"); | |
| 151 | - | tmpchar = bytearray_pop(bt); | |
| 146 | + | tmpchar = bytearray_pop(bt, NULL); | |
| 152 | 147 | assert(*tmpchar == 'a'); | |
| 153 | 148 | free(tmpchar); | |
| 154 | 149 | ||
| 155 | - | delete_bytearray(bt); | |
| 156 | - | ||
| 157 | - | /*test pushing and popping with self allocated array and fixed size*/ | |
| 158 | - | localarray = malloc(3*sizeof(struct test)); | |
| 159 | - | my_struct.a = 0; | |
| 160 | - | my_struct.b = 0; | |
| 161 | - | ||
| 162 | - | bt = new_bytearray_ext(3, sizeof(struct test), localarray, NULL, BT_FREE_ARRAY | BT_FIXED | BT_FREE_STRUCT); | |
| 163 | - | bytearray_push(bt, &my_struct); | |
| 164 | - | my_struct.a = 1; | |
| 165 | - | my_struct.b = 1; | |
| 166 | - | bytearray_push(bt, &my_struct); | |
| 167 | - | ||
| 168 | - | my_struct.a = 2; | |
| 169 | - | my_struct.b = 2; | |
| 170 | - | bytearray_push(bt, &my_struct); | |
| 171 | - | ||
| 172 | - | my_struct.a = 3; | |
| 173 | - | my_struct.b = 3; | |
| 174 | - | bytearray_push(bt, &my_struct); | |
| 175 | - | ||
| 176 | - | my_structptr = bytearray_pop(bt); | |
| 177 | - | ||
| 178 | - | my_struct.a = 2; | |
| 179 | - | my_struct.b = 2; | |
| 180 | - | assert(my_structptr->a == my_struct.a); | |
| 181 | - | assert(my_structptr->b == my_struct.b); | |
| 182 | - | free(my_structptr); | |
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | delete_bytearray(bt); | |
| 187 | - | ||
| 150 | + | delete_bytearray(bt, NULL); | |
| 188 | 151 | } | |
| 189 | - | ||
| 152 | + | #if __STDC_VERSION__ >= 201112L | |
| 190 | 153 | static void test4(void) | |
| 191 | 154 | { | |
| 192 | 155 | struct timespec ts1 = {10, 10}; | |
| @@ -236,13 +199,16 @@ static void test4(void) | |||
|---|---|---|---|
| 236 | 199 | res = timespec_diff(&ts1, &ts2); | |
| 237 | 200 | assert(res.tv_sec == 10 && res.tv_nsec == 999999998); | |
| 238 | 201 | } | |
| 202 | + | #endif | |
| 239 | 203 | ||
| 240 | 204 | int main(int argc, char** argv) | |
| 241 | 205 | { | |
| 242 | 206 | test1(); | |
| 243 | 207 | test2(); | |
| 244 | 208 | test3(); | |
| 209 | + | #if __STDC_VERSION__ >= 201112L | |
| 245 | 210 | test4(); | |
| 211 | + | #endif | |
| 246 | 212 | sleep_ms(1000); | |
| 247 | 213 | return 0; | |
| 248 | 214 | } | |
Msrc/vector.c
| @@ -49,6 +49,15 @@ void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)) | |||
|---|---|---|---|
| 49 | 49 | } | |
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | + | void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv)(void*)) | |
| 53 | + | { | |
| 54 | + | size_t i; | |
| 55 | + | for(i = 0; i < length; i++) | |
| 56 | + | { | |
| 57 | + | vector_remove(vector, index+i, rmv); | |
| 58 | + | } | |
| 59 | + | } | |
| 60 | + | ||
| 52 | 61 | size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*)) | |
| 53 | 62 | { | |
| 54 | 63 | size_t* ret; | |
| @@ -66,18 +75,18 @@ size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const | |||
|---|---|---|---|
| 66 | 75 | return NULL; | |
| 67 | 76 | } | |
| 68 | 77 | ||
| 69 | - | bool vector_insert(Vector* vector, size_t index, void* item) | |
| 78 | + | bool_t vector_insert(Vector* vector, size_t index, void* item) | |
| 70 | 79 | { | |
| 71 | 80 | if(index > vector->length || !vector_adjust_size(vector, vector->length+1)) | |
| 72 | - | return false; | |
| 81 | + | return b_false; | |
| 73 | 82 | ||
| 74 | 83 | memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items)); | |
| 75 | 84 | vector->items[index] = item; | |
| 76 | 85 | ++vector->length; | |
| 77 | - | return true; | |
| 86 | + | return b_true; | |
| 78 | 87 | } | |
| 79 | 88 | ||
| 80 | - | bool vector_adjust_size(Vector* vector, size_t size) | |
| 89 | + | bool_t vector_adjust_size(Vector* vector, size_t size) | |
| 81 | 90 | { | |
| 82 | 91 | while(vector->capacity < size) | |
| 83 | 92 | { | |
| @@ -86,14 +95,14 @@ bool vector_adjust_size(Vector* vector, size_t size) | |||
|---|---|---|---|
| 86 | 95 | if(!vector->items) | |
| 87 | 96 | { | |
| 88 | 97 | vector->items = tmp; | |
| 89 | - | return false; | |
| 98 | + | return b_false; | |
| 90 | 99 | } | |
| 91 | 100 | vector->capacity *= 2; | |
| 92 | 101 | } | |
| 93 | - | return true; | |
| 102 | + | return b_true; | |
| 94 | 103 | } | |
| 95 | 104 | ||
| 96 | - | bool vector_shrink(Vector* vector) | |
| 105 | + | bool_t vector_shrink(Vector* vector) | |
| 97 | 106 | { | |
| 98 | 107 | while(vector->capacity > (vector->length*2)) | |
| 99 | 108 | { | |
| @@ -102,11 +111,11 @@ bool vector_shrink(Vector* vector) | |||
|---|---|---|---|
| 102 | 111 | if(!vector->items) | |
| 103 | 112 | { | |
| 104 | 113 | vector->items = tmp; | |
| 105 | - | return false; | |
| 114 | + | return b_false; | |
| 106 | 115 | } | |
| 107 | 116 | vector->capacity /= 2; | |
| 108 | 117 | } | |
| 109 | - | return true; | |
| 118 | + | return b_true; | |
| 110 | 119 | } | |
| 111 | 120 | ||
| 112 | 121 | void delete_vector(Vector* vector, void (*rmv)(void*)) | |