Explorar el Código

Adding keybind to save file

Vinicius Teshima hace 1 año
padre
commit
5c7ba5be16
Se han modificado 6 ficheros con 72 adiciones y 9 borrados
  1. 33 3
      src/buffer.h
  2. 7 1
      src/config.h
  3. 6 0
      src/file.h
  4. 4 2
      src/font.h
  5. 10 0
      src/keybind.h
  6. 12 3
      src/main.c

+ 33 - 3
src/buffer.h

@@ -6,6 +6,7 @@
 struct buffer {
 	DA_DEF_STRUCT_ITEM(char, data);
 	size_t cur;
+	const char *filepath;
 };
 
 enum buffer_err {
@@ -16,6 +17,8 @@ enum buffer_err {
 	BUFFER_ERR_FAIL_READ_FILE,
 	BUFFER_ERR_START_GT_END,
 	BUFFER_ERR_INVALID_END_SIZE,
+	BUFFER_ERR_FAIL_GET_REALPATH,
+	BUFFER_ERR_FAIL_SAVE,
 };
 
 struct ret_buffer_err {
@@ -39,6 +42,7 @@ void buffer_destroy(struct buffer buf);
 
 struct ret_buffer_err buffer_load_from_file(struct buffer buf,
 					    const char *filepath);
+enum buffer_err buffer_save(struct buffer buf);
 enum buffer_err buffer_save_to_file(struct buffer buf, const char *filepath);
 
 struct buffer buffer_mv_cur_left(struct buffer buf);
@@ -81,6 +85,9 @@ void
 buffer_destroy(struct buffer buf)
 {
 	free(buf.data.items);
+	if ( buf.filepath != NULL ) {
+		free((char *) buf.filepath);
+	}
 }
 
 struct ret_buffer_err
@@ -90,10 +97,23 @@ buffer_load_from_file(struct buffer buf, const char *filepath)
 	void *ptr;
 	size_t file_size;
 	enum file_err err;
+	char *fp = realpath(filepath, NULL);
+	if ( fp == NULL ) {
+		return (struct ret_buffer_err) {
+			.f1 = buf,
+			.f2 = BUFFER_ERR_FAIL_GET_REALPATH
+		};
+	}
+
 	RET_UNWRAP3(ptr, file_size, err,
-		    struct ret_void_p_err, file_read_all(filepath));
+		    struct ret_void_p_err, file_read_all(fp));
 
 	if ( err != FILE_ERR_OK ) {
+		if ( err == FILE_ERR_EMPTY ) {
+			buf.filepath = fp;
+			goto exit;
+		}
+		free(fp);
 		return (struct ret_buffer_err) {
 			.f1 = buf,
 			.f2 = BUFFER_ERR_FAIL_READ_FILE
@@ -102,21 +122,31 @@ buffer_load_from_file(struct buffer buf, const char *filepath)
 
 	buffer_destroy(buf);
 
+	buf.filepath = fp;
 	buf.data.items = ptr;
 	buf.data.size = file_size - 1;
 	buf.data.cap = file_size;
 
+exit: ;
 	return (struct ret_buffer_err) {
 		.f1 = buf,
 		.f2 = BUFFER_ERR_OK
 	};
 }
 
+enum buffer_err
+buffer_save(struct buffer buf)
+{
+	return buffer_save_to_file(buf, buf.filepath);
+}
+
 enum buffer_err
 buffer_save_to_file(struct buffer buf, const char *filepath)
 {
-	(void) buf;
-	(void) filepath;
+	enum file_err err = file_save(filepath, buf.data.items, buf.data.size);
+	if ( err != FILE_ERR_OK ) {
+		return BUFFER_ERR_FAIL_SAVE;
+	}
 
 	return BUFFER_ERR_OK;
 }

+ 7 - 1
src/config.h

@@ -3,7 +3,7 @@
 
 #include "keybind.h"
 
-#define KEYBINDS_MAX_SIZE 50
+#define KEYBINDS_MAX_SIZE 1024
 #define KMS KEYBINDS_MAX_SIZE
 static struct keybinds keybinds[KMS] = {
 	[SDLK_LEFT % KMS] = {
@@ -66,6 +66,12 @@ static struct keybinds keybinds[KMS] = {
 		.binds = {
 			{KMOD_CTRL, keybind_mv_cur_end_line, {.ptr = NULL}}
 		}
+	},
+	[SDLK_s % KMS] = {
+		.size = 1,
+		.binds = {
+			{KMOD_CTRL, keybind_save_buffer, {.ptr = NULL}}
+		}
 	}
 };
 

+ 6 - 0
src/file.h

@@ -18,6 +18,7 @@ enum file_err {
 	FILE_ERR_FAIL_CLOSE,
 	FILE_ERR_FILE_EMPTY,
 	FILE_ERR_FAIL_WROTE_MORE,
+	FILE_ERR_EMPTY,
 };
 
 struct ret_void_p_err {
@@ -53,6 +54,11 @@ file_read_all(const char *filepath)
 	lseek(fd, 0, SEEK_SET);
 	printf("file->size = %ld\n", file_size);
 
+	if ( file_size == 0 ) {
+		err = FILE_ERR_EMPTY;
+		goto err_close;
+	}
+
 	const size_t buf_size = ((size_t)file_size) + 1;
 	uint8_t *buf = calloc(sizeof(uint8_t), buf_size);
 	if ( buf == NULL ) {

+ 4 - 2
src/font.h

@@ -9,6 +9,7 @@ struct font {
 	SDL_Surface *sur;
 	SDL_Texture *tex;
 	SDL_Rect *glyphs;
+	size_t glyphs_num;
 	double scale;
 	uint32_t w;
 	uint32_t h;
@@ -47,8 +48,9 @@ font_create(SDL_Renderer *rdr, const char *filepath,
 	f.ch_w = (uint32_t) (f.w / f.cols);
 	f.ch_h = (uint32_t) (f.h / f.rows);
 
-	f.glyphs = calloc(sizeof(SDL_Rect), 128);
-	for ( size_t i = 0; i < 128; ++i ) {
+	f.glyphs_num = 128;
+	f.glyphs = calloc(sizeof(SDL_Rect), f.glyphs_num);
+	for ( size_t i = 0; i < f.glyphs_num; ++i ) {
 		const size_t col = i % f.cols;
 		const size_t row = i / f.cols;
 

+ 10 - 0
src/keybind.h

@@ -29,6 +29,8 @@ struct keybinds {
 	const struct bind binds[BINDS_SIZE];
 };
 
+struct app keybind_save_buffer(struct app app, union keybind_args args);
+
 struct app keybind_mv_cur_char(struct app app, union keybind_args args);
 struct app keybind_forward_char(struct app app, union keybind_args args);
 struct app keybind_delete_char(struct app app, union keybind_args args);
@@ -44,6 +46,14 @@ struct app keybind_insert_newline(struct app app, union keybind_args args);
 
 #if defined(KEYBIND_IMP) || defined(IMP)
 
+struct app
+keybind_save_buffer(struct app app, union keybind_args args)
+{
+	(void) args;
+	buffer_save(app.buf);
+	return app;
+}
+
 struct app
 keybind_mv_cur_char(struct app app, union keybind_args args)
 {

+ 12 - 3
src/main.c

@@ -31,7 +31,11 @@ struct app handle_events(struct app app);
 int32_t
 main(int32_t argc, char **argv)
 {
-	(void) argc; (void) argv;
+	if ( argc != 2 ) {
+		fprintf(stderr, "MUST pass file to open\n");
+		exit(EXIT_FAILURE);
+	}
+	/* (void) argc; (void) argv; */
 
 	SCE(SDL_Init(SDL_INIT_VIDEO));
 
@@ -42,11 +46,16 @@ main(int32_t argc, char **argv)
 	app.font = font_create(app.rdr, font_path, 7, 18);
 	app.font.scale = 2.0;
 
-	const char *filepath = "./src/main.c";
+	/* const char *filepath = "./src/main.c"; */
 	enum buffer_err err;
 	RET_UNWRAP2(app.buf, err,
 		    struct ret_buffer_err,
-		    buffer_load_from_file(app.buf, filepath));
+		    buffer_load_from_file(app.buf, argv[1]));
+	if ( err != BUFFER_ERR_OK ) {
+		printf("%d\n", err);
+		fprintf(stderr, "Failed to open buffer\n");
+		exit(EXIT_FAILURE);
+	}
 	(void) err;
 
 	uint64_t i = 0;