26 #include <boost/property_tree/ptree.hpp>
27 #include <boost/property_tree/xml_parser.hpp>
46 bool PTreeFile::IsGzipped(
const string& path)
49 transform(s.begin(), s.end(), s.begin(), ::tolower);
50 return s.rfind(
".gz") != string::npos;
53 bool PTreeFile::IsPTreeFile(
const string& path)
56 transform(s.begin(), s.end(), s.begin(), ::tolower);
57 return path.rfind(
".xml.gz") != string::npos || path.rfind(
"xml") != string::npos ;
60 string PTreeFile::GetCompleteSuffix(
const string& path)
63 const auto posa = path.rfind(
"/");
64 const auto pos1 = (posa != string::npos) ? posa + 1 : 0;
67 const auto posb = path.substr(pos1).find(
".");
68 const auto len1 = (posb != string::npos) ? path.length() - posb : 0;
70 return path.substr(pos1 + posb + 1, len1);
73 string PTreeFile::GetBaseNameExcludingLabel(
const string& path)
76 const auto posa = path.rfind(
"/");
77 const auto pos1 = (posa != string::npos) ? posa + 1 : 0;
80 const auto posb = path.substr(pos1).find(
".");
81 const auto len1 = (posb != string::npos) ? posb : path.length() - pos1;
84 const auto posc = path.substr(pos1, len1).rfind(
"_");
85 const auto len2 = (posc != string::npos) ? posc : len1;
87 return path.substr(pos1, len2);
90 string PTreeFile::GetBaseNameIncludingLabel(
const string& path)
93 const auto posa = path.rfind(
"/");
94 const auto pos1 = (posa != string::npos) ? posa + 1 : 0;
97 const auto posb = path.substr(pos1).find(
".");
98 const auto len1 = (posb != string::npos) ? posb : path.length() - pos1;
100 return path.substr(pos1, len1);
103 string PTreeFile::GetLabel(
const string& path)
106 const auto posa = path.rfind(
"/");
107 const auto pos1 = (posa != string::npos) ? posa + 1 : 0;
110 const auto posb = path.substr(pos1).find(
".");
111 const auto len1 = (posb != string::npos) ? posb : path.length() - pos1;
114 const auto posc = path.substr(pos1, len1).rfind(
"_");
115 const auto len2 = (posc != string::npos) ? len1 - posc - 1 : 0;
117 return path.substr(pos1 + posc + 1, len2);
120 void PTreeFile::Read(
const string& path, ptree& pt)
122 if (IsGzipped(path)) {
129 void PTreeFile::ReadXml(
const string& path, ptree& pt)
131 read_xml(path, pt, trim_whitespace);
134 void PTreeFile::Write(
const string& path,
const ptree& pt)
136 if (IsGzipped(path)) {
137 WriteXmlGz(path, pt);
143 void PTreeFile::WriteXml(
const string& path,
const ptree& pt)
145 write_xml(path, pt, locale(), XmlWriterSettings::GetTab());
152 #ifdef USE_BOOST_GZIP
157 #include <boost/iostreams/copy.hpp>
158 #include <boost/iostreams/device/file.hpp>
159 #include <boost/iostreams/device/file_descriptor.hpp>
160 #include <boost/iostreams/filter/gzip.hpp>
161 #include <boost/iostreams/filtering_stream.hpp>
162 #include <boost/iostreams/filtering_streambuf.hpp>
163 #include <boost/iostreams/traits.hpp>
168 using namespace boost::iostreams;
170 void PTreeFile::Compress(
const string& in_path,
const string& out_path)
173 file_source in(in_path);
174 filtering_ostream out;
175 out.push(gzip_compressor());
176 out.push(file_sink(out_path));
180 void PTreeFile::Decompress(
const string& in_path,
const string& out_path)
183 filtering_istream in;
184 in.push(gzip_decompressor());
185 in.push(file_source(in_path));
186 file_sink out(out_path);
190 constexpr
bool PTreeFile::IsBoostGzipAvailable()
195 void PTreeFile::ReadXmlGz(
const string& path, ptree& pt)
197 filtering_streambuf<input> in_buffer;
198 in_buffer.push(gzip_decompressor());
199 in_buffer.push(file_source(path));
200 filtering_istream ptree_in_stream(in_buffer);
201 read_xml(ptree_in_stream, pt, trim_whitespace);
204 void PTreeFile::WriteXmlGz(
const string& path,
const ptree& pt)
206 filtering_streambuf<output> out_buffer;
207 out_buffer.push(gzip_compressor());
208 out_buffer.push(file_sink(path));
209 filtering_ostream ptree_out_stream(out_buffer);
210 write_xml(ptree_out_stream, pt, XmlWriterSettings::GetTab());
227 void PTreeFile::Compress(
const string& in_path,
const string& out_path)
231 ReadXml(in_path, pt);
232 WriteXmlGz(out_path, pt);
235 void PTreeFile::Decompress(
const string& in_path,
const string& out_path)
239 ReadXmlGz(in_path, pt);
240 WriteXml(out_path, pt);
243 constexpr
bool PTreeFile::IsBoostGzipAvailable()
248 void PTreeFile::ReadXmlGz(
const string& path, ptree& pt)
254 infile.open(path, ifstream::in | ifstream::binary);
255 stringstream ptree_in_stream;
258 const unsigned int in_buffer_size = 1 << 15;
259 const unsigned int out_buffer_size = 1 << 18;
263 auto in_buffer = unique_ptr<array<char, in_buffer_size>>(
264 new array<char, in_buffer_size>());
265 auto out_buffer = unique_ptr<array<char, out_buffer_size>>(
266 new array<char, out_buffer_size>());
275 if (inflateInit2(&s, 31) != Z_OK) {
277 throw runtime_error(
"could not initialize zlib inflate stream.");
283 infile.read(in_buffer->data(), in_buffer_size);
284 }
catch (exception& e) {
289 s.avail_in = infile.gcount();
292 s.next_in = (
unsigned char*) in_buffer->data();
295 s.avail_out = out_buffer_size;
296 s.next_out = (
unsigned char*) out_buffer->data();
298 status = inflate(&s, Z_NO_FLUSH);
299 if (status == Z_NEED_DICT || status == Z_DATA_ERROR || status == Z_MEM_ERROR) {
302 throw runtime_error(
"inflate error.");
304 unsigned int have = out_buffer_size - s.avail_out;
307 ptree_in_stream.write(out_buffer->data(), have);
308 if (ptree_in_stream.fail()) {
311 throw runtime_error(
"stringstream write error.");
313 }
while (s.avail_out == 0);
315 }
while (status != Z_STREAM_END);
320 if (status != Z_STREAM_END) {
321 throw runtime_error(
"data error.");
324 ptree_in_stream.seekg(0);
327 throw runtime_error(
"could not open file.");
330 read_xml(ptree_in_stream, pt, trim_whitespace);
333 void PTreeFile::WriteXmlGz(
const string& path,
const ptree& pt)
335 stringstream ptree_out_stream;
336 write_xml(ptree_out_stream, pt, XmlWriterSettings::GetTab());
338 const unsigned int in_buffer_size = 1 << 18;
339 const unsigned int out_buffer_size = 1 << 15;
340 ofstream outfile(path, ofstream::out | ofstream::binary | ofstream::trunc);
342 throw runtime_error(
"could not open file for writing.");
344 unsigned char in_buffer[in_buffer_size];
345 unsigned char out_buffer[out_buffer_size];
353 if (deflateInit2(&s, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15+16, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
355 throw runtime_error(
"could not initialize zlib deflate stream.");
361 ptree_out_stream.read((
char*) in_buffer, in_buffer_size);
362 }
catch (exception& e) {
367 s.avail_in = ptree_out_stream.gcount();
368 s.next_in = in_buffer;
369 flush = ptree_out_stream.eof() ? Z_FINISH : Z_NO_FLUSH;
371 s.avail_out = out_buffer_size;
372 s.next_out = out_buffer;
374 int have = out_buffer_size - s.avail_out;
375 outfile.write((
char*) out_buffer, have);
376 if (outfile.fail()) {
379 throw runtime_error(
"outfile write error.");
381 }
while (s.avail_out == 0);
383 }
while (flush != Z_FINISH);
Namespace for miscellaneous utilities.
Namespace for the core simulator.
Macro defs for debug and logging.
Header file for Exception class.
Xml writer settings class.