Compare commits

..

No commits in common. "b7a4fec16253177a95db956f1a39305ffc657913" and "8f917f058c459d2470a43a3ad40c2855080ba69a" have entirely different histories.

17 changed files with 82 additions and 92 deletions

8
b_getline.c 100644
View File

@ -0,0 +1,8 @@
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);
}

8
b_insert.c 100644
View File

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

10
b_truncate.c 100644
View File

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

View File

@ -1,27 +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);
}
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, char **argv) { c_copy(char *c) {
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, char **argv) {
} }
void void
c_paste(char *c, char **argv) { c_paste(char *c) {
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,4 +33,3 @@ c_paste(char *c, char **argv) {
} }
} }

View File

@ -1,5 +1,5 @@
void void
c_delete(char *c, char **argv) { c_delete(char *c) {
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,4 +10,3 @@ c_delete(char *c, char **argv) {
} }

View File

@ -1,5 +1,5 @@
void void
c_indent(char *c, char **argv) { c_indent(char *c) {
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, char **argv) {
} }
void void
c_unindent(char *c, char **argv) { c_unindent(char *c) {
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,4 +22,3 @@ c_unindent(char *c, char **argv) {
} }

View File

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

21
c_open.c 100644
View File

@ -0,0 +1,21 @@
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,16 +8,13 @@ else printf("%c", row->text[i]);
} }
void void
c_print(char *c, char **argv) { c_print(char *c) {
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);
puts(""); puts("");
if (row->n == NULL) break; if (row->n == NULL) break;
row = row -> n; row = row -> n;
} }
@ -27,5 +24,3 @@ c_print(char *c, char **argv) {

View File

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

View File

@ -1,26 +1,5 @@
void void
c_open(char *file, char **argv) { c_save(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);
}
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;
@ -36,5 +15,3 @@ c_save(char *file, char **argv) {
} }
} }

View File

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

View File

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

11
efn.c
View File

@ -9,14 +9,15 @@
#include "main.c" #include "main.c"
#include "c_quit.c" #include "c_quit.c"
#include "c_open.c"
#include "c_file.c" #include "b_truncate.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"
@ -25,5 +26,3 @@
#include "commands.c" #include "commands.c"

14
efn.h
View File

@ -1,18 +1,22 @@
#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 *);
extern void (*commands[256])(char *, char **); void c_save(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 @@
char ** void
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,7 +15,10 @@ 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
@ -29,11 +32,10 @@ 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, parse_command(com)); if (commands[com[0]] != NULL) commands[com[0]](buf);
else puts("invalid command."); else puts("invalid command.");
} }
} }
return 0; return 0;
} }