csvzall 0.4.0
CSV command-line tool and emerging C++ library
Loading...
Searching...
No Matches
gzip_stream.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <istream>
5#include <memory>
6#include <sstream>
7#include <stdexcept>
8#include <streambuf>
9#include <string>
10#include <string_view>
11#include <utility>
12
13#include <csv.hpp>
14#ifdef CSVZALL_HAVE_COMPRESSED_INPUT
15#include <zlib.h>
16#endif
17
19
20bool IsGzipPath(std::string_view path);
21bool IsZipPath(std::string_view path);
22std::string ReadZipEntry(const std::string& path, const std::string& requested_entry);
23
24#ifdef CSVZALL_HAVE_COMPRESSED_INPUT
25class GzipStreambuf : public std::streambuf {
26 public:
27 explicit GzipStreambuf(const std::string& path);
28 ~GzipStreambuf() override;
29
30 GzipStreambuf(const GzipStreambuf&) = delete;
31 GzipStreambuf& operator=(const GzipStreambuf&) = delete;
32
33 protected:
34 int_type underflow() override;
35
36 private:
37 gzFile file_ = nullptr;
38 std::array<char, 64 * 1024> buffer_{};
39};
40
41class GzipInputStream : public std::istream {
42 public:
43 explicit GzipInputStream(const std::string& path);
44
45 private:
47};
48#endif
49
50template <typename... Args>
51csv::CSVReader OpenCsvReader(const std::string& path,
52 const std::string& zip_entry,
53 Args&&... args) {
54#ifndef CSVZALL_HAVE_COMPRESSED_INPUT
55 (void)zip_entry;
56#endif
57 if (IsGzipPath(path)) {
58#ifdef CSVZALL_HAVE_COMPRESSED_INPUT
59 std::unique_ptr<std::istream> stream(new GzipInputStream(path));
60 return csv::CSVReader(std::move(stream), std::forward<Args>(args)...);
61#else
62 throw std::runtime_error("gzip CSV input is disabled in this build");
63#endif
64 }
65 if (IsZipPath(path)) {
66#ifdef CSVZALL_HAVE_COMPRESSED_INPUT
67 std::unique_ptr<std::istream> stream(new std::istringstream(ReadZipEntry(path, zip_entry)));
68 return csv::CSVReader(std::move(stream), std::forward<Args>(args)...);
69#else
70 throw std::runtime_error("ZIP CSV input is disabled in this build");
71#endif
72 }
73
74 return csv::CSVReader(path, std::forward<Args>(args)...);
75}
76
77template <typename... Args>
78csv::CSVReader OpenCsvReader(const std::string& path, Args&&... args) {
79 return OpenCsvReader(path, std::string{}, std::forward<Args>(args)...);
80}
81
82} // namespace csvzall::pipeline::common
std::vector< JsonValue > array
Definition json_extract.cpp:33
common::JsonPath path
Definition json_extract.cpp:348
Definition chart_spec.hpp:100
std::string ReadZipEntry(const std::string &path, const std::string &requested_entry)
Definition gzip_stream.cpp:257
bool IsZipPath(std::string_view path)
Definition gzip_stream.cpp:253
bool IsGzipPath(std::string_view path)
Definition gzip_stream.cpp:249
csv::CSVReader OpenCsvReader(const std::string &path, const std::string &zip_entry, Args &&... args)
Definition gzip_stream.hpp:51