From c2f3b9193d17cbcdf58fff65567ee010a56c2194 Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Sat, 30 May 2020 15:09:06 -0700 Subject: [PATCH] platform_file_handler functions now take strings rather than char arrays --- src/app/foldhaus_app.cpp | 6 ++--- src/app/foldhaus_assembly.cpp | 7 +++--- src/app/foldhaus_platform.h | 25 ++++++++----------- .../foldhaus_panel_animation_timeline.h | 4 +-- src/app/panels/foldhaus_panel_hierarchy.h | 6 ++--- src/app/win32_foldhaus_fileio.h | 16 ++++++------ src/gs_libs/gs_string.h | 12 ++++++--- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/app/foldhaus_app.cpp b/src/app/foldhaus_app.cpp index ee84a3c..43b913e 100644 --- a/src/app/foldhaus_app.cpp +++ b/src/app/foldhaus_app.cpp @@ -92,7 +92,7 @@ INITIALIZE_APPLICATION(InitializeApplication) // TODO(Peter): put in InitializeInterface? r32 FontSize = 14; { - platform_memory_result FontFile = ReadEntireFile(Context, "data/Anonymous Pro.ttf"); + platform_memory_result FontFile = ReadEntireFile(Context, MakeStringLiteral("data/Anonymous Pro.ttf")); if (!FontFile.Error) { bitmap_font* Font = PushStruct(&State->Permanent, bitmap_font); @@ -176,8 +176,8 @@ INITIALIZE_APPLICATION(InitializeApplication) State->Camera.LookAt = v3{0, 0, 0}; #if 1 - char Path[] = "data/blumen_lumen.fold"; - LoadAssembly(State, Context, Path); + string SculpturePath = MakeStringLiteral("data/blumen_lumen.fold"); + LoadAssembly(State, Context, SculpturePath); #endif State->PixelsToWorldScale = .01f; diff --git a/src/app/foldhaus_assembly.cpp b/src/app/foldhaus_assembly.cpp index c733c6b..7b870db 100644 --- a/src/app/foldhaus_assembly.cpp +++ b/src/app/foldhaus_assembly.cpp @@ -68,16 +68,15 @@ static v4 TempAssemblyOffsets[] = { v4{0, 0, 0, 0}, v4{250, 0, 75, 0}, v4{-250, s32 TempAssemblyOffsetsCount = 3; internal void -LoadAssembly (app_state* State, context Context, char* Path) +LoadAssembly (app_state* State, context Context, string Path) { platform_memory_result AssemblyFile = ReadEntireFile(Context, Path); if (AssemblyFile.Error == PlatformMemory_NoError) { assembly_definition AssemblyDefinition = ParseAssemblyFile(AssemblyFile.Base, AssemblyFile.Size, &State->Transient, State->GlobalLog); - string PathString = MakeStringLiteral(Path); - s32 IndexOfLastSlash = FastLastIndexOfCharInCharArray(PathString.Memory, PathString.Length, '\\'); - string FileName = Substring(PathString, IndexOfLastSlash + 1); + s32 IndexOfLastSlash = FastLastIndexOfCharInCharArray(Path.Memory, Path.Length, '\\'); + string FileName = Substring(Path, IndexOfLastSlash + 1); memory_arena AssemblyArena = {}; AssemblyArena.Alloc = (gs_memory_alloc*)Context.PlatformAlloc; diff --git a/src/app/foldhaus_platform.h b/src/app/foldhaus_platform.h index 180e27c..725284b 100644 --- a/src/app/foldhaus_platform.h +++ b/src/app/foldhaus_platform.h @@ -97,13 +97,13 @@ typedef PLATFORM_FREE(platform_free); #define PLATFORM_REALLOC(name) u8* name(u8* Base, u32 OldSize, u32 NewSize) typedef PLATFORM_REALLOC(platform_realloc); -#define PLATFORM_READ_ENTIRE_FILE(name) platform_memory_result name(char* Path) +#define PLATFORM_READ_ENTIRE_FILE(name) platform_memory_result name(string Path) typedef PLATFORM_READ_ENTIRE_FILE(platform_read_entire_file); -#define PLATFORM_WRITE_ENTIRE_FILE(name) b32 name(char* Path, u8* Contents, s32 Size) +#define PLATFORM_WRITE_ENTIRE_FILE(name) b32 name(string Path, u8* Contents, s32 Size) typedef PLATFORM_WRITE_ENTIRE_FILE(platform_write_entire_file); -#define PLATFORM_GET_FILE_PATH(name) b32 name(char* PathBuffer, s32 BufferLength, const char* FilterStrings) +#define PLATFORM_GET_FILE_PATH(name) b32 name(string* PathBuffer, const char* FilterStrings) typedef PLATFORM_GET_FILE_PATH(platform_get_file_path); struct platform_file_handler @@ -283,43 +283,40 @@ struct context // File Handler internal platform_memory_result -ReadEntireFile(platform_file_handler FileHandler, char* Path) +ReadEntireFile(platform_file_handler FileHandler, string Path) { - // TODO(Peter): Convert Path to be a string platform_memory_result Result = FileHandler.ReadEntireFile(Path); return Result; } internal platform_memory_result -ReadEntireFile(context Context, char* Path) +ReadEntireFile(context Context, string Path) { return ReadEntireFile(Context.FileHandler, Path); } internal b32 -WriteEntireFile(platform_file_handler FileHandler, char* Path, u8* Contents, u32 Size) +WriteEntireFile(platform_file_handler FileHandler, string Path, u8* Contents, u32 Size) { - // TODO(Peter): Convert Path to be a string // TODO(Peter): Overload to take a data struct instead of Contents And Size b32 Result = FileHandler.WriteEntireFile(Path, Contents, Size); return Result; } internal b32 -WriteEntireFile(context Context, char* Path, u8* Contents, u32 Size) +WriteEntireFile(context Context, string Path, u8* Contents, u32 Size) { return WriteEntireFile(Context.FileHandler, Path, Contents, Size); } internal b32 -GetFilePath(platform_file_handler FileHandler, char* PathBuffer, s32 BufferLength, char* FilterStrings) +GetFilePath(platform_file_handler FileHandler, string* PathBuffer, char* FilterStrings) { - // TODO(Peter): Convert Path to be a string - b32 Result = FileHandler.GetFilePath(PathBuffer, BufferLength, (const char*)FilterStrings); + b32 Result = FileHandler.GetFilePath(PathBuffer, (const char*)FilterStrings); return Result; } internal b32 -GetFilePath(context Context, char* PathBuffer, s32 BufferLength, char* FilterStrings) +GetFilePath(context Context, string* PathBuffer, char* FilterStrings) { - return GetFilePath(Context.FileHandler, PathBuffer, BufferLength, FilterStrings); + return GetFilePath(Context.FileHandler, PathBuffer, FilterStrings); } diff --git a/src/app/panels/foldhaus_panel_animation_timeline.h b/src/app/panels/foldhaus_panel_animation_timeline.h index 36b23fc..b4a2dfe 100644 --- a/src/app/panels/foldhaus_panel_animation_timeline.h +++ b/src/app/panels/foldhaus_panel_animation_timeline.h @@ -291,7 +291,7 @@ AnimationTimeline_Cleanup(panel* Panel, app_state* State) internal void DrawFrameBar (animation_system* AnimationSystem, render_command_buffer* RenderBuffer, frame_range VisibleFrames, rect BarBounds, mouse_state Mouse, app_state* State) { - MakeStringBuffer(TempString, 256); + string TempString = PushString(&State->Transient, 256); s32 VisibleFrameCount = VisibleFrames.Max - VisibleFrames.Min; @@ -450,7 +450,7 @@ DrawAnimationBlock (animation_block AnimationBlock, v4 BlockColor, frame_range V internal gs_list_handle DrawAnimationTimeline (animation_system* AnimationSystem, animation_timeline_state* TimelineState, rect PanelBounds, gs_list_handle SelectedBlockHandle, ui_interface* Interface, app_state* State) { - string TempString = MakeString(PushArray(&State->Transient, char, 256), 256); + string TempString = PushString(&State->Transient, 256); gs_list_handle Result = SelectedBlockHandle; rect LayerMenuBounds, TimelineBounds; diff --git a/src/app/panels/foldhaus_panel_hierarchy.h b/src/app/panels/foldhaus_panel_hierarchy.h index 2452f22..7f27c94 100644 --- a/src/app/panels/foldhaus_panel_hierarchy.h +++ b/src/app/panels/foldhaus_panel_hierarchy.h @@ -33,7 +33,7 @@ HierarchyView_Render(panel Panel, rect PanelBounds, render_command_buffer* Rende v4 ListItemHover = State->Interface_.Style.ListBGHover; v4 ListItemSelected = State->Interface_.Style.ListBGSelected; - string TempString = MakeString(PushArray(&State->Transient, char, 256), 0, 256); + string TempString = PushString(&State->Transient, 256); u32 LineCount = (u32)(gs_Height(PanelBounds) / Layout.RowHeight) + 1; u32 LinesDrawn = 0; @@ -65,8 +65,8 @@ HierarchyView_Render(panel Panel, rect PanelBounds, render_command_buffer* Rende PrintF(&TempString, "+ Add Assembly"); if (ui_LayoutButton(&State->Interface_, &Layout, TempString, ListItemBGColor, ListItemHover, ListItemSelected)) { - char FilePath[256]; - b32 Success = GetFilePath(Context, FilePath, 256, "Foldhaus Files\0*.fold\0\0"); + string FilePath = PushString(&State->Transient, 256); + b32 Success = GetFilePath(Context, &FilePath, "Foldhaus Files\0*.fold\0\0"); if (Success) { LoadAssembly(State, Context, FilePath); diff --git a/src/app/win32_foldhaus_fileio.h b/src/app/win32_foldhaus_fileio.h index 7104c5e..3a9a9aa 100644 --- a/src/app/win32_foldhaus_fileio.h +++ b/src/app/win32_foldhaus_fileio.h @@ -13,13 +13,14 @@ PLATFORM_READ_ENTIRE_FILE(Win32ReadEntireFile) platform_memory_result Result = {}; Result.Error = PlatformMemory_NoError; - HANDLE FileHandle = CreateFileA (Path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + Assert(IsNullTerminated(Path)); + HANDLE FileHandle = CreateFileA (Path.Memory, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (FileHandle != INVALID_HANDLE_VALUE) { DWORD FileSize = GetFileSize(FileHandle, NULL); Result.Base = (u8*)VirtualAlloc(NULL, FileSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); - if (Result.Base) + if (Result.Base) { Result.Size = FileSize; @@ -49,9 +50,10 @@ PLATFORM_READ_ENTIRE_FILE(Win32ReadEntireFile) PLATFORM_WRITE_ENTIRE_FILE(Win32WriteEntireFile) { + Assert(IsNullTerminated(Path)); b32 Result = false; HANDLE FileHandle = CreateFileA ( - Path, + Path.Memory, GENERIC_WRITE, 0, NULL, @@ -111,7 +113,7 @@ PLATFORM_GET_FILE_PATH(Win32SystemDialogueOpenFile) { b32 Result = false; - PathBuffer[0] = 0; + PathBuffer->Memory[0] = 0; OPENFILENAMEA OpenFileName = {}; OpenFileName.lStructSize = sizeof(OpenFileName); @@ -120,8 +122,8 @@ PLATFORM_GET_FILE_PATH(Win32SystemDialogueOpenFile) OpenFileName.lpstrCustomFilter = NULL; // NOTE(Peter): for preserving last filter string chosen OpenFileName.nMaxCustFilter = 0; // NOTE(Peter): ignored since we left CustomFilter null OpenFileName.nFilterIndex = 1; - OpenFileName.lpstrFile = PathBuffer; - OpenFileName.nMaxFile = BufferLength; + OpenFileName.lpstrFile = PathBuffer->Memory; + OpenFileName.nMaxFile = PathBuffer->Max; OpenFileName.lpstrFileTitle = NULL; OpenFileName.nMaxFileTitle = 0; // NOTE(Peter): Ignored since fileTitle is null OpenFileName.lpstrInitialDir = NULL; @@ -138,7 +140,7 @@ internal directory_listing EnumerateDirectory(char* Path, memory_arena* Storage) { directory_listing Result = {}; - // TODO(Peter): + // TODO(Peter): return Result; } diff --git a/src/gs_libs/gs_string.h b/src/gs_libs/gs_string.h index 1f335a4..edd80c7 100644 --- a/src/gs_libs/gs_string.h +++ b/src/gs_libs/gs_string.h @@ -14,6 +14,7 @@ struct string { char* Memory; s32 Length; + // TODO(Peter): Max -> LengthMax for clarity s32 Max; }; @@ -159,10 +160,7 @@ static float GSPowF (float N, s32 Power); #endif // Setup - -#ifdef GS_MEMORY_H -#define PushString(str, arena, size) (str)->Memory = PushArray(arena, char, size); (str)->Length = 0; (str)->Max = size; -#endif +#define PushString(arena, size) MakeString(PushArray(arena, char, size), 0, size); static void InitializeEmptyString (string* String, char* Data, s32 DataSize); static void InitializeString(string* String, char* Data, s32 Used, s32 Max); @@ -421,6 +419,12 @@ ClearString (string* String) // Char Value Types //////////////////////////////////////////////////////////////// +static bool IsNullTerminated(string Str) +{ + char LastChar = Str.Memory[Str.Length]; + bool Result = (LastChar == 0); + return Result; +} static bool IsSlash (char C) { return ((C == '\\') || (C == '/')); } static bool IsNewline (char C) { return (C == '\n') || (C == '\r'); } static bool IsWhitespace (char C) { return (C == ' ') || (C == '\t'); }