2012-08-30 10:59:41 -04:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2012-08-30 10:59:41 -04:00
|
|
|
|
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
|
|
|
|
2014-10-08 08:47:22 -04:00
|
|
|
#include "../Util/cast.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>
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2013-01-29 04:11:04 -05:00
|
|
|
namespace qi = boost::spirit::qi;
|
|
|
|
|
2014-05-09 10:17:31 -04:00
|
|
|
// TODO: Move into LUA
|
2012-11-03 03:18:22 -04:00
|
|
|
|
2014-08-29 04:08:26 -04:00
|
|
|
bool durationIsValid(const std::string &s)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
|
|
|
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);
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2014-05-09 10:17:31 -04:00
|
|
|
std::vector<std::string> result;
|
|
|
|
boost::algorithm::split_regex(result, s, boost::regex(":"));
|
2014-06-06 12:06:05 -04:00
|
|
|
const bool matched = regex_match(s, e);
|
2012-08-30 10:59:41 -04:00
|
|
|
return matched;
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:08:26 -04:00
|
|
|
unsigned parseDuration(const std::string &s)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
|
|
|
unsigned hours = 0;
|
2013-01-29 04:11:04 -05:00
|
|
|
unsigned minutes = 0;
|
|
|
|
unsigned seconds = 0;
|
2014-05-09 10:17:31 -04:00
|
|
|
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);
|
2012-08-30 10:59:41 -04:00
|
|
|
|
2014-05-09 10:17:31 -04:00
|
|
|
std::vector<std::string> result;
|
|
|
|
boost::algorithm::split_regex(result, s, boost::regex(":"));
|
2014-06-06 12:06:05 -04:00
|
|
|
const bool matched = regex_match(s, e);
|
2014-05-09 10:17:31 -04:00
|
|
|
if (matched)
|
|
|
|
{
|
|
|
|
if (1 == result.size())
|
|
|
|
{
|
2014-10-08 08:47:22 -04:00
|
|
|
minutes = cast::string_to_int(result[0]);
|
2014-05-09 10:17:31 -04:00
|
|
|
}
|
|
|
|
if (2 == result.size())
|
|
|
|
{
|
2014-10-08 08:47:22 -04:00
|
|
|
minutes = cast::string_to_int(result[1]);
|
|
|
|
hours = cast::string_to_int(result[0]);
|
2014-05-09 10:17:31 -04:00
|
|
|
}
|
|
|
|
if (3 == result.size())
|
|
|
|
{
|
2014-10-08 08:47:22 -04:00
|
|
|
seconds = cast::string_to_int(result[2]);
|
|
|
|
minutes = cast::string_to_int(result[1]);
|
|
|
|
hours = cast::string_to_int(result[0]);
|
2014-05-09 10:17:31 -04:00
|
|
|
}
|
|
|
|
return 10 * (3600 * hours + 60 * minutes + seconds);
|
2012-08-30 10:59:41 -04:00
|
|
|
}
|
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
|