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 
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(this->group, ODIMPathElem::getDictionary(), '|'){
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(this->group, ODIMPathElem::getDictionary(), '|') {
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(this->group, ODIMPathElem::getDictionary(), '|') {
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(this->group, ODIMPathElem::getDictionary(), '|') {
92  // TODO: virtual reset(){}
93  group = 0;
94  index = 0;
95  this->indexMax = INDEX_MAX;
96  set(s);
97  }
98 
99  virtual
100  ~ODIMPathElemMatcher(){};
101 
102  // ambiguous! index 5 could mean range 5:5 or 0:0xffff ?
103 
104 
105  virtual
106  //bool
107  void set(const std::string &s);
108 
109 
110  inline
111  bool isSingle() const {
112  return (!isIndexed()) || (index == indexMax);
113  }
114 
115  inline
117  index = 0;
118  indexMax = INDEX_MAX;
119  group = g;
120  return *this;
121  }
122 
123  inline
124  ODIMPathElemMatcher & operator=(const std::string &s){
125  index = 0;
126  indexMax = INDEX_MAX;
127  set(s);
128  return *this;
129  }
130 
131 
133  bool test(const ODIMPathElem & elem) const;
134 
135  // Applied only as a upper limit in path matching.
136  index_t indexMax;
137 
138  drain::Flagger flags;
139 
140  virtual
141  std::ostream & toStream(std::ostream & sstr) const;
142 
143  // FUTURE extension:
145 
146 
147 protected:
148 
150  virtual
151  void extractIndex(const std::string &s);
152 
153 
154  virtual
155  bool extractPrefix(const std::string &s, bool indexed);
156 
157 
158 };
159 
160 
161 // Note: Elem, not MatcherElem
162 template <>
163 void ODIMPathElem::extractIndex(const std::string &s, ODIMPathElemMatcher::idx_range_t & idx);
164 
165 inline
166 std::ostream & operator<<(std::ostream & ostr, const ODIMPathElemMatcher & p) {
167  p.toStream(ostr);
168  return ostr;
169 }
170 
171 //template <>
172 //char drain::TextReader::scanSegmentToValue(std::istream & istr, const std::string & endChars, ODIMPathElemMatcher::idx_range_t & dst);
173 
174 
175 
177 
182 class ODIMPathMatcher : public drain::Path<ODIMPathElemMatcher> {
183 
184 public:
185 
188  separator.acceptTrailing = true;
189  }
190 
193  }
194 
195  template<typename ... T>
196  ODIMPathMatcher(const ODIMPathElem & elem, const T &... rest){
197  separator.acceptTrailing = true;
198  setElems(elem, rest...);
199  //updateBean(); ?
200  }
201 
203  ODIMPathMatcher(const std::string & path){
204  separator.acceptTrailing = true;
205  set(path);
206  //assign(path);
207  }
208 
210  ODIMPathMatcher(const char * path){
211  separator.acceptTrailing = true;
212  set(path);
213  // assign(path);
214  }
215 
216 
218  // void parse(const std::string & path);
219 
221  bool ensureLimitingSlash();
222 
223 
225  bool isLiteral() const;
226 
228  void extractPath(ODIMPath & path) const;
229 
231  bool match(const rack::ODIMPath & path) const;
232 
234  bool matchHead(const rack::ODIMPath & path) const;
235 
237  bool matchTail(const rack::ODIMPath & path) const;
238 
240  bool matchElem(const rack::ODIMPathElem & elem, bool defaultValue = true) const;
241 
242 };
243 
244 
245 
246 } // namespace rack
247 
248 
249 #endif
A bit vector with external Dictionary mapping from strings to bits and vice versa.
Definition: FlagsOld.h:45
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:343
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:191
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:99
bool isIndexed() const
Abbreviation of (group == NONE)
Definition: ODIMPath.h:318
drain::Flagger::ivalue_t group_t
In H5, "groups" correspond to "directories" or "folders" in Unix and Windows.
Definition: ODIMPath.h:91
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:182
bool isLiteral() const
Checks if corresponds to a single path, implying that all the index ranges are singletons.
Definition: ODIMPathMatcher.cpp:230
ODIMPathMatcher(const std::string &path)
Almost copy constructor.
Definition: ODIMPathMatcher.h:203
bool matchHead(const rack::ODIMPath &path) const
Match leading part of path.
Definition: ODIMPathMatcher.cpp:268
bool ensureLimitingSlash()
Resolves "where|where".
Definition: ODIMPathMatcher.cpp:69
bool matchTail(const rack::ODIMPath &path) const
Match trailing part of path.
Definition: ODIMPathMatcher.cpp:296
ODIMPathMatcher(const char *path)
Almost copy constructor.
Definition: ODIMPathMatcher.h:210
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:323
ODIMPathMatcher()
Basic constructor.
Definition: ODIMPathMatcher.h:187
ODIMPathMatcher(const ODIMPathMatcher &matcher)
Copy constructor.
Definition: ODIMPathMatcher.h:192
void extractPath(ODIMPath &path) const
Convert to a single path, assuming uniqueness. Future option: extract all the enumerated paths.
Definition: ODIMPathMatcher.cpp:241
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:254
Definition: DataSelector.cpp:1277
Definition: DataSelector.cpp:44
bool & acceptTrailing
If true, allow trailing empty elements (appearing as repeated separators)
Definition: Path.h:79