A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
get-wildcard-matches.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mitch Watrous (watrous@u.washington.edu)
18 */
19
21
22#include "ns3/assert.h"
23
24#include <string>
25#include <vector>
26
27namespace ns3
28{
29
30std::string
31GetWildcardMatches(const std::string& configPath,
32 const std::string& matchedPath,
33 const std::string& wildcardSeparator)
34{
35 // If the Config path is just "*", return the whole matched path.
36 if (configPath == "*")
37 {
38 return matchedPath;
39 }
40
41 std::vector<std::string> nonWildcardTokens;
42 std::vector<std::size_t> nonWildcardTokenPositions;
43
44 size_t nonWildcardTokenCount;
45 size_t wildcardCount = 0;
46
47 // Get the non-wildcard tokens from the Config path.
48 size_t tokenStart;
49 size_t asterisk = -1;
50 do
51 {
52 // Find the non-wildcard token.
53 tokenStart = asterisk + 1;
54 asterisk = configPath.find('*', tokenStart);
55
56 // If a wildcard character was found, increment this counter.
57 if (asterisk != std::string::npos)
58 {
59 wildcardCount++;
60 }
61
62 // Save this non-wildcard token.
63 nonWildcardTokens.push_back(configPath.substr(tokenStart, asterisk - tokenStart));
64 } while (asterisk != std::string::npos);
65
66 // If there are no wildcards, return an empty string.
67 if (wildcardCount == 0)
68 {
69 return "";
70 }
71
72 // Set the number of non-wildcard tokens in the Config path.
73 nonWildcardTokenCount = nonWildcardTokens.size();
74
75 size_t i;
76
77 // Find the positions of the non-wildcard tokens in the matched path.
78 size_t token;
79 tokenStart = 0;
80 for (i = 0; i < nonWildcardTokenCount; i++)
81 {
82 // Look for the non-wildcard token.
83 token = matchedPath.find(nonWildcardTokens[i], tokenStart);
84
85 // Make sure that the token is found.
86 if (token == std::string::npos)
87 {
88 NS_ASSERT_MSG(false, "Error: non-wildcard token not found in matched path");
89 }
90
91 // Save the position of this non-wildcard token.
92 nonWildcardTokenPositions.push_back(token);
93
94 // Start looking for the next non-wildcard token after the end of
95 // this one.
96 tokenStart = token + nonWildcardTokens[i].size();
97 }
98
99 std::string wildcardMatches = "";
100
101 // Put the text matches from the matched path for each of the
102 // wildcards in the Config path into a string, separated by the
103 // specified separator.
104 size_t wildcardMatchesSet = 0;
105 size_t matchStart;
106 size_t matchEnd;
107 for (i = 0; i < nonWildcardTokenCount; i++)
108 {
109 // Find the start and end of this wildcard match.
110 matchStart = nonWildcardTokenPositions[i] + nonWildcardTokens[i].size();
111 if (i != nonWildcardTokenCount - 1)
112 {
113 matchEnd = nonWildcardTokenPositions[i + 1] - 1;
114 }
115 else
116 {
117 matchEnd = matchedPath.length() - 1;
118 }
119
120 // This algorithm gets confused by zero length non-wildcard
121 // tokens. So, only add this wildcard match and update the
122 // counters if the match was calculated to start before it began.
123 if (matchStart <= matchEnd)
124 {
125 // Add the wildcard match.
126 wildcardMatches += matchedPath.substr(matchStart, matchEnd - matchStart + 1);
127
128 // See if all of the wildcard matches have been set.
129 wildcardMatchesSet++;
130 if (wildcardMatchesSet == wildcardCount)
131 {
132 break;
133 }
134 else
135 {
136 // If there are still more to set, add the separator to
137 // the end of the one just added.
138 wildcardMatches += wildcardSeparator;
139 }
140 }
141 }
142
143 // Return the wildcard matches.
144 return wildcardMatches;
145}
146
147} // namespace ns3
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string GetWildcardMatches(const std::string &configPath, const std::string &matchedPath, const std::string &wildcardSeparator)
Returns the text matches from the matched path for each of the wildcards in the Config path,...