Move slurp_file() into library as jv_load_file()
Needed as part of creating a libjq.
This commit is contained in:
1 parent
81e2336ace
commit
bbf4e05fde
6 files changed
+63
-55
No files matched your search
+5
-5
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \
|
JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \
|
||||||
forkable_stack.h frame_layout.h jv.h jv_alloc.h jv_aux.h jv_dtoa.h \
|
forkable_stack.h frame_layout.h jv.h jv_alloc.h jv_aux.h jv_dtoa.h \
|
||||||
jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h parser.y \
|
jv_file.h jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h \
|
||||||
jv_utf8_tables.h main.h lexer.l
|
parser.y jv_utf8_tables.h main.h lexer.l
|
||||||
|
|
||||||
JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c \
|
JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c \
|
||||||
jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c \
|
jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_file.c \
|
||||||
jq_test.c ${JQ_INCS}
|
jv_alloc.c jq_test.c ${JQ_INCS}
|
||||||
|
|
||||||
|
|
||||||
### C build options
|
### C build options
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#include "jq_parser.h"
|
#include "jq_parser.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
#include "jv_aux.h"
|
#include "jv_aux.h"
|
||||||
|
#include "jv_file.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -585,7 +585,7 @@ int slurp_lib(block* bb) {
|
|||||||
char* home = getenv("HOME");
|
char* home = getenv("HOME");
|
||||||
if (home) { // silently ignore no $HOME
|
if (home) { // silently ignore no $HOME
|
||||||
jv filename = jv_string_append_str(jv_string(home), "/.jq");
|
jv filename = jv_string_append_str(jv_string(home), "/.jq");
|
||||||
jv data = slurp_file(jv_string_value(filename), 1);
|
jv data = jv_load_file(jv_string_value(filename), 1);
|
||||||
if (jv_is_valid(data)) {
|
if (jv_is_valid(data)) {
|
||||||
nerrors = builtins_bind_one(bb, jv_string_value(data) );
|
nerrors = builtins_bind_one(bb, jv_string_value(data) );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "jv.h"
|
||||||
|
#include "jv_aux.h"
|
||||||
|
#include "jv_parse.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();
|
||||||
|
jv_parser_init(&parser);
|
||||||
|
}
|
||||||
|
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, strlen(buf), !feof(file));
|
||||||
|
jv value;
|
||||||
|
while (jv_is_valid((value = jv_parser_next(&parser))))
|
||||||
|
data = jv_array_append(data, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef JV_FILE_H
|
||||||
|
#define JV_FILE_H
|
||||||
|
|
||||||
|
#include "jv.h"
|
||||||
|
|
||||||
|
jv jv_load_file(const char *, int);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
|
#include "jv_file.h"
|
||||||
#include "jv_parse.h"
|
#include "jv_parse.h"
|
||||||
#include "execute.h"
|
#include "execute.h"
|
||||||
#include "config.h" /* Autoconf generated header file */
|
#include "config.h" /* Autoconf generated header file */
|
||||||
@@ -96,43 +97,6 @@ static void process(jv value, int flags) {
|
|||||||
jq_teardown(&jq);
|
jq_teardown(&jq);
|
||||||
}
|
}
|
||||||
|
|
||||||
jv slurp_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();
|
|
||||||
jv_parser_init(&parser);
|
|
||||||
}
|
|
||||||
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, strlen(buf), !feof(file));
|
|
||||||
jv value;
|
|
||||||
while (jv_is_valid((value = jv_parser_next(&parser))))
|
|
||||||
data = jv_array_append(data, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE* current_input;
|
FILE* current_input;
|
||||||
const char** input_filenames;
|
const char** input_filenames;
|
||||||
int ninput_files;
|
int ninput_files;
|
||||||
@@ -224,7 +188,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
jv arg = jv_object();
|
jv arg = jv_object();
|
||||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||||
jv data = slurp_file(argv[i+2], 0);
|
jv data = jv_load_file(argv[i+2], 0);
|
||||||
if (!jv_is_valid(data)) {
|
if (!jv_is_valid(data)) {
|
||||||
data = jv_invalid_get_msg(data);
|
data = jv_invalid_get_msg(data);
|
||||||
fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname,
|
fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname,
|
||||||
@@ -260,7 +224,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options & FROM_FILE) {
|
if (options & FROM_FILE) {
|
||||||
jv data = slurp_file(program, 1);
|
jv data = jv_load_file(program, 1);
|
||||||
if (!jv_is_valid(data)) {
|
if (!jv_is_valid(data)) {
|
||||||
data = jv_invalid_get_msg(data);
|
data = jv_invalid_get_msg(data);
|
||||||
fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));
|
fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));
|
||||||
|
|||||||
Reference in new issue
Block a user