changed one-line functions to macros, made code c89 compliant for better portability

AuthorKonata <konata@posteo.jp>
Date
Commit957d0aa75d3e65ebf931c74ff026b572ae59f1fc
Parent8ca5b24
8 files changed, 41 insertions(+), 38 deletions(-)
Minclude/cutils/cutils.h
@@ -1,6 +1,8 @@
11 #ifndef CUTILS_H
22 #define CUTILS_H
3+
34 #include <cutils/vector.h>
45 #include <cutils/dyn_string.h>
56 #include <cutils/misc.h>
6-#endif //CUTILS_H
7+
8+#endif /* CUTILS_H */
Minclude/cutils/dyn_string.h
@@ -1,5 +1,6 @@
11 #ifndef DYN_STRING_H
22 #define DYN_STRING_H
3+
34 #define STRING_DEFAULT_SIZE 8
45 #include <stdint.h>
56 #include <stdlib.h>
@@ -32,4 +33,4 @@ String* from_cstring(const char* cstring);
3233 char* to_cstring(const String* string);
3334
3435
35-#endif //DYN_STRING_H
36+#endif /* DYN_STRING_H */
Minclude/cutils/misc.h
@@ -1,15 +1,19 @@
11 #ifndef MISC_H
22 #define MISC_H
3-#include <stdint.h>
4-#include <string.h>
53
64 #ifdef __unix__
5+ #define _BSD_SOURCE
6+ #define _DEFAULT_SOURCE
77 #include <unistd.h>
88 #endif
99 #ifdef _WIN32
1010 #include <windows.h>
1111 #endif
1212
13+#include <stdint.h>
14+#include <string.h>
15+
1316 void sleep_ms(unsigned int milliseconds);
1417 uint32_t ntoh32(uint32_t const net);
15-#endif //MISC_H
18+
19+#endif /* MISC_H */
Minclude/cutils/vector.h
@@ -1,5 +1,6 @@
11 #ifndef VECTOR_H
22 #define VECTOR_H
3+
34 #define VECTOR_DEFAULT_SIZE 4
45 #include <stdint.h>
56 #include <stdlib.h>
@@ -13,16 +14,16 @@ typedef struct Vector
1314 size_t length;
1415 } Vector;
1516
16-Vector* new_vector(void);
17+#define new_vector() vector_with_capacity(VECTOR_DEFAULT_SIZE);
1718 Vector* vector_with_capacity(size_t capacity);
1819 void delete_vector(Vector* vector, void(*rmv) (void*));
1920
2021 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);
2223 void* vector_pop_at(Vector* vector, size_t index);
2324
2425 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);
2627 void vector_remove(Vector* vector, size_t index, void (*rmv)(void*));
2728
2829 bool vector_adjust_size(Vector* vector, size_t size);
@@ -30,5 +31,4 @@ bool vector_shrink(Vector* vector);
3031 size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*));
3132
3233
33-
34-#endif //VECTOR_H
34+#endif /* VECTOR_H */
Mmeson.build
@@ -1,7 +1,7 @@
11 project('cutils', 'c')
22
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
55
66 subdir('include')
77 subdir('src')
Msrc/dyn_string.c
@@ -67,7 +67,8 @@ bool string_concat(String* string, const String* other)
6767 size_t* string_find_char(const String* haystack, const char needle)
6868 {
6969 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++)
7172 {
7273 if(haystack->chars[i] == needle)
7374 {
Msrc/test.c
@@ -7,18 +7,22 @@ struct test
77 int b;
88 };
99
10-int cmp_str(const void* str1, const void* str2)
10+static int cmp_str(const void* str1, const void* str2)
1111 {
1212 const struct test* str1_s = str1;
1313 const struct test* str2_s = str2;
1414 return !((str1_s->a == str2_s->a) && (str1_s->b == str2_s->b));
1515 }
1616
17-void test1(void)
17+static void test1(void)
1818 {
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()
2025
21- struct test* my_struct = malloc(sizeof(*my_struct));
2226 my_struct->a = 5;
2327 my_struct->b = 5;
2428 vector_push(test, my_struct);
@@ -47,7 +51,7 @@ void test1(void)
4751 my_struct->a = 8;
4852 my_struct->b = 8;
4953
50- size_t* find = vector_find(test, my_struct, cmp_str);
54+ find = vector_find(test, my_struct, cmp_str);
5155 printf("pos: %lu\n", *find);
5256 free(find);
5357
@@ -59,16 +63,18 @@ void test1(void)
5963 delete_vector(test,free);
6064 }
6165
62-void test2(void)
66+static void test2(void)
6367 {
6468 String* test = new_string();
69+ String* test2;
70+ char* cstring;
6571 string_append(test, 'a');
6672 string_append(test, 'b');
6773 string_append(test, 'c');
6874 string_append(test, 'd');
6975 printf("%ld\n", test->length);
7076
71- char* cstring = to_cstring(test);
77+ cstring = to_cstring(test);
7278 printf("%s\n", cstring);
7379 free(cstring);
7480
@@ -83,7 +89,7 @@ void test2(void)
8389 printf("%s\n", cstring);
8490 free(cstring);
8591
86- String* test2 = from_cstring("xyz");
92+ test2 = from_cstring("xyz");
8793
8894 string_concat(test,test2);
8995 cstring = to_cstring(test);
@@ -99,4 +105,5 @@ int main(int argc, char** argv)
99105 test1();
100106 test2();
101107 sleep_ms(1000);
108+ return 0;
102109 }
Msrc/vector.c
@@ -1,10 +1,5 @@
11 #include <cutils/vector.h>
22
3-Vector* new_vector(void)
4-{
5- return vector_with_capacity(VECTOR_DEFAULT_SIZE);
6-}
7-
83 Vector* vector_with_capacity(size_t capacity)
94 {
105 Vector* vector = malloc(sizeof(*vector));
@@ -30,11 +25,6 @@ void* vector_at(const Vector* vector, size_t index)
3025 return vector->items[index];
3126 }
3227
33-void* vector_pop(Vector* vector)
34-{
35- return vector_pop_at(vector, vector->length-1);
36-}
37-
3828 void* vector_pop_at(Vector* vector, size_t index)
3929 {
4030 void* tmp = vector_at(vector, index);
@@ -59,15 +49,12 @@ void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
5949 }
6050 }
6151
62-bool vector_push(Vector* vector, void* item)
63-{
64- return vector_insert(vector, vector->length, item);
65-}
66-
6752 size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*))
6853 {
6954 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++)
7158 {
7259 if(cmp ? cmp(haystack->items[i],needle) == 0 : haystack->items[i] == needle)
7360 {
@@ -124,7 +111,8 @@ bool vector_shrink(Vector* vector)
124111
125112 void delete_vector(Vector* vector, void (*rmv)(void*))
126113 {
127- for(size_t i = 0; i < vector->length; i++)
114+ size_t i;
115+ for(i = 0; i < vector->length; i++)
128116 {
129117 if(rmv)
130118 rmv(vector->items[i]);