blob: c570c1c4b88dc7f7f7bbe2fe30351890e8cfefcb [file] [log] [blame]
Davide Pesavento35185332019-01-14 04:00:15 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoef064892022-04-05 02:26:03 -04003 * Copyright (c) 2014-2022, Arizona Board of Regents.
Davide Pesavento35185332019-01-14 04:00:15 -05004 *
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 Pesaventoef064892022-04-05 02:26:03 -040021#ifndef NDNTG_UTIL_HPP
22#define NDNTG_UTIL_HPP
Davide Pesavento35185332019-01-14 04:00:15 -050023
Davide Pesavento306e5bc2019-01-26 16:20:34 -050024#include "logger.hpp"
25
Davide Pesavento35185332019-01-14 04:00:15 -050026#include <cctype>
27#include <string>
28
Davide Pesavento306e5bc2019-01-26 16:20:34 -050029#include <boost/algorithm/string/predicate.hpp>
30
Davide Pesaventoef064892022-04-05 02:26:03 -040031namespace ndntg {
Davide Pesavento35185332019-01-14 04:00:15 -050032
Davide Pesaventoef064892022-04-05 02:26:03 -040033inline bool
Davide Pesavento306e5bc2019-01-26 16:20:34 -050034extractParameterAndValue(const std::string& input, std::string& parameter, std::string& value)
Davide Pesavento35185332019-01-14 04:00:15 -050035{
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 Pesaventoef064892022-04-05 02:26:03 -040063inline bool
Davide Pesavento306e5bc2019-01-26 16:20:34 -050064parseBoolean(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
77template<typename TrafficConfigurationType>
Davide Pesaventoef064892022-04-05 02:26:03 -040078bool
Davide Pesavento306e5bc2019-01-26 16:20:34 -050079readConfigurationFile(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 Pesaventoef064892022-04-05 02:26:03 -0400122} // namespace ndntg
Davide Pesavento35185332019-01-14 04:00:15 -0500123
Davide Pesaventoef064892022-04-05 02:26:03 -0400124#endif // NDNTG_UTIL_HPP