From 6aec2ca4cf0337c356e90ae8522ae18ff8f0c762 Mon Sep 17 00:00:00 2001 From: doylet Date: Sun, 25 Feb 2024 22:37:14 +1100 Subject: [PATCH] Add date to unix time, fix buggy array and arena prototype --- dqn_allocator.h | 6 ++--- dqn_containers.h | 4 ++-- dqn_os.cpp | 28 ++++++++++++++++++---- dqn_os.h | 8 +++---- dqn_os_posix.cpp | 14 +++++++++++ dqn_os_win32.cpp | 31 ++++++++++++++++++++---- dqn_os_win32.h | 6 ++--- dqn_type_info.h | 62 ++++++++++++++++++++++++++++++------------------ dqn_win32.h | 7 ++++++ 9 files changed, 121 insertions(+), 45 deletions(-) diff --git a/dqn_allocator.h b/dqn_allocator.h index 13c5988..5c30951 100644 --- a/dqn_allocator.h +++ b/dqn_allocator.h @@ -150,9 +150,9 @@ DQN_API void Dqn_Arena_Clear (Dqn_Arena *arena); DQN_API Dqn_ArenaTempMem Dqn_Arena_TempMemBegin (Dqn_Arena *arena); DQN_API void Dqn_Arena_TempMemEnd (Dqn_ArenaTempMem mem); #define Dqn_Arena_New(arena, T, zero_mem) (T *)Dqn_Arena_Alloc(arena, sizeof(T), alignof(T), zero_mem) -#define Dqn_Arena_NewArray(arena, T, count, zero_mem) (T *)Dqn_Arena_Alloc(arena, sizeof(T) * (count), alignof(T), zero_mem) -#define Dqn_Arena_NewCopy(arena, T, src) (T *)Dqn_Arena_Copy (arena, (src), sizeof(*src), alignof(T)) -#define Dqn_Arena_NewArrayCopy(arena, T, src, count) (T *)Dqn_Arena_Copy (arena, (src), sizeof(*src) * (count), alignof(T)) +#define Dqn_Arena_NewArray(arena, T, count, zero_mem) (T *)Dqn_Arena_Alloc(arena, sizeof(T) * (count), alignof(T), zero_mem) +#define Dqn_Arena_NewCopy(arena, T, src) (T *)Dqn_Arena_Copy (arena, (src), sizeof(T), alignof(T)) +#define Dqn_Arena_NewArrayCopy(arena, T, src, count) (T *)Dqn_Arena_Copy (arena, (src), sizeof(T) * (count), alignof(T)) // NOTE: [$CHUN] Dqn_ChunkPool ///////////////////////////////////////////////////////////////////// #define Dqn_ChunkPool_New(pool, T) (T *)Dqn_ChunkPool_Alloc(pool, sizeof(T)) diff --git a/dqn_containers.h b/dqn_containers.h index 7745c59..bd45f16 100644 --- a/dqn_containers.h +++ b/dqn_containers.h @@ -223,7 +223,7 @@ template void Dqn_VArra #if !defined(DQN_NO_SARRAY) template Dqn_SArray Dqn_SArray_Init (Dqn_Arena *arena, Dqn_usize size, Dqn_ZeroMem zero_mem); template Dqn_SArray Dqn_SArray_InitSlice (Dqn_Arena *arena, Dqn_Slice slice, Dqn_usize size, Dqn_ZeroMem zero_mem); -template Dqn_SArray Dqn_SArray_InitCArray (Dqn_Arena *arena, T const (&array)[N], Dqn_usize size); +template Dqn_SArray Dqn_SArray_InitCArray (Dqn_Arena *arena, T const (&array)[N], Dqn_usize size, Dqn_ZeroMem); template bool Dqn_SArray_IsValid (Dqn_SArray const *array); template Dqn_Slice Dqn_SArray_Slice (Dqn_SArray const *array); template T * Dqn_SArray_AddArray (Dqn_SArray *array, T const *items, Dqn_usize count); @@ -646,7 +646,7 @@ template Dqn_SArray Dqn_SArray_InitSlice(Dqn_Arena *arena, Dqn_S template Dqn_SArray Dqn_SArray_InitCArray(Dqn_Arena *arena, T const (&array)[N], Dqn_usize size, Dqn_ZeroMem zero_mem) { - Dqn_SArray result = Dqn_SArray_InitSlice(arena, Dqn_Slice_Init(array, N), size, zero_mem); + Dqn_SArray result = Dqn_SArray_InitSlice(arena, Dqn_Slice_Init(DQN_CAST(T *)array, N), size, zero_mem); return result; } diff --git a/dqn_os.cpp b/dqn_os.cpp index 59ac5a3..c866549 100644 --- a/dqn_os.cpp +++ b/dqn_os.cpp @@ -47,6 +47,24 @@ DQN_API Dqn_OSDateTimeStr8 Dqn_OS_DateLocalTimeStr8Now(char date_separator, char return result; } +DQN_API bool Dqn_OS_DateIsValid(Dqn_OSDateTime date) +{ + if (date.year < 1970) + return false; + if (date.month <= 0 || date.month >= 13) + return false; + if (date.day <= 0 || date.day >= 32) + return false; + if (date.hour >= 24) + return false; + if (date.minutes >= 60) + return false; + if (date.seconds >= 60) + return false; + return true; +} + +// NOTE: Other ///////////////////////////////////////////////////////////////////////////////////// DQN_API Dqn_Str8 Dqn_OS_EXEDir(Dqn_Arena *arena) { Dqn_Str8 result = {}; @@ -60,6 +78,7 @@ DQN_API Dqn_Str8 Dqn_OS_EXEDir(Dqn_Arena *arena) return result; } +// NOTE: Counters ////////////////////////////////////////////////////////////////////////////////// DQN_API Dqn_f64 Dqn_OS_PerfCounterS(uint64_t begin, uint64_t end) { uint64_t frequency = Dqn_OS_PerfCounterFrequency(); @@ -200,14 +219,13 @@ DQN_API Dqn_Str8 Dqn_OS_ReadAll(Dqn_Str8 path, Dqn_Arena *arena, Dqn_ErrorSink * } // NOTE: Read the file from disk /////////////////////////////////////////////////////////////// - Dqn_OSFile file = Dqn_OS_FileOpen(path, Dqn_OSFileOpen_OpenIfExist, Dqn_OSFileAccess_Read, error); - Dqn_OS_FileRead(&file, result.data, result.size, error); - Dqn_OS_FileClose(&file); - - if (error->stack->error) { + Dqn_OSFile file = Dqn_OS_FileOpen(path, Dqn_OSFileOpen_OpenIfExist, Dqn_OSFileAccess_Read, error); + bool read_failed = !Dqn_OS_FileRead(&file, result.data, result.size, error); + if (file.error || read_failed) { Dqn_Arena_TempMemEnd(temp_mem); result = {}; } + Dqn_OS_FileClose(&file); return result; } diff --git a/dqn_os.h b/dqn_os.h index 8f4d311..69acd99 100644 --- a/dqn_os.h +++ b/dqn_os.h @@ -277,6 +277,8 @@ DQN_API Dqn_OSDateTime Dqn_OS_DateLocalTimeNow (); DQN_API Dqn_OSDateTimeStr8 Dqn_OS_DateLocalTimeStr8Now(char date_separator = '-', char hms_separator = ':'); DQN_API Dqn_OSDateTimeStr8 Dqn_OS_DateLocalTimeStr8 (Dqn_OSDateTime time, char date_separator = '-', char hms_separator = ':'); DQN_API uint64_t Dqn_OS_DateUnixTime (); +DQN_API uint64_t Dqn_OS_DateToUnixTime (Dqn_OSDateTime date); +DQN_API bool Dqn_OS_DateIsValid (Dqn_OSDateTime date); // NOTE: Other ///////////////////////////////////////////////////////////////////////////////////// DQN_API bool Dqn_OS_SecureRNGBytes (void *buffer, uint32_t size); @@ -340,11 +342,7 @@ DQN_API Dqn_Str8 Dqn_OS_PathConvert (Dqn_Arena *arena DQN_API Dqn_Str8 Dqn_OS_PathConvertF (Dqn_Arena *arena, DQN_FMT_ATTRIB char const *fmt, ...); #define Dqn_OS_PathBuildFwdSlash(allocator, fs_path) Dqn_OS_PathBuildWithSeparator(allocator, fs_path, DQN_STR8("/")) #define Dqn_OS_PathBuildBackSlash(allocator, fs_path) Dqn_OS_PathBuildWithSeparator(allocator, fs_path, DQN_STR8("\\")) -#if defined(DQN_OS_WIN32) - #define Dqn_OS_PathBuild(allocator, fs_path) Dqn_OS_PathBuildBackSlash(allocator, fs_path) -#else - #define Dqn_OS_PathBuild(allocator, fs_path) Dqn_OS_PathBuildFwdSlash(allocator, fs_path) -#endif +#define Dqn_OS_PathBuild(allocator, fs_path) Dqn_OS_PathBuildWithSeparator(allocator, fs_path, Dqn_OSPathSeparatorString) // NOTE: [$EXEC] Dqn_OSExec //////////////////////////////////////////////////////////////////////// DQN_API void Dqn_OS_Exit (uint32_t exit_code); diff --git a/dqn_os_posix.cpp b/dqn_os_posix.cpp index d0e64ba..112e7c7 100644 --- a/dqn_os_posix.cpp +++ b/dqn_os_posix.cpp @@ -117,6 +117,20 @@ DQN_API uint64_t Dqn_OS_DateUnixTime() return result; } +DQN_API uint64_t Dqn_OS_DateToUnixTime(Dqn_OSDateTime date) +{ + DQN_ASSERT(Dqn_OS_DateIsValid(date)); + struct tm timeinfo = {}; + timeinfo.tm_year = date.year - 1900; + timeinfo.tm_mon = date.month - 1; + timeinfo.tm_mday = date.day; + timeinfo.tm_hour = date.hour; + timeinfo.tm_min = date.minutes; + timeinfo.tm_sec = date.seconds; + uint64_t result = mktime(&timeinfo); + return result; +} + DQN_API bool Dqn_OS_SecureRNGBytes(void *buffer, uint32_t size) { #if defined(DQN_PLATFORM_EMSCRIPTEN) diff --git a/dqn_os_win32.cpp b/dqn_os_win32.cpp index 1423a21..d741142 100644 --- a/dqn_os_win32.cpp +++ b/dqn_os_win32.cpp @@ -112,18 +112,41 @@ DQN_API Dqn_OSDateTime Dqn_OS_DateLocalTimeNow() return result; } +const uint64_t DQN_OS_WIN32_UNIX_TIME_START = 0x019DB1DED53E8000; // January 1, 1970 (start of Unix epoch) in "ticks" +const uint64_t DQN_OS_WIN32_FILE_TIME_TICKS_PER_SECOND = 10'000'000; // Filetime returned is in intervals of 100 nanoseconds + DQN_API uint64_t Dqn_OS_DateUnixTime() { - const uint64_t UNIX_TIME_START = 0x019DB1DED53E8000; // January 1, 1970 (start of Unix epoch) in "ticks" - const uint64_t TICKS_PER_SECOND = 10'000'000; // Filetime returned is in intervals of 100 nanoseconds - FILETIME file_time; GetSystemTimeAsFileTime(&file_time); LARGE_INTEGER date_time; date_time.u.LowPart = file_time.dwLowDateTime; date_time.u.HighPart = file_time.dwHighDateTime; - uint64_t result = (date_time.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND; + uint64_t result = (date_time.QuadPart - DQN_OS_WIN32_UNIX_TIME_START) / DQN_OS_WIN32_FILE_TIME_TICKS_PER_SECOND; + return result; +} + +DQN_API uint64_t Dqn_OS_DateToUnixTime(Dqn_OSDateTime date) +{ + DQN_ASSERT(Dqn_OS_DateIsValid(date)); + + SYSTEMTIME sys_time = {}; + sys_time.wYear = date.year; + sys_time.wMonth = date.month; + sys_time.wDay = date.day; + sys_time.wHour = date.hour; + sys_time.wMinute = date.minutes; + sys_time.wSecond = date.seconds; + + FILETIME file_time = {}; + SystemTimeToFileTime(&sys_time, &file_time); + + LARGE_INTEGER date_time; + date_time.u.LowPart = file_time.dwLowDateTime; + date_time.u.HighPart = file_time.dwHighDateTime; + + uint64_t result = (date_time.QuadPart - DQN_OS_WIN32_UNIX_TIME_START) / DQN_OS_WIN32_FILE_TIME_TICKS_PER_SECOND; return result; } diff --git a/dqn_os_win32.h b/dqn_os_win32.h index 8748c8f..7a0ef98 100644 --- a/dqn_os_win32.h +++ b/dqn_os_win32.h @@ -22,9 +22,9 @@ struct Dqn_WinError // NOTE: Windows Str8 <-> Str16 /////////////////////////////////////////// struct Dqn_Win_FolderIteratorW { - void *handle; - Dqn_Str16 file_name; - wchar_t file_name_buf[512]; + void *handle; + Dqn_Str16 file_name; + wchar_t file_name_buf[512]; }; struct Dqn_Win_FolderIterator diff --git a/dqn_type_info.h b/dqn_type_info.h index a3d09f0..e2f4b4a 100644 --- a/dqn_type_info.h +++ b/dqn_type_info.h @@ -13,38 +13,54 @@ // //////////////////////////////////////////////////////////////////////////////////////////////////// -struct Dqn_TypeEnumField +enum Dqn_TypeKind { - uint16_t index; - Dqn_Str8 name; - Dqn_isize value; + Dqn_TypeKind_Nil, + Dqn_TypeKind_Basic, + Dqn_TypeKind_Enum, + Dqn_TypeKind_Struct, }; -struct Dqn_TypeStructField +struct Dqn_TypeField { uint16_t index; Dqn_Str8 name; - struct Dqn_TypeInfo const *type; + Dqn_isize value; + Dqn_usize offset_of; + Dqn_usize size_of; + Dqn_Str8 type_decl; + uint32_t type_enum; bool is_pointer; - Dqn_TypeStructField const *array_count; - uint16_t array_static_count; -}; - -enum Dqn_TypeInfoKind -{ - Dqn_TypeInfoKind_Basic, - Dqn_TypeInfoKind_Enum, - Dqn_TypeInfoKind_Struct, + uint16_t array_size; + Dqn_TypeField const * array_size_field; }; struct Dqn_TypeInfo { - Dqn_Str8 name; - Dqn_TypeInfoKind kind; - Dqn_TypeStructField const *struct_field; - uint16_t struct_field_count; - Dqn_TypeEnumField const *enum_field; - uint16_t enum_field_count; - Dqn_isize enum_min; - Dqn_isize enum_max; + Dqn_Str8 name; + Dqn_TypeKind kind; + Dqn_TypeField const *fields; + uint16_t fields_count; }; + +struct Dqn_TypeGetField +{ + bool success; + Dqn_usize index; + Dqn_TypeField *field; +}; + +Dqn_TypeGetField Dqn_Type_GetField(Dqn_TypeInfo const *type_info, Dqn_Str8 name) +{ + Dqn_TypeGetField result = {}; + for (Dqn_usize index = 0; index < type_info->fields_count; index++) { + Dqn_TypeField const *type_field = type_info->fields + index; + if (type_field->name == name) { + result.success = true; + result.index = index; + result.field = DQN_CAST(Dqn_TypeField *)type_field; + break; + } + } + return result; +} diff --git a/dqn_win32.h b/dqn_win32.h index 63b2622..ad94ad8 100644 --- a/dqn_win32.h +++ b/dqn_win32.h @@ -526,6 +526,13 @@ __declspec(dllimport) VOID __stdcall GetLocalTime(SYSTEMTIME *lpSystemTime); } + // NOTE: um/timezoneapi.h ////////////////////////////////////////////////////////////////////// + extern "C" + { + __declspec(dllimport) BOOL __stdcall FileTimeToSystemTime(const FILETIME* lpFileTime, SYSTEMTIME *lpSystemTime); + __declspec(dllimport) BOOL __stdcall SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, FILETIME *lpFileTime); + } + // NOTE: shared/windef.h /////////////////////////////////////////////////////////////////////// typedef struct tagRECT { LONG left;