Merge pull request #150 from nicowilliams/jv_load_file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Move slurp_file() into library as jv_load_file()
authored by Stephen Dolan · committed by Stephen Dolan · parents 81e2336, bbf4e05 · Browse files · 6 files·+64 added−56 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+64 added−56 removed
- other:+64 added−56 removed
Provenance
- Commit
69fa279- Tree
21f3416- Author
Stephen Dolan <mu@netsoc.tcd.ie>- Committer
Stephen Dolan <mu@netsoc.tcd.ie>- Parent 1
81e2336- Parent 2
bbf4e05
6 files changed+64 added−56 removed
modified:builtin.c+2 added-2 removed
3 unmodified lines4 #include "compile.h"5 #include "jq_parser.h"6 #include "locfile.h"7 #include "jv_aux.h"8+#include "jv_file.h"9 #include "jv_unicode.h"9-#include "main.h"10 11 12 13 typedef jv (*func_1)(jv);570 unmodified lines584 int nerrors = 0;585 char* home = getenv("HOME");586 if (home) { // silently ignore no $HOME587 jv filename = jv_string_append_str(jv_string(home), "/.jq");588- jv data = slurp_file(jv_string_value(filename), 1);588+ jv data = jv_load_file(jv_string_value(filename), 1);589 if (jv_is_valid(data)) {590 nerrors = builtins_bind_one(bb, jv_string_value(data) );591 }592 jv_free(filename);19 unmodified linesadded:jv_file.c+45 added
1+2+#include <errno.h>3+#include <stdio.h>4+#include <stdlib.h>5+#include <string.h>6+#include "jv.h"7+#include "jv_aux.h"8+#include "jv_parse.h"9+10+jv jv_load_file(const char* filename, int raw) {11+ FILE* file = fopen(filename, "r");12+ struct jv_parser parser;13+ jv data;14+ if (!file) {15+ return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",16+ filename,17+ strerror(errno)));18+ }19+ if (raw) {20+ data = jv_string("");21+ } else {22+ data = jv_array();23+ jv_parser_init(&parser);24+ }25+ while (!feof(file) && !ferror(file)) {26+ char buf[4096];27+ size_t n = fread(buf, 1, sizeof(buf), file);28+ if (raw) {29+ data = jv_string_concat(data, jv_string_sized(buf, (int)n));30+ } else {31+ jv_parser_set_buf(&parser, buf, strlen(buf), !feof(file));32+ jv value;33+ while (jv_is_valid((value = jv_parser_next(&parser))))34+ data = jv_array_append(data, value);35+ }36+ }37+ int badread = ferror(file);38+ fclose(file);39+ if (badread) {40+ jv_free(data);41+ return jv_invalid_with_msg(jv_string_fmt("Error reading from %s",42+ filename));43+ }44+ return data;45+}added:jv_file.h+8 added
1+#ifndef JV_FILE_H2+#define JV_FILE_H3+4+#include "jv.h"5+6+jv jv_load_file(const char *, int);7+8+#endifmodified:main.c+3 added-39 removed
4 unmodified lines5 #include <ctype.h>6 #include <unistd.h>7 #include "compile.h"8 #include "jv.h"9+#include "jv_file.h"10 #include "jv_parse.h"11 #include "execute.h"12 #include "config.h" /* Autoconf generated header file */13 #include "jv_alloc.h"82 unmodified lines96 jv_free(result);97 jq_teardown(&jq);98 }99 99-jv slurp_file(const char* filename, int raw) {100- FILE* file = fopen(filename, "r");101- struct jv_parser parser;102- jv data;103- if (!file) {104- return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",105- filename,106- strerror(errno)));107- }108- if (raw) {109- data = jv_string("");110- } else {111- data = jv_array();112- jv_parser_init(&parser);113- }114- while (!feof(file) && !ferror(file)) {115- char buf[4096];116- size_t n = fread(buf, 1, sizeof(buf), file);117- if (raw) {118- data = jv_string_concat(data, jv_string_sized(buf, (int)n));119- } else {120- jv_parser_set_buf(&parser, buf, strlen(buf), !feof(file));121- jv value;122- while (jv_is_valid((value = jv_parser_next(&parser))))123- data = jv_array_append(data, value);124- }125- }126- int badread = ferror(file);127- fclose(file);128- if (badread) {129- jv_free(data);130- return jv_invalid_with_msg(jv_string_fmt("Error reading from %s",131- filename));132- }133- return data;134-}135-100 FILE* current_input;101 const char** input_filenames;102 int ninput_files;103 int next_input_idx;83 unmodified lines187 die();188 }189 jv arg = jv_object();190 arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));227- jv data = slurp_file(argv[i+2], 0);191+ jv data = jv_load_file(argv[i+2], 0);192 if (!jv_is_valid(data)) {193 data = jv_invalid_get_msg(data);194 fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname,195 argv[i+1], argv[i+2], jv_string_value(data));27 unmodified lines223 die();224 }225 226 if (options & FROM_FILE) {263- jv data = slurp_file(program, 1);227+ jv data = jv_load_file(program, 1);228 if (!jv_is_valid(data)) {229 data = jv_invalid_get_msg(data);230 fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));231 jv_free(data);72 unmodified linesdeleted:main.h-9 removed
1-#ifndef MAIN_H2-#define MAIN_H3-4-#include "compile.h"5-6-jv slurp_file(const char*, int);7-8-9-#endifmodified:Makefile.am+6 added-6 removed
1 ### C source files to be built and distributed.2 3 JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \4 forkable_stack.h frame_layout.h jv.h jv_alloc.h jv_aux.h jv_dtoa.h \5- jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h parser.y \6- jv_utf8_tables.h main.h lexer.l5+ jv_file.h jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h \6+ parser.y jv_utf8_tables.h main.h lexer.l7 8-JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c \9- jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c \10- jq_test.c ${JQ_INCS}8+JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c \9+ jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_file.c \10+ jv_alloc.c jq_test.c ${JQ_INCS}11 12 13 ### C build options14 115 unmodified lines130 rm -f jq.1131 endif 132 133 clean-local: clean-local-docs134- rm -f version.h .remake-version-h134+ rm -f version.h .remake-version-h