bux API Reference 1.12.3
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(__ANDROID__) && (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 <cstdlib> // free()
16 #include <cxxabi.h> // abi::__cxa_demangle()
17#endif
18
19
20namespace bux {
21
22//
23// Functions
24//
25const char *ord_suffix(size_t i)
32{
33 switch (i)
34 {
35 case 1:
36 return "st";
37 case 2:
38 return "nd";
39 case 3:
40 return "rd";
41 }
42 return "th";
43}
44
45#ifndef DISABLE_EXPAND_ENV_
46std::string expand_env(const char *s)
50{
51#ifdef _WIN32
52 char buf[2048];
53 if (const auto n = ExpandEnvironmentStringsA(s, buf, sizeof buf))
54 return {buf, n};
55#elif defined(__unix__) || defined(__unix) || defined(__gnu_linux__)
56 wordexp_t p;
57 wordexp(s, &p, 0);
58 std::string ret;
59 bool done{};
60 if (p.we_wordc == 1)
61 {
62 ret = p.we_wordv[0];
63 done = true;
64 }
65 wordfree(&p);
66 if (done)
67 return ret;
68#endif
69 throw std::runtime_error(std::string("Fail to expand \"")+s+'"');
70}
71#endif //DISABLE_EXPAND_ENV_
72
73std::string _HRTN(const char *originalName)
77{
78 std::string ret;
79#ifdef __unix__
80 int status;
81 char *const name = abi::__cxa_demangle(originalName, NULL, NULL, &status);
82 if (!status)
83 {
84 ret = name;
85 free(name);
86 }
87 else
88 {
89 ret.assign(originalName).append(" with demangling error ") += std::to_string(status);
90 }
91#else
92 struct C_KeyMap // POD
93 {
94 const char *m_Key;
95 const char *m_Value;
96 };
97 static const C_KeyMap MAP[] ={
98 {typeid(std::string).name(), "std::string"}
99 };
100
101 for (auto i: MAP)
102 {
103 std::string t;
104 const char *cur = originalName;
105 const char *const key = i.m_Key;
106 for (const char *pos; *cur && (pos = strstr(cur,key)) != 0;)
107 {
108 if (cur < pos)
109 t.append(cur, pos);
110
111 t.append(i.m_Value);
112 cur = pos + strlen(key);
113 while (*cur == ' ') ++cur;
114 }
115
116 if (!t.empty())
117 {
118 if (*cur)
119 t.append(cur);
120
121 ret = t;
122 originalName = ret.c_str();
123 }
124 }
125 if (ret.empty())
126 ret = originalName;
127#endif
128 return ret;
129}
130
131std::string OXCPT(const std::exception &e)
135{
136 return HRTN(e) + ": " + e.what();
137}
138
139} // 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:25
std::string _HRTN(const char *originalName)
Definition StrUtil.cpp:73
std::string OXCPT(const std::exception &e)
Definition StrUtil.cpp:131
std::string expand_env(const char *s)
Definition StrUtil.cpp:46