aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-09-11 11:57:06 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-09-11 11:57:06 -0500
commit7c75037ec80efc01fe842ccca05fec304bb49741 (patch)
tree096c4d129659e15c7c79ac133e28a7d22e8d679a /src
parent82fbb56c97f41941b359b03c0033cb49295816f4 (diff)
add: static file reading implemented
Now is possible to read data from standard input
Diffstat (limited to 'src')
-rw-r--r--src/main.c33
1 files changed, 9 insertions, 24 deletions
diff --git a/src/main.c b/src/main.c
index 6d70fc0..210db7e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,7 +28,7 @@
#include "util.h"
#include "nn.h"
-const size_t MAX_FILE_SIZE = 1<<29; // 0.5 GiB
+#define MAX_FILE_SIZE 536870912 //1<<29; 0.5 GiB
typedef struct Array {
double *data;
@@ -59,32 +59,17 @@ void json_read(
bool read_output)
{
FILE *fp = NULL;
- char *fp_buffer = NULL;
- size_t ret;
- int64_t fp_size;
+ static char fp_buffer[MAX_FILE_SIZE];
- fp = fopen(filepath, "r");
- if (fp == NULL) goto json_read_error;
-
- ret = (size_t)fseek(fp, 0L, SEEK_END);
- if ((int)ret == -1) goto json_read_error;
-
- fp_size = ftell(fp);
- if (fp_size == -1) goto json_read_error;
- if (fp_size >= MAX_FILE_SIZE) {
- fprintf(stderr, "ftell Error(): '%s' size greater than '%zu'\n", filepath, MAX_FILE_SIZE);
- }
- rewind(fp);
+ fp = (!strcmp(filepath, "-")) ? fopen("/dev/stdin", "r") : fopen(filepath, "r");
- fp_buffer = calloc(sizeof(char), fp_size);
- if (fp_buffer == NULL) goto json_read_error;
-
- ret = fread(fp_buffer, sizeof(char), (size_t)fp_size, fp);
- if (ret != (size_t)fp_size) {
- fprintf(stderr, "json_read() Error: fread bytes '%zd' does not match with buffer size '%zd'", ret, (size_t)fp_size);
- exit(1);
- }
+ if (fp == NULL) goto json_read_error;
+ size_t i = 0;
+ do {
+ if (i >= MAX_FILE_SIZE) die("json_read() Error: file size is bigger than '%zu'", i, MAX_FILE_SIZE);
+ fp_buffer[i] = fgetc(fp);
+ } while (fp_buffer[i++] != EOF);
json_object *json_obj;
json_obj = json_tokener_parse(fp_buffer);
Feel free to download, copy and edit any repo