util: add helpers
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Fri, 3 Jun 2016 18:41:41 +0000 (21:41 +0300)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Sun, 5 Jun 2016 19:09:39 +0000 (22:09 +0300)
libkms++util/helpers.cpp [new file with mode: 0644]
libkms++util/kms++util.h
libkms++util/strhelpers.cpp [new file with mode: 0644]
libkms++util/strhelpers.h [new file with mode: 0644]

diff --git a/libkms++util/helpers.cpp b/libkms++util/helpers.cpp
new file mode 100644 (file)
index 0000000..4aa3194
--- /dev/null
@@ -0,0 +1,44 @@
+#include "kms++util.h"
+#include "strhelpers.h"
+
+using namespace std;
+
+namespace kms {
+
+Connector* resolve_connector(Card& card, const string& str)
+{
+       if (str.length() == 0)
+               return nullptr;
+
+       auto connectors = card.get_connectors();
+
+       if (str[0] == '@') {
+               char* endptr;
+               unsigned id = strtoul(str.c_str() + 1, &endptr, 10);
+               if (*endptr == 0) {
+                       Connector* c = card.get_connector(id);
+                       if (!c)
+                               return nullptr;
+                       else
+                               return c;
+               }
+       } else {
+               char* endptr;
+               unsigned idx = strtoul(str.c_str(), &endptr, 10);
+               if (*endptr == 0) {
+                       if (idx >= connectors.size())
+                               return nullptr;
+                       else
+                               return connectors[idx];
+               }
+       }
+
+       for (Connector* conn : connectors) {
+               if (to_lower(conn->fullname()).find(to_lower(str)) != string::npos)
+                       return conn;
+       }
+
+       return nullptr;
+}
+
+}
index c02c12cbfc4a37f08497bb73eb8c28122f527ac7..00e9fdfd31956fb67cbcdc78584c974cc725f4d3 100644 (file)
@@ -1,5 +1,7 @@
 #pragma once
 
 #pragma once
 
+#include <kms++.h>
+
 #include "color.h"
 #include <cstdio>
 #include <cstdlib>
 #include "color.h"
 #include <cstdio>
 #include <cstdlib>
@@ -13,6 +15,8 @@ void draw_color_bar(IMappedFramebuffer& buf, int old_xpos, int xpos, int width);
 void draw_test_pattern(IMappedFramebuffer &fb);
 
 void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color);
 void draw_test_pattern(IMappedFramebuffer &fb);
 
 void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color);
+
+Connector* resolve_connector(Card& card, const std::string& str);
 }
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 }
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
diff --git a/libkms++util/strhelpers.cpp b/libkms++util/strhelpers.cpp
new file mode 100644 (file)
index 0000000..f18c749
--- /dev/null
@@ -0,0 +1,27 @@
+#include "strhelpers.h"
+
+#include <algorithm>
+#include <stdarg.h>
+
+using namespace std;
+
+string to_lower(const string& str)
+{
+       string data = str;
+       transform(data.begin(), data.end(), data.begin(), ::tolower);
+       return data;
+}
+
+string sformat(const char *fmt, ...)
+{
+       static char s_format_buf[1024];
+
+       va_list args;
+       va_start(args, fmt);
+
+       vsnprintf(s_format_buf, sizeof(s_format_buf), fmt, args);
+
+       va_end(args);
+
+       return string(s_format_buf);
+}
diff --git a/libkms++util/strhelpers.h b/libkms++util/strhelpers.h
new file mode 100644 (file)
index 0000000..2c540f3
--- /dev/null
@@ -0,0 +1,33 @@
+#include <sstream>
+#include <string>
+#include <vector>
+#include <functional>
+
+std::string to_lower(const std::string& str);
+
+template <typename T>
+std::string join(const T& values, const std::string& delim)
+{
+       std::ostringstream ss;
+       for (const auto& v : values) {
+               if (&v != &values[0])
+                       ss << delim;
+               ss << v;
+       }
+       return ss.str();
+}
+
+template <typename T>
+std::string join(const std::vector<T>& values, const std::string& delim, std::function<std::string(T)> func)
+{
+       std::ostringstream ss;
+       for (const auto& v : values) {
+               if (&v != &values[0])
+                       ss << delim;
+               ss << func(v);
+       }
+       return ss.str();
+}
+
+std::string sformat(const char *fmt, ...)
+       __attribute__ ((format (printf, 1, 2)));