akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #ifndef NLSR_TOKENIZER_HPP |
| 2 | #define NLSR_TOKENIZER_HPP |
| 3 | |
| 4 | #include <iostream> |
| 5 | #include <boost/tokenizer.hpp> |
| 6 | #include <boost/algorithm/string.hpp> |
| 7 | #include <string> |
| 8 | #include <list> |
| 9 | #include <vector> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 10 | |
| 11 | namespace nlsr { |
| 12 | |
| 13 | class Tokenizer |
| 14 | { |
| 15 | public: |
| 16 | Tokenizer(const std::string& inputString) |
| 17 | : m_firstToken() |
| 18 | , m_restOfTheLine() |
| 19 | , m_currentPosition(0) |
| 20 | { |
| 21 | m_seps = " "; |
| 22 | m_originalString = inputString; |
| 23 | makeToken(); |
| 24 | } |
| 25 | |
| 26 | Tokenizer(const std::string& inputString, const std::string& separator) |
| 27 | : m_firstToken() |
| 28 | , m_restOfTheLine() |
| 29 | , m_currentPosition(0) |
| 30 | { |
| 31 | m_seps = separator; |
| 32 | m_originalString = inputString; |
| 33 | makeToken(); |
| 34 | } |
| 35 | |
| 36 | std::string |
| 37 | getFirstToken() |
| 38 | { |
| 39 | return m_firstToken; |
| 40 | } |
| 41 | |
| 42 | std::string |
| 43 | getRestOfLine() |
| 44 | { |
| 45 | return m_restOfTheLine; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | resetCurrentPosition(uint32_t cp = 0) |
| 50 | { |
| 51 | if (cp <= m_vTokenList.size()) |
| 52 | { |
| 53 | m_currentPosition = cp; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | std::string |
| 58 | getNextToken() |
| 59 | { |
| 60 | if (m_currentPosition <= (m_vTokenList.size() - 1)) |
| 61 | { |
| 62 | return m_vTokenList[m_currentPosition++]; |
| 63 | } |
| 64 | return ""; |
| 65 | } |
| 66 | |
| 67 | uint32_t |
| 68 | getTokenNumber() |
| 69 | { |
| 70 | return m_tokenList.size(); |
| 71 | } |
| 72 | |
| 73 | std::string |
| 74 | getToken(unsigned int position) |
| 75 | { |
| 76 | if (position < m_vTokenList.size()) |
| 77 | { |
| 78 | return m_vTokenList[position]; |
| 79 | } |
| 80 | return ""; |
| 81 | } |
| 82 | |
| 83 | uint32_t |
| 84 | getTokenPosition(std::string& token); |
| 85 | |
| 86 | std::string |
| 87 | getTokenString(uint32_t from , uint32_t to); |
| 88 | |
| 89 | std::string |
| 90 | getTokenString(uint32_t from); |
| 91 | |
| 92 | bool |
| 93 | doesTokenExist(std::string token); |
| 94 | |
| 95 | |
| 96 | private: |
| 97 | |
| 98 | void |
| 99 | makeToken(); |
| 100 | |
| 101 | void |
| 102 | insertToken(const std::string& token); |
| 103 | |
| 104 | void |
| 105 | makeRestOfTheLine(); |
| 106 | |
| 107 | std::string m_seps; |
| 108 | std::string m_originalString; |
| 109 | std::string m_firstToken; |
| 110 | std::string m_restOfTheLine; |
| 111 | std::list<std::string> m_tokenList; |
| 112 | std::vector<std::string> m_vTokenList; |
| 113 | uint32_t m_currentPosition; |
| 114 | }; |
| 115 | |
| 116 | }//namespace nlsr |
| 117 | #endif //NLSR_TOKENIZER_HPP |