mmCEsim 0.3.0
mmWave Channel Estimation Simulation
utils.h
Go to the documentation of this file.
1
12#ifndef _UTILS_H_
13#define _UTILS_H_
14
15#include <algorithm>
16#include <cctype>
17#include <random>
18#include <sstream>
19#include <string>
20#include <vector>
21#ifndef __linux__
22# include "_boost_config.h"
23# include <boost/dll/runtime_symbol_info.hpp>
24#else
25# include <libgen.h> // dirname
26# include <linux/limits.h> // PATH_MAX
27# include <unistd.h> // readlink
28#endif
29#ifndef _WIN32
30# include <pwd.h>
31# include <sys/types.h>
32# include <unistd.h>
33#endif
34
35using std::string_literals::operator""s;
36
37// https://stackoverflow.com/a/217605/15080514
38
39// trim from start (in place)
40static inline void ltrim(std::string& s) {
41 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
42}
43
44// trim from end (in place)
45static inline void rtrim(std::string& s) {
46 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
47}
48
49// trim from both ends (in place)
50static inline void trim(std::string& s) {
51 ltrim(s);
52 rtrim(s);
53}
54
55// trim from start (copying)
56static inline std::string ltrim_copy(std::string s) {
57 ltrim(s);
58 return s;
59}
60
61// trim from end (copying)
62static inline std::string rtrim_copy(std::string s) {
63 rtrim(s);
64 return s;
65}
66
67// trim from both ends (copying)
68static inline std::string trim_copy(std::string s) {
69 trim(s);
70 return s;
71}
72
73template <typename T>
74T strAs(const std::string& s) {
75 std::stringstream ss(s);
76 T t;
77 ss >> t;
78 return t;
79}
80
87static inline bool isUInt(const std::string& s) {
88 return !s.empty() && std::find_if(s.begin(), s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
89}
90
91// If the string is quoted with a character
92static inline bool isQuoted(const std::string& s, char c = '"') {
93 if (s.length() < 2) return false;
94 if (s[0] == c && *(s.end() - 1) == c) return true;
95 else return false;
96}
97
98// If the string is quoted, remove the quote.
99static std::string removeQuote(const std::string& s) {
100 if (isQuoted(s, '"') || isQuoted(s, '\'')) {
101 // If the above statement is true,
102 // 's' is surely longer than 2.
103 return s.substr(1, s.length() - 2);
104 } else {
105 return s;
106 }
107}
108
109// remove all white space in a string
110static inline std::string& removeSpaceInPlace(std::string& str) {
111 str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x) { return std::isspace(x); }), str.end());
112 return str;
113}
114
115// remove all white space in a string and make a copy
116static inline std::string removeSpaceCopy(const std::string& str) {
117 std::string new_str = str;
118 new_str.erase(std::remove_if(new_str.begin(), new_str.end(), [](unsigned char x) { return std::isspace(x); }),
119 new_str.end());
120 return new_str;
121}
122
123// get a vector of string as one string
124static std::string stringVecAsString(const std::vector<std::string>& l, std::string div = " ") {
125 if (l.empty()) return "";
126 std::string str;
127 for (auto iter = l.cbegin(); iter != l.cend() - 1; ++iter) { str += *iter + div; }
128 return str + *(l.cend() - 1);
129}
130
131// check if STL container contains a value
132template <typename T>
133static inline bool contains(const T& container, const typename T::value_type value) {
134 for (auto&& elem : container) {
135 if (elem == value) return true;
136 }
137 return false;
138}
139
140#ifndef _WIN32
141static inline std::string homeDir() {
142 const char* homedir = std::getenv("HOME");
143 return (homedir && homedir[0] != '~') ? homedir : getpwuid(getuid())->pw_dir;
144}
145#endif
146
147// get the application directory
148static inline std::string appDir() {
149#ifndef __linux__
150 // application directory
151 return boost::dll::program_location().remove_filename().string();
152#else
153 char result[PATH_MAX] = {};
154 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
155 const char* path = "";
156 if (count != -1) path = dirname(result);
157 return path;
158#endif
159}
160
161// get the data directory (i.e., log file directory)
162static inline std::string dataDir() {
163#ifdef _WIN32
164 return appDir();
165#else
166 // Reference: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
167 const char* XDG_DATA_HOME = std::getenv("XDG_DATA_HOME");
168 return XDG_DATA_HOME ? (XDG_DATA_HOME[0] == '~' ? homeDir() + std::string(XDG_DATA_HOME).substr(1)
169 : std::string(XDG_DATA_HOME))
170 : homeDir() + "/.local/share";
171#endif
172}
173
174// random string
175// Reference: https://stackoverflow.com/a/24586587/15080514
176static std::string randomString(std::string::size_type length) {
177 static auto& chrs = "0123456789"
178 "abcdefghijklmnopqrstuvwxyz"
179 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
180 thread_local static std::mt19937 rg{ std::random_device{}() };
181 thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
182 std::string s;
183 s.reserve(length);
184 while (length--) { s += chrs[pick(rg)]; }
185 return s;
186}
187
193namespace mmce {
202template <typename T>
203static inline std::string to_string(const T& x) {
204 std::ostringstream str;
205 str << x;
206 return str.str();
207}
208} // namespace mmce
209
210#endif
Boost Configurations.
Utilities specified for mmCEsim.
Definition: utils.h:193
static std::string to_string(const T &x)
Change a number to string.
Definition: utils.h:203
str
Definition: version_bump.py:14
static std::string removeQuote(const std::string &s)
Definition: utils.h:99
T strAs(const std::string &s)
Definition: utils.h:74
static void ltrim(std::string &s)
Definition: utils.h:40
static std::string appDir()
Definition: utils.h:148
static std::string dataDir()
Definition: utils.h:162
static std::string randomString(std::string::size_type length)
Definition: utils.h:176
static void trim(std::string &s)
Definition: utils.h:50
static std::string homeDir()
Definition: utils.h:141
static std::string trim_copy(std::string s)
Definition: utils.h:68
static bool isUInt(const std::string &s)
Check if the string is an unsigned integer.
Definition: utils.h:87
static bool isQuoted(const std::string &s, char c='"')
Definition: utils.h:92
static std::string removeSpaceCopy(const std::string &str)
Definition: utils.h:116
static std::string ltrim_copy(std::string s)
Definition: utils.h:56
static std::string rtrim_copy(std::string s)
Definition: utils.h:62
static std::string stringVecAsString(const std::vector< std::string > &l, std::string div=" ")
Definition: utils.h:124
static bool contains(const T &container, const typename T::value_type value)
Definition: utils.h:133
static std::string & removeSpaceInPlace(std::string &str)
Definition: utils.h:110
static void rtrim(std::string &s)
Definition: utils.h:45