dissect: print offset of parsing error
Change-Id: I2209a16330e513b46abe2dfcc57ca780b3478b82
diff --git a/tools/dissect/main.cpp b/tools/dissect/main.cpp
index fb14692..a7d1e3e 100644
--- a/tools/dissect/main.cpp
+++ b/tools/dissect/main.cpp
@@ -32,10 +32,9 @@
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 @@
{
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 @@
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 @@
}
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);
diff --git a/tools/dissect/ndn-dissect.cpp b/tools/dissect/ndn-dissect.cpp
index 907eef6..4971d03 100644
--- a/tools/dissect/ndn-dissect.cpp
+++ b/tools/dissect/ndn-dissect.cpp
@@ -118,9 +118,9 @@
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,13 +130,16 @@
void
NdnDissect::dissect(std::ostream& os, std::istream& is)
{
- while (is.peek() != std::char_traits<char>::eof()) {
- try {
- printBlock(os, Block::fromStream(is));
+ 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() << std::endl;
- }
+ }
+ catch (const std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
}
}