dump: code style corrections

refs #3683

Change-Id: I694e04f418fd084f4f1b59693a336dea1b205ad3
diff --git a/tools/dump/main.cpp b/tools/dump/main.cpp
index 1ed697c..39723fa 100644
--- a/tools/dump/main.cpp
+++ b/tools/dump/main.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2015,  Regents of the University of California.
+ * Copyright (c) 2014-2016,  Regents of the University of California.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -95,18 +95,14 @@
 
   po::options_description hiddenOptions;
   hiddenOptions.add_options()
-    ("pcap-program", po::value<std::vector<std::string> >());
-  ;
+    ("pcap-program", po::value<std::vector<std::string>>());
 
   po::positional_options_description positionalArguments;
-  positionalArguments
-    .add("pcap-program", -1);
+  positionalArguments.add("pcap-program", -1);
 
   po::options_description allOptions;
-  allOptions
-    .add(visibleOptions)
-    .add(hiddenOptions)
-    ;
+  allOptions.add(visibleOptions)
+            .add(hiddenOptions);
 
   po::variables_map vm;
 
@@ -118,8 +114,8 @@
               vm);
     po::notify(vm);
   }
-  catch (std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
+  catch (const po::error& e) {
+    std::cerr << "ERROR: " << e.what() << "\n\n";
     usage(std::cerr, argv[0], visibleOptions);
     return 1;
   }
@@ -130,7 +126,7 @@
   }
 
   if (vm.count("version") > 0) {
-    std::cout << "ndndump " << tools::VERSION << std::endl;
+    std::cout << "ndndump " << tools::VERSION << '\n';
     return 0;
   }
 
@@ -139,8 +135,7 @@
   }
 
   if (vm.count("pcap-program") > 0) {
-    typedef std::vector<std::string> Strings;
-    const Strings& items = vm["pcap-program"].as<Strings>();
+    const auto& items = vm["pcap-program"].as<std::vector<std::string>>();
 
     std::ostringstream os;
     std::copy(items.begin(), items.end(), std::ostream_iterator<std::string>(os, " "));
@@ -148,7 +143,7 @@
   }
 
   if (vm.count("read") > 0 && vm.count("interface") > 0) {
-    std::cerr << "ERROR: Conflicting -r and -i options" << std::endl;
+    std::cerr << "ERROR: Conflicting -r and -i options\n";
     usage(std::cerr, argv[0], visibleOptions);
     return 2;
   }
diff --git a/tools/dump/ndndump.cpp b/tools/dump/ndndump.cpp
index e964c58..95875a9 100644
--- a/tools/dump/ndndump.cpp
+++ b/tools/dump/ndndump.cpp
@@ -64,6 +64,17 @@
 
 const size_t MAX_SNAPLEN = 65535;
 
+Ndndump::Ndndump()
+  : isVerbose(false)
+  , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
+  // , isSuccinct(false)
+  // , isMatchInverted(false)
+  // , shouldPrintStructure(false)
+  // , isTcpOnly(false)
+  // , isUdpOnly(false)
+{
+}
+
 void
 Ndndump::run()
 {
@@ -71,7 +82,7 @@
     char errbuf[PCAP_ERRBUF_SIZE];
     const char* pcapDevice = pcap_lookupdev(errbuf);
 
-    if (pcapDevice == 0) {
+    if (pcapDevice == nullptr) {
       throw Error(errbuf);
     }
 
@@ -94,14 +105,14 @@
   if (!interface.empty()) {
     char errbuf[PCAP_ERRBUF_SIZE];
     m_pcap = pcap_open_live(interface.c_str(), MAX_SNAPLEN, 0, 1000, errbuf);
-    if (m_pcap == 0) {
+    if (m_pcap == nullptr) {
       throw Error("Cannot open interface " + interface + "(" + errbuf + ")");
     }
   }
   else {
     char errbuf[PCAP_ERRBUF_SIZE];
     m_pcap = pcap_open_offline(inputFile.c_str(), errbuf);
-    if (m_pcap == 0) {
+    if (m_pcap == nullptr) {
       throw Error("Cannot file " + inputFile + " for reading (" + errbuf + ")");
     }
   }
@@ -112,34 +123,32 @@
     }
 
     bpf_program program;
-    int returnValue = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
+    int res = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
 
-    if (returnValue < 0) {
+    if (res < 0) {
       throw Error("Cannot parse tcpdump expression '" + pcapProgram +
                   "' (" + pcap_geterr(m_pcap) + ")");
     }
 
-    returnValue = pcap_setfilter(m_pcap, &program);
+    res = pcap_setfilter(m_pcap, &program);
     pcap_freecode(&program);
 
-    if (returnValue < 0) {
+    if (res < 0) {
       throw Error(std::string("pcap_setfilter failed (") + pcap_geterr(m_pcap) + ")");
     }
   }
 
   m_dataLinkType = pcap_datalink(m_pcap);
-  if (m_dataLinkType != DLT_EN10MB &&
-      m_dataLinkType != DLT_PPP)
-    {
-      throw Error("Unsupported pcap format (" + boost::lexical_cast<std::string>(m_dataLinkType));
-    }
+  if (m_dataLinkType != DLT_EN10MB && m_dataLinkType != DLT_PPP) {
+    throw Error("Unsupported pcap format (" + to_string(m_dataLinkType));
+  }
 
   pcap_loop(m_pcap, -1, &Ndndump::onCapturedPacket, reinterpret_cast<uint8_t*>(this));
 }
 
 
 void
-Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet)
+Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const
 {
   std::ostringstream os;
   printInterceptTime(os, header);
@@ -153,8 +162,8 @@
     return;
   }
 
-  int returnValue = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
-  if (returnValue < 0) {
+  int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
+  if (res < 0) {
     return;
   }
 
@@ -223,13 +232,13 @@
       std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl;
     }
   }
-  catch (tlv::Error& e) {
+  catch (const tlv::Error& e) {
     std::cerr << e.what() << std::endl;
   }
 }
 
 void
-Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header)
+Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const
 {
   os << header->ts.tv_sec
      << "."
@@ -251,13 +260,12 @@
 }
 
 int
-Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize)
+Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const
 {
   int frameType = 0;
 
   switch (m_dataLinkType) {
-  case DLT_EN10MB: // Ethernet frames can have Ethernet or 802.3 encapsulation
-    {
+    case DLT_EN10MB: { // Ethernet frames can have Ethernet or 802.3 encapsulation
       const ether_header* etherHeader = reinterpret_cast<const ether_header*>(payload);
 
       if (payloadSize < 0) {
@@ -271,16 +279,15 @@
 
       break;
     }
-  case DLT_PPP:
-    {
+    case DLT_PPP: {
       frameType = *payload;
-      payloadSize--;
-      payload++;
+      --payloadSize;
+      ++payload;
 
       if (!(frameType & 1)) {
         frameType = (frameType << 8) | *payload;
-        payloadSize--;
-        payload++;
+        --payloadSize;
+        ++payload;
       }
 
       if (payloadSize < 0) {
@@ -298,78 +305,74 @@
 int
 Ndndump::skipAndProcessFrameHeader(int frameType,
                                    const uint8_t*& payload, ssize_t& payloadSize,
-                                   std::ostream& os)
+                                   std::ostream& os) const
 {
-  switch (frameType)
-    {
-    case /*ETHERTYPE_IP*/0x0800:
-    case DLT_EN10MB: // pcap encapsulation
-      {
-        const ip* ipHeader = reinterpret_cast<const ip*>(payload);
-        size_t ipHeaderSize = IP_HL(ipHeader) * 4;
-        if (ipHeaderSize < 20) {
-          std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
-          return -1;
-        }
+  switch (frameType) {
+    case 0x0800: // ETHERTYPE_IP
+    case DLT_EN10MB: { // pcap encapsulation
+      const ip* ipHeader = reinterpret_cast<const ip*>(payload);
+      size_t ipHeaderSize = IP_HL(ipHeader) * 4;
+      if (ipHeaderSize < 20) {
+        std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
+        return -1;
+      }
 
-        os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
-        os << "To: "   << inet_ntoa(ipHeader->ip_dst);
+      os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
+      os << "To: "   << inet_ntoa(ipHeader->ip_dst);
 
-        payloadSize -= ipHeaderSize;
-        payload += ipHeaderSize;
+      payloadSize -= ipHeaderSize;
+      payload += ipHeaderSize;
 
-        if (payloadSize < 0) {
-          std::cerr << "Invalid pcap IP packet" << std::endl;
-          return -1;
-        }
+      if (payloadSize < 0) {
+        std::cerr << "Invalid pcap IP packet" << std::endl;
+        return -1;
+      }
 
-        switch (ipHeader->ip_p) {
-        case IPPROTO_UDP:
-          {
-            // if (!flags.udp)
-            //   return -1;
+      switch (ipHeader->ip_p) {
+        case IPPROTO_UDP: {
+          // if (!flags.udp)
+          //   return -1;
 
-            payloadSize -= sizeof(udphdr);
-            payload += sizeof(udphdr);
+          payloadSize -= sizeof(udphdr);
+          payload += sizeof(udphdr);
 
-            if (payloadSize < 0) {
-                std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
-                return -1;
-              }
-
-            os << ", Tunnel Type: UDP";
-            break;
+          if (payloadSize < 0) {
+            std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
+            return -1;
           }
-        case IPPROTO_TCP:
-          {
-            // if (!flags.tcp)
-            //   return -1;
 
-            const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
-            size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
+          os << ", Tunnel Type: UDP";
+          break;
+        }
+        case IPPROTO_TCP: {
+          // if (!flags.tcp)
+          //   return -1;
 
-            if (tcpHeaderSize < 20) {
-              std::cerr << "Invalid TCP Header len: "<< tcpHeaderSize <<" bytes" << std::endl;
-              return -1;
-            }
+          const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
+          size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
 
-            payloadSize -= tcpHeaderSize;
-            payload += tcpHeaderSize;
-
-            if (payloadSize < 0) {
-              std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
-              return -1;
-            }
-
-            os << ", Tunnel Type: TCP";
-            break;
+          if (tcpHeaderSize < 20) {
+            std::cerr << "Invalid TCP Header len: " << tcpHeaderSize <<" bytes" << std::endl;
+            return -1;
           }
+
+          payloadSize -= tcpHeaderSize;
+          payload += tcpHeaderSize;
+
+          if (payloadSize < 0) {
+            std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
+            return -1;
+          }
+
+          os << ", Tunnel Type: TCP";
+          break;
+        }
         default:
           return -1;
-        }
-
-        break;
       }
+
+      break;
+    }
     case /*ETHERTYPE_NDN*/0x7777:
       os << "Tunnel Type: EthernetFrame";
       break;
@@ -381,13 +384,23 @@
       payloadSize -= 2;
       payload += 2;
       break;
-    default:
+    default: // do nothing if it is not a recognized type of a packet
       return -1;
-      break; // do nothing if it is not a recognized type of a packet
-    }
+  }
 
   return 0;
 }
 
+bool
+Ndndump::matchesFilter(const Name& name) const
+{
+  if (nameFilter.empty())
+    return true;
+
+  /// \todo Switch to NDN regular expressions
+
+  return boost::regex_match(name.toUri(), nameFilter);
+}
+
 } // namespace dump
 } // namespace ndn
diff --git a/tools/dump/ndndump.hpp b/tools/dump/ndndump.hpp
index 594406d..6961a73 100644
--- a/tools/dump/ndndump.hpp
+++ b/tools/dump/ndndump.hpp
@@ -60,52 +60,35 @@
     }
   };
 
-  Ndndump()
-    : isVerbose(false)
-    , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
-    // , isSuccinct(false)
-    // , isMatchInverted(false)
-    // , shouldPrintStructure(false)
-    // , isTcpOnly(false)
-    // , isUdpOnly(false)
-  {
-  }
+  Ndndump();
 
   void
   run();
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   void
-  onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet);
+  onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const;
 
 private:
   static void
   onCapturedPacket(uint8_t* userData, const pcap_pkthdr* header, const uint8_t* packet)
   {
-    reinterpret_cast<Ndndump*>(userData)->onCapturedPacket(header, packet);
+    reinterpret_cast<const Ndndump*>(userData)->onCapturedPacket(header, packet);
   }
 
   void
-  printInterceptTime(std::ostream& os, const pcap_pkthdr* header);
+  printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const;
 
   int
-  skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize);
+  skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const;
 
   int
   skipAndProcessFrameHeader(int frameType,
                             const uint8_t*& payload, ssize_t& payloadSize,
-                            std::ostream& os);
+                            std::ostream& os) const;
 
   bool
-  matchesFilter(const Name& name)
-  {
-    if (nameFilter.empty())
-      return true;
-
-    /// \todo Switch to NDN regular expressions
-
-    return boost::regex_match(name.toUri(), nameFilter);
-  }
+  matchesFilter(const Name& name) const;
 
 public:
   bool isVerbose;