Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Arizona Board of Regents. |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 19 | */ |
| 20 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 21 | #ifndef NDNTG_UTIL_HPP |
| 22 | #define NDNTG_UTIL_HPP |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 24 | #include "logger.hpp" |
| 25 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 26 | #include <cctype> |
| 27 | #include <string> |
| 28 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 29 | #include <boost/algorithm/string/predicate.hpp> |
| 30 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 31 | namespace ndntg { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 33 | inline bool |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 34 | extractParameterAndValue(const std::string& input, std::string& parameter, std::string& value) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 35 | { |
| 36 | static const std::string allowedCharacters = ":/+._-%"; |
| 37 | parameter = ""; |
| 38 | value = ""; |
| 39 | |
| 40 | std::size_t i = 0; |
| 41 | // parse the parameter name |
| 42 | while (i < input.length() && input[i] != '=') { |
| 43 | parameter += input[i]; |
| 44 | i++; |
| 45 | } |
| 46 | if (i == input.length()) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | // skip the equal sign |
| 51 | i++; |
| 52 | |
| 53 | // parse the value |
| 54 | while (i < input.length() && |
| 55 | (std::isalnum(input[i]) || allowedCharacters.find(input[i]) != std::string::npos)) { |
| 56 | value += input[i]; |
| 57 | i++; |
| 58 | } |
| 59 | |
| 60 | return !parameter.empty() && !value.empty() && i == input.length(); |
| 61 | } |
| 62 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 63 | inline bool |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 64 | parseBoolean(const std::string& input) |
| 65 | { |
| 66 | if (boost::iequals(input, "no") || boost::iequals(input, "off") || |
| 67 | boost::iequals(input, "false") || input == "0") |
| 68 | return false; |
| 69 | |
| 70 | if (boost::iequals(input, "yes") || boost::iequals(input, "on") || |
| 71 | boost::iequals(input, "true") || input == "1") |
| 72 | return true; |
| 73 | |
| 74 | throw std::invalid_argument("'" + input + "' is not a valid boolean value"); |
| 75 | } |
| 76 | |
| 77 | template<typename TrafficConfigurationType> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 78 | bool |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 79 | readConfigurationFile(const std::string& filename, |
| 80 | std::vector<TrafficConfigurationType>& patterns, |
| 81 | Logger& logger) |
| 82 | { |
| 83 | std::ifstream patternFile(filename); |
| 84 | if (!patternFile) { |
| 85 | logger.log("ERROR: Unable to open traffic configuration file: " + filename, false, true); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | logger.log("Reading traffic configuration file: " + filename, true, true); |
| 90 | |
| 91 | int lineNumber = 0; |
| 92 | std::string patternLine; |
| 93 | while (getline(patternFile, patternLine)) { |
| 94 | lineNumber++; |
| 95 | if (std::isalpha(patternLine[0])) { |
| 96 | TrafficConfigurationType trafficConf; |
| 97 | bool shouldSkipLine = false; |
| 98 | if (trafficConf.parseConfigurationLine(patternLine, logger, lineNumber)) { |
| 99 | while (getline(patternFile, patternLine) && std::isalpha(patternLine[0])) { |
| 100 | lineNumber++; |
| 101 | if (!trafficConf.parseConfigurationLine(patternLine, logger, lineNumber)) { |
| 102 | shouldSkipLine = true; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | lineNumber++; |
| 107 | } |
| 108 | else { |
| 109 | shouldSkipLine = true; |
| 110 | } |
| 111 | if (!shouldSkipLine) { |
| 112 | if (trafficConf.checkTrafficDetailCorrectness()) { |
| 113 | patterns.push_back(std::move(trafficConf)); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 122 | } // namespace ndntg |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 123 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 124 | #endif // NDNTG_UTIL_HPP |