From a5e23c3a1dfbb920a214d16e96a7e3c5a5372348 Mon Sep 17 00:00:00 2001 From: Mazar Farran Date: Fri, 28 Aug 2015 16:22:21 -0700 Subject: [PATCH] refactored load_file --- line.c | 7 +++++++ line.h | 1 + text.c | 22 ++++++++++++++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/line.c b/line.c index d9e1a37..a1acc7a 100644 --- a/line.c +++ b/line.c @@ -4,8 +4,10 @@ void init_line(LINE *s) { s->size = LINE_SIZE; s->line = (char *)malloc(LINE_SIZE * sizeof(char)); + s->line[0] = '\0'; } // init_line + // Insert char into string. void insert_char(LINE *s, char c, int index) { @@ -41,3 +43,8 @@ void expand(LINE *s) s->line = temp; s->size = new_size; } // expand + +void add_char(LINE *s, char c) +{ + insert_char(s, c, strlen(s->line)); +} diff --git a/line.h b/line.h index e0f7799..a45bbfe 100644 --- a/line.h +++ b/line.h @@ -19,5 +19,6 @@ void init_line(LINE *s); void insert_char(LINE *s, char c, int index); // inserts char to string void remove_char(LINE *s, int index); void expand(LINE *s); // doubles the size of the line +void add_char(LINE *s, char c); // add to end of line #endif diff --git a/text.c b/text.c index f91bf90..a8f6b29 100644 --- a/text.c +++ b/text.c @@ -223,18 +223,32 @@ void load_file(int argc, char **argv, PAGE *p) FILE *fp = fopen(argv[1], "r"); int size = count_lines(argc, argv) * 2; char ch = '\0'; - int line, col; + int line;//, col; init_page(p, argv[1], size); - for(line = 0; line < PAGE_SIZE && ch != EOF; line++) + /*for(line = 0; line < PAGE_SIZE && ch != EOF; line++) { - for(col = 0; col < LINE_SIZE - 1 && ((ch = fgetc(fp)) != '\n') && ch != EOF; col++) + for(col = 0; + col < LINE_SIZE - 1 && ((ch = fgetc(fp)) != '\n') && ch != EOF; + col++) { p->text[line].line[col] = ch; } p->text[line].line[col] = '\0'; p->numlines++; - } + }*/ + + for(line = 0; line < PAGE_SIZE && ch != EOF; line++) + { + ch = fgetc(fp); + while(ch != '\n' && ch != EOF) + { + LINE *currline = &(p->text[line]); + add_char(currline, ch); + ch = fgetc(fp); + } + p->numlines++; + } fclose(fp);