2015-10-16 20:31:04 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
* Four Tech
|
|
|
|
*
|
2015-10-19 01:13:15 +00:00
|
|
|
* public domain -- no warranty is offered or implied; use this code at your own risk
|
|
|
|
*
|
2015-10-16 20:31:04 +00:00
|
|
|
* 16.10.2015
|
|
|
|
*
|
|
|
|
* Buffer data object
|
|
|
|
* type - Golden Array
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-10-19 01:13:15 +00:00
|
|
|
// TOP
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Buffer{
|
2015-10-16 20:31:04 +00:00
|
|
|
char *data;
|
|
|
|
int size, max;
|
2015-10-19 01:13:15 +00:00
|
|
|
|
|
|
|
int *line_starts;
|
2015-10-24 22:14:33 +00:00
|
|
|
float *line_widths;
|
2015-10-19 01:13:15 +00:00
|
|
|
int line_count;
|
2015-11-02 01:04:37 +00:00
|
|
|
int widths_count;
|
2015-10-19 01:13:15 +00:00
|
|
|
int line_max;
|
|
|
|
int widths_max;
|
2015-10-16 20:31:04 +00:00
|
|
|
} Buffer;
|
|
|
|
|
2015-10-31 02:33:13 +00:00
|
|
|
inline_4tech int
|
|
|
|
buffer_good(Buffer *buffer){
|
|
|
|
int good;
|
|
|
|
good = (buffer->data != 0);
|
|
|
|
return(good);
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:59:39 +00:00
|
|
|
inline_4tech int
|
|
|
|
buffer_size(Buffer *buffer){
|
|
|
|
return buffer->size;
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Buffer_Init{
|
2015-10-31 02:33:13 +00:00
|
|
|
Buffer *buffer;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
} Buffer_Init;
|
|
|
|
|
|
|
|
internal_4tech Buffer_Init
|
|
|
|
buffer_begin_init(Buffer *buffer, char *data, int size){
|
|
|
|
Buffer_Init init;
|
|
|
|
init.buffer = buffer;
|
|
|
|
init.data = data;
|
|
|
|
init.size = size;
|
|
|
|
return(init);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech int
|
|
|
|
buffer_init_need_more(Buffer_Init *init){
|
|
|
|
int result;
|
|
|
|
result = 1;
|
|
|
|
if (init->buffer->data) result = 0;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech int
|
|
|
|
buffer_init_page_size(Buffer_Init *init){
|
|
|
|
int result;
|
|
|
|
result = init->size * 2;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech void
|
|
|
|
buffer_init_provide_page(Buffer_Init *init, void *page, int page_size){
|
|
|
|
Buffer *buffer;
|
|
|
|
buffer = init->buffer;
|
|
|
|
buffer->data = (char*)page;
|
|
|
|
buffer->max = page_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
2015-11-03 23:57:04 +00:00
|
|
|
buffer_end_init(Buffer_Init *init, void *scratch, int scratch_size){
|
2015-10-31 02:33:13 +00:00
|
|
|
Buffer *buffer;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
buffer = init->buffer;
|
|
|
|
if (buffer->data){
|
|
|
|
if (buffer->max >= init->size){
|
|
|
|
buffer->size = eol_convert_in(buffer->data, init->data, init->size);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Buffer_Stringify_Loop{
|
2015-10-16 20:31:04 +00:00
|
|
|
Buffer *buffer;
|
2015-10-22 03:23:59 +00:00
|
|
|
char *data, *end;
|
2015-10-24 22:14:33 +00:00
|
|
|
int absolute_pos;
|
2015-10-16 20:31:04 +00:00
|
|
|
int size;
|
2015-10-19 01:13:15 +00:00
|
|
|
} Buffer_Stringify_Loop;
|
|
|
|
|
|
|
|
inline_4tech Buffer_Stringify_Loop
|
2015-10-31 21:41:10 +00:00
|
|
|
buffer_stringify_loop(Buffer *buffer, int start, int end){
|
2015-10-19 01:13:15 +00:00
|
|
|
Buffer_Stringify_Loop result;
|
2015-10-22 03:23:59 +00:00
|
|
|
if (0 <= start && start < end && end <= buffer->size){
|
2015-10-19 01:13:15 +00:00
|
|
|
result.buffer = buffer;
|
2015-10-24 22:14:33 +00:00
|
|
|
result.absolute_pos = start;
|
2015-10-19 01:13:15 +00:00
|
|
|
result.data = buffer->data + start;
|
|
|
|
result.size = end - start;
|
2015-10-22 03:23:59 +00:00
|
|
|
result.end = buffer->data + end;
|
2015-10-19 01:13:15 +00:00
|
|
|
}
|
|
|
|
else result.buffer = 0;
|
2015-10-16 20:31:04 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech int
|
2015-10-19 01:13:15 +00:00
|
|
|
buffer_stringify_good(Buffer_Stringify_Loop *loop){
|
2015-10-16 20:31:04 +00:00
|
|
|
int result;
|
|
|
|
result = (loop->buffer != 0);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech void
|
2015-10-19 01:13:15 +00:00
|
|
|
buffer_stringify_next(Buffer_Stringify_Loop *loop){
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->buffer = 0;
|
2015-10-16 20:31:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Buffer_Backify_Loop{
|
2015-10-24 22:14:33 +00:00
|
|
|
Buffer *buffer;
|
|
|
|
char *data, *end;
|
|
|
|
int absolute_pos;
|
|
|
|
int size;
|
|
|
|
} Buffer_Backify_Loop;
|
2015-10-19 01:13:15 +00:00
|
|
|
|
2015-10-24 22:14:33 +00:00
|
|
|
inline_4tech Buffer_Backify_Loop
|
2015-10-31 21:41:10 +00:00
|
|
|
buffer_backify_loop(Buffer *buffer, int start, int end){
|
2015-10-24 22:14:33 +00:00
|
|
|
Buffer_Backify_Loop result;
|
2015-10-28 22:59:39 +00:00
|
|
|
|
|
|
|
++start;
|
2015-10-24 22:14:33 +00:00
|
|
|
if (0 <= end && end < start && start <= buffer->size){
|
|
|
|
result.buffer = buffer;
|
|
|
|
result.end = buffer->data + end;
|
|
|
|
result.size = start - end;
|
|
|
|
result.absolute_pos = start - result.size;
|
|
|
|
result.data = buffer->data + result.absolute_pos;
|
2015-10-16 20:31:04 +00:00
|
|
|
}
|
2015-10-24 22:14:33 +00:00
|
|
|
else result.buffer = 0;
|
|
|
|
return(result);
|
2015-10-16 20:31:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 22:14:33 +00:00
|
|
|
inline_4tech int
|
|
|
|
buffer_backify_good(Buffer_Backify_Loop *loop){
|
2015-10-16 20:31:04 +00:00
|
|
|
int result;
|
2015-10-24 22:14:33 +00:00
|
|
|
result = (loop->buffer != 0);
|
2015-10-19 01:13:15 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:14:33 +00:00
|
|
|
inline_4tech void
|
|
|
|
buffer_backify_next(Buffer_Backify_Loop *loop){
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->buffer = 0;
|
2015-10-19 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
2015-11-02 01:04:37 +00:00
|
|
|
buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount, int *request_amount){
|
2015-10-19 01:13:15 +00:00
|
|
|
char *data;
|
|
|
|
int result;
|
2015-10-28 22:59:39 +00:00
|
|
|
int size;
|
|
|
|
|
|
|
|
size = buffer_size(buffer);
|
|
|
|
assert_4tech(0 <= start);
|
|
|
|
assert_4tech(start <= end);
|
|
|
|
assert_4tech(end <= size);
|
2015-10-19 01:13:15 +00:00
|
|
|
|
|
|
|
*shift_amount = (len - (end - start));
|
2015-10-28 22:59:39 +00:00
|
|
|
if (*shift_amount + size <= buffer->max){
|
2015-10-19 01:13:15 +00:00
|
|
|
data = (char*)buffer->data;
|
|
|
|
memmove_4tech(data + end + *shift_amount, data + end, buffer->size - end);
|
|
|
|
buffer->size += *shift_amount;
|
|
|
|
if (len != 0) memcpy_4tech(data + start, str, len);
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
else{
|
2015-11-02 01:04:37 +00:00
|
|
|
*request_amount = round_up_4tech(2*(*shift_amount + size), 4 << 10);
|
|
|
|
result = 1;
|
2015-10-19 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
2015-11-06 04:31:53 +00:00
|
|
|
buffer_batch_edit_step(Buffer_Batch_State *state, Buffer *buffer, Buffer_Edit *sorted_edits,
|
|
|
|
char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){
|
2015-10-19 01:13:15 +00:00
|
|
|
Buffer_Edit *edit;
|
|
|
|
int i, result;
|
|
|
|
int shift_total, shift_amount;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
shift_total = state->shift_total;
|
|
|
|
i = state->i;
|
|
|
|
|
|
|
|
edit = sorted_edits + i;
|
|
|
|
for (; i < edit_count; ++i, ++edit){
|
|
|
|
result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total,
|
2015-11-06 04:31:53 +00:00
|
|
|
strings + edit->str_start, edit->len, &shift_amount,
|
|
|
|
scratch, scratch_size, request_amount);
|
2015-10-19 01:13:15 +00:00
|
|
|
if (result) break;
|
|
|
|
shift_total += shift_amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->shift_total = shift_total;
|
|
|
|
state->i = i;
|
|
|
|
|
2015-10-22 03:23:59 +00:00
|
|
|
return(result);
|
2015-10-19 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:43:36 +00:00
|
|
|
internal_4tech void*
|
|
|
|
buffer_edit_provide_memory(Buffer *buffer, void *new_data, int new_max){
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
assert_4tech(new_max >= buffer->size);
|
|
|
|
|
|
|
|
result = buffer->data;
|
|
|
|
memcpy_4tech(new_data, buffer->data, buffer->size);
|
|
|
|
buffer->data = (char*)new_data;
|
|
|
|
buffer->max = new_max;
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-10-19 01:13:15 +00:00
|
|
|
// BOTTOM
|
|
|
|
|