added big-endian to host converter, added remove function for vectors, changed return type of search functions
MMakefile
| @@ -1,3 +1,3 @@ | |||
|---|---|---|---|
| 1 | 1 | all: | |
| 2 | 2 | gcc -Og -g -march=native -shared -fPIC -fstrict-aliasing vector.c dyn_string.c -o cutils.so -flto \ | |
| 3 | - | -Wall -Werror -Wpedantic -Wnull-dereference -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wstrict-overflow=5 -Wunreachable-code -Wno-unused-parameter | |
| 3 | + | -Wall -Wpedantic -Wnull-dereference -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wstrict-overflow=5 -Wunreachable-code -Wno-unused-parameter | |
Mcutils.h
| @@ -1,5 +1,6 @@ | |||
|---|---|---|---|
| 1 | 1 | #ifndef CUTILS_H | |
| 2 | 2 | #define CUTILS_H | |
| 3 | - | #include <vector.h> | |
| 4 | - | #include <dyn_string.h> | |
| 3 | + | #include "vector.h" | |
| 4 | + | #include "dyn_string.h" | |
| 5 | + | #include "cutils_endian.h" | |
| 5 | 6 | #endif //CUTILS_H | |
Acutils_endian.h
| @@ -0,0 +1,17 @@ | |||
|---|---|---|---|
| 1 | + | #ifndef CUTILS_ENDIAN_H | |
| 2 | + | #define CUTILS_ENDIAN_H | |
| 3 | + | #include <stdint.h> | |
| 4 | + | #include <string.h> | |
| 5 | + | ||
| 6 | + | inline uint32_t ntoh32(uint32_t const net) | |
| 7 | + | { | |
| 8 | + | uint8_t data[4]; | |
| 9 | + | memcpy(&data, &net, sizeof(data)); | |
| 10 | + | ||
| 11 | + | return ((uint32_t) data[3] << 0) | |
| 12 | + | | ((uint32_t) data[2] << 8) | |
| 13 | + | | ((uint32_t) data[1] << 16) | |
| 14 | + | | ((uint32_t) data[0] << 24); | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | #endif //CUTILS_ENDIAN_H | |
Mdyn_string.c
| @@ -61,20 +61,19 @@ bool string_concat(String* string, String* other) | |||
|---|---|---|---|
| 61 | 61 | return true; | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | - | FindReturn string_find_char(const String* haystack, const char needle) | |
| 64 | + | size_t* string_find_char(const String* haystack, const char needle) | |
| 65 | 65 | { | |
| 66 | - | FindReturn ret; | |
| 66 | + | size_t* ret; | |
| 67 | 67 | for(size_t i = 0; i < haystack->length; i++) | |
| 68 | 68 | { | |
| 69 | 69 | if(haystack->chars[i] == needle) | |
| 70 | 70 | { | |
| 71 | - | ret.found = true; | |
| 72 | - | ret.index = i; | |
| 71 | + | ret = malloc(sizeof(*ret)); | |
| 72 | + | *ret = i; | |
| 73 | 73 | return ret; | |
| 74 | 74 | } | |
| 75 | 75 | } | |
| 76 | - | ret.found = false; | |
| 77 | - | return ret; | |
| 76 | + | return NULL; | |
| 78 | 77 | } | |
| 79 | 78 | ||
| 80 | 79 | bool string_adjust_size(String* string, size_t size) | |
Mdyn_string.h
| @@ -13,18 +13,12 @@ typedef struct String | |||
|---|---|---|---|
| 13 | 13 | size_t length; | |
| 14 | 14 | } String; | |
| 15 | 15 | ||
| 16 | - | typedef struct FindReturn | |
| 17 | - | { | |
| 18 | - | bool found; | |
| 19 | - | size_t index; //if found is false, this contains rubbish | |
| 20 | - | } FindReturn; | |
| 21 | - | ||
| 22 | 16 | String* new_string(void); | |
| 23 | 17 | char string_at(const String* string, size_t index); | |
| 24 | 18 | bool string_append(String* string, char item); | |
| 25 | 19 | void string_remove(String* string, size_t index); | |
| 26 | 20 | bool string_adjust_size(String* string, size_t size); | |
| 27 | - | FindReturn string_find_char(const String* haystack, const char needle); | |
| 21 | + | size_t* string_find_char(const String* haystack, const char needle); | |
| 28 | 22 | bool string_insert(String* string, size_t index, char character); | |
| 29 | 23 | bool string_concat(String* string, String* other); | |
| 30 | 24 | ||
Mvector.c
| @@ -38,11 +38,14 @@ void* vector_pop(Vector* vector, size_t index) | |||
|---|---|---|---|
| 38 | 38 | } | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | - | void vector_remove(Vector* vector, size_t index) | |
| 41 | + | void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)) | |
| 42 | 42 | { | |
| 43 | 43 | if(index < vector->length) | |
| 44 | 44 | { | |
| 45 | - | free(vector->items[index]); | |
| 45 | + | if(rmv) | |
| 46 | + | rmv(vector->items[index]); | |
| 47 | + | else | |
| 48 | + | free(vector->items[index]); | |
| 46 | 49 | memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items)); | |
| 47 | 50 | vector->length--; | |
| 48 | 51 | } | |
| @@ -56,30 +59,19 @@ bool vector_push(Vector* vector, void* item) | |||
|---|---|---|---|
| 56 | 59 | return true; | |
| 57 | 60 | } | |
| 58 | 61 | ||
| 59 | - | FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*)) | |
| 62 | + | size_t* vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*)) | |
| 60 | 63 | { | |
| 61 | - | FindReturn ret; | |
| 64 | + | size_t* ret; | |
| 62 | 65 | for(size_t i = 0; i < haystack->length; i++) | |
| 63 | 66 | { | |
| 64 | - | if(cmp) | |
| 67 | + | if(cmp ? cmp(haystack->items[i],needle) : haystack->items[i] == needle) | |
| 65 | 68 | { | |
| 66 | - | if(cmp(haystack->items[i],needle)) | |
| 67 | - | { | |
| 68 | - | ret.found = true; | |
| 69 | - | ret.index = i; | |
| 70 | - | return ret; | |
| 71 | - | } | |
| 72 | - | } else { | |
| 73 | - | if(haystack->items[i] == needle) | |
| 74 | - | { | |
| 75 | - | ret.found = true; | |
| 76 | - | ret.index = i; | |
| 77 | - | return ret; | |
| 78 | - | } | |
| 69 | + | ret = malloc(sizeof(*ret)); | |
| 70 | + | *ret = i; | |
| 71 | + | return ret; | |
| 79 | 72 | } | |
| 80 | 73 | } | |
| 81 | - | ret.found = false; | |
| 82 | - | return ret; | |
| 74 | + | return NULL; | |
| 83 | 75 | } | |
| 84 | 76 | ||
| 85 | 77 | bool vector_insert(Vector* vector, size_t index, void* item) | |
| @@ -119,3 +111,15 @@ bool vector_adjust_size(Vector* vector, size_t size) | |||
|---|---|---|---|
| 119 | 111 | } | |
| 120 | 112 | return true; | |
| 121 | 113 | } | |
| 114 | + | ||
| 115 | + | void delete_vector(Vector* vector, void (*rmv)(void*)) | |
| 116 | + | { | |
| 117 | + | for(size_t i = 0; i < vector->length; i++) | |
| 118 | + | { | |
| 119 | + | if(rmv) | |
| 120 | + | rmv(vector->items[i]); | |
| 121 | + | else | |
| 122 | + | free(vector->items[i]); | |
| 123 | + | } | |
| 124 | + | free(vector); | |
| 125 | + | } | |
Mvector.h
| @@ -13,19 +13,14 @@ typedef struct Vector | |||
|---|---|---|---|
| 13 | 13 | size_t length; | |
| 14 | 14 | } Vector; | |
| 15 | 15 | ||
| 16 | - | typedef struct FindReturn | |
| 17 | - | { | |
| 18 | - | bool found; | |
| 19 | - | size_t index; //if found is false, this contains rubbish | |
| 20 | - | } FindReturn; | |
| 21 | - | ||
| 22 | 16 | Vector* new_vector(void); | |
| 23 | 17 | void* vector_at(const Vector* vector, size_t index); | |
| 24 | 18 | void* vector_pop(Vector* vector, size_t index); | |
| 25 | 19 | bool vector_push(Vector* vector, void* item); | |
| 26 | - | void vector_remove(Vector* vector, size_t index); | |
| 20 | + | void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)); | |
| 21 | + | void delete_vector(Vector* vector, void(*rmv) (void*)); | |
| 27 | 22 | bool vector_adjust_size(Vector* vector, size_t size); | |
| 28 | - | FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*)); | |
| 23 | + | size_t* vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*)); | |
| 29 | 24 | bool vector_insert(Vector* vector, size_t index, void* item); | |
| 30 | 25 | ||
| 31 | 26 | ||