65 "Could not allocate memory for new FileBuffer structure\n");
70 fbuf->
full_path = calloc(PATH_MAX,
sizeof(
char));
74 "Could not allocate memory for new FileBuffer structure\n");
80 fbuf->
stats = calloc(1,
sizeof(
struct stat));
84 "Could not allocate memory for new FileBuffer structure\n");
150 const char *full_path,
158 if (data) *data = (
char *)NULL;
165 if (fbuf->
data) fbuf->
data[0] =
'\0';
166 if (fbuf->
lines) fbuf->
lines[0] = (
char *)NULL;
170 strncpy((
char *)fbuf->
full_path, full_path, PATH_MAX);
174 if (stat(full_path, fbuf->
stats) != 0) {
177 "Could not read file: %s\n"
179 full_path, strerror(errno));
186 length = fbuf->
stats->st_size;
193 new_data = realloc(fbuf->
data, (length + 1) *
sizeof(
char));
197 "Could not read file: %s\n"
198 " -> memory allocation error",
205 fbuf->
data = new_data;
210 fp = fopen(full_path,
"r");
214 "Could not open file: %s\n"
216 full_path, strerror(errno));
221 nread = fread(fbuf->
data, 1, length, fp);
226 "Could not read file: %s\n"
228 full_path, strerror(errno));
236 if (nread != length) {
239 "Could not read file: %s\n"
240 " -> number of bytes read (%d) != file size %d bytes\n",
241 full_path, nread, length);
249 if (data) *data = fbuf->
data;
292 if (nlines) *nlines = fbuf->
nlines;
293 if (lines) *lines = fbuf->
lines;
298 if (nlines) *nlines = 0;
299 if (lines) *lines = (
char **)NULL;
306 while( (chrp = strchr(chrp,
'\n')) ) {
315 new_lines = realloc(fbuf->
lines, (count+1) *
sizeof(
char *));
319 "Could not allocate memory for FileBuffer line pointers\n");
324 fbuf->
lines = new_lines;
333 while( (chrp = strchr(chrp,
'\n')) ) {
340 if (nlines) *nlines = fbuf->
nlines;
341 if (lines) *lines = fbuf->
lines;