First commit, need to add more helper methods for buffer (e.g. backspace, insert character, etc.)

master
Aadhavan Srinivasan 2 years ago
commit b0add8f26a

@ -0,0 +1,116 @@
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>
typedef struct Buffer_struct Buffer;
struct Buffer_struct {
char* text;
char* start;
char* end;
int size;
int gap_size;
};
Buffer* new_buffer(int size) {
Buffer* buffer = malloc(sizeof(Buffer));
buffer->size = size;
buffer->text = malloc(buffer->size);
buffer->gap_size = size;
buffer->start = buffer->text;
buffer->end = buffer->start + buffer->gap_size;
return buffer;
}
void buffer_grow(Buffer* buffer) {
int start_offset = buffer->start - buffer->text;
buffer->size *= 2;
buffer->text = realloc(buffer->text,buffer->size);
buffer->start = buffer->text + start_offset;
buffer->gap_size = 10;
for (int i=strlen(buffer->text)-1; i > (buffer->start - buffer->text); i--) {
*(buffer->text + i + buffer->gap_size) = *(buffer->start + i);
*(buffer->start + i) = 0;
}
buffer->end = buffer->start + buffer->gap_size;
}
void buffer_insert(char ch, Buffer* buffer) {
*(buffer->start) = ch;
buffer->start++;
buffer->gap_size--;
if (buffer->gap_size == 0) {
buffer_grow(buffer);
}
}
void buffer_right(Buffer* buffer) {
buffer->start++;
buffer->end--;
}
void buffer_left(Buffer* buffer) {
buffer->start--;
buffer->end--;
}
void init_curses() {
initscr();
noecho();
keypad(stdscr,TRUE);
cbreak();
}
void sigint_handler(int dummy) {
endwin();
exit(130);
}
int main() {
signal(SIGINT,sigint_handler);
Buffer* buffer = new_buffer(10);
init_curses();
int ch;
while (true) {
clear();
int i=0;
if (buffer->start != buffer->text) { /* We don't want to print the string, if the
gap starts at the first index of the string */
while (i < strlen(buffer->text)) {
addch(*(buffer->text + i));
i++;
if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */
i += buffer->gap_size;
}
}
}
ch = getch();
switch(ch) {
case KEY_BACKSPACE:
if (buffer->start != buffer->text) {
buffer->start--;
buffer->gap_size++;
}
goto continue_while_loop;
default:
}
buffer_insert(ch,buffer);
continue_while_loop:
}
endwin();
}
Loading…
Cancel
Save