30#ifndef APF_STRINGTOOLS_H
31#define APF_STRINGTOOLS_H
52std::string
A2S(
const T& input)
54 std::ostringstream converter;
55 converter << std::boolalpha << input;
56 return converter.str();
68template<
typename out_T>
69inline bool convert(std::istream& input, out_T& output)
71 auto result = out_T();
74 if (input.fail())
return false;
77 if (!input.eof())
return false;
90inline bool convert(std::istream& input,
bool& output)
100 input >> std::boolalpha >> result;
102 if (input.fail())
return false;
105 if (!input.eof())
return false;
121template<
typename out_T>
122bool S2A(
const std::string& input, out_T& output)
124 std::istringstream converter(input);
125 return convert(converter, output);
132inline bool S2A(
const std::string& input, std::string& output)
151template<
typename out_T>
152out_T
S2RV(
const std::string& input, out_T def)
162inline std::string
S2RV(
const std::string& input,
const char* def)
164 std::string temp(def);
177template<
typename out_T,
typename in_T>
180 auto result = out_T();
181 if (!
S2A(input, result))
183 throw std::invalid_argument(
184 "S2RV(): Couldn't convert \"" +
S2RV(input, std::string()) +
"\"!");
204template<
typename char_T,
typename traits>
205std::basic_ios<char_T, traits>&
208 stream.clear(stream.rdstate() & std::ios_base::eofbit);
226template<
int digits,
typename char_T,
typename traits,
typename out_T>
227std::basic_istream<char_T, traits>&
230 static_assert(digits > 0,
"'digits' must be at least 1!");
233 if (input.fail())
return input;
235 if (input.flags() & std::ios_base::skipws) input >> std::ws;
238 if (!input.read(ch, digits))
return input;
240 out_T factor = 1, result = 0;
241 for (
int i = digits - 1; i >= 0; --i)
244 if (ch[i] < input.widen(
'0') || ch[i] > input.widen(
'9'))
246 input.setstate(std::ios_base::failbit);
250 result += (ch[i] -
'0') * factor;
268template<
typename char_T,
typename traits>
269std::basic_istream<char_T, traits>&
270remove_char(std::basic_istream<char_T, traits>& input,
const char_T character)
273 if (input.fail())
return input;
275 if (input.flags() & std::ios_base::skipws) input >> std::ws;
278 if (input.get(ch) && (ch != character))
280 input.setstate(std::ios_base::failbit);
302template<
typename char_T,
typename traits>
303std::basic_istream<char_T, traits>&
328template<
template<
typename,
typename,
typename>
class in_T,
329 typename char_T,
typename traits,
typename Allocator,
typename out_T>
330bool string2time(
const in_T<char_T, traits, Allocator>& input, out_T& output)
334 for (
size_t found = 0
335 ; (found = input.find(
':', found + 1)) != std::string::npos
338 std::basic_istringstream<char_T> iss(input);
346 if (iss.fail())
return false;
355 auto the_rest = std::basic_string<char_T>();
358 if (iss.fail())
return false;
361 if (!iss.eof())
return false;
364 if (the_rest ==
"h") seconds = number * 60 * 60;
365 else if (the_rest ==
"min") seconds = number * 60;
366 else if (the_rest ==
"s") seconds = number;
367 else if (the_rest ==
"ms")
371 if (number / 1000 * 1000 != number)
return false;
372 else seconds = number / 1000;
377 else if (colons == 1 || colons == 2)
380 bool negative =
false;
382 if (iss.peek() ==
'-')
394 if ((iss >> minutes).fail())
return false;
396 if (minutes < 0 || minutes > 59)
return false;
398 else if (colons == 2)
403 convert_chars<2>(iss, minutes);
404 if (iss.fail())
return false;
405 if (hours < 0)
return false;
406 if (minutes > 59)
return false;
410 out_T fraction_of_second(0);
415 convert_chars<2>(iss, whole_seconds);
416 if (iss.fail())
return false;
418 if (whole_seconds > 59)
return false;
420 if (iss.peek() ==
'.')
422 if ((iss >> fraction_of_second).fail())
return false;
425 if (!(iss >> std::ws).eof())
return false;
427 auto the_rest = whole_seconds + fraction_of_second;
430 if (the_rest >= 60)
return false;
436 the_rest = -the_rest;
439 seconds =
static_cast<out_T
>(hours * 60 * 60);
440 seconds +=
static_cast<out_T
>(minutes * 60);
457template<
typename char_T,
typename out_T>
460 return string2time(std::basic_string<char_T>(input), output);
out_T S2RV(const std::string &input, out_T def)
Converter "String to Return Value".
bool S2A(const std::string &input, out_T &output)
Converter "String to Anything".
std::basic_istream< char_T, traits > & convert_chars(std::basic_istream< char_T, traits > &input, out_T &output)
Remove a specified number of characters from a stream and convert them to a numeric type.
bool convert(std::istream &input, out_T &output)
Convert a stream to a given type.
std::string A2S(const T &input)
Converter "Anything to String".
bool string2time(const in_T< char_T, traits, Allocator > &input, out_T &output)
Convert time string to numeric value in seconds.
std::basic_ios< char_T, traits > & clear_iostate_except_eof(std::basic_ios< char_T, traits > &stream)
Clear the state of a stream but leave eofbit as is.
std::basic_istream< char_T, traits > & remove_char(std::basic_istream< char_T, traits > &input, const char_T character)
Remove a character from a stream and check if it is the one given as parameter.
std::basic_istream< char_T, traits > & remove_colon(std::basic_istream< char_T, traits > &input)
Remove a colon from an input stream.
Audio Processing Framework.