bux API Reference 1.9.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 #error "Platform other than Windows and Unix-like not supported yet"
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
44std::string expand_env(const char *s)
48{
49#ifdef _WIN32
50 char buf[2048];
51 if (const auto n = ExpandEnvironmentStringsA(s, buf, sizeof buf))
52 return {buf, n};
53#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
54 wordexp_t p;
55 wordexp(s, &p, 0);
56 std::string ret;
57 bool done{};
58 if (p.we_wordc == 1)
59 {
60 ret = p.we_wordv[0];
61 done = true;
62 }
63 wordfree(&p);
64 if (done)
65 return ret;
66#endif
67 throw std::runtime_error(std::string("Fail to expand \"")+s+'"');
68}
69
70std::string _HRTN(const char *originalName)
74{
75 std::string ret;
76#ifdef __unix__
77 int status;
78 char *const name = abi::__cxa_demangle(originalName, NULL, NULL, &status);
79 if (!status)
80 {
81 ret = name;
82 free(name);
83 }
84 else
85 {
86 ret.assign(originalName).append(" with demangling error ") += std::to_string(status);
87 }
88#else
89 struct C_KeyMap // POD
90 {
91 const char *m_Key;
92 const char *m_Value;
93 };
94 static const C_KeyMap MAP[] ={
95 {typeid(std::string).name(), "std::string"}
96 };
97
98 for (auto i: MAP)
99 {
100 std::string t;
101 const char *cur = originalName;
102 const char *const key = i.m_Key;
103 for (const char *pos; *cur && (pos = strstr(cur,key)) != 0;)
104 {
105 if (cur < pos)
106 t.append(cur, pos);
107
108 t.append(i.m_Value);
109 cur = pos + strlen(key);
110 while (*cur == ' ') ++cur;
111 }
112
113 if (!t.empty())
114 {
115 if (*cur)
116 t.append(cur);
117
118 ret = t;
119 originalName = ret.c_str();
120 }
121 }
122 if (ret.empty())
123 ret = originalName;
124#endif
125 return ret;
126}
127
128std::string OXCPT(const std::exception &e)
132{
133 return HRTN(e) + ": " + e.what();
134}
135
136} // 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:70
std::string OXCPT(const std::exception &e)
Definition StrUtil.cpp:128
std::string expand_env(const char *s)
Definition StrUtil.cpp:44