Skip to content

Commit

Permalink
define abuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 22, 2022
1 parent cdd1fef commit d32130e
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions kilo.c
Expand Up @@ -4,6 +4,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
Expand Down Expand Up @@ -76,8 +77,8 @@ int getCursorPosition(int *rows, int *cols) {

if (buf[0] != '\x1b' || buf[1] != '[') return -1;
if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) return -1;
return 0;

return 0;
}

int getWindowSize(int *rows, int *cols) {
Expand All @@ -93,7 +94,30 @@ int getWindowSize(int *rows, int *cols) {
}
}

/*** append buffer ***/
struct abuf {
char *b;
int len;
};

#define ABUF_INIT \
{ NULL, 0 }

void abAppend(struct abuf *ab, const char *s, int len) {
char *new = realloc(ab->b, ab->len + len);

if (new == NULL) return;
memcpy(&new[ab->len], s, len);
ab->b = new;
ab->len = += len;
}

void abFree(struct abuf *ab) {
free(ab->b)
}

/*** input ***/

void editorProcessKeyPress() {
char c = editorReadKey();

Expand All @@ -112,7 +136,11 @@ void editorProcessKeyPress() {
void editorDrawRows() {
int y;
for (y = 0; y < E.screenrows; y++) {
write(STDOUT_FILENO, "~\r\n", 3);
write(STDIN_FILENO, "~", 1);

if (y < E.screenrows - 1) {
write(STDIN_FILENO, "\r\n", 2);
}
}
}

Expand Down

0 comments on commit d32130e

Please sign in to comment.