mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
This reverts commit 77936a594d797c480f26bfcef3636a74588a6918. There are too many odd bugs in this mode, and it turns out to be a bad idea anyways. Instead, in the future a better option will be to pursue alternative parsers, such as: - streaming parser that outputs only when a new leaf value is added or an array/object is opened/closed; options here include whether to include a path in each output; - parsers for binary JSON encodings (there's a variety of them). Then one might run jq with a streaming parser and use `reduce` to coalesce inputs from some depth down (instead of from one level down as the reverted commit had intended). Besides, a fully streaming parser is desirable in some cases, therefore we should have such a thing as an option. I've explored modifying the current parser to support a streaming option, but it only makes the code very difficult to follow, which is one reason that alternate parsers makes sense. At any rate, this is all for the future. For now there's no streaming of individual texts, just text sequences.
47 lines
1.2 KiB
C
47 lines
1.2 KiB
C
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "jv.h"
|
|
|
|
jv jv_load_file(const char* filename, int raw) {
|
|
FILE* file = fopen(filename, "r");
|
|
struct jv_parser* parser;
|
|
jv data;
|
|
if (!file) {
|
|
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
|
filename,
|
|
strerror(errno)));
|
|
}
|
|
if (raw) {
|
|
data = jv_string("");
|
|
} else {
|
|
data = jv_array();
|
|
parser = jv_parser_new();
|
|
}
|
|
while (!feof(file) && !ferror(file)) {
|
|
char buf[4096];
|
|
size_t n = fread(buf, 1, sizeof(buf), file);
|
|
if (raw) {
|
|
data = jv_string_concat(data, jv_string_sized(buf, (int)n));
|
|
} else {
|
|
jv_parser_set_buf(parser, buf, n, !feof(file));
|
|
jv value;
|
|
while (jv_is_valid((value = jv_parser_next(parser))))
|
|
data = jv_array_append(data, value);
|
|
jv_free(value);
|
|
}
|
|
}
|
|
if (!raw)
|
|
jv_parser_free(parser);
|
|
int badread = ferror(file);
|
|
fclose(file);
|
|
if (badread) {
|
|
jv_free(data);
|
|
return jv_invalid_with_msg(jv_string_fmt("Error reading from %s",
|
|
filename));
|
|
}
|
|
return data;
|
|
}
|