refactored load_file

This commit is contained in:
Mazar Farran 2015-08-28 16:22:21 -07:00
parent f83099492a
commit a5e23c3a1d
3 changed files with 26 additions and 4 deletions

7
line.c
View File

@ -4,8 +4,10 @@ void init_line(LINE *s)
{ {
s->size = LINE_SIZE; s->size = LINE_SIZE;
s->line = (char *)malloc(LINE_SIZE * sizeof(char)); s->line = (char *)malloc(LINE_SIZE * sizeof(char));
s->line[0] = '\0';
} // init_line } // init_line
// Insert char into string. // Insert char into string.
void insert_char(LINE *s, char c, int index) void insert_char(LINE *s, char c, int index)
{ {
@ -41,3 +43,8 @@ void expand(LINE *s)
s->line = temp; s->line = temp;
s->size = new_size; s->size = new_size;
} // expand } // expand
void add_char(LINE *s, char c)
{
insert_char(s, c, strlen(s->line));
}

1
line.h
View File

@ -19,5 +19,6 @@ void init_line(LINE *s);
void insert_char(LINE *s, char c, int index); // inserts char to string void insert_char(LINE *s, char c, int index); // inserts char to string
void remove_char(LINE *s, int index); void remove_char(LINE *s, int index);
void expand(LINE *s); // doubles the size of the line void expand(LINE *s); // doubles the size of the line
void add_char(LINE *s, char c); // add to end of line
#endif #endif

20
text.c
View File

@ -223,17 +223,31 @@ void load_file(int argc, char **argv, PAGE *p)
FILE *fp = fopen(argv[1], "r"); FILE *fp = fopen(argv[1], "r");
int size = count_lines(argc, argv) * 2; int size = count_lines(argc, argv) * 2;
char ch = '\0'; char ch = '\0';
int line, col; int line;//, col;
init_page(p, argv[1], size); 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] = ch;
} }
p->text[line].line[col] = '\0'; p->text[line].line[col] = '\0';
p->numlines++; 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); fclose(fp);