bux API Reference 1.11.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
StrUtil.cpp
Go to the documentation of this file.
1#ifdef _WIN32
2 #include <windows.h> // to evade "No Target Architecture" error
3#endif
4#include "StrUtil.h"
5//------------------------------------------------------------------------
6#include <cstring> // strstr(), strlen()
7#ifdef _WIN32
8 #include <processenv.h> // ExpandEnvironmentStringsA()
9#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
10 #include <wordexp.h> // wordexp(), wordfree()
11#else
12 #define DISABLE_EXPAND_ENV_
13#endif
14#ifdef __unix__
15 #include <cxxabi.h> // abi::__cxa_demangle()
16#endif
17
18
19namespace bux {
20
21//
22// Functions
23//
24const char *ord_suffix(size_t i)
31{
32 switch (i)
33 {
34 case 1:
35 return "st";
36 case 2:
37 return "nd";
38 case 3:
39 return "rd";
40 }
41 return "th";
42}
43
44#ifndef DISABLE_EXPAND_ENV_
45std::string expand_env(const char *s)
49{
50#ifdef _WIN32
51 char buf[2048];
52 if (const auto n = ExpandEnvironmentStringsA(s, buf, sizeof buf))
53 return {buf, n};
54#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
55 wordexp_t p;
56 wordexp(s, &p, 0);
57 std::string ret;
58 bool done{};
59 if (p.we_wordc == 1)
60 {
61 ret = p.we_wordv[0];
62 done = true;
63 }
64 wordfree(&p);
65 if (done)
66 return ret;
67#endif
68 throw std::runtime_error(std::string("Fail to expand \"")+s+'"');
69}
70#endif //DISABLE_EXPAND_ENV_
71
72std::string _HRTN(const char *originalName)
76{
77 std::string ret;
78#ifdef __unix__
79 int status;
80 char *const name = abi::__cxa_demangle(originalName, NULL, NULL, &status);
81 if (!status)
82 {
83 ret = name;
84 free(name);
85 }
86 else
87 {
88 ret.assign(originalName).append(" with demangling error ") += std::to_string(status);
89 }
90#else
91 struct C_KeyMap // POD
92 {
93 const char *m_Key;
94 const char *m_Value;
95 };
96 static const C_KeyMap MAP[] ={
97 {typeid(std::string).name(), "std::string"}
98 };
99
100 for (auto i: MAP)
101 {
102 std::string t;
103 const char *cur = originalName;
104 const char *const key = i.m_Key;
105 for (const char *pos; *cur && (pos = strstr(cur,key)) != 0;)
106 {
107 if (cur < pos)
108 t.append(cur, pos);
109
110 t.append(i.m_Value);
111 cur = pos + strlen(key);
112 while (*cur == ' ') ++cur;
113 }
114
115 if (!t.empty())
116 {
117 if (*cur)
118 t.append(cur);
119
120 ret = t;
121 originalName = ret.c_str();
122 }
123 }
124 if (ret.empty())
125 ret = originalName;
126#endif
127 return ret;
128}
129
130std::string OXCPT(const std::exception &e)
134{
135 return HRTN(e) + ": " + e.what();
136}
137
138} // namespace bux
#define HRTN(t)
Definition StrUtil.h:22
THE common namespace of bux library.
Definition AtomiX.cpp:3
const char * ord_suffix(size_t i)
Definition StrUtil.cpp:24
std::string _HRTN(const char *originalName)
Definition StrUtil.cpp:72
std::string OXCPT(const std::exception &e)
Definition StrUtil.cpp:130
std::string expand_env(const char *s)
Definition StrUtil.cpp:45