add get_binary

This commit is contained in:
Evan Burkey 2023-05-03 14:26:58 -07:00
parent 9eb4b82f58
commit 594c1922bc
3 changed files with 53 additions and 2 deletions

6
docs.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
mkdocs build
ssh debian@fputs.com rm -rf /var/www/fputs.com/docs/libflint
ssh debian@fputs.com mkdir -p /var/www/fputs.com/docs/libflint
scp -r site/* debian@fputs.com:/var/www/fputs.com/docs/libflint/

View File

@ -4,6 +4,20 @@ I/O module to assist with consuming data from files
## Functions
### get_binary
Reads a file at `path` and returns the contents as a char* buffer. The buffer is allocated inside the function and
the user is responsible for freeing it when finished.
```c
char *get_binary(const char *path);
/* Usage */
char *buf = get_binary("/home/evan/binfile");
free(buf);
```
### get_input
Reads a file at `path` and returns the contents as a single string. The string is allocated inside the function and

View File

@ -11,7 +11,7 @@
#include "lfinput.h"
char *get_input(const char *path) {
static FILE* open_file(const char *path, size_t *fsz) {
FILE *fp = NULL;
fp = fopen(path, "r");
if (fp == NULL) {
@ -20,9 +20,40 @@ char *get_input(const char *path) {
}
fseek(fp, 0, SEEK_END);
size_t fsz = ftell(fp);
*fsz = ftell(fp);
rewind(fp);
return fp;
}
char *get_binary(const char *path) {
size_t fsz = 0;
FILE *fp = open_file(path, &fsz);
if (fp == NULL) {
return NULL;
}
char *buf = NULL;
buf = malloc(fsz);
if (buf == NULL) {
fprintf(stderr, "Failed to malloc buf. Returning NULL\n");
fclose(fp);
return NULL;
}
fread(buf, 1, fsz, fp);
fclose(fp);
return buf;
}
char *get_input(const char *path) {
size_t fsz = 0;
FILE *fp = open_file(path, &fsz);
if (fp == NULL) {
return NULL;
}
char *buf = NULL;
buf = malloc(fsz + 1);
if (buf == NULL) {