JSON-reading glue to use the stream abstraction

This commit is contained in:
Eric Fischer
2018-05-11 14:58:29 -07:00
parent 33b0d5c15c
commit 72b8142388
+14 -4
View File
@@ -476,6 +476,8 @@ void do_read_parallel(char *map, long long len, long long initial_offset, const
} }
} }
static ssize_t read_stream(json_pull *j, char *buffer, size_t n);
struct STREAM { struct STREAM {
FILE *fp; FILE *fp;
@@ -498,19 +500,27 @@ struct STREAM {
} }
json_pull *json_begin() { json_pull *json_begin() {
return json_begin_file(fp); return ::json_begin(read_stream, this);
} }
}; };
STREAM *streamfdopen(int fd, const char *mode) { static ssize_t read_stream(json_pull *j, char *buffer, size_t n) {
FILE *fp = fdopen(fd, mode); return ((STREAM *) j->source)->fread(buffer, 1, n);
}
STREAM *streamfdopen(int fd, const char *mode, std::string const &fname) {
FILE *fp = fdopen(fd, mode);
if (fp == NULL) { if (fp == NULL) {
return NULL; return NULL;
} }
STREAM *s = new STREAM; STREAM *s = new STREAM;
s->fp = fp; s->fp = fp;
if (fname.size() > 3 && fname.substr(fname.size() - 3) == std::string(".gz")) {
//
}
return s; return s;
} }
@@ -1483,7 +1493,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {
STREAM *fp = streamfdopen(fd, "r"); STREAM *fp = streamfdopen(fd, "r", sources[layer].file);
if (fp == NULL) { if (fp == NULL) {
perror(sources[layer].file.c_str()); perror(sources[layer].file.c_str());
if (close(fd) != 0) { if (close(fd) != 0) {