votca 2024.2-dev
Loading...
Searching...
No Matches
rangeparser.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <functional>
20#include <iostream>
21#include <stdexcept>
22
23// Local VOTCA includes
26
27namespace votca {
28namespace tools {
29
30RangeParser::RangeParser() = default;
31
32void RangeParser::Parse(std::string str) {
33 // remove all spaces in string
34 std::string::iterator it =
35 std::remove_if(str.begin(), str.end(), [](char a) { return a == ' '; });
36 str = std::string(str.begin(), it);
37
38 Tokenizer tok(str, ",");
39 for (std::string bl : tok) {
40 ParseBlock(bl);
41 }
42}
43
44void RangeParser::ParseBlock(std::string str) {
45 std::vector<std::string> toks = Tokenizer(str, ":").ToVector();
46
47 block_t block;
48 block.stride_ = 1;
49
50 if (toks.size() > 3 || toks.size() < 1) {
51 throw std::runtime_error("invalid range");
52 }
53
54 block.begin_ = block.end_ = std::stoi(toks[0]);
55
56 if (toks.size() == 2) {
57 block.end_ = std::stoi(toks[1]);
58 }
59
60 if (toks.size() == 3) {
61 block.stride_ = std::stoi(toks[1]);
62 block.end_ = std::stoi(toks[2]);
63 }
64
65 if (block.begin_ * block.stride_ > block.end_ * block.stride_) {
66 throw std::runtime_error(
67 std::string("invalid range " + str +
68 ": begin, end and stride do not form a closed interval"));
69 }
70
71 blocks_.push_back(block);
72}
73
75 current_ += (*block_).stride_;
76 if (current_ > (*block_).end_) {
77 ++block_;
78 if (block_ != parent_->blocks_.end()) {
79 current_ = (*block_).begin_;
80 } else {
81 current_ = -1;
82 }
83 }
84 return *this;
85}
86
87} // namespace tools
88} // namespace votca
void Parse(std::string str)
void ParseBlock(std::string str)
std::list< block_t > blocks_
Definition rangeparser.h:81
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
base class for all analysis tools
Definition basebead.h:33
RangeParser::iterator & operator++()
std::list< block_t >::iterator block_
Definition rangeparser.h:69