1 #ifndef teca_string_util_h
2 #define teca_string_util_h
6 #include "teca_config.h"
32 int tokenize(
char *istr,
char delim,
int n_cols,
char **ostr);
40 template <
typename container_t = std::vector<
char*>>
42 int tokenize(
char *istr,
char delim, container_t &ostr)
45 while ((*istr == delim) && (*istr !=
'\0'))
57 while ((*istr != delim) && (*istr !=
'\0'))
82 while ((*buf !=
'\0') &&
83 ((*buf ==
' ') || (*buf ==
'\n') || (*buf ==
'\r') || (*buf ==
'\t')))
85 return *buf ==
'\0' ? -1 : 0;
99 template <
typename num_t>
102 #define DECLARE_SCANF_TT(_CPP_T, _FMT_STR) \
105 struct scanf_tt<_CPP_T> \
108 const char *format() { return _FMT_STR; } \
110 DECLARE_SCANF_TT(
float,
" %g")
111 DECLARE_SCANF_TT(
double," %lg")
112 DECLARE_SCANF_TT(
char," %hhi")
113 DECLARE_SCANF_TT(
short, " %hi")
114 DECLARE_SCANF_TT(
int, " %i")
115 DECLARE_SCANF_TT(
long, " %li")
116 DECLARE_SCANF_TT(
long long, "%lli")
117 DECLARE_SCANF_TT(
unsigned char," %hhu")
118 DECLARE_SCANF_TT(
unsigned short, " %hu")
119 DECLARE_SCANF_TT(
unsigned int, " %u")
120 DECLARE_SCANF_TT(
unsigned long, " %lu")
121 DECLARE_SCANF_TT(
unsigned long long, "%llu")
122 DECLARE_SCANF_TT(std::
string, " \"%128s")
125 template <typename T>
128 #define DECLARE_STR_CONVERSION_I(_CPP_T, _FUNC) \
131 struct string_tt<_CPP_T> \
133 static const char *type_name() { return # _CPP_T; } \
135 static int convert(const char *str, _CPP_T &val) \
138 char *endp = nullptr; \
139 _CPP_T tmp = _FUNC(str, &endp, 0); \
142 TECA_ERROR("Failed to convert string \"" \
143 << str << "\" to a nunber." << strerror(errno)) \
146 else if (endp == str) \
148 TECA_ERROR("Failed to convert string \"" \
149 << str << "\" to a nunber. Invalid string.") \
157 #define DECLARE_STR_CONVERSION_F(_CPP_T, _FUNC) \
160 struct string_tt<_CPP_T> \
162 static const char *type_name() { return # _CPP_T; } \
164 static int convert(const char *str, _CPP_T &val) \
167 char *endp = nullptr; \
168 _CPP_T tmp = _FUNC(str, &endp); \
171 TECA_ERROR("Failed to convert string \"" \
172 << str << "\" to a nunber." << strerror(errno)) \
175 else if (endp == str) \
177 TECA_ERROR("Failed to convert string \"" \
178 << str << "\" to a nunber. Invalid string.") \
186 DECLARE_STR_CONVERSION_F(
float, strtof)
187 DECLARE_STR_CONVERSION_F(
double, strtod)
188 DECLARE_STR_CONVERSION_I(
char, strtol)
189 DECLARE_STR_CONVERSION_I(
short, strtol)
190 DECLARE_STR_CONVERSION_I(
int, strtol)
191 DECLARE_STR_CONVERSION_I(
long, strtoll)
192 DECLARE_STR_CONVERSION_I(
long long, strtoll)
198 static const char *type_name() {
return "bool"; }
200 static int convert(
const char *str,
bool &val)
204 size_t n = strlen(str);
206 for (
size_t i = 0; i < n && i < 16; ++i)
207 buf[i] = tolower(str[i]);
209 if ((strcmp(buf,
"0") == 0)
210 || (strcmp(buf,
"false") == 0) || (strcmp(buf,
"off") == 0))
215 else if ((strcmp(buf,
"1") == 0)
216 || (strcmp(buf,
"true") == 0) || (strcmp(buf,
"on") == 0))
222 TECA_ERROR(
"Failed to convert string \"" << str <<
"\" to a bool")
231 static const char *type_name() {
return "std::string"; }
233 static int convert(
const char *str, std::string &val)
246 static const char *type_name() {
return "char*"; }
248 static int convert(
const char *str,
char *&val)
259 template <
typename val_t>
263 std::vector<char*> tmp;
264 if (
tokenize(l,
'=', tmp) || (tmp.size() != 2))
266 TECA_ERROR(
"Invalid name specifier in \"" << l <<
"\"")
274 <<
" value \"" << r <<
"\" in \"" << l <<
"\"")
292 return (in ==
"\"\"" ? std::string() : in);
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
Codes for dealing with string processing.
Definition: teca_string_util.h:18
TECA_EXPORT void remove_postfix(std::set< std::string > &names, std::string postfix)
TECA_EXPORT int extract_value(char *l, val_t &val)
Definition: teca_string_util.h:261
TECA_EXPORT int extract_string(const char *istr, std::string &field)
int is_comment(char *buf)
return 0 if the first non-pad character is #
Definition: teca_string_util.h:90
int skip_pad(char *&buf)
Definition: teca_string_util.h:80
TECA_EXPORT int tokenize(char *istr, char delim, int n_cols, char **ostr)
TECA_EXPORT std::string emptystr(const std::string &in)
When passed the string "" return empty string otherwise return the passed string.
Definition: teca_string_util.h:290
A traits class for scanf conversion codes.
Definition: teca_string_util.h:100
A traits class for conversion from text to numbers.
Definition: teca_string_util.h:126
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161