Archived
1
Fork 0

Implement writing to disk

This commit is contained in:
Hadeed 2023-09-03 16:01:22 +05:00
parent f866add58f
commit 7a9727a550
7 changed files with 56 additions and 3 deletions

View file

@ -1,6 +1,8 @@
#ifndef BUFFER_H
#define BUFFER_H
#include "utils.h"
struct erow {
char *chars;
int n_chars;
@ -18,6 +20,7 @@ struct buffer {
struct buffer *buffer_create(void);
void buffer_read_file(struct buffer *buffer, const char *filename);
void buffer_append_row(struct buffer *buffer, const char *chars, int n_chars);
ERRCODE buffer_write_file(struct buffer *buffer);
void erow_update_rendering(struct erow *erow);
void erow_insert_char(struct erow *erow, int at, char c);

View file

@ -5,5 +5,6 @@
void command_move_cursor(KEY key);
void command_insert_char(char c);
void command_save_buffer(void);
#endif // COMMANDS_H

View file

@ -5,6 +5,7 @@
enum KEYS {
TAB = 9,
BACKSPACE = 127,
HOME = 0x100,
DEL,
PG_UP,

View file

@ -1,7 +1,9 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "buffer.h"
#include "kilo.h"
@ -19,6 +21,7 @@ struct buffer *buffer_create(void) {
return buf;
}
// TODO: Handle file not existing
void buffer_read_file(struct buffer *buffer, const char *filename) {
if (buffer->filename)
free(buffer->filename);
@ -56,6 +59,33 @@ void buffer_read_file(struct buffer *buffer, const char *filename) {
fclose(file);
}
ERRCODE buffer_write_file(struct buffer *buffer) {
if (buffer->filename == NULL)
return -1;
size_t len = 0;
for (int i = 0; i < buffer->n_rows; i++)
len += buffer->rows[i].n_chars + 1;
char *write_buffer = malloc(len);
char *p = write_buffer;
for (int i = 0; i < buffer->n_rows; i++) {
struct erow *row = buffer->rows+i;
memcpy(p, row->chars, row->n_chars);
p += row->n_chars;
*p = '\n';
p++;
}
int fd = open(buffer->filename, O_RDWR | O_TRUNC | O_CREAT, 0644);
write(fd, write_buffer, len);
close(fd);
free(write_buffer);
return 0;
}
void buffer_append_row(struct buffer *buffer, const char *chars, int n_chars) {
buffer->rows = realloc(buffer->rows, sizeof(struct erow) * (buffer->n_rows + 1));
struct erow *new_row = &buffer->rows[buffer->n_rows++];

View file

@ -4,6 +4,7 @@
#include "input.h"
#include "kilo.h"
#include "buffer.h"
#include "utils.h"
static void cursor_adjust_viewport(void);
static void cursor_check_file_bounds(bool horizontal);
@ -63,6 +64,15 @@ void command_insert_char(char c) {
E.rx = erow_cx_to_rx(E.current_buf->rows+E.cy, E.cx);
}
void command_save_buffer(void) {
ERRCODE errcode = buffer_write_file(E.current_buf);
if (errcode == 0)
editor_set_message("Saved file %s", E.current_buf->filename);
else if (errcode == -1)
editor_set_message("Save failed, no filename");
}
static void cursor_check_file_bounds(bool horizontal) {
struct erow *rows = E.current_buf->rows;
int n_rows = E.current_buf->n_rows;

View file

@ -20,6 +20,10 @@ void input_process_key(void) {
command_move_cursor(c);
break;
case CTRL_KEY('S'):
command_save_buffer();
break;
case CTRL_KEY('Q'):
terminal_clear();
exit(0);
@ -28,9 +32,13 @@ void input_process_key(void) {
case '\r':
break;
case 127:
case DEL:
case BACKSPACE:
case CTRL_KEY('H'):
case DEL:
break;
case CTRL_KEY('L'):
// case NOP:
break;
default:

View file

@ -45,7 +45,7 @@ void editor_init(char *filename) {
if (filename)
buffer_read_file(E.current_buf, filename);
editor_set_message("Welcome to kilo. Press CTRL-Q to quit.");
editor_set_message("Welcome to kilo! | CTRL-Q: Quit | CTRL-S: SAVE");
terminal_clear();
}