2014-08-06 19:59:02 +05:30
|
|
|
#include "xml_utils.h"
|
|
|
|
|
2017-04-29 23:21:52 +05:30
|
|
|
#include "base/file_utils.h"
|
2014-08-26 17:35:21 +05:30
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2017-04-29 23:21:52 +05:30
|
|
|
#include <sys/stat.h>
|
2014-08-26 17:35:21 +05:30
|
|
|
|
|
|
|
using namespace xml_utils;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
xml_parser::parse(std::string const &backup_file, bool quiet)
|
|
|
|
{
|
2017-04-29 23:21:52 +05:30
|
|
|
file_utils::check_file_exists(backup_file);
|
2014-08-26 17:35:21 +05:30
|
|
|
ifstream in(backup_file.c_str(), ifstream::in);
|
|
|
|
|
2016-02-04 14:32:42 +05:30
|
|
|
std::unique_ptr<base::progress_monitor> monitor = create_monitor(quiet);
|
2014-08-26 17:35:21 +05:30
|
|
|
|
|
|
|
size_t total = 0;
|
2017-04-29 23:21:52 +05:30
|
|
|
size_t input_length = file_utils::get_file_length(backup_file);
|
2014-08-26 17:35:21 +05:30
|
|
|
|
2016-02-27 12:50:45 +05:30
|
|
|
XML_Error error_code = XML_ERROR_NONE;
|
|
|
|
while (!in.eof() && error_code == XML_ERROR_NONE) {
|
2014-08-26 17:35:21 +05:30
|
|
|
char buffer[4096];
|
|
|
|
in.read(buffer, sizeof(buffer));
|
|
|
|
size_t len = in.gcount();
|
|
|
|
int done = in.eof();
|
|
|
|
|
2016-02-27 12:50:45 +05:30
|
|
|
// Do not throw while normally aborted by element handlers
|
|
|
|
if (!XML_Parse(parser_, buffer, len, done) &&
|
|
|
|
(error_code = XML_GetErrorCode(parser_)) != XML_ERROR_ABORTED) {
|
2014-08-26 17:35:21 +05:30
|
|
|
ostringstream out;
|
|
|
|
out << "Parse error at line "
|
|
|
|
<< XML_GetCurrentLineNumber(parser_)
|
|
|
|
<< ":\n"
|
|
|
|
<< XML_ErrorString(XML_GetErrorCode(parser_))
|
|
|
|
<< endl;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
total += len;
|
|
|
|
monitor->update_percent(total * 100 / input_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:32:42 +05:30
|
|
|
unique_ptr<base::progress_monitor>
|
2014-08-26 17:35:21 +05:30
|
|
|
xml_parser::create_monitor(bool quiet)
|
|
|
|
{
|
|
|
|
if (!quiet && isatty(fileno(stdout)))
|
|
|
|
return base::create_progress_bar("Restoring");
|
|
|
|
else
|
|
|
|
return base::create_quiet_progress_monitor();
|
|
|
|
}
|
|
|
|
|
2014-08-06 19:59:02 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
xml_utils::build_attributes(attributes &a, char const **attr)
|
|
|
|
{
|
|
|
|
while (*attr) {
|
|
|
|
char const *key = *attr;
|
|
|
|
|
|
|
|
attr++;
|
|
|
|
if (!*attr) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "No value given for xml attribute: " << key;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
char const *value = *attr;
|
|
|
|
a.insert(make_pair(string(key), string(value)));
|
|
|
|
attr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|