2014-11-28 09:00:48 -05:00
|
|
|
#ifndef EXTRACTION_HELPER_FUNCTIONS_HPP
|
|
|
|
#define EXTRACTION_HELPER_FUNCTIONS_HPP
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/cast.hpp"
|
|
|
|
#include "util/iso_8601_duration_parser.hpp"
|
2013-06-26 19:48:02 -04:00
|
|
|
|
2012-11-19 13:04:59 -05:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string_regex.hpp>
|
2014-10-08 08:47:22 -04:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
2012-11-19 13:04:59 -05:00
|
|
|
#include <boost/regex.hpp>
|
2014-05-09 10:17:31 -04:00
|
|
|
|
|
|
|
#include <limits>
|
2015-09-08 21:31:03 -04:00
|
|
|
#include <string>
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2015-03-02 10:41:03 -05:00
|
|
|
bool simple_duration_is_valid(const std::string &s)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-03-02 10:41:03 -05:00
|
|
|
boost::regex simple_format(
|
2014-05-09 10:17:31 -04:00
|
|
|
"((\\d|\\d\\d):(\\d|\\d\\d):(\\d|\\d\\d))|((\\d|\\d\\d):(\\d|\\d\\d))|(\\d|\\d\\d)",
|
|
|
|
boost::regex_constants::icase | boost::regex_constants::perl);
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2015-03-02 10:41:03 -05:00
|
|
|
const bool simple_matched = regex_match(s, simple_format);
|
|
|
|
|
|
|
|
if (simple_matched)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-08-30 10:59:41 -04:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:41:03 -05:00
|
|
|
bool iso_8601_duration_is_valid(const std::string &s)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-03-02 10:41:03 -05:00
|
|
|
iso_8601_grammar<std::string::const_iterator> iso_parser;
|
|
|
|
const bool result = qi::parse(s.begin(), s.end(), iso_parser);
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2015-03-02 10:41:03 -05:00
|
|
|
// check if the was an error with the request
|
|
|
|
if (result && (0 != iso_parser.get_duration()))
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-03-02 10:41:03 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool durationIsValid(const std::string &s)
|
|
|
|
{
|
|
|
|
return simple_duration_is_valid(s) || iso_8601_duration_is_valid(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned parseDuration(const std::string &s)
|
|
|
|
{
|
|
|
|
if (simple_duration_is_valid(s))
|
|
|
|
{
|
|
|
|
unsigned hours = 0;
|
|
|
|
unsigned minutes = 0;
|
|
|
|
unsigned seconds = 0;
|
|
|
|
boost::regex e(
|
|
|
|
"((\\d|\\d\\d):(\\d|\\d\\d):(\\d|\\d\\d))|((\\d|\\d\\d):(\\d|\\d\\d))|(\\d|\\d\\d)",
|
|
|
|
boost::regex_constants::icase | boost::regex_constants::perl);
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
boost::algorithm::split_regex(result, s, boost::regex(":"));
|
|
|
|
const bool matched = regex_match(s, e);
|
|
|
|
if (matched)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-03-02 10:41:03 -05:00
|
|
|
if (1 == result.size())
|
|
|
|
{
|
2015-09-08 21:31:03 -04:00
|
|
|
minutes = std::stoul(result[0]);
|
2015-03-02 10:41:03 -05:00
|
|
|
}
|
|
|
|
if (2 == result.size())
|
|
|
|
{
|
2015-09-08 21:31:03 -04:00
|
|
|
minutes = std::stoul(result[1]);
|
|
|
|
hours = std::stoul(result[0]);
|
2015-03-02 10:41:03 -05:00
|
|
|
}
|
|
|
|
if (3 == result.size())
|
|
|
|
{
|
2015-09-08 21:31:03 -04:00
|
|
|
seconds = std::stoul(result[2]);
|
|
|
|
minutes = std::stoul(result[1]);
|
|
|
|
hours = std::stoul(result[0]);
|
2015-03-02 10:41:03 -05:00
|
|
|
}
|
2015-05-09 11:21:36 -04:00
|
|
|
return (3600 * hours + 60 * minutes + seconds);
|
2014-05-09 10:17:31 -04:00
|
|
|
}
|
2012-08-30 10:59:41 -04:00
|
|
|
}
|
2015-03-02 10:41:03 -05:00
|
|
|
else if (iso_8601_duration_is_valid(s))
|
|
|
|
{
|
|
|
|
iso_8601_grammar<std::string::const_iterator> iso_parser;
|
|
|
|
qi::parse(s.begin(), s.end(), iso_parser);
|
|
|
|
|
|
|
|
return iso_parser.get_duration();
|
|
|
|
}
|
|
|
|
|
2014-05-09 10:17:31 -04:00
|
|
|
return std::numeric_limits<unsigned>::max();
|
2012-08-30 10:59:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-28 09:00:48 -05:00
|
|
|
#endif // EXTRACTION_HELPER_FUNCTIONS_HPP
|