ODIMPathMatcher.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25  */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30  */
31 #ifndef ODIM_PATH_MATCHER
32 #define ODIM_PATH_MATCHER
33 
34 
35 //#include <drain/util/Dictionary.h>
36 #include <drain/util/Flags.h>
37 #include <drain/util/Range.h>
38 
39 #include "ODIMPath.h"
40 
41 
42 namespace rack {
43 
45 /*
46  * Index range [index,indexMax] is applied in testing path elements of a same group.
47  * \see ODIMPathMatcher
48  */
50 
51 public:
52 
53  inline
54  ODIMPathElemMatcher(ODIMPathElem::group_t groups = ROOT, index_t index = 0, index_t indexMax = INDEX_MAX) :
55  // ODIMPathElem(group, index),
56  // index(index),
57  // indexMax(indexMax),
58  flags(ODIMPathElem::getDictionary(), this->group, '|'){
59  this->group = groups;
60  this->index = index;
61  this->indexMax = indexMax;
62  };
63 
64 
65  inline
67  // ODIMPathElem(e),
68  // index(e.index),
69  // indexMax(e.indexMax),
70  flags(ODIMPathElem::getDictionary(), this->group, '|') {
71  this->group = e.group; // needed?
72  this->index = e.index;
73  this->indexMax = e.indexMax;
74  }
75 
76  inline
78  // ODIMPathElem(e),
79  // index(e.index),
80  // indexMax(0xffff),
81  flags(ODIMPathElem::getDictionary(), this->group, '|') {
82  this->group = e.group; // needed?
83  this->index = e.index;
84  this->indexMax = INDEX_MAX; // ??
85  }
86 
87  inline
88  ODIMPathElemMatcher(const std::string &s) :
89  // index(0),
90  // indexMax(0xffff),
91  flags(ODIMPathElem::getDictionary(), this->group, '|') {
92 
93  // TODO: virtual reset(){}
94  group = 0;
95  index = 0;
96  this->indexMax = INDEX_MAX;
97  set(s);
98  }
99 
100  virtual
101  ~ODIMPathElemMatcher(){};
102 
103  // ambiguous! index 5 could mean range 5:5 or 0:0xffff ?
104 
105 
106  virtual
107  //bool
108  void set(const std::string &s);
109 
110 
111  inline
112  bool isSingle() const {
113  return (!isIndexed()) || (index == indexMax);
114  }
115 
116  inline
118  index = 0;
119  indexMax = INDEX_MAX;
120  group = g;
121  return *this;
122  }
123 
124  inline
125  ODIMPathElemMatcher & operator=(const std::string &s){
126  index = 0;
127  indexMax = INDEX_MAX;
128  set(s);
129  return *this;
130  }
131 
132 
134  bool test(const ODIMPathElem & elem) const;
135 
136  // Applied only as a upper limit in path matching.
137  index_t indexMax;
138 
139  //drain::Flagger flags;
140  //drain::Flagger2<drain::MultiFlagger<group_t> > flags;
142 
143  virtual
144  std::ostream & toStream(std::ostream & sstr) const;
145 
146  // FUTURE extension:
148 
149 
150 protected:
151 
153  virtual
154  void extractIndex(const std::string &s);
155 
156 
157  virtual
158  bool extractPrefix(const std::string &s, bool indexed);
159 
160 
161 };
162 
163 
164 // Note: Elem, not MatcherElem
165 template <>
166 void ODIMPathElem::extractIndex(const std::string &s, ODIMPathElemMatcher::idx_range_t & idx);
167 
168 inline
169 std::ostream & operator<<(std::ostream & ostr, const ODIMPathElemMatcher & p) {
170  p.toStream(ostr);
171  return ostr;
172 }
173 
174 //template <>
175 //char drain::TextReader::scanSegmentToValue(std::istream & istr, const std::string & endChars, ODIMPathElemMatcher::idx_range_t & dst);
176 
177 
178 
180 
185 class ODIMPathMatcher : public drain::Path<ODIMPathElemMatcher> {
186 
187 public:
188 
191  separator.acceptTrailing = true;
192  }
193 
195  ODIMPathMatcher(const ODIMPathMatcher & matcher) : drain::Path<ODIMPathElemMatcher>(matcher) {
196  }
197 
198  template<typename ... T>
199  ODIMPathMatcher(const ODIMPathElem & elem, const T &... rest){
200  separator.acceptTrailing = true;
201  setElems(elem, rest...);
202  //updateBean(); ?
203  }
204 
206  ODIMPathMatcher(const std::string & path){
207  separator.acceptTrailing = true;
208  set(path);
209  //assign(path);
210  }
211 
213  ODIMPathMatcher(const char * path){
214  separator.acceptTrailing = true;
215  set(path);
216  // assign(path);
217  }
218 
219 
221  // void parse(const std::string & path);
222 
224  bool ensureLimitingSlash();
225 
226 
228  bool isLiteral() const;
229 
231  void extractPath(ODIMPath & path) const;
232 
234  bool match(const rack::ODIMPath & path) const;
235 
237  bool matchHead(const rack::ODIMPath & path) const;
238 
240  bool matchTail(const rack::ODIMPath & path) const;
241 
243  bool matchElem(const rack::ODIMPathElem & elem, bool defaultValue = true) const;
244 
245 };
246 
247 
248 
249 } // namespace rack
250 
251 
252 #endif
Definition: Path.h:112
Definition: Range.h:52
Element in a chain in testing a path for a match.
Definition: ODIMPathMatcher.h:49
virtual void set(const std::string &s)
Assign a string to this path element.
Definition: ODIMPathMatcher.cpp:46
virtual std::ostream & toStream(std::ostream &sstr) const
For string presentation.
Definition: ODIMPathMatcher.cpp:345
virtual bool extractPrefix(const std::string &s, bool indexed)
Given the non-numeric prefix of a group, like "dataset" or "data", set the group.
Definition: ODIMPathMatcher.cpp:140
bool test(const ODIMPathElem &elem) const
Test if the elem has the same group, and elem.index is within [index,indexMax].
Definition: ODIMPathMatcher.cpp:193
virtual void extractIndex(const std::string &s)
Extracts index range of type <index>[:<indexMax>].
Definition: ODIMPathMatcher.cpp:94
Definition: ODIMPath.h:82
static const group_t ROOT
Definition: ODIMPath.h:102
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition: ODIMPath.h:92
bool isIndexed() const
Abbreviation of (group == NONE)
Definition: ODIMPath.h:321
virtual void extractIndex(const std::string &s)
Given a string starting with a numeral, try to extract the index.
Definition: ODIMPath.cpp:138
Structure for testing if a path matches a given sequence of path elements.
Definition: ODIMPathMatcher.h:185
bool isLiteral() const
Checks if corresponds to a single path, implying that all the index ranges are singletons.
Definition: ODIMPathMatcher.cpp:232
ODIMPathMatcher(const std::string &path)
Almost copy constructor.
Definition: ODIMPathMatcher.h:206
bool matchHead(const rack::ODIMPath &path) const
Match leading part of path.
Definition: ODIMPathMatcher.cpp:270
bool ensureLimitingSlash()
Resolves "where|where".
Definition: ODIMPathMatcher.cpp:69
bool matchTail(const rack::ODIMPath &path) const
Match trailing part of path.
Definition: ODIMPathMatcher.cpp:298
ODIMPathMatcher(const char *path)
Almost copy constructor.
Definition: ODIMPathMatcher.h:213
bool matchElem(const rack::ODIMPathElem &elem, bool defaultValue=true) const
Match single element. If matcher path does not contain it, return defaultValue.
Definition: ODIMPathMatcher.cpp:325
ODIMPathMatcher()
Basic constructor.
Definition: ODIMPathMatcher.h:190
ODIMPathMatcher(const ODIMPathMatcher &matcher)
Copy constructor.
Definition: ODIMPathMatcher.h:195
void extractPath(ODIMPath &path) const
Convert to a single path, assuming uniqueness. Future option: extract all the enumerated paths.
Definition: ODIMPathMatcher.cpp:243
bool match(const rack::ODIMPath &path) const
Match the leading part of path , if matcher starts with root. Else, match the trailing part.
Definition: ODIMPathMatcher.cpp:256
Definition: DataSelector.cpp:1277
Definition: DataSelector.cpp:44
bool & acceptTrailing
If true, allow trailing empty elements (appearing as repeated separators)
Definition: Path.h:79