more cleanup of the buffer creation code

This commit is contained in:
Allen Webster 2017-07-17 16:59:28 -04:00
parent 0f8dc7f205
commit 6d90c58f81
3 changed files with 38 additions and 25 deletions

View File

@ -193,12 +193,9 @@ ENUM(int32_t, View_Setting_ID){
ENUM(uint32_t, Buffer_Create_Flag){
/* DOC(BufferCreate_Background is not currently implemented.) */
BufferCreate_Background = 0x1,
/* DOC(When BufferCreate_AlwaysNew is set it indicates the buffer should be
cleared to empty even if it's associated file already has content.) */
/* DOC(When BufferCreate_AlwaysNew is set it indicates the buffer should be cleared to empty even if it's associated file already has content.) */
BufferCreate_AlwaysNew = 0x2,
/* DOC(When BufferCreate_NeverNew is set it indicates that the buffer should
only be created if it is an existing file or if a buffer with the given name
is already open.) */
/* DOC(When BufferCreate_NeverNew is set it indicates that the buffer should only be created if it is an existing file or if a buffer with the given name is already open.) */
BufferCreate_NeverNew = 0x4,
};

View File

@ -1226,7 +1226,14 @@ DOC(Whenever a buffer is killed an end signal is sent which triggers the end fil
}
API_EXPORT Buffer_Summary
Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buffer_Create_Flag flags){
Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buffer_Create_Flag flags)
/*
DOC_PARAM(filename, The name of the file to associate to the new buffer. If the file is not found, the buffer will be created empty and will not be associated to a file until it is saved.)
DOC_PARAM(filename_len, The length of the filename string.)
DOC_PARAM(flags, Flags controlling the buffer creation behavior.)
DOC()
DOC_SEE(Buffer_Create_Flag)
*/{
PRFL_FUNC_GROUP();
Command_Data *cmd = (Command_Data*)app->cmd_context;
@ -1239,14 +1246,12 @@ Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buff
Buffer_Summary result = {0};
if (filename_len > 0){
String fname = make_string(filename, filename_len);
Editing_File *file = 0;
b32 do_new_file = false;
Plat_Handle handle = {0};
Temp_Memory temp = begin_temp_memory(part);
// NOTE(allen): Try to get the file by canon name.
String fname = make_string(filename, filename_len);
Editing_File *file = 0;
b32 do_new_file = false;
Editing_File_Canon_Name canon = {0};
if (get_canon_name(system, &canon, fname)){
file = working_set_canon_contains(working_set, canon.name);
@ -1255,11 +1260,16 @@ Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buff
do_new_file = true;
}
// NOTE(allen): Try to get the file by buffer name.
if (file == 0){
file = working_set_name_contains(working_set, fname);
}
// NOTE(allen): If there is still no file, create a new buffer.
if (file == 0){
Plat_Handle handle = {0};
// NOTE(allen): Figure out whether this is a new file, or an existing file.
if (!do_new_file){
if (flags & BufferCreate_AlwaysNew){
do_new_file = true;
@ -1271,7 +1281,17 @@ Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buff
}
}
if (!do_new_file){
if (do_new_file){
if (!(flags & BufferCreate_NeverNew)){
file = working_set_alloc_always(working_set, general);
if (file != 0){
buffer_bind_name(general, working_set, file, fname);
init_normal_file(system, models, file, 0, 0);
fill_buffer_summary(&result, file, cmd);
}
}
}
else{
Assert(!handle_equal(handle, null_plat_handle));
i32 size = system->load_size(handle);
@ -1302,14 +1322,6 @@ Create_Buffer(Application_Links *app, char *filename, int32_t filename_len, Buff
general_memory_free(general, buffer);
}
}
else if (!(flags & BufferCreate_NeverNew)){
file = working_set_alloc_always(working_set, general);
if (file){
buffer_bind_name(general, working_set, file, fname);
init_normal_file(system, models, file, 0, 0);
fill_buffer_summary(&result, file, cmd);
}
}
}
else{
fill_buffer_summary(&result, file, cmd);

View File

@ -407,10 +407,12 @@ fm_copy_file(char *file, char *newname){
static void
fm_copy_all(char *source, char *tag, char *folder){
if (source){
systemf("copy %s\\%s %s\\*", source, tag, folder);
fprintf(stdout, "copy %s\\%s to %s\n", source, tag, folder);
systemf("copy %s\\%s %s\\* > nul", source, tag, folder);
}
else{
systemf("copy %s %s\\*", tag, folder);
fprintf(stdout, "copy %s to %s\n", tag, folder);
systemf("copy %s %s\\* > nul", tag, folder);
}
}
@ -545,10 +547,12 @@ fm_copy_file(char *file, char *newname){
static void
fm_copy_all(char *source, char *tag, char *folder){
if (source){
systemf("cp -f %s/%s %s", source, tag, folder);
fprintf(stdout, "copy %s/%s to %s\n", source, tag, folder);
systemf("cp -f %s/%s %s > /dev/null", source, tag, folder);
}
else{
systemf("cp -f %s %s", tag, folder);
fprintf(stdout, "copy %s to %s\n", tag, folder);
systemf("cp -f %s %s > /dev/null", tag, folder);
}
}