changed one-line functions to macros, made code c89 compliant for better portability
Minclude/cutils/cutils.h
| @@ -1,6 +1,8 @@ | |||
|---|---|---|---|
| 1 | 1 | #ifndef CUTILS_H | |
| 2 | 2 | #define CUTILS_H | |
| 3 | + | ||
| 3 | 4 | #include <cutils/vector.h> | |
| 4 | 5 | #include <cutils/dyn_string.h> | |
| 5 | 6 | #include <cutils/misc.h> | |
| 6 | - | #endif //CUTILS_H | |
| 7 | + | ||
| 8 | + | #endif /* CUTILS_H */ | |
Minclude/cutils/dyn_string.h
| @@ -1,5 +1,6 @@ | |||
|---|---|---|---|
| 1 | 1 | #ifndef DYN_STRING_H | |
| 2 | 2 | #define DYN_STRING_H | |
| 3 | + | ||
| 3 | 4 | #define STRING_DEFAULT_SIZE 8 | |
| 4 | 5 | #include <stdint.h> | |
| 5 | 6 | #include <stdlib.h> | |
| @@ -32,4 +33,4 @@ String* from_cstring(const char* cstring); | |||
|---|---|---|---|
| 32 | 33 | char* to_cstring(const String* string); | |
| 33 | 34 | ||
| 34 | 35 | ||
| 35 | - | #endif //DYN_STRING_H | |
| 36 | + | #endif /* DYN_STRING_H */ | |
Minclude/cutils/misc.h
| @@ -1,15 +1,19 @@ | |||
|---|---|---|---|
| 1 | 1 | #ifndef MISC_H | |
| 2 | 2 | #define MISC_H | |
| 3 | - | #include <stdint.h> | |
| 4 | - | #include <string.h> | |
| 5 | 3 | ||
| 6 | 4 | #ifdef __unix__ | |
| 5 | + | #define _BSD_SOURCE | |
| 6 | + | #define _DEFAULT_SOURCE | |
| 7 | 7 | #include <unistd.h> | |
| 8 | 8 | #endif | |
| 9 | 9 | #ifdef _WIN32 | |
| 10 | 10 | #include <windows.h> | |
| 11 | 11 | #endif | |
| 12 | 12 | ||
| 13 | + | #include <stdint.h> | |
| 14 | + | #include <string.h> | |
| 15 | + | ||
| 13 | 16 | void sleep_ms(unsigned int milliseconds); | |
| 14 | 17 | uint32_t ntoh32(uint32_t const net); | |
| 15 | - | #endif //MISC_H | |
| 18 | + | ||
| 19 | + | #endif /* MISC_H */ | |
Minclude/cutils/vector.h
| @@ -1,5 +1,6 @@ | |||
|---|---|---|---|
| 1 | 1 | #ifndef VECTOR_H | |
| 2 | 2 | #define VECTOR_H | |
| 3 | + | ||
| 3 | 4 | #define VECTOR_DEFAULT_SIZE 4 | |
| 4 | 5 | #include <stdint.h> | |
| 5 | 6 | #include <stdlib.h> | |
| @@ -13,16 +14,16 @@ typedef struct Vector | |||
|---|---|---|---|
| 13 | 14 | size_t length; | |
| 14 | 15 | } Vector; | |
| 15 | 16 | ||
| 16 | - | Vector* new_vector(void); | |
| 17 | + | #define new_vector() vector_with_capacity(VECTOR_DEFAULT_SIZE); | |
| 17 | 18 | Vector* vector_with_capacity(size_t capacity); | |
| 18 | 19 | void delete_vector(Vector* vector, void(*rmv) (void*)); | |
| 19 | 20 | ||
| 20 | 21 | void* vector_at(const Vector* vector, size_t index); | |
| 21 | - | void* vector_pop(Vector* vector); | |
| 22 | + | #define vector_pop(vector) vector_pop_at(vector, vector->length-1); | |
| 22 | 23 | void* vector_pop_at(Vector* vector, size_t index); | |
| 23 | 24 | ||
| 24 | 25 | bool vector_insert(Vector* vector, size_t index, void* item); | |
| 25 | - | bool vector_push(Vector* vector, void* item); | |
| 26 | + | #define vector_push(vector, item) vector_insert(vector, vector->length, item); | |
| 26 | 27 | void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)); | |
| 27 | 28 | ||
| 28 | 29 | bool vector_adjust_size(Vector* vector, size_t size); | |
| @@ -30,5 +31,4 @@ bool vector_shrink(Vector* vector); | |||
|---|---|---|---|
| 30 | 31 | size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*)); | |
| 31 | 32 | ||
| 32 | 33 | ||
| 33 | - | ||
| 34 | - | #endif //VECTOR_H | |
| 34 | + | #endif /* VECTOR_H */ | |
Mmeson.build
| @@ -1,7 +1,7 @@ | |||
|---|---|---|---|
| 1 | 1 | project('cutils', 'c') | |
| 2 | 2 | ||
| 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=gnu11', '-fstrict-aliasing', '-fPIC'] + WARNINGS | |
| 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=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS | |
| 5 | 5 | ||
| 6 | 6 | subdir('include') | |
| 7 | 7 | subdir('src') | |
Msrc/dyn_string.c
| @@ -67,7 +67,8 @@ bool string_concat(String* string, const String* other) | |||
|---|---|---|---|
| 67 | 67 | size_t* string_find_char(const String* haystack, const char needle) | |
| 68 | 68 | { | |
| 69 | 69 | size_t* ret; | |
| 70 | - | for(size_t i = 0; i < haystack->length; i++) | |
| 70 | + | size_t i; | |
| 71 | + | for(i = 0; i < haystack->length; i++) | |
| 71 | 72 | { | |
| 72 | 73 | if(haystack->chars[i] == needle) | |
| 73 | 74 | { | |
Msrc/test.c
| @@ -7,18 +7,22 @@ struct test | |||
|---|---|---|---|
| 7 | 7 | int b; | |
| 8 | 8 | }; | |
| 9 | 9 | ||
| 10 | - | int cmp_str(const void* str1, const void* str2) | |
| 10 | + | static int cmp_str(const void* str1, const void* str2) | |
| 11 | 11 | { | |
| 12 | 12 | const struct test* str1_s = str1; | |
| 13 | 13 | const struct test* str2_s = str2; | |
| 14 | 14 | return !((str1_s->a == str2_s->a) && (str1_s->b == str2_s->b)); | |
| 15 | 15 | } | |
| 16 | 16 | ||
| 17 | - | void test1(void) | |
| 17 | + | static void test1(void) | |
| 18 | 18 | { | |
| 19 | - | Vector* test = new_vector(); | |
| 19 | + | size_t* find; | |
| 20 | + | Vector* test; | |
| 21 | + | struct test* my_struct; | |
| 22 | + | ||
| 23 | + | my_struct = malloc(sizeof(*my_struct)); | |
| 24 | + | test = new_vector() | |
| 20 | 25 | ||
| 21 | - | struct test* my_struct = malloc(sizeof(*my_struct)); | |
| 22 | 26 | my_struct->a = 5; | |
| 23 | 27 | my_struct->b = 5; | |
| 24 | 28 | vector_push(test, my_struct); | |
| @@ -47,7 +51,7 @@ void test1(void) | |||
|---|---|---|---|
| 47 | 51 | my_struct->a = 8; | |
| 48 | 52 | my_struct->b = 8; | |
| 49 | 53 | ||
| 50 | - | size_t* find = vector_find(test, my_struct, cmp_str); | |
| 54 | + | find = vector_find(test, my_struct, cmp_str); | |
| 51 | 55 | printf("pos: %lu\n", *find); | |
| 52 | 56 | free(find); | |
| 53 | 57 | ||
| @@ -59,16 +63,18 @@ void test1(void) | |||
|---|---|---|---|
| 59 | 63 | delete_vector(test,free); | |
| 60 | 64 | } | |
| 61 | 65 | ||
| 62 | - | void test2(void) | |
| 66 | + | static void test2(void) | |
| 63 | 67 | { | |
| 64 | 68 | String* test = new_string(); | |
| 69 | + | String* test2; | |
| 70 | + | char* cstring; | |
| 65 | 71 | string_append(test, 'a'); | |
| 66 | 72 | string_append(test, 'b'); | |
| 67 | 73 | string_append(test, 'c'); | |
| 68 | 74 | string_append(test, 'd'); | |
| 69 | 75 | printf("%ld\n", test->length); | |
| 70 | 76 | ||
| 71 | - | char* cstring = to_cstring(test); | |
| 77 | + | cstring = to_cstring(test); | |
| 72 | 78 | printf("%s\n", cstring); | |
| 73 | 79 | free(cstring); | |
| 74 | 80 | ||
| @@ -83,7 +89,7 @@ void test2(void) | |||
|---|---|---|---|
| 83 | 89 | printf("%s\n", cstring); | |
| 84 | 90 | free(cstring); | |
| 85 | 91 | ||
| 86 | - | String* test2 = from_cstring("xyz"); | |
| 92 | + | test2 = from_cstring("xyz"); | |
| 87 | 93 | ||
| 88 | 94 | string_concat(test,test2); | |
| 89 | 95 | cstring = to_cstring(test); | |
| @@ -99,4 +105,5 @@ int main(int argc, char** argv) | |||
|---|---|---|---|
| 99 | 105 | test1(); | |
| 100 | 106 | test2(); | |
| 101 | 107 | sleep_ms(1000); | |
| 108 | + | return 0; | |
| 102 | 109 | } | |
Msrc/vector.c
| @@ -1,10 +1,5 @@ | |||
|---|---|---|---|
| 1 | 1 | #include <cutils/vector.h> | |
| 2 | 2 | ||
| 3 | - | Vector* new_vector(void) | |
| 4 | - | { | |
| 5 | - | return vector_with_capacity(VECTOR_DEFAULT_SIZE); | |
| 6 | - | } | |
| 7 | - | ||
| 8 | 3 | Vector* vector_with_capacity(size_t capacity) | |
| 9 | 4 | { | |
| 10 | 5 | Vector* vector = malloc(sizeof(*vector)); | |
| @@ -30,11 +25,6 @@ void* vector_at(const Vector* vector, size_t index) | |||
|---|---|---|---|
| 30 | 25 | return vector->items[index]; | |
| 31 | 26 | } | |
| 32 | 27 | ||
| 33 | - | void* vector_pop(Vector* vector) | |
| 34 | - | { | |
| 35 | - | return vector_pop_at(vector, vector->length-1); | |
| 36 | - | } | |
| 37 | - | ||
| 38 | 28 | void* vector_pop_at(Vector* vector, size_t index) | |
| 39 | 29 | { | |
| 40 | 30 | void* tmp = vector_at(vector, index); | |
| @@ -59,15 +49,12 @@ void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)) | |||
|---|---|---|---|
| 59 | 49 | } | |
| 60 | 50 | } | |
| 61 | 51 | ||
| 62 | - | bool vector_push(Vector* vector, void* item) | |
| 63 | - | { | |
| 64 | - | return vector_insert(vector, vector->length, item); | |
| 65 | - | } | |
| 66 | - | ||
| 67 | 52 | size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*)) | |
| 68 | 53 | { | |
| 69 | 54 | size_t* ret; | |
| 70 | - | for(size_t i = 0; i < haystack->length; i++) | |
| 55 | + | size_t i; | |
| 56 | + | ||
| 57 | + | for(i = 0; i < haystack->length; i++) | |
| 71 | 58 | { | |
| 72 | 59 | if(cmp ? cmp(haystack->items[i],needle) == 0 : haystack->items[i] == needle) | |
| 73 | 60 | { | |
| @@ -124,7 +111,8 @@ bool vector_shrink(Vector* vector) | |||
|---|---|---|---|
| 124 | 111 | ||
| 125 | 112 | void delete_vector(Vector* vector, void (*rmv)(void*)) | |
| 126 | 113 | { | |
| 127 | - | for(size_t i = 0; i < vector->length; i++) | |
| 114 | + | size_t i; | |
| 115 | + | for(i = 0; i < vector->length; i++) | |
| 128 | 116 | { | |
| 129 | 117 | if(rmv) | |
| 130 | 118 | rmv(vector->items[i]); | |