bpp-core3  3.0.0
StringTokenizer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_TEXT_STRINGTOKENIZER_H
6 #define BPP_TEXT_STRINGTOKENIZER_H
7 
8 #include <deque>
9 #include <iostream>
10 #include <string>
11 
12 #include "../Exceptions.h"
13 
14 namespace bpp
15 {
22 {
23 protected:
25  std::deque<std::string> tokens_;
26  std::deque<std::string> splits_;
27 
30 
31 public:
40  StringTokenizer(const std::string& s, const std::string& delimiters = " \t\n\f\r", bool solid = false, bool allowEmptyTokens = false);
41 
42  virtual ~StringTokenizer() {}
43 
44 public:
45  StringTokenizer() : tokens_(), splits_(), currentPosition_(0) {}
46 
47 public:
54  const std::string& nextToken()
55  {
56  if (!hasMoreToken()) throw Exception("No more token in tokenizer.");
57  return tokens_[currentPosition_++];
58  }
59 
64  bool hasMoreToken() const
65  {
66  return currentPosition_ < tokens_.size();
67  }
68 
74  size_t numberOfRemainingTokens() const { return tokens_.size() - currentPosition_; }
75 
84  const std::string& getToken(size_t pos) const { return tokens_[pos]; }
85 
91  const std::deque<std::string>& getTokens() const { return tokens_; }
92 
96  void removeEmptyTokens();
97 
101  std::string unparseRemainingTokens() const;
102 };
103 } // end of namespace bpp.
104 #endif // BPP_TEXT_STRINGTOKENIZER_H
A tokenizer for strings.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
bool hasMoreToken() const
Tell if some tokens are still available.
const std::string & getToken(size_t pos) const
Get a particular token.
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
void removeEmptyTokens()
remove all empty token from the current position.
std::deque< std::string > tokens_
Where the tokens are stored.
const std::deque< std::string > & getTokens() const
Retrieve all tokens.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
std::string unparseRemainingTokens() const
std::deque< std::string > splits_
size_t currentPosition_
the current position in the token list.