Add date to unix time, fix buggy array and arena prototype

This commit is contained in:
doylet 2024-02-25 22:37:14 +11:00
parent 8b2aea5b1d
commit 6aec2ca4cf
9 changed files with 121 additions and 45 deletions

View File

@ -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 Dqn_ArenaTempMem Dqn_Arena_TempMemBegin (Dqn_Arena *arena);
DQN_API void Dqn_Arena_TempMemEnd (Dqn_ArenaTempMem mem); 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_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_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_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(*src) * (count), 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 ///////////////////////////////////////////////////////////////////// // NOTE: [$CHUN] Dqn_ChunkPool /////////////////////////////////////////////////////////////////////
#define Dqn_ChunkPool_New(pool, T) (T *)Dqn_ChunkPool_Alloc(pool, sizeof(T)) #define Dqn_ChunkPool_New(pool, T) (T *)Dqn_ChunkPool_Alloc(pool, sizeof(T))

View File

@ -223,7 +223,7 @@ template <typename T> void Dqn_VArra
#if !defined(DQN_NO_SARRAY) #if !defined(DQN_NO_SARRAY)
template <typename T> Dqn_SArray<T> Dqn_SArray_Init (Dqn_Arena *arena, Dqn_usize size, Dqn_ZeroMem zero_mem); template <typename T> Dqn_SArray<T> Dqn_SArray_Init (Dqn_Arena *arena, Dqn_usize size, Dqn_ZeroMem zero_mem);
template <typename T> Dqn_SArray<T> Dqn_SArray_InitSlice (Dqn_Arena *arena, Dqn_Slice<T> slice, Dqn_usize size, Dqn_ZeroMem zero_mem); template <typename T> Dqn_SArray<T> Dqn_SArray_InitSlice (Dqn_Arena *arena, Dqn_Slice<T> slice, Dqn_usize size, Dqn_ZeroMem zero_mem);
template <typename T, size_t N> Dqn_SArray<T> Dqn_SArray_InitCArray (Dqn_Arena *arena, T const (&array)[N], Dqn_usize size); template <typename T, size_t N> Dqn_SArray<T> Dqn_SArray_InitCArray (Dqn_Arena *arena, T const (&array)[N], Dqn_usize size, Dqn_ZeroMem);
template <typename T> bool Dqn_SArray_IsValid (Dqn_SArray<T> const *array); template <typename T> bool Dqn_SArray_IsValid (Dqn_SArray<T> const *array);
template <typename T> Dqn_Slice<T> Dqn_SArray_Slice (Dqn_SArray<T> const *array); template <typename T> Dqn_Slice<T> Dqn_SArray_Slice (Dqn_SArray<T> const *array);
template <typename T> T * Dqn_SArray_AddArray (Dqn_SArray<T> *array, T const *items, Dqn_usize count); template <typename T> T * Dqn_SArray_AddArray (Dqn_SArray<T> *array, T const *items, Dqn_usize count);
@ -646,7 +646,7 @@ template <typename T> Dqn_SArray<T> Dqn_SArray_InitSlice(Dqn_Arena *arena, Dqn_S
template <typename T, size_t N> Dqn_SArray<T> Dqn_SArray_InitCArray(Dqn_Arena *arena, T const (&array)[N], Dqn_usize size, Dqn_ZeroMem zero_mem) template <typename T, size_t N> Dqn_SArray<T> Dqn_SArray_InitCArray(Dqn_Arena *arena, T const (&array)[N], Dqn_usize size, Dqn_ZeroMem zero_mem)
{ {
Dqn_SArray<T> result = Dqn_SArray_InitSlice(arena, Dqn_Slice_Init(array, N), size, zero_mem); Dqn_SArray<T> result = Dqn_SArray_InitSlice(arena, Dqn_Slice_Init(DQN_CAST(T *)array, N), size, zero_mem);
return result; return result;
} }

View File

@ -47,6 +47,24 @@ DQN_API Dqn_OSDateTimeStr8 Dqn_OS_DateLocalTimeStr8Now(char date_separator, char
return result; 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_API Dqn_Str8 Dqn_OS_EXEDir(Dqn_Arena *arena)
{ {
Dqn_Str8 result = {}; Dqn_Str8 result = {};
@ -60,6 +78,7 @@ DQN_API Dqn_Str8 Dqn_OS_EXEDir(Dqn_Arena *arena)
return result; return result;
} }
// NOTE: Counters //////////////////////////////////////////////////////////////////////////////////
DQN_API Dqn_f64 Dqn_OS_PerfCounterS(uint64_t begin, uint64_t end) DQN_API Dqn_f64 Dqn_OS_PerfCounterS(uint64_t begin, uint64_t end)
{ {
uint64_t frequency = Dqn_OS_PerfCounterFrequency(); 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 /////////////////////////////////////////////////////////////// // NOTE: Read the file from disk ///////////////////////////////////////////////////////////////
Dqn_OSFile file = Dqn_OS_FileOpen(path, Dqn_OSFileOpen_OpenIfExist, Dqn_OSFileAccess_Read, error); Dqn_OSFile file = Dqn_OS_FileOpen(path, Dqn_OSFileOpen_OpenIfExist, Dqn_OSFileAccess_Read, error);
Dqn_OS_FileRead(&file, result.data, result.size, error); bool read_failed = !Dqn_OS_FileRead(&file, result.data, result.size, error);
Dqn_OS_FileClose(&file); if (file.error || read_failed) {
if (error->stack->error) {
Dqn_Arena_TempMemEnd(temp_mem); Dqn_Arena_TempMemEnd(temp_mem);
result = {}; result = {};
} }
Dqn_OS_FileClose(&file);
return result; return result;
} }

View File

@ -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_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 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_DateUnixTime ();
DQN_API uint64_t Dqn_OS_DateToUnixTime (Dqn_OSDateTime date);
DQN_API bool Dqn_OS_DateIsValid (Dqn_OSDateTime date);
// NOTE: Other ///////////////////////////////////////////////////////////////////////////////////// // NOTE: Other /////////////////////////////////////////////////////////////////////////////////////
DQN_API bool Dqn_OS_SecureRNGBytes (void *buffer, uint32_t size); 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, ...); 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_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("\\")) #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_PathBuildWithSeparator(allocator, fs_path, Dqn_OSPathSeparatorString)
#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
// NOTE: [$EXEC] Dqn_OSExec //////////////////////////////////////////////////////////////////////// // NOTE: [$EXEC] Dqn_OSExec ////////////////////////////////////////////////////////////////////////
DQN_API void Dqn_OS_Exit (uint32_t exit_code); DQN_API void Dqn_OS_Exit (uint32_t exit_code);

View File

@ -117,6 +117,20 @@ DQN_API uint64_t Dqn_OS_DateUnixTime()
return result; 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) DQN_API bool Dqn_OS_SecureRNGBytes(void *buffer, uint32_t size)
{ {
#if defined(DQN_PLATFORM_EMSCRIPTEN) #if defined(DQN_PLATFORM_EMSCRIPTEN)

View File

@ -112,18 +112,41 @@ DQN_API Dqn_OSDateTime Dqn_OS_DateLocalTimeNow()
return result; 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() 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; FILETIME file_time;
GetSystemTimeAsFileTime(&file_time); GetSystemTimeAsFileTime(&file_time);
LARGE_INTEGER date_time; LARGE_INTEGER date_time;
date_time.u.LowPart = file_time.dwLowDateTime; date_time.u.LowPart = file_time.dwLowDateTime;
date_time.u.HighPart = file_time.dwHighDateTime; 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; return result;
} }

View File

@ -22,9 +22,9 @@ struct Dqn_WinError
// NOTE: Windows Str8 <-> Str16 /////////////////////////////////////////// // NOTE: Windows Str8 <-> Str16 ///////////////////////////////////////////
struct Dqn_Win_FolderIteratorW struct Dqn_Win_FolderIteratorW
{ {
void *handle; void *handle;
Dqn_Str16 file_name; Dqn_Str16 file_name;
wchar_t file_name_buf[512]; wchar_t file_name_buf[512];
}; };
struct Dqn_Win_FolderIterator struct Dqn_Win_FolderIterator

View File

@ -13,38 +13,54 @@
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
struct Dqn_TypeEnumField enum Dqn_TypeKind
{ {
uint16_t index; Dqn_TypeKind_Nil,
Dqn_Str8 name; Dqn_TypeKind_Basic,
Dqn_isize value; Dqn_TypeKind_Enum,
Dqn_TypeKind_Struct,
}; };
struct Dqn_TypeStructField struct Dqn_TypeField
{ {
uint16_t index; uint16_t index;
Dqn_Str8 name; 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; bool is_pointer;
Dqn_TypeStructField const *array_count; uint16_t array_size;
uint16_t array_static_count; Dqn_TypeField const * array_size_field;
};
enum Dqn_TypeInfoKind
{
Dqn_TypeInfoKind_Basic,
Dqn_TypeInfoKind_Enum,
Dqn_TypeInfoKind_Struct,
}; };
struct Dqn_TypeInfo struct Dqn_TypeInfo
{ {
Dqn_Str8 name; Dqn_Str8 name;
Dqn_TypeInfoKind kind; Dqn_TypeKind kind;
Dqn_TypeStructField const *struct_field; Dqn_TypeField const *fields;
uint16_t struct_field_count; uint16_t fields_count;
Dqn_TypeEnumField const *enum_field;
uint16_t enum_field_count;
Dqn_isize enum_min;
Dqn_isize enum_max;
}; };
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;
}

View File

@ -526,6 +526,13 @@
__declspec(dllimport) VOID __stdcall GetLocalTime(SYSTEMTIME *lpSystemTime); __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 /////////////////////////////////////////////////////////////////////// // NOTE: shared/windef.h ///////////////////////////////////////////////////////////////////////
typedef struct tagRECT { typedef struct tagRECT {
LONG left; LONG left;