platformdirs 0.1.0
📂 Python's platformdirs module for C++
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cctype>
4#include <filesystem>
5#include <locale>
6#include <string>
7#include <vector>
8#if __COSMOPOLITAN__
9#include <cosmo.h>
10#endif
11
12namespace platformdirs {
13namespace utils {
14
15// https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
16// trim from start (in place)
17inline void ltrim(std::string &s) {
18 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
19 return !std::isspace(ch);
20 }));
21}
22
23// https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
24// trim from end (in place)
25inline void rtrim(std::string &s) {
26 s.erase(std::find_if(s.rbegin(), s.rend(),
27 [](unsigned char ch) { return !std::isspace(ch); })
28 .base(),
29 s.end());
30}
31
32inline auto home_path() -> std::filesystem::path {
33#if __COSMOPOLITAN__
34 if (IsWindows()) {
35 if (auto const userprofile_dir = std::getenv("USERPROFILE")) {
36 return std::filesystem::path(userprofile_dir);
37 } else {
38 throw std::runtime_error("%USERPROFILE% not set");
39 }
40 } else {
41 if (auto const home_dir = std::getenv("HOME")) {
42 return std::filesystem::path(home_dir);
43 } else {
44 throw std::runtime_error("$HOME not set");
45 }
46 }
47#elif _WIN32
48 if (auto const userprofile_dir = std::getenv("USERPROFILE")) {
49 return std::filesystem::path(userprofile_dir);
50 } else {
51 throw std::runtime_error("%USERPROFILE% not set");
52 }
53#else
54 if (auto const home_dir = std::getenv("HOME")) {
55 return std::filesystem::path(home_dir);
56 } else {
57 throw std::runtime_error("$HOME not set");
58 }
59#endif
60}
61
62inline std::vector<std::string> split(std::string s, std::string delimiter) {
63 size_t pos_start = 0, pos_end, delim_len = delimiter.length();
64 std::string token;
65 std::vector<std::string> res;
66
67 while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
68 token = s.substr(pos_start, pos_end - pos_start);
69 pos_start = pos_end + delim_len;
70 res.push_back(token);
71 }
72
73 res.push_back(s.substr(pos_start));
74 return res;
75}
76
77inline std::string pathsep() {
78#if __COSMOPOLITAN__
79 return IsWindows() ? ";" : ":";
80#elif _WIN32
81 return ";";
82#else
83 return ":";
84#endif
85}
86
87inline std::string join(const std::vector<std::string> &lst,
88 const std::string &delim) {
89 std::string ret;
90 for (const auto &s : lst) {
91 if (!ret.empty())
92 ret += delim;
93 ret += s;
94 }
95 return ret;
96}
97
98} // namespace utils
99} // namespace platformdirs
auto home_path() -> std::filesystem::path
Definition utils.h:32
void rtrim(std::string &s)
Definition utils.h:25
void ltrim(std::string &s)
Definition utils.h:17
std::vector< std::string > split(std::string s, std::string delimiter)
Definition utils.h:62
std::string pathsep()
Definition utils.h:77
std::string join(const std::vector< std::string > &lst, const std::string &delim)
Definition utils.h:87
Definition platformdirs.h:27