diff --git a/macros.hpp b/macros.hpp index 94f8ac7..37e2760 100644 --- a/macros.hpp +++ b/macros.hpp @@ -96,6 +96,7 @@ expands to: goto TOKENPASTE2(finished, __LINE__); \ } \ else TOKENPASTE2(body,__LINE__): + ; /** diff --git a/platform.hpp b/platform.hpp new file mode 100644 index 0000000..7e86b9f --- /dev/null +++ b/platform.hpp @@ -0,0 +1,9 @@ +#pragma once + +#if defined(_WIN32) || defined(_WIN64) +// #pragma message ("Compiling for Windows") +#define FTB_WINDOWS +#else +// #pragma message ("Compiling for Linux") +#define FTB_LINUX +#endif diff --git a/print.hpp b/print.hpp index f1cc96c..74939a1 100644 --- a/print.hpp +++ b/print.hpp @@ -8,6 +8,7 @@ #include #include "hashmap.hpp" +#include "hooks.hpp" FILE* ftb_stdout = stdout; @@ -443,27 +444,28 @@ auto print_str_line(FILE* f, char* str) -> s32 { return print_to_file(f, "%.*s", length, str); } -void deinit_printer() { - printer_map.dealloc(); - type_map.dealloc(); -} - void init_printer() { color_stack.alloc(); printer_map.alloc(); type_map.alloc(); - register_printer("u32", print_u32, Printer_Function_Type::_32b); - register_printer("u64", print_u64, Printer_Function_Type::_64b); - register_printer("bool", print_bool, Printer_Function_Type::_32b); - register_printer("s64", print_s64, Printer_Function_Type::_64b); - register_printer("s32", print_s32, Printer_Function_Type::_32b); - register_printer("f32", print_flt, Printer_Function_Type::_flt); - register_printer("f64", print_flt, Printer_Function_Type::_flt); - register_printer("->char", print_str, Printer_Function_Type::_ptr); - register_printer("->", print_ptr, Printer_Function_Type::_ptr); - register_printer("color<", print_color_start, Printer_Function_Type::_ptr); - register_printer(">color", print_color_end, Printer_Function_Type::_void); - register_printer("->Str", print_Str, Printer_Function_Type::_ptr); - register_printer("->char_line", print_str_line, Printer_Function_Type::_ptr); + system_shutdown_hook << [](){ + color_stack.dealloc(); + printer_map.dealloc(); + type_map.dealloc(); + }; + + register_printer("u32", print_u32, Printer_Function_Type::_32b); + register_printer("u64", print_u64, Printer_Function_Type::_64b); + register_printer("bool", print_bool, Printer_Function_Type::_32b); + register_printer("s64", print_s64, Printer_Function_Type::_64b); + register_printer("s32", print_s32, Printer_Function_Type::_32b); + register_printer("f32", print_flt, Printer_Function_Type::_flt); + register_printer("f64", print_flt, Printer_Function_Type::_flt); + register_printer("->char", print_str, Printer_Function_Type::_ptr); + register_printer("->", print_ptr, Printer_Function_Type::_ptr); + register_printer("color<", print_color_start, Printer_Function_Type::_ptr); + register_printer(">color", print_color_end, Printer_Function_Type::_void); + register_printer("->Str", print_Str, Printer_Function_Type::_ptr); + register_printer("->char_line", print_str_line, Printer_Function_Type::_ptr); } diff --git a/testing.hpp b/testing.hpp index fa01097..06f3572 100644 --- a/testing.hpp +++ b/testing.hpp @@ -1,3 +1,5 @@ +#include "./types.hpp" + typedef s32 testresult; #define epsilon 2.2204460492503131E-16 @@ -84,8 +86,8 @@ typedef s32 testresult; for(s32 i = -1; i < 70; ++i) \ fputs((i%3==1)? "." : " ", stdout); \ fputs(console_red "failed\n" console_normal, stdout); \ - if(error) { \ - free(error); \ - error = nullptr; \ + if(error) { \ + free(error); \ + error = nullptr; \ } \ - } \ + } diff --git a/types.hpp b/types.hpp index 83e69a7..537b35a 100644 --- a/types.hpp +++ b/types.hpp @@ -1,5 +1,6 @@ #pragma once +#include "platform.hpp" #include #include @@ -22,24 +23,33 @@ typedef wchar_t path_char; typedef char path_char; #endif -struct String { +struct StringSlice { + const char* data; u64 length; - char* data; }; -struct StringSlice { +struct String { + char* data; u64 length; - const char* data; }; + +inline auto heap_copy_c_string(const char* str) -> char* { +#ifdef FTB_WINDOWS + return _strdup(str); +#else + return strdup(str); +#endif +} + inline auto make_heap_string(const char* str) -> String { String ret; ret.length = strlen(str); - ret.data = _strdup(str); + ret.data = heap_copy_c_string(str); return ret; } -inline const StringSlice make_static_string(const char* str) { +inline auto make_static_string(const char* str) -> const StringSlice { StringSlice ret; ret.length = strlen(str); ret.data = str; @@ -50,26 +60,21 @@ auto inline string_equal(const char* input, const char* check) -> bool { return strcmp(input, check) == 0; } -template -auto inline string_equal(Str str, const char* check) -> bool { +auto inline string_equal(StringSlice str, const char* check) -> bool { if (str.length != strlen(check)) return false; return strncmp(str.data, check, str.length) == 0; } -template -auto inline string_equal(const char* check, Str str) -> bool { +auto inline string_equal(const char* check, StringSlice str) -> bool { if (str.length != strlen(check)) return false; return strncmp(str.data, check, str.length) == 0; } -template -auto inline string_equal(StrA str1, StrB str2) -> bool { +auto inline string_equal(StringSlice str1, StringSlice str2) -> bool { if (str1.length != str2.length) return false; return strncmp(str1.data, str2.data, str2.length) == 0; } - -template auto string_equal(StringSlice str1, StringSlice str2) -> bool;