diff options
author | Nicolas Williams <nico@cryptonector.com> | 2013-06-15 00:08:59 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2013-06-15 00:08:59 -0500 |
commit | bbf4e05fde301bc48907ca6d485c59ae10a6467c (patch) | |
tree | 21f34165b2634bacb88e0c79a5557ee1fa01bb04 /main.c | |
parent | 81e2336aceed8877f2c20a466166d5b1759136fc (diff) |
Move slurp_file() into library as jv_load_file()
Needed as part of creating a libjq.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 42 |
1 files changed, 3 insertions, 39 deletions
@@ -6,6 +6,7 @@ #include <unistd.h> #include "compile.h" #include "jv.h" +#include "jv_file.h" #include "jv_parse.h" #include "execute.h" #include "config.h" /* Autoconf generated header file */ @@ -96,43 +97,6 @@ static void process(jv value, int flags) { 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; const char** input_filenames; int ninput_files; @@ -224,7 +188,7 @@ int main(int argc, char* argv[]) { } jv arg = jv_object(); 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)) { data = jv_invalid_get_msg(data); 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) { - jv data = slurp_file(program, 1); + jv data = jv_load_file(program, 1); if (!jv_is_valid(data)) { data = jv_invalid_get_msg(data); fprintf(stderr, "%s: %s\n", progname, jv_string_value(data)); |