srctree

Robin Linden parent 48042bcc 10b958ad
os: Add new library for OS abstraction layer stuff

inlinesplit
filename was Deleted added: 111, removed: 5, total 106
@@ -0,0 +1,29 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 
cc_library(
name = "os",
srcs = select({
"@platforms//os:linux": ["linux.cpp"],
"@platforms//os:windows": ["windows.cpp"],
}),
hdrs = ["os.h"],
linkopts = select({
"@platforms//os:linux": [],
"@platforms//os:windows": [
"-DEFAULTLIB:Kernel32",
"-DEFAULTLIB:Ole32",
"-DEFAULTLIB:Shell32",
],
}),
visibility = ["//visibility:public"],
)
 
cc_test(
name = "os_test",
size = "small",
srcs = ["os_test.cpp"],
deps = [
":os",
"//etest",
],
)
 
filename was Deleted added: 111, removed: 5, total 106
@@ -0,0 +1,27 @@
#include "os/os.h"
 
#include <cstdlib>
 
using namespace std::literals;
 
namespace os {
 
std::vector<std::string> font_paths() {
std::vector<std::string> paths{};
if (char const *xdg_data_home = std::getenv("XDG_DATA_HOME")) {
paths.push_back(xdg_data_home + "/fonts"s);
}
 
if (char const *home = std::getenv("HOME")) {
if (paths.empty()) {
paths.push_back(home + "/.local/share/fonts"s);
}
paths.push_back(home + "/.fonts"s);
}
 
paths.push_back("/usr/share/fonts"s);
paths.push_back("/usr/local/share/fonts"s);
return paths;
}
 
} // namespace os
 
filename was Deleted added: 111, removed: 5, total 106
@@ -0,0 +1,13 @@
#ifndef OS_OS_H_
#define OS_OS_H_
 
#include <string>
#include <vector>
 
namespace os {
 
std::vector<std::string> font_paths();
 
} // namespace os
 
#endif
 
filename was Deleted added: 111, removed: 5, total 106
@@ -0,0 +1,14 @@
#include "os/os.h"
 
#include "etest/etest.h"
 
using etest::expect;
 
int main() {
etest::test("font_paths", [] {
auto font_paths = os::font_paths();
expect(!font_paths.empty());
});
 
return etest::run_all_tests();
}
 
filename was Deleted added: 111, removed: 5, total 106
@@ -0,0 +1,23 @@
#include "os/os.h"
 
// Must be included first because Windows headers don't include what they use.
#include <Windows.h>
 
#include <Knownfolders.h>
#include <Objbase.h>
#include <Shlobj.h>
 
namespace os {
 
std::vector<std::string> font_paths() {
PWSTR bad_font_path{nullptr};
SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &bad_font_path);
auto chars_needed = WideCharToMultiByte(CP_UTF8, 0, bad_font_path, -1, nullptr, 0, NULL, NULL);
std::string font_path;
font_path.resize(chars_needed);
WideCharToMultiByte(CP_UTF8, 0, bad_font_path, -1, font_path.data(), chars_needed, NULL, NULL);
CoTaskMemFree(bad_font_path);
return {font_path};
}
 
} // namespace os