dissect: print offset of parsing error

Change-Id: I2209a16330e513b46abe2dfcc57ca780b3478b82
This commit is contained in:
Davide Pesavento committed 2021-01-26 22:16:02 -05:00
1 parent 6929b4344a
commit 9b1fd4b332
2 files changed
+19 -21

No files matched your search

+8 -13
View File
@@ -32,10 +32,9 @@ namespace dissect {
namespace po = boost::program_options;
static void
usage(std::ostream& os, const std::string& appName, const po::options_description& options)
usage(std::ostream& os, const std::string& programName, const po::options_description& options)
{
os << "Usage:\n"
<< " " << appName << " [input-file] \n"
os << "Usage: " << programName << " [options] [input-file]\n"
<< "\n"
<< options;
}
@@ -45,7 +44,7 @@ main(int argc, char* argv[])
{
std::string inputFileName;
po::options_description visibleOptions;
po::options_description visibleOptions("Options");
visibleOptions.add_options()
("help,h", "print help and exit")
("version,V", "print version and exit")
@@ -67,8 +66,7 @@ main(int argc, char* argv[])
po::notify(vm);
}
catch (const po::error& e) {
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
usage(std::cerr, argv[0], visibleOptions);
std::cerr << "ERROR: " << e.what() << "\n";
return 2;
}
@@ -78,24 +76,21 @@ main(int argc, char* argv[])
}
if (vm.count("version") > 0) {
std::cout << "ndn-dissect " << tools::VERSION << std::endl;
std::cout << "ndn-dissect " << tools::VERSION << "\n";
return 0;
}
std::ifstream inputFile;
std::istream* inputStream;
std::istream* inputStream = &std::cin;
if (vm.count("input-file") > 0 && inputFileName != "-") {
inputFile.open(inputFileName);
if (!inputFile.is_open()) {
std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl;
if (!inputFile) {
std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable\n";
return 3;
}
inputStream = &inputFile;
}
else {
inputStream = &std::cin;
}
NdnDissect program;
program.dissect(std::cout, *inputStream);
+11 -8
View File
@@ -118,9 +118,9 @@ NdnDissect::printBlock(std::ostream& os, const Block& block)
if (block.elements().empty()) {
os << " [[";
name::Component(block.value(), block.value_size()).toUri(os);
os<< "]]";
os << "]]";
}
os << std::endl;
os << "\n";
util::IndentedStream os2(os, " ");
std::for_each(block.elements_begin(), block.elements_end(),
@@ -130,14 +130,17 @@ NdnDissect::printBlock(std::ostream& os, const Block& block)
void
NdnDissect::dissect(std::ostream& os, std::istream& is)
{
while (is.peek() != std::char_traits<char>::eof()) {
try {
printBlock(os, Block::fromStream(is));
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
size_t offset = 0;
try {
while (is.peek() != std::istream::traits_type::eof()) {
auto block = Block::fromStream(is);
printBlock(os, block);
offset += block.size();
}
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
}
}
} // namespace dissect