Add log_displayer support for -m and -p.

These options work as advertised in the help message. Also add
a new function in util for converting strings to numbers.

Change-Id: I33ab9add3f55c9139557b30ffe4de633782933e5
diff --git a/aos/common/util/string_to_num.h b/aos/common/util/string_to_num.h
new file mode 100644
index 0000000..c6dcb30
--- /dev/null
+++ b/aos/common/util/string_to_num.h
@@ -0,0 +1,29 @@
+#ifndef AOS_COMMON_UTIL_STRING_TO_NUM_H_
+#define AOS_COMMON_UTIL_STRING_TO_NUM_H_
+
+#include <sstream>
+#include <string>
+
+namespace aos {
+namespace util {
+
+// Converts a string into a specified integral type. If it can't be converted
+// completely or at all, or if the converted number would overflow the
+// specified integral type, it returns false.
+template<typename T>
+inline bool StringToInteger(const ::std::string &input, T *out_num) {
+  ::std::istringstream stream(input);
+  stream >> *out_num;
+
+  if (stream.fail() || !stream.eof()) {
+    return false;
+  }
+
+  return true;
+}
+
+
+}  // util
+}  // aos
+
+#endif
diff --git a/aos/common/util/string_to_num_test.cc b/aos/common/util/string_to_num_test.cc
new file mode 100644
index 0000000..e1ee1cb
--- /dev/null
+++ b/aos/common/util/string_to_num_test.cc
@@ -0,0 +1,43 @@
+#include <stdint.h>
+
+#include <string>
+
+#include "gtest/gtest.h"
+
+#include "aos/common/util/string_to_num.h"
+
+namespace aos {
+namespace util {
+namespace testing {
+
+TEST(StringToNumTest, CorrectNumber) {
+  int result;
+  ASSERT_TRUE(StringToInteger<int>(::std::string("42"), &result));
+  EXPECT_EQ(result, 42);
+}
+
+TEST(StringToNumTest, NegativeTest) {
+  int result;
+  ASSERT_TRUE(StringToInteger<int>(::std::string("-42"), &result));
+  EXPECT_EQ(result, -42);
+}
+
+TEST(StringToNumTest, NonNumber) {
+  int result;
+  ASSERT_FALSE(StringToInteger<int>(::std::string("Daniel"), &result));
+}
+
+TEST(StringToNumTest, NumberWithText) {
+  int result;
+  ASSERT_FALSE(StringToInteger<int>(::std::string("42Daniel"), &result));
+}
+
+TEST(StringToNumTest, OverflowTest) {
+  uint32_t result;
+  // 2 << 32 should overflow.
+  ASSERT_FALSE(StringToInteger<uint32_t>(::std::string("4294967296"), &result));
+}
+
+}  // testing
+}  // util
+}  // aos
diff --git a/aos/common/util/util.gyp b/aos/common/util/util.gyp
index fa1fb7f..5008dd8 100644
--- a/aos/common/util/util.gyp
+++ b/aos/common/util/util.gyp
@@ -70,6 +70,24 @@
       ],
     },
     {
+      'target_name': 'string_to_num',
+      'type': 'static_library',
+      'sources': [
+        #'string_to_num.h',
+      ],
+    },
+    {
+      'target_name': 'string_to_num_test',
+      'type': 'executable',
+      'sources': [
+        'string_to_num_test.cc',
+      ],
+      'dependencies': [
+        ':string_to_num',
+        '<(EXTERNALS):gtest',
+      ],
+    },
+    {
       'target_name': 'thread',
       'type': 'static_library',
       'sources': [