|
TECA
The Toolkit for Extreme Climate Analysis
|
Go to the documentation of this file. 1 #ifndef teca_string_util_h
2 #define teca_string_util_h
29 int tokenize(
char *istr,
char delim,
int n_cols,
char **ostr);
37 template <
typename container_t = std::vector<
char*>>
38 int tokenize(
char *istr,
char delim, container_t &ostr)
41 while ((*istr == delim) && (*istr !=
'\0'))
53 while ((*istr != delim) && (*istr !=
'\0'))
78 while ((*buf !=
'\0') &&
79 ((*buf ==
' ') || (*buf ==
'\n') || (*buf ==
'\r') || (*buf ==
'\t')))
81 return *buf ==
'\0' ? -1 : 0;
95 template <
typename num_t>
98 #define DECLARE_SCANF_TT(_CPP_T, _FMT_STR) \
101 struct scanf_tt<_CPP_T> \
104 const char *format() { return _FMT_STR; } \
106 DECLARE_SCANF_TT(
float,
" %g")
107 DECLARE_SCANF_TT(
double," %lg")
108 DECLARE_SCANF_TT(
char," %hhi")
109 DECLARE_SCANF_TT(
short, " %hi")
110 DECLARE_SCANF_TT(
int, " %i")
111 DECLARE_SCANF_TT(
long, " %li")
112 DECLARE_SCANF_TT(
long long, "%lli")
113 DECLARE_SCANF_TT(
unsigned char," %hhu")
114 DECLARE_SCANF_TT(
unsigned short, " %hu")
115 DECLARE_SCANF_TT(
unsigned int, " %u")
116 DECLARE_SCANF_TT(
unsigned long, " %lu")
117 DECLARE_SCANF_TT(
unsigned long long, "%llu")
118 DECLARE_SCANF_TT(std::
string, " \"%128s")
121 template <typename T>
124 #define DECLARE_STR_CONVERSION_I(_CPP_T, _FUNC) \
127 struct string_tt<_CPP_T> \
129 static const char *type_name() { return # _CPP_T; } \
131 static int convert(char *str, _CPP_T &val) \
134 char *endp = nullptr; \
135 _CPP_T tmp = _FUNC(str, &endp, 0); \
138 TECA_ERROR("Failed to convert string \"" \
139 << str << "\" to a nunber." << strerror(errno)) \
142 else if (endp == str) \
144 TECA_ERROR("Failed to convert string \"" \
145 << str << "\" to a nunber. Invalid string.") \
153 #define DECLARE_STR_CONVERSION_F(_CPP_T, _FUNC) \
156 struct string_tt<_CPP_T> \
158 static const char *type_name() { return # _CPP_T; } \
160 static int convert(const char *str, _CPP_T &val) \
163 char *endp = nullptr; \
164 _CPP_T tmp = _FUNC(str, &endp); \
167 TECA_ERROR("Failed to convert string \"" \
168 << str << "\" to a nunber." << strerror(errno)) \
171 else if (endp == str) \
173 TECA_ERROR("Failed to convert string \"" \
174 << str << "\" to a nunber. Invalid string.") \
182 DECLARE_STR_CONVERSION_F(
float, strtof)
183 DECLARE_STR_CONVERSION_F(
double, strtod)
184 DECLARE_STR_CONVERSION_I(
char, strtol)
185 DECLARE_STR_CONVERSION_I(
short, strtol)
186 DECLARE_STR_CONVERSION_I(
int, strtol)
187 DECLARE_STR_CONVERSION_I(
long, strtoll)
188 DECLARE_STR_CONVERSION_I(
long long, strtoll)
194 static const char *type_name() {
return "bool"; }
196 static int convert(
const char *str,
bool &val)
200 size_t n = strlen(str);
202 for (
size_t i = 0; i < n && i < 16; ++i)
203 buf[i] = tolower(str[i]);
205 if ((strcmp(buf,
"0") == 0)
206 || (strcmp(buf,
"false") == 0) || (strcmp(buf,
"off") == 0))
211 else if ((strcmp(buf,
"1") == 0)
212 || (strcmp(buf,
"true") == 0) || (strcmp(buf,
"on") == 0))
218 TECA_ERROR(
"Failed to convert string \"" << str <<
"\" to a bool")
227 static const char *type_name() {
return "std::string"; }
229 static int convert(
const char *str, std::string &val)
242 static const char *type_name() {
return "char*"; }
244 static int convert(
const char *str,
char *&val)
255 template <
typename val_t>
258 std::vector<char*> tmp;
259 if (
tokenize(l,
'=', tmp) || (tmp.size() != 2))
261 TECA_ERROR(
"Invalid name specifier in \"" << l <<
"\"")
269 <<
" value \"" << r <<
"\" in \"" << l <<
"\"")
280 void remove_post_fix(std::set<std::string> &names, std::string post_fix);
285 return (in ==
"\"\"" ? std::string() : in);
A traits class for scanf conversion codes.
Definition: teca_string_util.h:96
int skip_pad(char *&buf)
Definition: teca_string_util.h:76
int extract_string(const char *istr, std::string &field)
int tokenize(char *istr, char delim, int n_cols, char **ostr)
A traits class for conversion from text to numbers.
Definition: teca_string_util.h:122
std::string emptystr(const std::string &in)
When passed the string "" return empty string otherwise return the passed string.
Definition: teca_string_util.h:283
Codes for dealing with string processing.
Definition: teca_string_util.h:16
int extract_value(char *l, val_t &val)
Definition: teca_string_util.h:256
int is_comment(char *buf)
return 0 if the first non-pad character is #
Definition: teca_string_util.h:86
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:138
void remove_post_fix(std::set< std::string > &names, std::string post_fix)