20 #ifndef MIRTK_String_H 21 #define MIRTK_String_H 23 #include "mirtk/CommonExport.h" 32 #include "mirtk/Array.h" 58 inline bool iequal(
char const *a,
char const *b)
61 if (tolower(*a) != tolower(*b))
return false;
68 inline bool iequal(
const string &a,
char const *b)
70 return iequal(a.c_str(), b);
74 inline bool iequal(
const char *a,
const string &b)
76 return iequal(a, b.c_str());
80 inline bool iequal(
const string &a,
const string &b)
82 return iequal(a.c_str(), b.c_str());
90 MIRTK_Common_DEPRECATED
int ReadInt(std::ifstream &);
93 MIRTK_Common_DEPRECATED
float ReadFloat(std::ifstream &);
96 MIRTK_Common_DEPRECATED
char *
ReadString(std::ifstream &);
102 if (str == NULL || str[0] ==
'\0')
return false;
103 std::istringstream is(str);
104 return !(is >> value).fail() && is.eof();
108 template <
typename T>
118 if (strcmp(str,
"yes") == 0 || strcmp(str,
"Yes") == 0 || strcmp(str,
"YES") == 0) {
121 }
else if (strcmp(str,
"no") == 0 || strcmp(str,
"No") == 0 || strcmp(str,
"NO") == 0) {
124 }
else if (strcmp(str,
"true") == 0 || strcmp(str,
"True") == 0 || strcmp(str,
"TRUE") == 0) {
127 }
else if (strcmp(str,
"false") == 0 || strcmp(str,
"False") == 0 || strcmp(str,
"FALSE") == 0) {
130 }
else if (strcmp(str,
"on") == 0 || strcmp(str,
"On") == 0 || strcmp(str,
"ON") == 0) {
133 }
else if (strcmp(str,
"off") == 0 || strcmp(str,
"Off") == 0 || strcmp(str,
"OFF") == 0) {
137 std::istringstream is(str);
138 return !(is >> value).fail() && is.eof();
146 if (strcmp(str,
"nan") == 0 || strcmp(str,
"NaN") == 0) {
147 value = std::numeric_limits<float>::quiet_NaN();
149 }
else if (strcmp(str,
"-inf") == 0 || strcmp(str,
"-Inf") == 0) {
150 value = -std::numeric_limits<float>::infinity();
152 }
else if (strcmp(str,
"+inf") == 0 || strcmp(str,
"inf") == 0 || strcmp(str,
"+Inf") == 0 || strcmp(str,
"Inf") == 0) {
153 value = +std::numeric_limits<float>::infinity();
156 std::istringstream is(str);
157 return !(is >> value).fail() && is.eof();
165 if (strcmp(str,
"nan") == 0 || strcmp(str,
"NaN") == 0) {
166 value = std::numeric_limits<double>::quiet_NaN();
168 }
else if (strcmp(str,
"-inf") == 0 || strcmp(str,
"-Inf") == 0) {
169 value = -std::numeric_limits<double>::infinity();
171 }
else if (strcmp(str,
"+inf") == 0 || strcmp(str,
"inf") == 0 || strcmp(str,
"+Inf") == 0 || strcmp(str,
"Inf") == 0) {
172 value = +std::numeric_limits<double>::infinity();
175 std::istringstream is(str);
176 return !(is >> value).fail() && is.eof();
190 return IsValueOfType<double>(str);
196 return IsValueOfType<int>(str);
200 template <
typename T>
201 string ToString(
const T &value,
int w = 0,
char c =
' ',
bool left =
false)
203 std::ostringstream os;
205 if (left) os << std::left;
206 else os << std::right;
207 os << std::setw(w) << value;
213 inline string ToString(
const bool &value,
int w,
char c,
bool left)
215 std::ostringstream os;
217 if (left) os << std::left;
218 else os << std::right;
219 os << std::setw(w) << (value ?
"Yes" :
"No");
224 inline void PrintParameter(std::ostream &os,
const char *name,
const char *value)
226 const std::streamsize w = os.width(40);
227 os << std::left << name << std::setw(0) <<
" = " << value <<
"\n";
232 inline void PrintParameter(std::ostream &os,
const char *name,
const string &value)
238 inline void PrintParameter(std::ostream &os,
const string &name,
const string &value)
244 template <
class TValue>
245 inline void PrintParameter(std::ostream &os,
const char *name,
const TValue &value)
251 template <
class TValue>
252 inline void PrintParameter(std::ostream &os,
const string &name,
const TValue &value)
258 template <
typename T>
259 std::ostream &
Print(std::ostream &os, T value)
267 template <
typename T,
typename... Args>
268 std::ostream &
Print(std::ostream &os, T value, Args... args)
271 return Print(os, args...);
275 string ToLower(
const string &);
278 string ToUpper(
const string &);
286 string Trim(
const string &str,
const string &what =
" \t\r\n");
294 string TrimAll(
const string &str,
const string &what =
" \t\r\n");
308 Array<string>
Split(
string s,
const char *d,
int n = 0,
bool e =
false,
bool q =
false);
322 Array<string>
Split(
string s,
char d,
int n = 0,
bool e =
false,
bool q =
false);
353 string ParameterUnits(
const string &str,
string *name =
nullptr,
const char *dflt =
"");
366 string ValueUnits(
const string &str,
string *value =
nullptr,
const char *dflt =
"");
371 #endif // MIRTK_String_H bool IsValueOfType(const char *str)
Check if given string represents a value of the specified template type.
string Trim(const string &str, const string &what=" \\)
string StandardUnits(const string &str)
MIRTK_Common_DEPRECATED float ReadFloat(std::ifstream &)
General routine to read float from a file stream.
MIRTK_Common_DEPRECATED int ReadInt(std::ifstream &)
General routine to read float from a file stream.
string TrimAll(const string &str, const string &what=" \\)
bool iequal(char const *a, char const *b)
Case insensitive string comparison.
void PrintParameter(std::ostream &os, const char *name, const char *value)
Write "<name> = <value>" configuration entry to output stream.
string ParameterUnits(const string &str, string *name=nullptr, const char *dflt="")
bool IsInteger(const char *str)
Check if given string is an integer value.
string ToLower(const string &)
Convert string to lowercase letters.
string ValueUnits(const string &str, string *value=nullptr, const char *dflt="")
MIRTK_Common_DEPRECATED char * ReadString(std::ifstream &)
General routine to read list of char (string) from a file stream.
Array< string > Split(string s, const char *d, int n=0, bool e=false, bool q=false)
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
bool IsNumber(const char *str)
Check if given string is a (floating point) number.
string CamelCaseToPrettyParameterName(const string &s)
std::ostream & Print(std::ostream &os, T value)
Print single argument to output stream.
string ToUpper(const string &)
Convert string to uppercase letters.