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