s/xbm_read_file()/grab_file/()

This commit is contained in:
Johan Malm 2020-08-06 15:01:08 +01:00
parent 042c157378
commit 6627a47305
5 changed files with 51 additions and 22 deletions

31
src/common/grab-file.c Normal file
View file

@ -0,0 +1,31 @@
/*
* Read file into memory
*
* Copyright Johan Malm 2020
*/
#define _POSIX_C_SOURCE 200809L
#include "common/grab-file.h"
#include "common/buf.h"
#include <stdio.h>
char *grab_file(const char *filename)
{
char *line = NULL;
size_t len = 0;
FILE *stream = fopen(filename, "r");
if (!stream)
return NULL;
struct buf buffer;
buf_init(&buffer);
while ((getline(&line, &len, stream) != -1)) {
char *p = strrchr(line, '\n');
if (p)
*p = '\0';
buf_add(&buffer, line);
}
free(line);
fclose(stream);
return (buffer.buf);
}

View file

@ -1,5 +1,6 @@
labwc_sources += files(
'buf.c',
'font.c',
'grab-file.c',
'spawn.c',
)