4coder/code/custom/4coder_insertion.cpp

122 lines
3.6 KiB
C++
Raw Permalink Normal View History

2019-02-24 07:22:16 +00:00
/*
* Serial inserts helpers
*/
// TOP
2019-12-13 00:03:21 +00:00
function Buffer_Insertion
begin_buffer_insertion_at(Application_Links *app, Buffer_ID buffer_id, i64 at){
2019-02-24 07:22:16 +00:00
Buffer_Insertion result = {};
result.app = app;
result.buffer = buffer_id;
result.at = at;
return(result);
}
2019-12-13 00:03:21 +00:00
function Buffer_Insertion
begin_buffer_insertion_at_buffered(Application_Links *app, Buffer_ID buffer_id, i64 at, Cursor *cursor){
Buffer_Insertion result = begin_buffer_insertion_at(app, buffer_id, at);
result.buffering = true;
2019-06-01 23:58:28 +00:00
result.cursor = cursor;
result.temp = begin_temp(cursor);
return(result);
}
2019-12-13 00:03:21 +00:00
function Buffer_Insertion
begin_buffer_insertion_at_buffered(Application_Links *app, Buffer_ID buffer_id, i64 at, Arena *buffer_memory, u64 buffer_memory_size){
Cursor *cursor = push_array(buffer_memory, Cursor, 1);
*cursor = make_cursor(push_array(buffer_memory, u8, buffer_memory_size), buffer_memory_size);
return(begin_buffer_insertion_at_buffered(app, buffer_id, at, cursor));
}
2019-12-13 00:03:21 +00:00
function Buffer_Insertion
2019-02-24 07:22:16 +00:00
begin_buffer_insertion(Application_Links *app){
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
i64 cursor_pos = view_get_cursor_pos(app, view);
Buffer_Insertion result = begin_buffer_insertion_at(app, buffer, cursor_pos);
2019-02-24 07:22:16 +00:00
return(result);
}
2019-12-13 00:03:21 +00:00
function void
2019-06-01 23:58:28 +00:00
insert_string__no_buffering(Buffer_Insertion *insertion, String_Const_u8 string){
buffer_replace_range(insertion->app, insertion->buffer, Ii64(insertion->at), string);
2019-02-24 07:22:16 +00:00
insertion->at += string.size;
}
2019-12-23 04:50:54 +00:00
function void
insert__flush(Buffer_Insertion *insertion){
2019-06-01 23:58:28 +00:00
Cursor *cursor = insertion->cursor;
u64 pos = insertion->temp.temp_memory_cursor.pos;
2019-06-01 23:58:28 +00:00
String_Const_u8 string = SCu8(cursor->base + pos, cursor->pos - pos);
insert_string__no_buffering(insertion, string);
2019-06-01 23:58:28 +00:00
end_temp(insertion->temp);
}
2019-12-23 04:50:54 +00:00
function char*
insert__reserve(Buffer_Insertion *insertion, u64 size){
2019-06-01 23:58:28 +00:00
char *space = push_array(insertion->cursor, char, size);
if (space == 0){
insert__flush(insertion);
2019-06-01 23:58:28 +00:00
space = push_array(insertion->cursor, char, size);
}
return(space);
}
2019-12-23 04:50:54 +00:00
function void
2019-06-01 23:58:28 +00:00
end_buffer_insertion(Buffer_Insertion *insertion){
if (insertion->buffering){
insert__flush(insertion);
}
}
2019-12-23 04:50:54 +00:00
function void
2019-06-01 23:58:28 +00:00
insert_string(Buffer_Insertion *insertion, String_Const_u8 string){
if (!insertion->buffering){
insert_string__no_buffering(insertion, string);
}
else{
char *space = insert__reserve(insertion, string.size);
if (space != 0){
2019-09-27 23:56:05 +00:00
block_copy(space, string.str, string.size);
}
else{
insert_string__no_buffering(insertion, string);
}
}
}
2019-12-23 04:50:54 +00:00
function u64
2019-02-24 07:22:16 +00:00
insertf(Buffer_Insertion *insertion, char *format, ...){
Scratch_Block scratch(insertion->app);
2019-02-24 07:22:16 +00:00
va_list args;
va_start(args, format);
String_Const_u8 string = push_u8_stringfv(scratch, format, args);
2019-02-24 07:22:16 +00:00
va_end(args);
2019-04-01 06:14:31 +00:00
insert_string(insertion, string);
return(string.size);
2019-02-24 07:22:16 +00:00
}
2019-12-23 04:50:54 +00:00
function void
2019-02-24 07:22:16 +00:00
insertc(Buffer_Insertion *insertion, char C){
2019-06-01 23:58:28 +00:00
insert_string(insertion, SCu8(&C, 1));
2019-02-24 07:22:16 +00:00
}
2019-12-23 04:50:54 +00:00
function b32
2019-02-26 23:17:53 +00:00
insert_line_from_buffer(Buffer_Insertion *insertion, Buffer_ID buffer_id, i32 line, i32 truncate_at){
b32 success = is_valid_line(insertion->app, buffer_id, line);
if (success){
Scratch_Block scratch(insertion->app);
insert_string(insertion, push_buffer_line(insertion->app, scratch, buffer_id, line));
2019-02-24 07:22:16 +00:00
}
return(success);
}
2019-12-23 04:50:54 +00:00
function b32
2019-02-26 23:17:53 +00:00
insert_line_from_buffer(Buffer_Insertion *insertion, Buffer_ID buffer_id, i32 line){
2019-02-24 07:22:16 +00:00
return(insert_line_from_buffer(insertion, buffer_id, line, 0));
}
// BOTTOM