| 37 |
#include "file.h" |
#include "file.h" |
| 38 |
|
|
| 39 |
/* maximum length of lines -> buffer maybe use $COLS */ |
/* maximum length of lines -> buffer maybe use $COLS */ |
| 40 |
#define LENGTH 80 |
#define LENGTH 78 |
| 41 |
|
|
| 42 |
/* the first line of all */ |
/* the first line of all */ |
| 43 |
contentPtr firstLine = NULL; |
contentPtr first_line = NULL; |
| 44 |
contentPtr next = NULL; |
contentPtr next = NULL; |
| 45 |
|
contentPtr last_line = NULL; |
| 46 |
|
|
| 47 |
contentPtr |
contentPtr |
| 48 |
readFile(char *filename) |
read_file(char *filename) |
| 49 |
{ |
{ |
| 50 |
/* variables */ |
/* variables */ |
|
int lines = 0; |
|
| 51 |
char buf[LENGTH + 1]; |
char buf[LENGTH + 1]; |
| 52 |
FILE* stream; |
FILE* stream; |
| 53 |
contentPtr new = NULL; |
contentPtr new = NULL, tmp = NULL; |
| 54 |
|
|
| 55 |
/* is there a file, which can be opened? */ |
/* is there a file, which can be opened? */ |
| 56 |
if ((stream = fopen(filename, "r")) == NULL) |
if ((stream = fopen(filename, "r")) == NULL) |
| 57 |
{ |
{ |
| 58 |
printf("Argh, no file found! Returning.\n"); |
printf("Argh, no file found! Returning.\n"); |
|
return 0; |
|
|
} |
|
|
|
|
|
/* does the first one already exist? */ |
|
|
if ((firstLine = (content *) malloc (sizeof (content))) == NULL) |
|
|
{ |
|
|
printf ("Memory could not be allocated for first element - exiting...\n"); |
|
| 59 |
return NULL; |
return NULL; |
| 60 |
} |
} |
| 61 |
|
|
| 64 |
/* get LENGTH chars and put them in buf */ |
/* get LENGTH chars and put them in buf */ |
| 65 |
fgets (buf, LENGTH, stream); |
fgets (buf, LENGTH, stream); |
| 66 |
|
|
| 67 |
/* now we have to store it in a _first_ struct for display */ |
/* does the first one already exist? */ |
| 68 |
if ( lines == 0 ) |
if (first_line == NULL) |
| 69 |
{ |
{ |
| 70 |
firstLine->lineNumber = lines; |
if ((first_line = (content *) malloc (sizeof (content))) == NULL) |
| 71 |
firstLine->text = (char *) malloc (sizeof (buf)); |
{ |
| 72 |
strncpy (firstLine->text, buf, strlen (buf)); |
printf ("Memory could not be allocated for first element - exiting...\n"); |
| 73 |
|
return NULL; |
| 74 |
|
} |
| 75 |
|
/* now we have to store it in a _first_ struct for display */ |
| 76 |
|
first_line->line_number = 0; |
| 77 |
|
first_line->text = (char *) malloc (sizeof (buf)); |
| 78 |
|
strncpy (first_line->text, buf, strlen (buf)); |
| 79 |
|
|
| 80 |
firstLine->next = NULL; |
first_line->next = NULL; |
| 81 |
|
last_line = first_line; |
| 82 |
|
last_line->prev = NULL; |
| 83 |
} |
} |
| 84 |
else |
else |
| 85 |
{ |
{ |
| 86 |
/* fun with linking the list :-) */ |
/* fun with linking the list :-) */ |
| 87 |
new = firstLine; |
new = first_line; |
| 88 |
/* jump to th last */ |
/* jump to th last */ |
| 89 |
while (new->next != NULL) |
while (new->next != NULL) |
| 90 |
{ |
{ |
| 99 |
return NULL; |
return NULL; |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
|
tmp = new; |
| 103 |
|
|
| 104 |
/* ok, got it, now fill */ |
/* ok, got it, now fill */ |
| 105 |
new = new->next; |
new = new->next; |
| 106 |
|
new->line_number = tmp->line_number + 1; |
| 107 |
new->text = (char *) malloc (sizeof (buf)); |
new->text = (char *) malloc (sizeof (buf)); |
| 108 |
strncpy (new->text, buf, strlen (buf)); |
strncpy (new->text, buf, strlen (buf)); |
|
new->lineNumber = lines; |
|
| 109 |
|
|
| 110 |
new->next = NULL; |
new->next = NULL; |
| 111 |
|
last_line = new; |
| 112 |
|
new->prev = tmp; |
| 113 |
} |
} |
|
lines++; |
|
| 114 |
} |
} |
| 115 |
return firstLine; |
return first_line; |
| 116 |
} |
} |