implements file reading
This commit is contained in:
parent
6f985888a0
commit
c699089e1d
11
src/Makefile
11
src/Makefile
@ -1,17 +1,18 @@
|
|||||||
TARGET=editor
|
TARGET=editor
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
OBJ = main.o term.o draw.o controls.o
|
OBJ = main.o term.o draw.o controls.o file.o
|
||||||
|
|
||||||
$(TARGET): $(OBJ)
|
$(TARGET): $(OBJ)
|
||||||
mkdir -p ../build
|
mkdir -p ../build
|
||||||
$(CC) -o $(TARGET) $(OBJ) -g
|
$(CC) -o $(TARGET) $(OBJ) -g
|
||||||
mv $(TARGET) ../build/
|
mv $(TARGET) ../build/
|
||||||
|
|
||||||
main.o: headers/term.h headers/draw.h headers/controls.h
|
main.o: include/term.h include/draw.h include/controls.h
|
||||||
term.o: headers/term.h
|
term.o: include/term.h
|
||||||
draw.o: headers/term.h headers/draw.h
|
draw.o: include/term.h include/draw.h
|
||||||
controls.o: headers/controls.h headers/__defines.h headers/draw.h headers/term.h
|
controls.o: include/controls.h include/__defines.h include/draw.h include/term.h
|
||||||
|
file.o: include/file.h include/term.h
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "headers/__defines.h"
|
#include "include/__defines.h"
|
||||||
#include "headers/controls.h"
|
#include "include/controls.h"
|
||||||
#include "headers/draw.h"
|
#include "include/draw.h"
|
||||||
#include "headers/term.h"
|
#include "include/term.h"
|
||||||
|
|
||||||
extern editor_t editor;
|
extern editor_t editor;
|
||||||
extern screen_buffer_t screen_buffer;
|
extern screen_buffer_t screen_buffer;
|
||||||
|
30
src/draw.c
30
src/draw.c
@ -2,8 +2,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "headers/draw.h"
|
#include "include/draw.h"
|
||||||
#include "headers/term.h"
|
#include "include/term.h"
|
||||||
|
|
||||||
#define MAX_LEN 256
|
#define MAX_LEN 256
|
||||||
|
|
||||||
@ -27,34 +27,44 @@ void refresh_screen(void)
|
|||||||
cur_y = editor.cy;
|
cur_y = editor.cy;
|
||||||
|
|
||||||
// hide the cursor to prevent flickering
|
// hide the cursor to prevent flickering
|
||||||
screen_buffer_append(&screen_buffer, HIDE_CURSOR_STR, HIDE_CURSOR_STR_LEN);
|
screen_buffer_append(HIDE_CURSOR_STR, HIDE_CURSOR_STR_LEN);
|
||||||
|
|
||||||
screen_buffer_append(&screen_buffer, MOVE_CURSOR_HOME_STR, MOVE_CURSOR_HOME_STR_LEN);
|
screen_buffer_append(MOVE_CURSOR_HOME_STR, MOVE_CURSOR_HOME_STR_LEN);
|
||||||
|
|
||||||
screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN);
|
screen_buffer_append(CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN);
|
||||||
|
|
||||||
|
draw_file_buffer();
|
||||||
|
|
||||||
draw_editor_rows();
|
draw_editor_rows();
|
||||||
draw_cursor_pos();
|
draw_cursor_pos();
|
||||||
|
|
||||||
move_cursor_pos(cur_x, cur_y);
|
move_cursor_pos(cur_x, cur_y);
|
||||||
|
|
||||||
screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN);
|
screen_buffer_append(SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN);
|
||||||
|
|
||||||
write(STDOUT_FILENO, screen_buffer.text, screen_buffer.len);
|
write(STDOUT_FILENO, screen_buffer.text, screen_buffer.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw_file_buffer(void)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < editor.num_rows; i++) {
|
||||||
|
screen_buffer_append(editor.rows[i].line, editor.rows[i].len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void draw_editor_rows(void)
|
void draw_editor_rows(void)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < editor.rows - 1; i++)
|
for (i = editor.num_rows; i < editor.screen_rows - 1; i++)
|
||||||
screen_buffer_append(&screen_buffer, "~\r\n", 3);
|
screen_buffer_append("~\r\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_cursor_pos(void)
|
void draw_cursor_pos(void)
|
||||||
{
|
{
|
||||||
char* pos = malloc(sizeof(char) * MAX_LEN);
|
char* pos = malloc(sizeof(char) * MAX_LEN);
|
||||||
snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy);
|
snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy);
|
||||||
screen_buffer_append(&screen_buffer, pos, strlen(pos));
|
screen_buffer_append(pos, strlen(pos));
|
||||||
free(pos);
|
free(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +73,7 @@ void move_cursor_pos(int x, int y)
|
|||||||
// TODO: add check for boundaries
|
// TODO: add check for boundaries
|
||||||
char* out = malloc(sizeof(char) * MAX_LEN);
|
char* out = malloc(sizeof(char) * MAX_LEN);
|
||||||
snprintf(out, MAX_LEN, "\e[%d;%dH", y, x);
|
snprintf(out, MAX_LEN, "\e[%d;%dH", y, x);
|
||||||
screen_buffer_append(&screen_buffer, out, strlen(out));
|
screen_buffer_append(out, strlen(out));
|
||||||
free(out);
|
free(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
src/file.c
Normal file
26
src/file.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/file.h"
|
||||||
|
#include "include/term.h"
|
||||||
|
|
||||||
|
#define MAX_LINE_LEN 1024
|
||||||
|
|
||||||
|
void read_file(const char* filename)
|
||||||
|
{
|
||||||
|
ssize_t linelen;
|
||||||
|
size_t n;
|
||||||
|
char* line = NULL;
|
||||||
|
|
||||||
|
FILE* fp = fopen(filename, "r");
|
||||||
|
if (NULL == fp) {
|
||||||
|
fprintf(stderr, "failed to open file for reading.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (-1 != (linelen = getline(&line, &n, fp))) {
|
||||||
|
editor_add_row(line, linelen);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
}
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
void refresh_screen(void);
|
void refresh_screen(void);
|
||||||
|
|
||||||
|
void draw_file_buffer(void);
|
||||||
void draw_editor_rows(void);
|
void draw_editor_rows(void);
|
||||||
void draw_cursor_pos(void);
|
void draw_cursor_pos(void);
|
||||||
|
|
6
src/include/file.h
Normal file
6
src/include/file.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef FILE_H
|
||||||
|
#define FILE_H
|
||||||
|
|
||||||
|
void read_file(const char* filename);
|
||||||
|
|
||||||
|
#endif
|
@ -9,10 +9,19 @@ typedef enum {
|
|||||||
INSERT_MODE,
|
INSERT_MODE,
|
||||||
} control_type_t;
|
} control_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* line;
|
||||||
|
size_t len;
|
||||||
|
} row_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct termios term_settings;
|
struct termios term_settings;
|
||||||
control_type_t control_type;
|
control_type_t control_type;
|
||||||
size_t cols, rows;
|
|
||||||
|
row_t* rows;
|
||||||
|
size_t num_rows;
|
||||||
|
|
||||||
|
size_t screen_cols, screen_rows;
|
||||||
int cx, cy; // cursor pos
|
int cx, cy; // cursor pos
|
||||||
} editor_t;
|
} editor_t;
|
||||||
|
|
||||||
@ -67,7 +76,6 @@ void kill_application(void);
|
|||||||
/**
|
/**
|
||||||
* # screen_buffer_append
|
* # screen_buffer_append
|
||||||
*
|
*
|
||||||
* - screen_buffer_t* buf : The screen buffer
|
|
||||||
* - const char* in : The input text
|
* - const char* in : The input text
|
||||||
* - size_t len : Length of the input text
|
* - size_t len : Length of the input text
|
||||||
*
|
*
|
||||||
@ -78,7 +86,7 @@ void kill_application(void);
|
|||||||
* Returns -1 on failure, 0 on success
|
* Returns -1 on failure, 0 on success
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int screen_buffer_append(screen_buffer_t* buf, const char* in, size_t len);
|
int screen_buffer_append(const char* in, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # screen_buffer_free
|
* # screen_buffer_free
|
||||||
@ -90,4 +98,14 @@ int screen_buffer_append(screen_buffer_t* buf, const char* in, size_t len);
|
|||||||
*/
|
*/
|
||||||
void screen_buffer_free(screen_buffer_t* buf);
|
void screen_buffer_free(screen_buffer_t* buf);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* # free_editor_row
|
||||||
|
*
|
||||||
|
* Frees all of the strings in each row
|
||||||
|
* and the row array
|
||||||
|
*/
|
||||||
|
void free_editor_row(void);
|
||||||
|
|
||||||
|
void editor_add_row(const char* line, size_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
19
src/main.c
19
src/main.c
@ -1,19 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "headers/controls.h"
|
#include "include/controls.h"
|
||||||
#include "headers/draw.h"
|
#include "include/draw.h"
|
||||||
#include "headers/term.h"
|
#include "include/file.h"
|
||||||
|
#include "include/term.h"
|
||||||
|
|
||||||
editor_t editor;
|
editor_t editor;
|
||||||
screen_buffer_t screen_buffer;
|
screen_buffer_t screen_buffer;
|
||||||
|
|
||||||
int main(void)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
char* in;
|
char* in;
|
||||||
|
|
||||||
|
if (argc > 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <filename>\n || Usage: %s", argv[0], argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
setup_terminal();
|
setup_terminal();
|
||||||
|
|
||||||
|
if (argc == 2)
|
||||||
|
read_file(argv[1]);
|
||||||
|
|
||||||
refresh_screen(); // want to draw first since we're going to be waiting on the read
|
refresh_screen(); // want to draw first since we're going to be waiting on the read
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
45
src/term.c
45
src/term.c
@ -6,7 +6,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
#include "headers/term.h"
|
#include "include/term.h"
|
||||||
|
|
||||||
/** BEGIN extern section **/
|
/** BEGIN extern section **/
|
||||||
|
|
||||||
@ -109,11 +109,14 @@ void setup_terminal(void)
|
|||||||
|
|
||||||
editor.control_type = COMMAND_MODE;
|
editor.control_type = COMMAND_MODE;
|
||||||
|
|
||||||
|
editor.num_rows = 0;
|
||||||
|
editor.rows = NULL;
|
||||||
|
|
||||||
editor.cx = 1;
|
editor.cx = 1;
|
||||||
editor.cy = 1;
|
editor.cy = 1;
|
||||||
|
|
||||||
editor.cols = win.ws_col;
|
editor.screen_cols = win.ws_col;
|
||||||
editor.rows = win.ws_row;
|
editor.screen_rows = win.ws_row;
|
||||||
}
|
}
|
||||||
|
|
||||||
void kill_application(void)
|
void kill_application(void)
|
||||||
@ -125,19 +128,21 @@ void kill_application(void)
|
|||||||
reset_input_mode();
|
reset_input_mode();
|
||||||
|
|
||||||
screen_buffer_free(&screen_buffer);
|
screen_buffer_free(&screen_buffer);
|
||||||
|
|
||||||
|
free_editor_row();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int screen_buffer_append(screen_buffer_t *buf, const char *in, size_t len)
|
int screen_buffer_append(const char *in, size_t len)
|
||||||
{
|
{
|
||||||
// Need to realloc to allow for the input to fit into the buffer
|
// Need to realloc to allow for the input to fit into the buffer
|
||||||
char* new = realloc(buf->text, buf->len + len);
|
char* new = realloc(screen_buffer.text, screen_buffer.len + len);
|
||||||
|
|
||||||
if (NULL == new) return -1;
|
if (NULL == new) return -1;
|
||||||
|
|
||||||
memcpy(&new[buf->len], in, len);
|
memcpy(&new[screen_buffer.len], in, len);
|
||||||
buf->text = new;
|
screen_buffer.text = new;
|
||||||
buf->len += len;
|
screen_buffer.len += len;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -146,3 +151,27 @@ void screen_buffer_free(screen_buffer_t *buf)
|
|||||||
{
|
{
|
||||||
free(buf->text);
|
free(buf->text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_editor_row(void)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < editor.num_rows; i++)
|
||||||
|
free(editor.rows[i].line);
|
||||||
|
free(editor.rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
void editor_add_row(const char* line, size_t len)
|
||||||
|
{
|
||||||
|
// Need to add space for another row
|
||||||
|
editor.rows = realloc(editor.rows, sizeof(row_t) * (editor.num_rows + 1));
|
||||||
|
|
||||||
|
// Set len and malloc space for the incoming line +1 for \0
|
||||||
|
size_t idx = editor.num_rows;
|
||||||
|
editor.rows[idx].len = len;
|
||||||
|
editor.rows[idx].line = malloc(sizeof(char) * (len + 1));
|
||||||
|
|
||||||
|
memcpy(editor.rows[idx].line, line, len);
|
||||||
|
editor.rows[idx].line[len] = '\0'; // need to null terminate it
|
||||||
|
|
||||||
|
editor.num_rows++;
|
||||||
|
}
|
||||||
|
5
src/test.txt
Normal file
5
src/test.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
hell othis
|
||||||
|
is a test of the
|
||||||
|
fiel reading
|
||||||
|
capabiliteis of my
|
||||||
|
edito
|
Loading…
x
Reference in New Issue
Block a user