summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libkms++util/helpers.cpp44
-rw-r--r--libkms++util/kms++util.h4
-rw-r--r--libkms++util/strhelpers.cpp27
-rw-r--r--libkms++util/strhelpers.h33
4 files changed, 108 insertions, 0 deletions
diff --git a/libkms++util/helpers.cpp b/libkms++util/helpers.cpp
new file mode 100644
index 0000000..4aa3194
--- /dev/null
+++ b/libkms++util/helpers.cpp
@@ -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;
+}
+
+}
diff --git a/libkms++util/kms++util.h b/libkms++util/kms++util.h
index c02c12c..00e9fdf 100644
--- a/libkms++util/kms++util.h
+++ b/libkms++util/kms++util.h
@@ -1,5 +1,7 @@
#pragma once
+#include <kms++.h>
+
#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);
+
+Connector* resolve_connector(Card& card, const std::string& str);
}
#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
index 0000000..f18c749
--- /dev/null
+++ b/libkms++util/strhelpers.cpp
@@ -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
index 0000000..2c540f3
--- /dev/null
+++ b/libkms++util/strhelpers.h
@@ -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)));