refactored load_file
This commit is contained in:
parent
f83099492a
commit
a5e23c3a1d
7
line.c
7
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));
|
||||
}
|
||||
|
1
line.h
1
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
|
||||
|
20
text.c
20
text.c
@ -223,17 +223,31 @@ 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user