From 654ae784e23ab941d77b61a657b66381dd8147de Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Wed, 11 May 2016 16:40:53 -0400 Subject: [PATCH] fixed open empty file bug --- 4ed.cpp | 14 +++++---- 4ed_exchange.cpp | 39 +++++++++++++++--------- 4ed_rendering.cpp | 20 ++++++------- system_shared.h | 7 ++++- win32_4ed.cpp | 75 ++++++++++++++++++++++++++--------------------- 5 files changed, 92 insertions(+), 63 deletions(-) diff --git a/4ed.cpp b/4ed.cpp index a574bc9e..d44638a9 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -4156,22 +4156,22 @@ App_Step_Sig(app_step){ b32 failed = 0; binding = vars->sys_app_bindings + i; - byte *data; - i32 size, max; Editing_File *ed_file; Editing_File_Preload preload_settings; char *filename; Working_Set *working_set = &models->working_set; + File_Ready_Result file_result = + exchange_file_ready(exchange, binding->sys_id); - if (exchange_file_ready(exchange, binding->sys_id, &data, &size, &max)){ + if (file_result.ready){ ed_file = working_set_get_active_file(working_set, binding->app_id); Assert(ed_file); filename = exchange_file_filename(exchange, binding->sys_id); preload_settings = ed_file->preload; - if (data){ - String val = make_string((char*)data, size); + if (file_result.exists){ + String val = make_string((char*)file_result.data, file_result.size); file_create_from_string(system, models, ed_file, filename, val); if (ed_file->settings.tokens_exist){ @@ -4206,6 +4206,10 @@ App_Step_Sig(app_step){ remove = 1; } + // TODO(allen): Switch to multiple return struct. + byte *data; + i32 size, max; + if (exchange_file_save_complete(exchange, binding->sys_id, &data, &size, &max, &failed)){ Assert(remove == 0); diff --git a/4ed_exchange.cpp b/4ed_exchange.cpp index 7908cace..615381c2 100644 --- a/4ed_exchange.cpp +++ b/4ed_exchange.cpp @@ -92,28 +92,39 @@ exchange_request_file(Exchange *exchange, char *filename, int len){ return result; } -internal b32 -exchange_file_ready(Exchange *exchange, i32 file_id, byte **data, int *size, int *max){ - File_Exchange *files = &exchange->file; - b32 result = 0; +struct File_Ready_Result{ + byte *data; + i32 size; + i32 max; + b32 exists; + b32 ready; +}; +internal File_Ready_Result +exchange_file_ready(Exchange *exchange, i32 file_id){ + File_Ready_Result result = {0}; + File_Exchange *files = &exchange->file; + File_Slot *file = 0; + if (file_id > 0 && file_id <= files->max){ - File_Slot *file = files->files + file_id - 1; + file = files->files + file_id - 1; if (file->flags & FEx_Ready){ - *data = file->data; - *size = file->size; - *max = file->max; - result = 1; + result.data = file->data; + result.size = file->size; + result.max = file->max; + result.exists = 1; + result.ready = 1; } if (file->flags & FEx_Not_Exist){ - *data = 0; - *size = 0; - *max = 0; - result = 1; + result.data = 0; + result.size = 0; + result.max = 0; + result.exists = 0; + result.ready = 1; } } - return result; + return(result); } internal b32 diff --git a/4ed_rendering.cpp b/4ed_rendering.cpp index 7a5be278..6b87dea5 100644 --- a/4ed_rendering.cpp +++ b/4ed_rendering.cpp @@ -304,7 +304,7 @@ draw_font_info_load(Partition *partition, if (!translate_success) return 0; i32 result = 1; - Data file; + File_Data file; file = system_load_file(filename.str); Temp_Memory temp = begin_temp_memory(partition); @@ -317,12 +317,12 @@ draw_font_info_load(Partition *partition, tex_height = pt_size*2*oversample; void *block = push_block(partition, tex_width * tex_height); - if (!file.data){ + if (!file.data.data){ result = 0; } else{ stbtt_fontinfo font; - if (!stbtt_InitFont(&font, (u8*)file.data, 0)){ + if (!stbtt_InitFont(&font, (u8*)file.data.data, 0)){ result = 0; } else{ @@ -343,7 +343,7 @@ draw_font_info_load(Partition *partition, stbtt_pack_context spc; if (stbtt_PackBegin(&spc, (u8*)block, tex_width, tex_height, tex_width, 1, partition)){ stbtt_PackSetOversampling(&spc, oversample, oversample); - if (stbtt_PackFontRange(&spc, (u8*)file.data, 0, + if (stbtt_PackFontRange(&spc, (u8*)file.data.data, 0, STBTT_POINT_SIZE((f32)pt_size), 0, 128, chardata)){ // do nothing } @@ -373,7 +373,7 @@ draw_font_info_load(Partition *partition, } } - system_free_memory(file.data); + system_free_memory(file.data.data); } end_temp_memory(temp); @@ -395,7 +395,7 @@ draw_font_load(void *base_block, i32 size, if (!translate_success) return 0; i32 result = 1; - Data file; + File_Data file; file = system_load_file(filename.str); Partition partition_ = partition_open(base_block, size); @@ -409,13 +409,13 @@ draw_font_load(void *base_block, i32 size, tex_height = pt_size*2*oversample; void *block = push_block(partition, tex_width * tex_height); - if (!file.data){ + if (!file.data.data){ result = 0; } else{ stbtt_fontinfo font; - if (!stbtt_InitFont(&font, (u8*)file.data, 0)){ + if (!stbtt_InitFont(&font, (u8*)file.data.data, 0)){ result = 0; } else{ @@ -443,7 +443,7 @@ draw_font_load(void *base_block, i32 size, if (stbtt_PackBegin(&spc, (u8*)block, tex_width, tex_height, tex_width, 1, partition)){ stbtt_PackSetOversampling(&spc, oversample, oversample); - if (stbtt_PackFontRange(&spc, (u8*)file.data, 0, + if (stbtt_PackFontRange(&spc, (u8*)file.data.data, 0, STBTT_POINT_SIZE((f32)pt_size), 0, 128, chardata)){ // do nothing } @@ -492,7 +492,7 @@ draw_font_load(void *base_block, i32 size, } } - system_free_memory(file.data); + system_free_memory(file.data.data); } return result; diff --git a/system_shared.h b/system_shared.h index 8e272976..2b1000db 100644 --- a/system_shared.h +++ b/system_shared.h @@ -32,10 +32,15 @@ struct Font_Load_System{ i32 max; }; +struct File_Data{ + Data data; + b32 got_file; +}; + #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(name) b32 name(char *filename) -#define Sys_Load_File_Sig(name) Data 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_To_Binary_Path(name) b32 name(String *out_filename, char *filename) diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 2ff0da28..2fe2ea5c 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -193,21 +193,23 @@ internal void* Win32GetMemory_(i32 size, i32 line_number, char *file_name){ void *ptr = 0; + if (size > 0){ #if FRED_INTERNAL - ptr = VirtualAlloc(0, size + sizeof(Sys_Bubble), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + ptr = VirtualAlloc(0, size + sizeof(Sys_Bubble), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); - Sys_Bubble *bubble = (Sys_Bubble*)ptr; - bubble->flags = MEM_BUBBLE_SYS_DEBUG; - bubble->line_number = line_number; - bubble->file_name = file_name; - bubble->size = size; - WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE); - insert_bubble(&win32vars.internal_bubble, bubble); - ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0); - ptr = bubble + 1; + Sys_Bubble *bubble = (Sys_Bubble*)ptr; + bubble->flags = MEM_BUBBLE_SYS_DEBUG; + bubble->line_number = line_number; + bubble->file_name = file_name; + bubble->size = size; + WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE); + insert_bubble(&win32vars.internal_bubble, bubble); + ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0); + ptr = bubble + 1; #else - ptr = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + ptr = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); #endif + } return ptr; } @@ -265,9 +267,9 @@ system_free_memory(void *block){ Win32FreeMemory(block); } -internal Data +internal File_Data system_load_file(char *filename){ - Data result = {}; + File_Data result = {0}; HANDLE file; String fname_str = make_string_slowly(filename); @@ -293,23 +295,31 @@ system_load_file(char *filename){ return result; } - result.size = (lo) + (((u64)hi) << 32); - result.data = (byte*)Win32GetMemory(result.size); + result.data.size = (lo) + (((u64)hi) << 32); - if (!result.data){ - CloseHandle(file); - result = {}; - return result; + if (result.data.size > 0){ + result.data.data = (byte*)Win32GetMemory(result.data.size); + + if (!result.data.data){ + CloseHandle(file); + result = {0}; + return result; + } + + DWORD read_size; + BOOL read_result = ReadFile(file, + result.data.data, result.data.size, + &read_size, 0); + result.got_file = 1; + if (!read_result || read_size != (u32)result.data.size){ + CloseHandle(file); + Win32FreeMemory(result.data.data); + result = {0}; + return result; + } } - - DWORD read_size; - BOOL read_result = ReadFile(file, result.data, result.size, - &read_size, 0); - if (!read_result || read_size != (u32)result.size){ - CloseHandle(file); - Win32FreeMemory(result.data); - result = {}; - return result; + else{ + result.got_file = 1; } CloseHandle(file); @@ -1654,15 +1664,14 @@ UpdateStep(){ if (file->flags & FEx_Request){ Assert((file->flags & FEx_Save) == 0); file->flags &= (~FEx_Request); - Data sysfile = - system_load_file(file->filename); - if (sysfile.data == 0){ + File_Data sysfile = system_load_file(file->filename); + if (!sysfile.got_file){ file->flags |= FEx_Not_Exist; } else{ file->flags |= FEx_Ready; - file->data = sysfile.data; - file->size = sysfile.size; + file->data = sysfile.data.data; + file->size = sysfile.data.size; } PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); }