|
28 | 28 | #include <boost/archive/binary_iarchive.hpp> |
29 | 29 | #include <boost/filesystem.hpp> |
30 | 30 |
|
| 31 | +#include <boost/iostreams/device/file_descriptor.hpp> |
| 32 | +#ifdef ORE_USE_ZLIB |
| 33 | +#include <boost/iostreams/filter/gzip.hpp> |
| 34 | +#endif |
| 35 | +#include <boost/iostreams/filtering_stream.hpp> |
| 36 | + |
31 | 37 | #include <fstream> |
32 | 38 |
|
33 | 39 | namespace ore { |
@@ -197,5 +203,78 @@ void InMemoryReport::toFile(const string& filename, const char sep, const bool c |
197 | 203 | cReport.end(); |
198 | 204 | } |
199 | 205 |
|
| 206 | +bool use_compression(const std::string& filename) { |
| 207 | +#ifdef ORE_USE_ZLIB |
| 208 | + // assume compression for all filenames that do not end with csv or txt |
| 209 | + |
| 210 | + std::string extension = boost::filesystem::path(filename).extension().string(); |
| 211 | + return extension != ".csv" && extension != ".txt"; |
| 212 | +#else |
| 213 | + return false; |
| 214 | +#endif |
| 215 | +} |
| 216 | + |
| 217 | +void InMemoryReport::toZip(const string& filename, const char sep, const bool commentCharacter, char quoteChar, |
| 218 | + const string& nullString, bool lowerHeader) { |
| 219 | + |
| 220 | + bool gzip = use_compression(filename); |
| 221 | + std::ofstream out1(filename, gzip ? (std::ios::binary | std::ios::out) : std::ios::out); |
| 222 | + boost::iostreams::filtering_stream<boost::iostreams::output> out; |
| 223 | +#ifdef ORE_USE_ZLIB |
| 224 | + if (gzip) |
| 225 | + out.push(boost::iostreams::gzip_compressor(/*boost::iostreams::gzip_params(9)*/)); |
| 226 | +#endif |
| 227 | + out.push(out1); |
| 228 | + |
| 229 | + for (Size i = 0; i < headers_.size(); ++i) { |
| 230 | + if (i > 0) |
| 231 | + out << sep; |
| 232 | + |
| 233 | + std::string h = headers_[i]; |
| 234 | + if (lowerHeader) |
| 235 | + std::transform(h.begin(), h.end(), h.begin(), ::tolower); |
| 236 | + out << quoteChar << h << quoteChar; |
| 237 | + } |
| 238 | + out << "\n"; |
| 239 | + |
| 240 | + Size numColumns = columns(); |
| 241 | + if (numColumns > 0) { |
| 242 | + for (Size cacheIndex = 0; cacheIndex < files_.size(); ++cacheIndex) { |
| 243 | + const auto& data = cache(cacheIndex); |
| 244 | + Size numRows = data[0].size(); |
| 245 | + |
| 246 | + for (Size row = 0; row < numRows; ++row) { |
| 247 | + for (Size col = 0; col < numColumns; ++col) { |
| 248 | + if (col > 0) |
| 249 | + out << sep; |
| 250 | + |
| 251 | + const ReportType& val = data[col][row]; |
| 252 | + if (val.empty()) |
| 253 | + out << nullString; |
| 254 | + else |
| 255 | + out << quoteChar << val << quoteChar; |
| 256 | + } |
| 257 | + out << "\n"; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + Size numRows = data_[0].size(); |
| 262 | + for (Size row = 0; row < numRows; ++row) { |
| 263 | + for (Size col = 0; col < numColumns; ++col) { |
| 264 | + if (col > 0) |
| 265 | + out << sep; |
| 266 | + |
| 267 | + const ReportType& val = data_[col][row]; |
| 268 | + if (val.empty()) |
| 269 | + out << nullString; |
| 270 | + else |
| 271 | + out << quoteChar << val << quoteChar; |
| 272 | + } |
| 273 | + out << "\n"; |
| 274 | + } |
| 275 | + } |
| 276 | + out.flush(); |
| 277 | +} |
| 278 | + |
200 | 279 | } // namespace data |
201 | 280 | } // namespace ore |
0 commit comments