From dce7c0afa22ca6568bbf6cca6a9cd669ef8b4b28 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Fri, 3 Jun 2016 21:41:41 +0300 Subject: util: add helpers --- libkms++util/helpers.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ libkms++util/kms++util.h | 4 ++++ libkms++util/strhelpers.cpp | 27 +++++++++++++++++++++++++++ libkms++util/strhelpers.h | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 libkms++util/helpers.cpp create mode 100644 libkms++util/strhelpers.cpp create mode 100644 libkms++util/strhelpers.h 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 + #include "color.h" #include #include @@ -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 +#include + +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 +#include +#include +#include + +std::string to_lower(const std::string& str); + +template +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 +std::string join(const std::vector& values, const std::string& delim, std::function 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))); -- cgit v1.2.3