Fix for a segfault when calling cybergfx.library/ExtractColor()
[cake.git] / tools / genmodule / fileread.c
blobe8a8830efc2d8dd569c322efb5ab9be31ee90650
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
4 Desc: The functions to read lines from a file
5 */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
11 static char *line = NULL; /* The current read file */
12 static char *filename = NULL; /* The name of the opened file */
13 static FILE *file = NULL; /* The opened file */
14 static unsigned int slen = 0; /* The allocation length pointed to be line */
15 static unsigned int lineno = 0; /* The line number, will be increased by one everytime a line is read */
17 int fileopen(const char *fname)
19 if (file!=NULL)
21 fclose(file);
22 free(filename);
23 file = NULL;
24 filename = NULL;
25 lineno = 0;
27 file = fopen(fname, "r");
28 if (file!=NULL)
29 filename = strdup(fname);
31 return file!=NULL;
34 void fileclose(void)
36 if (file!=NULL)
38 fclose(file);
39 free(filename);
40 file = NULL;
41 filename = NULL;
45 char *readline(void)
47 char haseol;
49 if (file==NULL || feof(file))
50 return NULL;
52 if (slen==0)
54 slen = 256;
55 line = malloc(slen);
57 if (fgets(line, slen, file))
59 haseol = line[strlen(line)-1]=='\n';
60 if (haseol) line[strlen(line)-1]='\0';
62 while (!(haseol || feof(file)))
64 slen += 256;
65 line = (char *)realloc(line, slen);
66 fgets(line+strlen(line), slen, file);
67 haseol = line[strlen(line)-1]=='\n';
68 if (haseol) line[strlen(line)-1]='\0';
71 else
72 line[0]='\0';
73 lineno++;
75 return line;
78 void filewarning(const char *format, ...)
80 va_list ap;
82 fprintf(stderr, "%s:%d:warning ", filename, lineno);
84 va_start(ap, format);
85 vfprintf(stderr, format, ap);
86 va_end(ap);
89 void exitfileerror(int code, const char *format, ...)
91 va_list ap;
93 fprintf(stderr, "%s:%d:error ", filename, lineno);
95 va_start(ap, format);
96 vfprintf(stderr, format, ap);
97 va_end(ap);
99 exit(code);