Compare commits

..

3 Commits

Author SHA1 Message Date
bx b7a4fec162 changed command to take char * and char ** 2022-04-03 09:05:25 +01:00
bx de01f1cf91 moved buffer function into buffer.c 2022-04-03 08:15:22 +01:00
bx 338bda9c80 moved file commmands to c_file.c 2022-04-03 08:12:16 +01:00
17 changed files with 92 additions and 82 deletions

View File

@ -1,8 +0,0 @@
Row *
b_getline(Row *r, int l) {
if (l == 1) return r;
if (r->n == NULL) return NULL;
return b_getline(r->n, l - 1);
}

View File

@ -1,8 +0,0 @@
Row *
b_insert(Row *r) {
Row *new = calloc(1, sizeof(Row));
new->n = r->n;
new->p = r;
r->n = new;
return new;
}

View File

@ -1,10 +0,0 @@
void
b_truncate(Row *row) {
Row *n = row->n;
row->n = NULL;
if (n != NULL) {
b_truncate(n);
free(n);
}
}

27
buffer.c 100644
View File

@ -0,0 +1,27 @@
Row *
b_getline(Row *r, int l) {
if (l == 1) return r;
if (r->n == NULL) return NULL;
return b_getline(r->n, l - 1);
}
Row *
b_insert(Row *r) {
Row *new = calloc(1, sizeof(Row));
new->n = r->n;
new->p = r;
r->n = new;
return new;
}
void
b_truncate(Row *row) {
Row *n = row->n;
row->n = NULL;
if (n != NULL) {
b_truncate(n);
free(n);
}
}

View File

@ -1,5 +1,5 @@
void void
c_copy(char *c) { c_copy(char *c, char **argv) {
c += 2; c += 2;
int start = atoi(c); int start = atoi(c);
for(; *c != '\0' && *c != ' '; c++); for(; *c != '\0' && *c != ' '; c++);
@ -23,7 +23,7 @@ c_copy(char *c) {
} }
void void
c_paste(char *c) { c_paste(char *c, char **argv) {
Row *line = b_getline(buffer, atoi(c + 2)); Row *line = b_getline(buffer, atoi(c + 2));
if (line == NULL) {puts("invalid line."); return; } if (line == NULL) {puts("invalid line."); return; }
if (clipboard == NULL) {puts("empty clipboard"); return; } if (clipboard == NULL) {puts("empty clipboard"); return; }
@ -33,3 +33,4 @@ c_paste(char *c) {
} }
} }

View File

@ -1,5 +1,5 @@
void void
c_delete(char *c) { c_delete(char *c, char **argv) {
Row *line = b_getline(buffer, atoi(c + 2)); Row *line = b_getline(buffer, atoi(c + 2));
Row *prev_line = b_getline(buffer, atoi(c + 2) - 1); Row *prev_line = b_getline(buffer, atoi(c + 2) - 1);
if (line == NULL || prev_line == NULL) {puts ("invalid line"); return; } if (line == NULL || prev_line == NULL) {puts ("invalid line"); return; }
@ -10,3 +10,4 @@ c_delete(char *c) {
} }

View File

@ -1,5 +1,26 @@
void void
c_save(char *file) { c_open(char *file, char **argv) {
FILE *f = fopen(file + 2, "r");
if(f == NULL) { puts("couldnt open file."); return; }
b_truncate(buffer);
Row *row = buffer;
while (1) {
size_t red = 0;
for (int i = 0; i < ROW_SIZE; i++) {
red = fread(&row->text[i], 1, 1, f);
if (red != 1) return;
if (row->text[i] == '\n') {
row->text[i] = 0;
break;
}
}
row = b_insert(row);
}
fclose(f);
}
void
c_save(char *file, char **argv) {
FILE *f = fopen(file + 2, "w"); FILE *f = fopen(file + 2, "w");
if(f == NULL) { puts("couldnt open file."); return; } if(f == NULL) { puts("couldnt open file."); return; }
Row *row = buffer; Row *row = buffer;
@ -15,3 +36,5 @@ c_save(char *file) {
} }
} }

View File

@ -1,5 +1,5 @@
void void
c_indent(char *c) { c_indent(char *c, char **argv) {
Row *row = b_getline(buffer, atoi(c + 2)); Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; } if (row == NULL) { puts ("invalid line"); return; }
@ -11,7 +11,7 @@ c_indent(char *c) {
} }
void void
c_unindent(char *c) { c_unindent(char *c, char **argv) {
Row *row = b_getline(buffer, atoi(c + 2)); Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; } if (row == NULL) { puts ("invalid line"); return; }
@ -22,3 +22,4 @@ c_unindent(char *c) {
} }

View File

@ -1,5 +1,5 @@
void void
c_insertline(char *c) { c_insertline(char *c, char **argv) {
c += 2; c += 2;
int line = atoi(c); int line = atoi(c);
for(; *c != 0 && *c != ' '; c++); for(; *c != 0 && *c != ' '; c++);
@ -20,3 +20,4 @@ c_insertline(char *c) {
r->text[i] = 0; r->text[i] = 0;
} }
} }

View File

@ -1,21 +0,0 @@
void
c_open(char *file) {
FILE *f = fopen(file + 2, "r");
if(f == NULL) { puts("couldnt open file."); return; }
b_truncate(buffer);
Row *row = buffer;
while (1) {
size_t red = 0;
for (int i = 0; i < ROW_SIZE; i++) {
red = fread(&row->text[i], 1, 1, f);
if (red != 1) return;
if (row->text[i] == '\n') {
row->text[i] = 0;
break;
}
}
row = b_insert(row);
}
fclose(f);
}

View File

@ -8,9 +8,12 @@ else printf("%c", row->text[i]);
} }
void void
c_print(char *c) { c_print(char *c, char **argv) {
Row *row = buffer; Row *row = buffer;
int line = 1; int line = 1;
if (atoi(c + 2) != 0) line = atoi(c + 2);
row = b_getline(buffer, line);
if (row == NULL) {puts("invalid line."); return;}
for (;; line++) { for (;; line++) {
printf("%3.i| ", line); printf("%3.i| ", line);
print_with_fixed_tabs(row); print_with_fixed_tabs(row);
@ -24,3 +27,5 @@ puts("");

View File

@ -1,5 +1,6 @@
void void
c_quit(char *b) { c_quit(char *b, char **argv) {
exit(0); exit(0);
} }

View File

@ -1,5 +1,5 @@
void void
c_writeline(char *c) { c_writeline(char *c, char **argv) {
c += 2; c += 2;
int line = atoi(c); int line = atoi(c);
for(; *c != 0 && *c != ' '; c++); for(; *c != 0 && *c != ' '; c++);
@ -17,3 +17,4 @@ c_writeline(char *c) {
r->text[i] = 0; r->text[i] = 0;
} }
} }

View File

@ -1,4 +1,4 @@
void (*commands[256])(char *) = { void (*commands[256])(char *, char **) = {
NULL, NULL,
['O'] = c_open, ['O'] = c_open,
['w'] = c_writeline, ['w'] = c_writeline,
@ -15,3 +15,4 @@ NULL,

11
efn.c
View File

@ -9,15 +9,14 @@
#include "main.c" #include "main.c"
#include "c_quit.c" #include "c_quit.c"
#include "c_open.c"
#include "b_truncate.c" #include "c_file.c"
#include "buffer.c"
#include "c_print.c" #include "c_print.c"
#include "b_insert.c"
#include "c_writeline.c" #include "c_writeline.c"
#include "b_getline.c"
#include "c_save.c"
#include "c_insertline.c" #include "c_insertline.c"
#include "c_indent.c" #include "c_indent.c"
#include "c_delete.c" #include "c_delete.c"
@ -26,3 +25,5 @@
#include "commands.c" #include "commands.c"

14
efn.h
View File

@ -1,22 +1,18 @@
#include <stdlib.h> #include <stdlib.h>
#include "globals.h" #include "globals.h"
/*
extern void (*commands[256])(char *); extern void (*commands[256])(char *);
*/
void c_quit(char *);
void c_open(char *);
#include <string.h> #include <string.h>
void b_truncate(Row *); void b_truncate(Row *);
void c_print(char *);
Row *b_insert(Row *); Row *b_insert(Row *);
Row *b_getline(Row *, int); Row *b_getline(Row *, int);
void c_writeline(char *);
void c_save(char*); extern void (*commands[256])(char *, char **);
void c_insertline(char *);
void c_indent(char *);
void c_unindent(char *);
void c_delete(char *);

10
main.c
View File

@ -1,4 +1,4 @@
void char **
parse_command(char *input) { parse_command(char *input) {
static char com[COM_BUF_SIZE]; static char com[COM_BUF_SIZE];
memcpy(com, input, COM_BUF_SIZE); memcpy(com, input, COM_BUF_SIZE);
@ -15,10 +15,7 @@ parse_command(char *input) {
com[i] = '\0'; com[i] = '\0';
next_start++; next_start++;
} }
return &starts[0];
for (int i = 0; starts[i] != NULL; i++) {
puts(starts[i]);
}
} }
int int
@ -32,10 +29,11 @@ main(int argc, char **argv) {
if (buf[i] == '\n') if (buf[i] == '\n')
buf[i] = '\0'; buf[i] = '\0';
if (com != NULL) { if (com != NULL) {
if (commands[com[0]] != NULL) commands[com[0]](buf); if (commands[com[0]] != NULL) commands[com[0]](buf, parse_command(com));
else puts("invalid command."); else puts("invalid command.");
} }
} }
return 0; return 0;
} }