File: utils.cpp

package info (click to toggle)
wcm 0.9.0%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,792 kB
  • sloc: cpp: 2,472; makefile: 7
file content (68 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "utils.hpp"
#include <algorithm>

#include <xkbcommon/xkbregistry.h>

bool find_string(std::string text, std::string pattern)
{
    if (text.empty() || pattern.empty())
    {
        return pattern.empty();
    }

    std::transform(text.begin(), text.end(), text.begin(), ::tolower);
    std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower);

    return text.find(pattern) != std::string::npos;
}

bool begins_with(const std::string & str, const std::string & prefix)
{
    return prefix.length() <= str.length() &&
           str.substr(0, prefix.length()) == prefix;
}

using rxkb_context_ptr = std::unique_ptr<rxkb_context, decltype(&rxkb_context_unref)>;

std::map<std::string, std::string> get_xkb_layouts(const std::string& ruleset)
{
    rxkb_context_ptr context =
    {rxkb_context_new(rxkb_context_flags::RXKB_CONTEXT_NO_FLAGS), &rxkb_context_unref};
    if (!rxkb_context_parse(context.get(), ruleset.c_str()))
    {
        return {};
    }

    std::map<std::string, std::string> layouts;
    for (rxkb_layout *layout = rxkb_layout_first(context.get());
         layout != nullptr;
         layout = rxkb_layout_next(layout))
    {
        if (rxkb_layout_get_variant(layout) == nullptr)
        {
            layouts.emplace(rxkb_layout_get_name(layout), rxkb_layout_get_description(layout));
        }
    }

    return layouts;
}

std::map<std::string, std::string> get_xkb_models(const std::string& ruleset)
{
    rxkb_context_ptr context =
    {rxkb_context_new(rxkb_context_flags::RXKB_CONTEXT_NO_FLAGS), &rxkb_context_unref};
    if (!rxkb_context_parse(context.get(), ruleset.c_str()))
    {
        return {};
    }

    std::map<std::string, std::string> models;
    for (rxkb_model *model = rxkb_model_first(context.get());
         model != nullptr;
         model = rxkb_model_next(model))
    {
        models.emplace(rxkb_model_get_name(model), rxkb_model_get_description(model));
    }

    return models;
}