From a90c33f09b6287d6f1d8c54a969ad4952bc83430 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 27 May 2016 20:46:22 -0400 Subject: [PATCH] make old file load/save shared --- 4ed_rendering.cpp | 4 +-- system_shared.cpp | 33 +++++++++++++++++++ system_shared.h | 4 --- win32_4ed.cpp | 84 ++++++++++++++--------------------------------- 4 files changed, 60 insertions(+), 65 deletions(-) diff --git a/4ed_rendering.cpp b/4ed_rendering.cpp index 19f9a0a7..bb70c897 100644 --- a/4ed_rendering.cpp +++ b/4ed_rendering.cpp @@ -309,7 +309,7 @@ draw_font_info_load(Partition *partition, i32 result = 1; File_Data file; - file = system_load_file(filename.str); + file = sysshared_load_file(filename.str); Temp_Memory temp = begin_temp_memory(partition); stbtt_packedchar *chardata = push_array(partition, stbtt_packedchar, 256); @@ -401,7 +401,7 @@ draw_font_load(void *base_block, i32 size, i32 result = 1; File_Data file; - file = system_load_file(filename.str); + file = sysshared_load_file(filename.str); Partition partition_ = make_part(base_block, size); Partition *partition = &partition_; diff --git a/system_shared.cpp b/system_shared.cpp index a4f69a02..20354d3a 100644 --- a/system_shared.cpp +++ b/system_shared.cpp @@ -9,6 +9,39 @@ // TOP +internal File_Data +sysshared_load_file(char *filename){ + File_Data result = {0}; + + File_Loading loading = + system_file_load_begin(filename); + + result.got_file = loading.exists; + + if (loading.size > 0){ + result.data.size = loading.size; + result.data.data = (byte*)Win32GetMemory(result.data.size); + + if (!result.data.data){ + system_file_load_end(loading, 0); + result = file_data_zero(); + } + else{ + if (!system_file_load_end(loading, (char*)result.data.data)){ + Win32FreeMemory(result.data.data); + result = file_data_zero(); + } + } + } + + return(result); +} + +internal b32 +sysshared_save_file(char *filename, char *data, i32 size){ + return(system_file_save(filename, data, size)); +} + internal b32 usable_ascii(char c){ b32 result = 1; diff --git a/system_shared.h b/system_shared.h index ee76b594..874ebec4 100644 --- a/system_shared.h +++ b/system_shared.h @@ -27,15 +27,11 @@ file_data_zero(){ #define Sys_Get_Memory_Sig(name) void* name(i32 size, i32 line_number, char *file_name) #define Sys_Free_Memory_Sig(name) void name(void *block) #define Sys_File_Can_Be_Made_Sig(name) b32 name(char *filename) -#define Sys_Load_File_Sig(name) File_Data name(char *filename) -#define Sys_Save_File_Sig(name) b32 name(char *filename, char *data, i32 size) #define Sys_Get_Binary_Path_Sig(name) i32 name(String *out) internal Sys_Get_Memory_Sig(system_get_memory_); internal Sys_Free_Memory_Sig(system_free_memory); internal Sys_File_Can_Be_Made_Sig(system_file_can_be_made); -internal Sys_Load_File_Sig(system_load_file); -internal Sys_Save_File_Sig(system_save_file); internal Sys_Get_Binary_Path_Sig(system_get_binary_path); #define system_get_memory(size) system_get_memory_((size), __LINE__, __FILE__) diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 3061397d..12dce74d 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -296,20 +296,30 @@ Sys_File_Load_Begin_Sig(system_file_load_begin){ File_Loading loading = {0}; HANDLE file = 0; - file = CreateFile(filename, GENERIC_READ, 0, 0, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - - if (file && file != INVALID_HANDLE_VALUE){ - DWORD lo, hi; - lo = GetFileSize(file, &hi); + String fname_str = make_string_slowly(filename); + if (fname_str.size < 1024){ + char fixed_space[1024]; + String fixed_str = make_fixed_width_string(fixed_space); + copy(&fixed_str, fname_str); + terminate_with_null(&fixed_str); - if (hi == 0){ - loading.handle = Win32Handle(file); - loading.size = lo; - loading.exists = 1; - } - else{ - CloseHandle(file); + replace_char(fixed_str, '/', '\\'); + + file = CreateFile(fixed_str.str, GENERIC_READ, 0, 0, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + + if (file && file != INVALID_HANDLE_VALUE){ + DWORD lo, hi; + lo = GetFileSize(file, &hi); + + if (hi == 0){ + loading.handle = Win32Handle(file); + loading.size = lo; + loading.exists = 1; + } + else{ + CloseHandle(file); + } } } @@ -369,50 +379,6 @@ Sys_File_Save_Sig(system_file_save){ return(success); } -internal -Sys_Load_File_Sig(system_load_file){ - File_Data result = {0}; - - String fname_str = make_string_slowly(filename); - if (fname_str.size >= 1024) return(result); - - char fixed_space[1024]; - String fixed_str = make_fixed_width_string(fixed_space); - copy(&fixed_str, fname_str); - terminate_with_null(&fixed_str); - - replace_char(fixed_str, '/', '\\'); - - File_Loading loading = - system_file_load_begin(fixed_str.str); - - result.got_file = loading.exists; - - if (loading.size > 0){ - result.data.size = loading.size; - result.data.data = (byte*)Win32GetMemory(result.data.size); - - if (!result.data.data){ - system_file_load_end(loading, 0); - result = file_data_zero(); - } - else{ - if (!system_file_load_end(loading, (char*)result.data.data)){ - Win32FreeMemory(result.data.data); - result = file_data_zero(); - } - } - } - - return(result); -} - -// TODO(allen): eliminate this -internal -Sys_Save_File_Sig(system_save_file){ - return(system_file_save(filename, data, size)); -} - // TODO(allen): THIS system does not really work. // I want to eliminate them both entirely and find a better // way to track the dirty state of files. It shouldn't be too @@ -2103,7 +2069,7 @@ int main(int argc, char **argv){ if (file->flags & FEx_Save){ Assert((file->flags & FEx_Request) == 0); file->flags &= (~FEx_Save); - if (system_save_file(file->filename, (char*)file->data, file->size)){ + if (sysshared_save_file(file->filename, (char*)file->data, file->size)){ file->flags |= FEx_Save_Complete; } else{ @@ -2115,7 +2081,7 @@ int main(int argc, char **argv){ if (file->flags & FEx_Request){ Assert((file->flags & FEx_Save) == 0); file->flags &= (~FEx_Request); - File_Data sysfile = system_load_file(file->filename); + File_Data sysfile = sysshared_load_file(file->filename); if (!sysfile.got_file){ file->flags |= FEx_Not_Exist; }