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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mitch Watrous (watrous@u.washington.edu)
7 */
8
10
11#include "ns3/assert.h"
12
13#include <string>
14#include <vector>
15
16namespace ns3
17{
18
19std::string
20GetWildcardMatches(const std::string& configPath,
21 const std::string& matchedPath,
22 const std::string& wildcardSeparator)
23{
24 // If the Config path is just "*", return the whole matched path.
25 if (configPath == "*")
26 {
27 return matchedPath;
28 }
29
30 std::vector<std::string> nonWildcardTokens;
31 std::vector<std::size_t> nonWildcardTokenPositions;
32
33 size_t nonWildcardTokenCount;
34 size_t wildcardCount = 0;
35
36 // Get the non-wildcard tokens from the Config path.
37 size_t tokenStart;
38 size_t asterisk = -1;
39 do
40 {
41 // Find the non-wildcard token.
42 tokenStart = asterisk + 1;
43 asterisk = configPath.find('*', tokenStart);
44
45 // If a wildcard character was found, increment this counter.
46 if (asterisk != std::string::npos)
47 {
48 wildcardCount++;
49 }
50
51 // Save this non-wildcard token.
52 nonWildcardTokens.push_back(configPath.substr(tokenStart, asterisk - tokenStart));
53 } while (asterisk != std::string::npos);
54
55 // If there are no wildcards, return an empty string.
56 if (wildcardCount == 0)
57 {
58 return "";
59 }
60
61 // Set the number of non-wildcard tokens in the Config path.
62 nonWildcardTokenCount = nonWildcardTokens.size();
63
64 size_t i;
65
66 // Find the positions of the non-wildcard tokens in the matched path.
67 size_t token;
68 tokenStart = 0;
69 for (i = 0; i < nonWildcardTokenCount; i++)
70 {
71 // Look for the non-wildcard token.
72 token = matchedPath.find(nonWildcardTokens[i], tokenStart);
73
74 // Make sure that the token is found.
75 if (token == std::string::npos)
76 {
77 NS_ASSERT_MSG(false, "Error: non-wildcard token not found in matched path");
78 }
79
80 // Save the position of this non-wildcard token.
81 nonWildcardTokenPositions.push_back(token);
82
83 // Start looking for the next non-wildcard token after the end of
84 // this one.
85 tokenStart = token + nonWildcardTokens[i].size();
86 }
87
88 std::string wildcardMatches = "";
89
90 // Put the text matches from the matched path for each of the
91 // wildcards in the Config path into a string, separated by the
92 // specified separator.
93 size_t wildcardMatchesSet = 0;
94 size_t matchStart;
95 size_t matchEnd;
96 for (i = 0; i < nonWildcardTokenCount; i++)
97 {
98 // Find the start and end of this wildcard match.
99 matchStart = nonWildcardTokenPositions[i] + nonWildcardTokens[i].size();
100 if (i != nonWildcardTokenCount - 1)
101 {
102 matchEnd = nonWildcardTokenPositions[i + 1] - 1;
103 }
104 else
105 {
106 matchEnd = matchedPath.length() - 1;
107 }
108
109 // This algorithm gets confused by zero length non-wildcard
110 // tokens. So, only add this wildcard match and update the
111 // counters if the match was calculated to start before it began.
112 if (matchStart <= matchEnd)
113 {
114 // Add the wildcard match.
115 wildcardMatches += matchedPath.substr(matchStart, matchEnd - matchStart + 1);
116
117 // See if all of the wildcard matches have been set.
118 wildcardMatchesSet++;
119 if (wildcardMatchesSet == wildcardCount)
120 {
121 break;
122 }
123 else
124 {
125 // If there are still more to set, add the separator to
126 // the end of the one just added.
127 wildcardMatches += wildcardSeparator;
128 }
129 }
130 }
131
132 // Return the wildcard matches.
133 return wildcardMatches;
134}
135
136} // 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:75
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,...