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 @@
all:
gcc -Og -g -march=native -shared -fPIC -fstrict-aliasing vector.c dyn_string.c -o cutils.so -flto \
-Wall -Werror -Wpedantic -Wnull-dereference -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wstrict-overflow=5 -Wunreachable-code -Wno-unused-parameter
-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 @@
#ifndef CUTILS_H
#define CUTILS_H
#include <vector.h>
#include <dyn_string.h>
#include "vector.h"
#include "dyn_string.h"
#include "cutils_endian.h"
#endif //CUTILS_H
▾Acutils_endian.h
@@ -0,0 +1,17 @@
#ifndef CUTILS_ENDIAN_H
#define CUTILS_ENDIAN_H
#include <stdint.h>
#include <string.h>
inline uint32_t ntoh32(uint32_t const net)
{
uint8_t data[4];
memcpy(&data, &net, sizeof(data));
return ((uint32_t) data[3] << 0)
| ((uint32_t) data[2] << 8)
| ((uint32_t) data[1] << 16)
| ((uint32_t) data[0] << 24);
}
#endif //CUTILS_ENDIAN_H
▾Mdyn_string.c
@@ -61,20 +61,19 @@ bool string_concat(String* string, String* other)
return true;
}
FindReturn string_find_char(const String* haystack, const char needle)
size_t* string_find_char(const String* haystack, const char needle)
{
FindReturn ret;
size_t* ret;
for(size_t i = 0; i < haystack->length; i++)
{
if(haystack->chars[i] == needle)
{
ret.found = true;
ret.index = i;
ret = malloc(sizeof(*ret));
*ret = i;
return ret;
}
}
ret.found = false;
return ret;
return NULL;
}
bool string_adjust_size(String* string, size_t size)
▾Mdyn_string.h
@@ -13,18 +13,12 @@ typedef struct String
size_t length;
} String;
typedef struct FindReturn
{
bool found;
size_t index; //if found is false, this contains rubbish
} FindReturn;
String* new_string(void);
char string_at(const String* string, size_t index);
bool string_append(String* string, char item);
void string_remove(String* string, size_t index);
bool string_adjust_size(String* string, size_t size);
FindReturn string_find_char(const String* haystack, const char needle);
size_t* string_find_char(const String* haystack, const char needle);
bool string_insert(String* string, size_t index, char character);
bool string_concat(String* string, String* other);
▾Mvector.c
@@ -38,11 +38,14 @@ void* vector_pop(Vector* vector, size_t index)
}
}
void vector_remove(Vector* vector, size_t index)
void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
{
if(index < vector->length)
{
free(vector->items[index]);
if(rmv)
rmv(vector->items[index]);
else
free(vector->items[index]);
memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
vector->length--;
}
@@ -56,30 +59,19 @@ bool vector_push(Vector* vector, void* item)
return true;
}
FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*))
size_t* vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*))
{
FindReturn ret;
size_t* ret;
for(size_t i = 0; i < haystack->length; i++)
{
if(cmp)
if(cmp ? cmp(haystack->items[i],needle) : haystack->items[i] == needle)
{
if(cmp(haystack->items[i],needle))
{
ret.found = true;
ret.index = i;
return ret;
}
} else {
if(haystack->items[i] == needle)
{
ret.found = true;
ret.index = i;
return ret;
}
ret = malloc(sizeof(*ret));
*ret = i;
return ret;
}
}
ret.found = false;
return ret;
return NULL;
}
bool vector_insert(Vector* vector, size_t index, void* item)
@@ -119,3 +111,15 @@ bool vector_adjust_size(Vector* vector, size_t size)
}
return true;
}
void delete_vector(Vector* vector, void (*rmv)(void*))
{
for(size_t i = 0; i < vector->length; i++)
{
if(rmv)
rmv(vector->items[i]);
else
free(vector->items[i]);
}
free(vector);
}
▾Mvector.h
@@ -13,19 +13,14 @@ typedef struct Vector
size_t length;
} Vector;
typedef struct FindReturn
{
bool found;
size_t index; //if found is false, this contains rubbish
} FindReturn;
Vector* new_vector(void);
void* vector_at(const Vector* vector, size_t index);
void* vector_pop(Vector* vector, size_t index);
bool vector_push(Vector* vector, void* item);
void vector_remove(Vector* vector, size_t index);
void vector_remove(Vector* vector, size_t index, void (*rmv)(void*));
void delete_vector(Vector* vector, void(*rmv) (void*));
bool vector_adjust_size(Vector* vector, size_t size);
FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*));
size_t* vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*));
bool vector_insert(Vector* vector, size_t index, void* item);