First commit
This commit is contained in:
111
include/DynArray.h
Normal file
111
include/DynArray.h
Normal file
@@ -0,0 +1,111 @@
|
||||
#ifndef DYN_ARRAY_H_
|
||||
#define DYN_ARRAY_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
// #define SCUFCONT_BOUNDS_CHECK
|
||||
|
||||
#ifdef SCUFCONT_BOUNDS_CHECK
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#endif // SCUFCONT_BOUNDS_CHECK
|
||||
|
||||
#define DYN_ARRAY_EXPANDER(macro, type, type_name, func_name) macro(type, DynArray_##type_name, DynArray_##type_name##_##func_name);
|
||||
|
||||
#define DYN_ARRAY_IMPL(type, type_name) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_ALLOC, type, type_name, alloc) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_IS_NULL, type, type_name, is_null) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_PUSH_BACK, type, type_name, push_back) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_AT, type, type_name, at)
|
||||
|
||||
#define DYN_ARRAY_HEADER(type, type_name) \
|
||||
DYN_ARRAY_STRUCT(type, type_name) \
|
||||
DYN_ARRAY_SIGS(type, type_name)
|
||||
|
||||
#define DYN_ARRAY_SIGS(type, type_name) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_ALLOC_SIG, type, type_name, alloc) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_IS_NULL_SIG, type, type_name, is_null) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_PUSH_BACK_SIG, type, type_name, push_back) \
|
||||
DYN_ARRAY_EXPANDER(DYN_ARRAY_AT_SIG, type, type_name, at)
|
||||
|
||||
// DYN_ARRAY_ALLOC_SIG(type, DynArray_##type_name, DynArray_##type_name##_alloc); \
|
||||
// DYN_ARRAY_PUSH_BACK_SIG(type, DynArray_##type_name, DynArray_##type_name##_push_back);
|
||||
|
||||
#define DYN_ARRAY_STRUCT(type, type_name) \
|
||||
typedef struct { \
|
||||
type* data; \
|
||||
size_t len; \
|
||||
size_t capacity; \
|
||||
} DynArray_##type_name;
|
||||
|
||||
// DYN_ARRAY_STRUCT(int);
|
||||
|
||||
#define DYN_ARRAY_ALLOC_SIG(type, type_name, func_name) bool func_name(type_name* darr)
|
||||
|
||||
#define DYN_ARRAY_ALLOC(type, type_name, func_name) \
|
||||
DYN_ARRAY_ALLOC_SIG(type, type_name, func_name) { \
|
||||
darr->data = (type*) calloc(1, sizeof(type)); \
|
||||
if (darr->data == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
darr->len = 0; \
|
||||
darr->capacity = 1; \
|
||||
return true; \
|
||||
}
|
||||
|
||||
#define DYN_ARRAY_IS_NULL_SIG(type, type_name, func_name) bool func_name(const type_name* darr)
|
||||
|
||||
#define DYN_ARRAY_IS_NULL(type, type_name, func_name) \
|
||||
DYN_ARRAY_IS_NULL_SIG(type, type_name, func_name) { \
|
||||
return darr->data == NULL; \
|
||||
}
|
||||
|
||||
// DYN_ARRAY_ALLOC(int, DynArray_int, DynArray_int_alloc);
|
||||
|
||||
#define DYN_ARRAY_PUSH_BACK_SIG(type, type_name, func_name) int func_name(type_name* darr, const type* new_elem)
|
||||
|
||||
#define DYN_ARRAY_PUSH_BACK(type, type_name, func_name) \
|
||||
DYN_ARRAY_PUSH_BACK_SIG(type, type_name, func_name) { \
|
||||
if (darr->len + 1 > darr->capacity) { \
|
||||
darr->capacity *= 2; \
|
||||
darr->data = (type*) realloc(darr->data, darr->capacity * sizeof(type)); \
|
||||
if (darr->data == NULL) { \
|
||||
return -1; \
|
||||
} \
|
||||
} \
|
||||
memcpy(darr->data + darr->len, new_elem, sizeof(type)); \
|
||||
darr->len++; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
#define DYN_ARRAY_AT_SIG(type, type_name, func_name) type* func_name(type_name* darr, const size_t i)
|
||||
|
||||
// DYN_ARRAY_PUSH_BACK(int, DynArray_int, DynArray_int_push_back);
|
||||
|
||||
#ifdef SCUFCONT_BOUNDS_CHECK
|
||||
|
||||
#define DYN_ARRAY_AT(type, type_name, func_name) \
|
||||
DYN_ARRAY_AT_SIG(type, type_name, func_name) { \
|
||||
if (i < 0 || i > darr->len) { \
|
||||
fprintf(stderr, #func_name " out-of-bounds. i: %zu, len: %zu", i, darr->len); \
|
||||
exit(1); \
|
||||
} \
|
||||
\
|
||||
return darr->data + i; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define DYN_ARRAY_AT(type, type_name, func_name) \
|
||||
DYN_ARRAY_AT_SIG(type, type_name, func_name) { \
|
||||
return darr->data + i; \
|
||||
}
|
||||
|
||||
#endif // SCUFCONT_BOUNDS_CHECK
|
||||
|
||||
// DYN_ARRAY_AT(char*, DynArray_charp, DynArray_charp_at)
|
||||
|
||||
#endif // DYN_ARRAY_H_
|
||||
51
include/DynString.h
Normal file
51
include/DynString.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef DYN_STRING_H_
|
||||
#define DYN_STRING_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "scufcont.h"
|
||||
|
||||
#define DYN_STRING_FMT(dstr) (int) (dstr).len, (dstr).data
|
||||
|
||||
#define DynString_is_null(dstr) ((dstr)->data == NULL)
|
||||
#define DynString_c_str(dstr) ((dstr)->data)
|
||||
|
||||
typedef struct {
|
||||
char* data;
|
||||
size_t len;
|
||||
size_t capacity;
|
||||
} DynString;
|
||||
|
||||
ScufContResult DynString_alloc(DynString* dstr, const char* str, const size_t len);
|
||||
ScufContResult DynString_reserve(DynString* dstr, const size_t new_capacity);
|
||||
void DynString_free(DynString* dstr);
|
||||
bool DynString_equal(const DynString* s1, const DynString* s2);
|
||||
bool DynString_equal_c_str(const DynString* s1, const char* s2);
|
||||
bool DynString_equal_next_chars_c_str(
|
||||
const DynString* dstr,
|
||||
const char* str,
|
||||
const size_t str_len,
|
||||
const size_t s
|
||||
);
|
||||
ScufContResult DynString_concat_c_str(
|
||||
DynString* dstr,
|
||||
const char* str,
|
||||
const size_t str_len
|
||||
);
|
||||
ScufContResult DynString_alloc_concat_c_strs(
|
||||
DynString* dstr,
|
||||
const char* s1,
|
||||
const size_t s1_len,
|
||||
const char* s2,
|
||||
const size_t s2_len
|
||||
);
|
||||
ScufContResult DynString_split(
|
||||
const DynString* dstr,
|
||||
const char* delim,
|
||||
DynString** split_arr,
|
||||
size_t* arr_len
|
||||
);
|
||||
void DynString_split_arr_free(DynString** arr, const size_t arr_len);
|
||||
|
||||
#endif // DYN_STRING_H_
|
||||
11
include/scufcont.h
Normal file
11
include/scufcont.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef SCUFCONT_H_
|
||||
#define SCUFCONT_H_
|
||||
|
||||
typedef enum {
|
||||
SCUFCONT_SUCCESS,
|
||||
SCUFCONT_OUT_OF_MEMORY
|
||||
} ScufContResult;
|
||||
|
||||
extern const char* SCUFCONT_VERSION;
|
||||
|
||||
#endif // SCUFCONT_H_
|
||||
Reference in New Issue
Block a user