2020-09-06 00:58:11 +00:00
|
|
|
#pragma once
|
2020-09-08 03:03:29 +00:00
|
|
|
#include <cctype>
|
2020-09-06 00:58:11 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2020-09-06 01:05:52 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <iomanip>
|
2020-10-04 06:28:48 +00:00
|
|
|
#include <regex>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2020-11-20 00:18:59 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <type_traits>
|
2020-12-13 02:57:39 +00:00
|
|
|
#include <gtkmm.h>
|
2020-11-20 00:18:59 +00:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
template<typename T>
|
|
|
|
struct is_optional : ::std::false_type {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct is_optional<::std::optional<T>> : ::std::true_type {};
|
2020-12-13 02:57:39 +00:00
|
|
|
} // namespace util
|
2020-09-06 00:58:11 +00:00
|
|
|
|
2020-10-04 06:28:48 +00:00
|
|
|
class Semaphore {
|
|
|
|
public:
|
|
|
|
Semaphore(int count = 0)
|
|
|
|
: m_count(count) {}
|
|
|
|
|
|
|
|
inline void notify() {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
m_count++;
|
|
|
|
lock.unlock();
|
|
|
|
m_cv.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void wait() {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
while (m_count == 0)
|
|
|
|
m_cv.wait(lock);
|
|
|
|
m_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex m_mutex;
|
|
|
|
std::condition_variable m_cv;
|
|
|
|
int m_count;
|
|
|
|
};
|
2020-09-30 04:12:38 +00:00
|
|
|
// gtkmm doesnt seem to work
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-18 02:07:12 +00:00
|
|
|
inline void LaunchBrowser(Glib::ustring url) {
|
|
|
|
GError *err = nullptr;
|
|
|
|
if (!gtk_show_uri_on_window(nullptr, url.c_str(), GDK_CURRENT_TIME, &err))
|
|
|
|
printf("failed to open uri: %s\n", err->message);
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 22:49:22 +00:00
|
|
|
inline void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw = 400, int clamph = 300) {
|
|
|
|
const auto frac = static_cast<float>(inw) / inh;
|
|
|
|
|
|
|
|
outw = inw;
|
|
|
|
outh = inh;
|
|
|
|
|
|
|
|
if (outw > clampw) {
|
|
|
|
outw = clampw;
|
|
|
|
outh = clampw / frac;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outh > clamph) {
|
|
|
|
outh = clamph;
|
|
|
|
outw = clamph * frac;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::vector<uint8_t> ReadWholeFile(std::string path) {
|
|
|
|
std::vector<uint8_t> ret;
|
|
|
|
FILE *fp = std::fopen(path.c_str(), "rb");
|
|
|
|
if (fp == nullptr)
|
|
|
|
return ret;
|
|
|
|
std::fseek(fp, 0, SEEK_END);
|
|
|
|
int len = std::ftell(fp);
|
|
|
|
std::rewind(fp);
|
|
|
|
ret.resize(len);
|
|
|
|
std::fread(ret.data(), 1, ret.size(), fp);
|
|
|
|
std::fclose(fp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
inline std::string HumanReadableBytes(uint64_t bytes) {
|
|
|
|
constexpr static const char *x[] = { "B", "KB", "MB", "GB", "TB" };
|
|
|
|
int order = 0;
|
|
|
|
while (bytes >= 1000 && order < 4) { // 4=len(x)-1
|
|
|
|
order++;
|
|
|
|
bytes /= 1000;
|
|
|
|
}
|
|
|
|
return std::to_string(bytes) + x[order];
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:15:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Bitwise {
|
|
|
|
static const bool enable = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<Bitwise<T>::enable, T>::type operator|(T a, T b) {
|
|
|
|
using x = typename std::underlying_type<T>::type;
|
|
|
|
return static_cast<T>(static_cast<x>(a) | static_cast<x>(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<Bitwise<T>::enable, T>::type operator|=(T &a, T b) {
|
|
|
|
using x = typename std::underlying_type<T>::type;
|
|
|
|
a = static_cast<T>(static_cast<x>(a) | static_cast<x>(b));
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<Bitwise<T>::enable, T>::type operator&(T a, T b) {
|
|
|
|
using x = typename std::underlying_type<T>::type;
|
|
|
|
return static_cast<T>(static_cast<x>(a) & static_cast<x>(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<Bitwise<T>::enable, T>::type operator&=(T &a, T b) {
|
|
|
|
using x = typename std::underlying_type<T>::type;
|
|
|
|
a = static_cast<T>(static_cast<x>(a) & static_cast<x>(b));
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<Bitwise<T>::enable, T>::type operator~(T a) {
|
2020-09-24 06:58:05 +00:00
|
|
|
return static_cast<T>(~static_cast<typename std::underlying_type<T>::type>(a));
|
2020-09-24 06:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 00:58:11 +00:00
|
|
|
template<typename T>
|
2020-09-08 03:03:29 +00:00
|
|
|
inline void AlphabeticalSort(T start, T end, std::function<std::string(const typename std::iterator_traits<T>::value_type &)> get_string) {
|
2020-09-06 00:58:11 +00:00
|
|
|
std::sort(start, end, [&](const auto &a, const auto &b) -> bool {
|
|
|
|
const std::string &s1 = get_string(a);
|
|
|
|
const std::string &s2 = get_string(b);
|
|
|
|
|
|
|
|
if (s1.empty() || s2.empty())
|
|
|
|
return s1 < s2;
|
|
|
|
|
|
|
|
bool ac[] = {
|
|
|
|
!isalnum(s1[0]),
|
|
|
|
!isalnum(s2[0]),
|
|
|
|
!!isdigit(s1[0]),
|
|
|
|
!!isdigit(s2[0]),
|
|
|
|
!!isalpha(s1[0]),
|
|
|
|
!!isalpha(s2[0]),
|
|
|
|
};
|
|
|
|
|
|
|
|
if ((ac[0] && ac[1]) || (ac[2] && ac[3]) || (ac[4] && ac[5]))
|
|
|
|
return s1 < s2;
|
|
|
|
|
|
|
|
return ac[0] || ac[5];
|
|
|
|
});
|
|
|
|
}
|
2020-09-06 01:05:52 +00:00
|
|
|
|
|
|
|
inline std::string IntToCSSColor(int color) {
|
|
|
|
int r = (color & 0xFF0000) >> 16;
|
|
|
|
int g = (color & 0x00FF00) >> 8;
|
|
|
|
int b = (color & 0x0000FF) >> 0;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << std::setw(2) << std::setfill('0') << r
|
|
|
|
<< std::hex << std::setw(2) << std::setfill('0') << g
|
|
|
|
<< std::hex << std::setw(2) << std::setfill('0') << b;
|
|
|
|
return ss.str();
|
|
|
|
}
|
2020-09-06 03:04:11 +00:00
|
|
|
|
2020-12-13 02:57:39 +00:00
|
|
|
inline void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu) {
|
|
|
|
widget->signal_button_press_event().connect([&menu](GdkEventButton *ev) -> bool {
|
|
|
|
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
|
|
|
|
menu.popup_at_pointer(reinterpret_cast<const GdkEvent *>(ev));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
// clang-format off
|
|
|
|
}, false);
|
|
|
|
// clang-format on
|
|
|
|
}
|
2020-12-18 06:13:31 +00:00
|
|
|
|
|
|
|
inline std::vector<std::string> StringSplit(const std::string &str, const char *delim) {
|
|
|
|
std::vector<std::string> parts;
|
|
|
|
char *token = std::strtok(const_cast<char *>(str.c_str()), delim);
|
|
|
|
while (token != nullptr) {
|
|
|
|
parts.push_back(token);
|
|
|
|
token = std::strtok(nullptr, delim);
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string GetExtension(std::string url) {
|
|
|
|
url = StringSplit(url, "?")[0];
|
|
|
|
url = StringSplit(url, "/").back();
|
|
|
|
return url.find(".") != std::string::npos ? url.substr(url.find_last_of(".")) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsURLViewableImage(const std::string &url) {
|
|
|
|
const auto ext = GetExtension(url);
|
|
|
|
static const char *exts[] = { ".jpeg",
|
|
|
|
".jpg",
|
|
|
|
".png", nullptr };
|
|
|
|
const char *str = ext.c_str();
|
|
|
|
for (int i = 0; exts[i] != nullptr; i++)
|
|
|
|
if (strcmp(str, exts[i]) == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|