4coder/custom/4coder_insertion.cpp

122 lines
3.6 KiB
C++
Raw Normal View History

2019-02-24 07:22:16 +00:00
/*
* Serial inserts helpers
*/
// TOP
static 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);
}
static 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);
}
internal Buffer_Insertion
begin_buffer_insertion_at_buffered(Application_Links *app, Buffer_ID buffer_id, i64 at, Arena *buffer_memory, umem 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-02-24 07:22:16 +00:00
static Buffer_Insertion
begin_buffer_insertion(Application_Links *app){
2019-06-19 02:31:59 +00:00
View_ID view = get_active_view(app, AccessAll);
Buffer_ID buffer = view_get_buffer(app, view, AccessAll);
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);
}
static 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;
}
static void
insert__flush(Buffer_Insertion *insertion){
2019-06-01 23:58:28 +00:00
Cursor *cursor = insertion->cursor;
umem pos = insertion->temp.temp_memory_cursor.pos;
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);
}
static char*
2019-06-01 23:58:28 +00:00
insert__reserve(Buffer_Insertion *insertion, umem size){
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);
}
static void
2019-06-01 23:58:28 +00:00
end_buffer_insertion(Buffer_Insertion *insertion){
if (insertion->buffering){
insert__flush(insertion);
}
}
static void
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-06-01 23:58:28 +00:00
static umem
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
}
static void
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
}
static 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);
}
static 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