added big-endian to host converter, added remove function for vectors, changed return type of search functions

AuthorKonata <konata@posteo.jp>
Date
Commit76f64f83371e70b67ae987a6e6dd85d7d2ac45cf
Parent0d73963
7 files changed, 54 insertions(+), 44 deletions(-)
MMakefile
@@ -1,3 +1,3 @@
11 all:
22 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 @@
11 #ifndef CUTILS_H
22 #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"
56 #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)
6161 return true;
6262 }
6363
64-FindReturn string_find_char(const String* haystack, const char needle)
64+size_t* string_find_char(const String* haystack, const char needle)
6565 {
66- FindReturn ret;
66+ size_t* ret;
6767 for(size_t i = 0; i < haystack->length; i++)
6868 {
6969 if(haystack->chars[i] == needle)
7070 {
71- ret.found = true;
72- ret.index = i;
71+ ret = malloc(sizeof(*ret));
72+ *ret = i;
7373 return ret;
7474 }
7575 }
76- ret.found = false;
77- return ret;
76+ return NULL;
7877 }
7978
8079 bool string_adjust_size(String* string, size_t size)
Mdyn_string.h
@@ -13,18 +13,12 @@ typedef struct String
1313 size_t length;
1414 } String;
1515
16-typedef struct FindReturn
17-{
18- bool found;
19- size_t index; //if found is false, this contains rubbish
20-} FindReturn;
21-
2216 String* new_string(void);
2317 char string_at(const String* string, size_t index);
2418 bool string_append(String* string, char item);
2519 void string_remove(String* string, size_t index);
2620 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);
2822 bool string_insert(String* string, size_t index, char character);
2923 bool string_concat(String* string, String* other);
3024
Mvector.c
@@ -38,11 +38,14 @@ void* vector_pop(Vector* vector, size_t index)
3838 }
3939 }
4040
41-void vector_remove(Vector* vector, size_t index)
41+void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
4242 {
4343 if(index < vector->length)
4444 {
45- free(vector->items[index]);
45+ if(rmv)
46+ rmv(vector->items[index]);
47+ else
48+ free(vector->items[index]);
4649 memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
4750 vector->length--;
4851 }
@@ -56,30 +59,19 @@ bool vector_push(Vector* vector, void* item)
5659 return true;
5760 }
5861
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*))
6063 {
61- FindReturn ret;
64+ size_t* ret;
6265 for(size_t i = 0; i < haystack->length; i++)
6366 {
64- if(cmp)
67+ if(cmp ? cmp(haystack->items[i],needle) : haystack->items[i] == needle)
6568 {
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;
7972 }
8073 }
81- ret.found = false;
82- return ret;
74+ return NULL;
8375 }
8476
8577 bool vector_insert(Vector* vector, size_t index, void* item)
@@ -119,3 +111,15 @@ bool vector_adjust_size(Vector* vector, size_t size)
119111 }
120112 return true;
121113 }
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
1313 size_t length;
1414 } Vector;
1515
16-typedef struct FindReturn
17-{
18- bool found;
19- size_t index; //if found is false, this contains rubbish
20-} FindReturn;
21-
2216 Vector* new_vector(void);
2317 void* vector_at(const Vector* vector, size_t index);
2418 void* vector_pop(Vector* vector, size_t index);
2519 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*));
2722 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*));
2924 bool vector_insert(Vector* vector, size_t index, void* item);
3025
3126