| 1 |
james |
16 |
/* |
| 2 |
|
|
* messages.c |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#include "gui.h" |
| 6 |
|
|
|
| 7 |
|
|
|
| 8 |
|
|
/* |
| 9 |
|
|
* gui_messages_read |
| 10 |
|
|
*/ |
| 11 |
|
|
|
| 12 |
|
|
char **gui_messages_read(const char *file_name) |
| 13 |
|
|
{ |
| 14 |
|
|
char *data; |
| 15 |
|
|
char **table; |
| 16 |
|
|
unsigned int size = 0, count = 0, pos = 0, i, len; |
| 17 |
|
|
int c; |
| 18 |
|
|
FILE *file; |
| 19 |
|
|
|
| 20 |
|
|
file = fopen(file_name, "rb"); |
| 21 |
|
|
if (file == NULL) gui_raise_error("unable to open file"); |
| 22 |
|
|
|
| 23 |
|
|
while ((c = fgetc(file)) != EOF) |
| 24 |
|
|
{ |
| 25 |
|
|
size++; |
| 26 |
|
|
if (c == '\n') count++; |
| 27 |
|
|
} |
| 28 |
|
|
size++; |
| 29 |
|
|
|
| 30 |
|
|
data = malloc(size); |
| 31 |
|
|
if (data == NULL) gui_raise_error("out of memory"); |
| 32 |
|
|
table = calloc(count, sizeof(char *)); |
| 33 |
|
|
if (table == NULL) gui_raise_error("out of memory"); |
| 34 |
|
|
|
| 35 |
|
|
if (fseek(file, 0, SEEK_SET)) gui_raise_error("file seek failed"); |
| 36 |
|
|
|
| 37 |
|
|
for (i = 0; i < count; i++) |
| 38 |
|
|
{ |
| 39 |
|
|
if (!fgets(data + pos, size - pos, file)) gui_raise_error("fgets failed"); |
| 40 |
|
|
len = strlen(data + pos); |
| 41 |
|
|
data[pos + len - 1] = '\0'; |
| 42 |
|
|
table[i] = data + pos; |
| 43 |
|
|
pos += len; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
|
fclose(file); |
| 47 |
|
|
|
| 48 |
|
|
return table; |
| 49 |
|
|
} |