From 7ca3f68ac2aa6ee7cee632f8cd72cc3ef6c4faa9 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Mon, 25 Sep 2023 02:38:54 +0200 Subject: fix impl include order bug --- gen_inl.c | 33 +++-- include/kit.inl.h | 400 +++++++++++++++++++++++++------------------------- source/kit/kit_test.h | 57 ++++--- 3 files changed, 260 insertions(+), 230 deletions(-) diff --git a/gen_inl.c b/gen_inl.c index 975b7a5..e5ec31e 100644 --- a/gen_inl.c +++ b/gen_inl.c @@ -101,12 +101,15 @@ int write_file(FILE *out, char *source) { return 1; } - fprintf(out, "/*%s*\n", repeat(67, '*')); - fprintf(out, " *%s*\n", repeat(67, ' ')); - fprintf(out, " * File: %s%s*\n", source, - repeat(55 - strlen(source), ' ')); - fprintf(out, " *%s*\n", repeat(67, ' ')); - fprintf(out, " *%s*/\n", repeat(67, '*')); + fprintf(out, "// " + "=====================================================" + "===========\n"); + fprintf(out, "//\n"); + fprintf(out, "// File: %s\n", source); + fprintf(out, "//\n"); + fprintf(out, "// " + "=====================================================" + "===========\n"); char line[200]; char buf[400]; @@ -147,7 +150,8 @@ int main(int argc, char **argv) { fprintf(out, "// kit.inl.h\n"); fprintf(out, "// https://guattari.tech/kit.git\n"); fprintf(out, "//\n"); - fprintf(out, "// Single header-only cross-platform utility library for C.\n"); + fprintf(out, "// Single header-only cross-platform utility " + "library for C.\n"); fprintf(out, "//\n"); fprintf(out, "// " "=====================================================" @@ -207,7 +211,19 @@ int main(int argc, char **argv) { return 1; } - fprintf(out, "#ifdef KIT_IMPLEMENTATION\n"); + fprintf(out, "#endif\n"); + fprintf(out, "// " + "=====================================================" + "===========\n"); + fprintf(out, "//\n"); + fprintf(out, "// IMPLEMENTATION\n"); + fprintf(out, "//\n"); + fprintf(out, "// " + "=====================================================" + "===========\n"); + fprintf(out, "#if defined(KIT_IMPLEMENTATION) && " + "!defined(KIT_INL_H_IMPL)\n"); + fprintf(out, "#define KIT_INL_H_IMPL\n"); for (int i = 0; i < sizeof IMPL / sizeof *IMPL; i++) if (write_file(out, IMPL[i]) != 0) { @@ -216,7 +232,6 @@ int main(int argc, char **argv) { } fprintf(out, "#endif\n"); - fprintf(out, "#endif\n"); fclose(out); return 0; diff --git a/include/kit.inl.h b/include/kit.inl.h index da90638..cf5b696 100644 --- a/include/kit.inl.h +++ b/include/kit.inl.h @@ -34,11 +34,11 @@ // ================================================================ #ifndef KIT_INL_H #define KIT_INL_H -/********************************************************************* - * * - * File: source/kit/types.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/types.h +// +// ================================================================ #ifndef KIT_TYPES_H #define KIT_TYPES_H #ifndef _GNU_SOURCE @@ -55,11 +55,11 @@ typedef unsigned long long u64; typedef float f32; typedef double f64; #endif -/********************************************************************* - * * - * File: source/kit/status.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/status.h +// +// ================================================================ #ifndef KIT_STATUS_H #define KIT_STATUS_H enum { @@ -89,11 +89,11 @@ enum { }; typedef i32 kit_status_t; #endif -/********************************************************************* - * * - * File: source/kit/allocator.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/allocator.h +// +// ================================================================ #ifndef KIT_ALLOCATOR_H #define KIT_ALLOCATOR_H #ifdef __cplusplus @@ -133,11 +133,11 @@ kit_allocator_t kit_alloc_buffer(i64 size, void *buffer); } #endif #endif -/********************************************************************* - * * - * File: source/kit/time.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/time.h +// +// ================================================================ #ifndef KIT_TIME_H #define KIT_TIME_H #ifndef _GNU_SOURCE @@ -180,11 +180,11 @@ static int timespec_get(struct timespec *ts, int base) { } #endif #endif -/********************************************************************* - * * - * File: source/kit/atomic.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/atomic.h +// +// ================================================================ #ifndef KIT_ATOMIC_H #define KIT_ATOMIC_H #ifndef _MSC_VER @@ -365,11 +365,11 @@ u64 kit_atomic_fetch_add_explicit_64(u64 volatile *var, u64 value, # define ATOMIC KIT_ATOMIC #endif #endif -/********************************************************************* - * * - * File: source/kit/threads.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/threads.h +// +// ================================================================ #ifndef KIT_THREADS_H #define KIT_THREADS_H #ifndef KIT_DISABLE_SYSTEM_THREADS @@ -483,11 +483,11 @@ void thrd_yield(void); # endif #endif #endif -/********************************************************************* - * * - * File: source/kit/array_ref.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/array_ref.h +// +// ================================================================ #ifndef KIT_ARRAY_REF_H #define KIT_ARRAY_REF_H #ifdef __cplusplus @@ -534,11 +534,11 @@ i8 kit_ar_compare(i64 left_element_size, i64 left_size, # define AR_COMPARE KIT_AR_COMPARE #endif #endif -/********************************************************************* - * * - * File: source/kit/dynamic_array.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/dynamic_array.h +// +// ================================================================ #ifndef KIT_DYNAMIC_ARRAY_H #define KIT_DYNAMIC_ARRAY_H #include @@ -647,11 +647,11 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size, # define DA_ERASE KIT_DA_ERASE #endif #endif -/********************************************************************* - * * - * File: source/kit/string_ref.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/string_ref.h +// +// ================================================================ #ifndef KIT_STRING_REF_H #define KIT_STRING_REF_H #include @@ -707,11 +707,11 @@ static char *kit_make_bs(kit_str_t s) { } #endif #endif -/********************************************************************* - * * - * File: source/kit/string_builder.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/string_builder.h +// +// ================================================================ #ifndef KIT_STRING_BUILDER_H #define KIT_STRING_BUILDER_H typedef KIT_DA(char) kit_str_builder_t; @@ -719,11 +719,11 @@ typedef KIT_DA(char) kit_str_builder_t; # define str_builder_t kit_str_builder_t #endif #endif -/********************************************************************* - * * - * File: source/kit/lower_bound.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/lower_bound.h +// +// ================================================================ #ifndef KIT_LOWER_BOUND_H #define KIT_LOWER_BOUND_H #ifdef __cplusplus @@ -759,11 +759,11 @@ extern "C" { # define LOWER_BOUND_REF KIT_LOWER_BOUND_REF #endif #endif -/********************************************************************* - * * - * File: source/kit/move_back.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/move_back.h +// +// ================================================================ #ifndef KIT_MOVE_BACK_H #define KIT_MOVE_BACK_H #include @@ -805,11 +805,11 @@ extern "C" { # define MOVE_BACK_REF KIT_MOVE_BACK_REF #endif #endif -/********************************************************************* - * * - * File: source/kit/bigint.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/bigint.h +// +// ================================================================ #ifndef KIT_BIGINT_H #define KIT_BIGINT_H #include @@ -1286,11 +1286,11 @@ static kit_bigint_t kit_bi_from_base58(kit_str_t base58) { # define BASE58 KIT_BASE58 #endif #endif -/********************************************************************* - * * - * File: source/kit/input_stream.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/input_stream.h +// +// ================================================================ #ifndef KIT_INPUT_STREAM_H #define KIT_INPUT_STREAM_H #include @@ -1324,11 +1324,11 @@ void kit_is_destroy(kit_is_handle_t in); # define IS_READ KIT_IS_READ #endif #endif -/********************************************************************* - * * - * File: source/kit/input_buffer.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/input_buffer.h +// +// ================================================================ #ifndef KIT_INPUT_BUFFER_H #define KIT_INPUT_BUFFER_H #ifdef __cplusplus @@ -1379,11 +1379,11 @@ void kit_ib_destroy(kit_ib_t buf); # define IB_SKIP KIT_IB_SKIP #endif #endif -/********************************************************************* - * * - * File: source/kit/async_function.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/async_function.h +// +// ================================================================ #ifndef KIT_ASYNC_FUNCTION_H #define KIT_ASYNC_FUNCTION_H #include @@ -1576,11 +1576,11 @@ static void kit_async_function_dispatch(void *promise) { # define AF_FINISHED_ALL KIT_AF_FINISHED_ALL #endif #endif -/********************************************************************* - * * - * File: source/kit/file.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/file.h +// +// ================================================================ #ifndef KIT_FILE_H #define KIT_FILE_H #include @@ -1681,11 +1681,11 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf); # define PATH_FOLDER KIT_PATH_FOLDER #endif #endif -/********************************************************************* - * * - * File: source/kit/mersenne_twister_64.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/mersenne_twister_64.h +// +// ================================================================ #ifndef KIT_MERSENNE_TWISTER_64_H #define KIT_MERSENNE_TWISTER_64_H #ifdef __cplusplus @@ -1714,11 +1714,11 @@ u64 kit_mt64_generate(kit_mt64_state_t *state); # define mt64_generate kit_mt64_generate #endif #endif -/********************************************************************* - * * - * File: source/kit/secure_random.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/secure_random.h +// +// ================================================================ #ifndef KIT_SECURE_RANDOM_H #define KIT_SECURE_RANDOM_H #ifdef __cplusplus @@ -1732,11 +1732,11 @@ void kit_secure_random(i64 size, void *data); # define secure_random kit_secure_random #endif #endif -/********************************************************************* - * * - * File: source/kit/sha256.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/sha256.h +// +// ================================================================ #ifndef KIT_SHA256_H #define KIT_SHA256_H #ifdef __cplusplus @@ -1756,11 +1756,11 @@ kit_sha256_hash_t kit_sha256(i64 size, u8 *data); # define sha256 kit_sha256 #endif #endif -/********************************************************************* - * * - * File: source/kit/sockets.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/sockets.h +// +// ================================================================ #ifndef KIT_SOCKETS_H #define KIT_SOCKETS_H #ifndef KIT_DISABLE_SYSTEM_SOCKETS @@ -1844,11 +1844,11 @@ static i32 kit_socket_set_nonblocking(socket_t s) { # endif #endif #endif -/********************************************************************* - * * - * File: source/kit/shared_memory.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/shared_memory.h +// +// ================================================================ #ifndef KIT_SHARED_MEMORY_H #define KIT_SHARED_MEMORY_H #if !defined(_WIN32) || defined(__CYGWIN__) @@ -1888,11 +1888,11 @@ kit_status_t kit_shared_memory_clean(kit_str_t name); # define SHARED_MEMORY_CREATE KIT_SHARED_MEMORY_CREATE #endif #endif -/********************************************************************* - * * - * File: source/kit/xml.h * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/xml.h +// +// ================================================================ #ifndef KIT_XML_H #define KIT_XML_H #ifdef __cplusplus @@ -1941,12 +1941,19 @@ void kit_xml_destroy(kit_xml_t *xml); # define xml_text_t kit_xml_text_t #endif #endif -#ifdef KIT_IMPLEMENTATION -/********************************************************************* - * * - * File: source/kit/allocator.c * - * * - *********************************************************************/ +#endif +// ================================================================ +// +// IMPLEMENTATION +// +// ================================================================ +#if defined(KIT_IMPLEMENTATION) && !defined(KIT_INL_H_IMPL) +#define KIT_INL_H_IMPL +// ================================================================ +// +// File: source/kit/allocator.c +// +// ================================================================ #include #include #ifndef KIT_DISABLE_SYSTEM_MALLOC @@ -2104,11 +2111,11 @@ kit_allocator_t kit_alloc_buffer(i64 size, void *buffer) { .data = buffer }; return alloc; } -/********************************************************************* - * * - * File: source/kit/atomic.win32.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/atomic.win32.c +// +// ================================================================ #ifdef _MSC_VER static_assert(sizeof(char) == 1, "Wrong char size"); static_assert(sizeof(short) == 2, "Wrong short size"); @@ -2291,11 +2298,11 @@ u64 kit_atomic_fetch_add_explicit_64(u64 volatile *var, u64 value, # endif } #endif -/********************************************************************* - * * - * File: source/kit/threads.posix.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/threads.posix.c +// +// ================================================================ #ifndef KIT_DISABLE_SYSTEM_THREADS # if !defined(_WIN32) || defined(__CYGWIN__) # include @@ -2527,11 +2534,11 @@ void thrd_yield(void) { } # endif #endif -/********************************************************************* - * * - * File: source/kit/threads.win32.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/threads.win32.c +// +// ================================================================ #ifndef KIT_DISABLE_SYSTEM_THREADS # if defined(_WIN32) && !defined(__CYGWIN__) # include @@ -2864,11 +2871,11 @@ void thrd_yield(void) { } # endif #endif -/********************************************************************* - * * - * File: source/kit/array_ref.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/array_ref.c +// +// ================================================================ #include i8 kit_ar_equal_bytes(i64 left_element_size, i64 left_size, void *left_data, i64 right_element_size, @@ -2906,11 +2913,11 @@ i8 kit_ar_compare(i64 left_element_size, i64 left_size, return 1; return 0; } -/********************************************************************* - * * - * File: source/kit/dynamic_array.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/dynamic_array.c +// +// ================================================================ #include void kit_da_init(kit_da_void_t *array, i64 element_size, i64 size, kit_allocator_t *alloc) { @@ -2977,16 +2984,16 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size, array->values = bytes; } } -/********************************************************************* - * * - * File: source/kit/string_builder.c * - * * - *********************************************************************/ -/********************************************************************* - * * - * File: source/kit/input_stream.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/string_builder.c +// +// ================================================================ +// ================================================================ +// +// File: source/kit/input_stream.c +// +// ================================================================ #include enum { KIT_INPUT_STREAM_STR, KIT_INPUT_STREAM_FILE }; typedef struct { @@ -3068,11 +3075,11 @@ void kit_is_destroy(kit_is_handle_t in) { if (basic != NULL) kit_alloc_dispatch(basic->alloc, KIT_DEALLOCATE, 0, 0, in.state); } -/********************************************************************* - * * - * File: source/kit/input_buffer.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/input_buffer.c +// +// ================================================================ #include #include typedef struct { @@ -3341,11 +3348,11 @@ void kit_ib_destroy(kit_ib_t buf) { kit_buf_release_(buf.internal); DA_DESTROY(buf.data); } -/********************************************************************* - * * - * File: source/kit/file.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/file.c +// +// ================================================================ #include #include #include @@ -3878,11 +3885,11 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf) { #endif return status; } -/********************************************************************* - * * - * File: source/kit/mersenne_twister_64.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/mersenne_twister_64.c +// +// ================================================================ #define MM 156 #define MATRIX_A 0xb5026f5aa96619e9ull #define UM 0xffffffff80000000ull @@ -3934,11 +3941,11 @@ u64 kit_mt64_generate(kit_mt64_state_t *state) { #undef MATRIX_A #undef UM #undef LM -/********************************************************************* - * * - * File: source/kit/secure_random.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/secure_random.c +// +// ================================================================ #include #include #include @@ -3973,11 +3980,11 @@ void kit_secure_random(i64 size, void *data) { abort(); #endif } -/********************************************************************* - * * - * File: source/kit/sha256.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/sha256.c +// +// ================================================================ #include #include #define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) @@ -4102,11 +4109,11 @@ kit_sha256_hash_t kit_sha256(i64 in_size, u8 *in_data) { #undef EP1 #undef SIG0 #undef SIG1 -/********************************************************************* - * * - * File: source/kit/shared_memory.posix.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/shared_memory.posix.c +// +// ================================================================ #if !defined(_WIN32) || defined(__CYGWIN__) # include # include @@ -4203,11 +4210,11 @@ kit_status_t kit_shared_memory_clean(kit_str_t name) { return KIT_OK; } #endif -/********************************************************************* - * * - * File: source/kit/shared_memory.win32.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/shared_memory.win32.c +// +// ================================================================ #if defined(_WIN32) && !defined(__CYGWIN__) # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN @@ -4277,11 +4284,11 @@ kit_status_t kit_shared_memory_clean(kit_str_t name) { return KIT_OK; } #endif -/********************************************************************* - * * - * File: source/kit/xml.c * - * * - *********************************************************************/ +// ================================================================ +// +// File: source/kit/xml.c +// +// ================================================================ #include typedef struct { ib_t last; @@ -4619,4 +4626,3 @@ void kit_xml_destroy(kit_xml_t *xml) { DA_DESTROY(xml->children); } #endif -#endif diff --git a/source/kit/kit_test.h b/source/kit/kit_test.h index 0f07b32..910bdf1 100644 --- a/source/kit/kit_test.h +++ b/source/kit/kit_test.h @@ -289,26 +289,37 @@ int kit_run_benchmarks(int argc, char **argv); # define run_benchmarks kit_run_benchmarks #endif -#ifdef KIT_TEST_IMPLEMENTATION +#ifdef __cplusplus +} +#endif -# ifndef KIT_TIME_H -# define KIT_TIME_H +#endif -# include +#if defined(KIT_TEST_IMPLEMENTATION) && !defined(KIT_TEST_H_IMPL) +#define KIT_TEST_H_IMPL -# ifndef TIME_UTC -# define TIME_UTC 1 -# endif +#ifdef __cplusplus +extern "C" { +#endif -# ifdef KIT_REQUIRE_TIMESPEC_GET -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN 1 -# endif -# include +#ifndef KIT_TIME_H +# define KIT_TIME_H -# define KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS \ - 116444736000000000ull -# define KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull +# include + +# ifndef TIME_UTC +# define TIME_UTC 1 +# endif + +# ifdef KIT_REQUIRE_TIMESPEC_GET +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN 1 +# endif +# include + +# define KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS \ + 116444736000000000ull +# define KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull static int timespec_get(struct timespec *ts, int base) { if (ts == NULL || base != TIME_UTC) @@ -328,15 +339,15 @@ static int timespec_get(struct timespec *ts, int base) { return base; } -# endif - # endif -# include -# include -# include -# include -# include +#endif + +#include +#include +#include +#include +#include enum { kit_white_, @@ -1095,8 +1106,6 @@ int kit_run_benchmarks(int argc, char **argv) { return status; } -#endif - #ifdef __cplusplus } #endif -- cgit v1.2.3