From e5abd6a135eb0e2867fada0e4e94045fd21fcba0 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Mon, 10 Jul 2017 13:42:09 -0400 Subject: [PATCH] fully removed out context --- meta/4ed_file_moving.h | 82 ++++++++++++++++++++++++++++++ meta/4ed_metagen.cpp | 21 +++++--- meta/4ed_out_context.cpp | 32 ------------ site/4ed_sitegen.cpp | 3 +- string/4coder_string_build_num.txt | 2 +- string/4ed_string_builder.cpp | 3 +- 6 files changed, 100 insertions(+), 43 deletions(-) delete mode 100644 meta/4ed_out_context.cpp diff --git a/meta/4ed_file_moving.h b/meta/4ed_file_moving.h index 970a236c..75762f51 100644 --- a/meta/4ed_file_moving.h +++ b/meta/4ed_file_moving.h @@ -60,6 +60,9 @@ internal void fm_copy_file(char *file, char *newname); internal void fm_copy_all(char *source, char *tag, char *folder); internal void fm_copy_folder(char *src_dir, char *dst_dir, char *src_folder); +// File Reading and Writing +internal void fm_write_file(char *file_name, char *data, u32 size); + // Zip internal void fm_zip(char *parent, char *folder, char *dest); @@ -219,6 +222,10 @@ fm_align(){ fm_arena_pos = (fm_arena_pos+7)&(~7); } +// +// Windows implementation +// + #if defined(IS_WINDOWS) typedef uint32_t DWORD; @@ -239,6 +246,28 @@ typedef union _LARGE_INTEGER { } u; LONGLONG QuadPart; } LARGE_INTEGER, *PLARGE_INTEGER; +typedef void* HANDLE; +typedef void* PVOID; +typedef void* LPVOID; +typedef void* LPCVOID; +typedef DWORD* LPDWORD; +#if defined(_WIN64) +typedef unsigned __int64 ULONG_PTR; +#else +typedef unsigned long ULONG_PTR; +#endif +typedef struct _OVERLAPPED { + ULONG_PTR Internal; + ULONG_PTR InternalHigh; + union { + struct { + DWORD Offset; + DWORD OffsetHigh; + }; + PVOID Pointer; + }; + HANDLE hEvent; +} OVERLAPPED, *LPOVERLAPPED; #define WINAPI @@ -249,8 +278,30 @@ extern "C"{ BOOL WINAPI QueryPerformanceFrequency(_Out_ LARGE_INTEGER *lpFrequency); BOOL WINAPI CreateDirectoryA(_In_ LPCTSTR lpPathName, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes); BOOL WINAPI CopyFileA(_In_ LPCTSTR lpExistingFileName, _In_ LPCTSTR lpNewFileName, _In_ BOOL bFailIfExists); + + HANDLE WINAPI CreateFileA(_In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode,_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile); + BOOL WINAPI WriteFile(_In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped); + BOOL WINAPI ReadFile(_In_ HANDLE hFile, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead, _Inout_opt_ LPOVERLAPPED lpOverlapped); + BOOL WINAPI CloseHandle(_In_ HANDLE hObject); } +#define INVALID_HANDLE_VALUE ((HANDLE) -1) + +#define GENERIC_READ 0x80000000 +#define GENERIC_WRITE 0x40000000 +#define GENERIC_EXECUTE 0x20000000 +#define GENERIC_ALL 0x10000000 + +#define CREATE_NEW 1 +#define CREATE_ALWAYS 2 +#define OPEN_EXISTING 3 +#define OPEN_ALWAYS 4 +#define TRUNCATE_EXISTING 5 + +#define FILE_ATTRIBUTE_READONLY 0x00000001 +#define FILE_ATTRIBUTE_NORMAL 0x00000080 +#define FILE_ATTRIBUTE_TEMPORARY 0x00000100 + static uint64_t perf_frequency; static void @@ -362,6 +413,23 @@ fm_copy_all(char *source, char *tag, char *folder){ } } +internal void +fm_write_file(char *file_name, char *data, u32 size){ + HANDLE file = CreateFileA(file_name, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + if (file != INVALID_HANDLE_VALUE){ + DWORD written = 0; + for (;written < size;){ + DWORD newly_written = 0; + if (!WriteFile(file, data + written, size - written, &newly_written, 0)){ + break; + } + written += newly_written; + } + Assert(written == size); + CloseHandle(file); + } +} + static void fm_zip(char *parent, char *folder, char *dest){ char cdir[512]; @@ -374,6 +442,10 @@ fm_zip(char *parent, char *folder, char *dest){ systemf("copy %s\\4ed_gobble.zip %s & del %s\\4ed_gobble.zip", cdir, dest, cdir); } +// +// Unix implementation +// + #elif defined(IS_LINUX) || defined(IS_MAC) #include @@ -478,6 +550,16 @@ fm_copy_all(char *source, char *tag, char *folder){ } } +internal void +fm_write_file(char *file_name, char *data, u32 size){ + // TODO(allen): Real unix version? + FILE *file = fopen(file_name, "wb"); + if (file != 0){ + fwrite(data, 1, size, file); + fclose(file); + } +} + static void fm_zip(char *parent, char *folder, char *file){ Temp_Dir temp = fm_pushdir(parent); diff --git a/meta/4ed_metagen.cpp b/meta/4ed_metagen.cpp index 0aef8d8c..96c253b2 100644 --- a/meta/4ed_metagen.cpp +++ b/meta/4ed_metagen.cpp @@ -115,7 +115,8 @@ generate_keycode_enum(){ "return(result);\n" "}\n"); - end_file_out(filename_keycodes, &out); + fm_write_file(filename_keycodes, out.str, out.size); + out.size = 0; fm_end_temp(temp); } @@ -240,7 +241,8 @@ generate_style(){ } append(&out, "};\n"); - end_file_out(filename_4coder, &out); + fm_write_file(filename_4coder, out.str, out.size); + out.size = 0; struct_begin(&out, "Interactive_Style"); { @@ -298,7 +300,8 @@ generate_style(){ "}\n\n"); } - end_file_out(filename_4ed, &out); + fm_write_file(filename_4ed, out.str, out.size); + out.size = 0; fm_end_temp(temp); } @@ -400,11 +403,11 @@ generate_custom_headers(){ String *public_name = &func_4ed_names.names[i].public_name; *macro = str_alloc(name_string.size+4); - to_upper_ss(macro, name_string); + to_upper(macro, name_string); append(macro, make_lit_string("_SIG")); *public_name = str_alloc(name_string.size); - to_lower_ss(public_name, name_string); + to_lower(public_name, name_string); fm_align(); } @@ -435,7 +438,8 @@ generate_custom_headers(){ append(&out, "_Function);\n"); } - end_file_out(OS_API_H, &out); + fm_write_file(OS_API_H, out.str, out.size); + out.size = 0; append(&out, "struct Application_Links;\n"); @@ -518,7 +522,7 @@ generate_custom_headers(){ } append(&out, "){"); - if (match_ss(ret, make_lit_string("void"))){ + if (match(ret, make_lit_string("void"))){ append(&out, "("); } else{ @@ -548,7 +552,8 @@ generate_custom_headers(){ } append(&out, "#endif\n"); - end_file_out(API_H, &out); + fm_write_file(API_H, out.str, out.size); + out.size = 0; fm_end_temp(temp); } diff --git a/meta/4ed_out_context.cpp b/meta/4ed_out_context.cpp deleted file mode 100644 index c3950294..00000000 --- a/meta/4ed_out_context.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Mr. 4th Dimention - Allen Webster - * - * 25.02.2016 - * - * File editing view for 4coder - * - */ - -// TOP - -#if !defined(OUT_CONTEXT_4CODER) -#define OUT_CONTEXT_4CODER - -internal void -end_file_out(char *out_file, String *out_data){ - FILE *file = fopen(out_file, "wb"); - if (file != 0){ - fwrite(out_data->str, 1, out_data->size, file); - fclose(file); - } - else{ - fprintf(stdout, "Could not open output file %s\n", out_file); - } - out_data->size = 0; -} - -#endif - -// BOTTOM - - diff --git a/site/4ed_sitegen.cpp b/site/4ed_sitegen.cpp index ee1ebaf0..6a75bf7c 100644 --- a/site/4ed_sitegen.cpp +++ b/site/4ed_sitegen.cpp @@ -98,7 +98,8 @@ do_html_output(Document_System *doc_system, char *dst_directory, Abstract_Item * if (doc_get_link_string(doc, doc_link, sizeof(doc_link))){ generate_document_html(&out, doc_system, doc); char *name = fm_str(dst_directory, "/", doc_link); - end_file_out(name, &out); + fm_write_file(name, out.str, out.size); + out.size = 0; } } diff --git a/string/4coder_string_build_num.txt b/string/4coder_string_build_num.txt index ddb1bfd4..694ecf9f 100644 --- a/string/4coder_string_build_num.txt +++ b/string/4coder_string_build_num.txt @@ -1,5 +1,5 @@ 1 0 -100 +101 diff --git a/string/4ed_string_builder.cpp b/string/4ed_string_builder.cpp index 7055bd72..6b17a908 100644 --- a/string/4ed_string_builder.cpp +++ b/string/4ed_string_builder.cpp @@ -433,7 +433,8 @@ int main(){ pstr = str_start_end(pcontext.data, start, parse.code.size); append(&out, pstr); - end_file_out(GENERATED_FILE, &out); + fm_write_file(GENERATED_FILE, out.str, out.size); + out.size = 0; // NOTE(allen): Publish the new file. (Would like to be able to automatically test the result before publishing). {