akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [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> | ||||
10 | #include <ndn-cpp-dev/face.hpp> | ||||
11 | |||||
12 | namespace nlsr | ||||
13 | { | ||||
14 | |||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 15 | using namespace std; |
16 | using namespace boost; | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 17 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 18 | class nlsrTokenizer |
19 | { | ||||
20 | public: | ||||
21 | nlsrTokenizer(const string& inputString) | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 22 | : m_firstToken() |
23 | , m_restOfTheLine() | ||||
24 | , m_currentPosition(0) | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 25 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 26 | m_seps = " "; |
27 | m_originalString = inputString; | ||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 28 | makeToken(); |
29 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 30 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 31 | nlsrTokenizer(const string& inputString, const string& separator) |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 32 | : m_firstToken() |
33 | , m_restOfTheLine() | ||||
34 | , m_currentPosition(0) | ||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 35 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 36 | m_seps = separator; |
37 | m_originalString = inputString; | ||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 38 | makeToken(); |
39 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 40 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 41 | string getFirstToken() |
42 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 43 | return m_firstToken; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 44 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 45 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 46 | string getRestOfLine() |
47 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 48 | return m_restOfTheLine; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 49 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 50 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 51 | void resetCurrentPosition(uint32_t cp=0) |
52 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 53 | if( cp >=0 && cp <= m_vTokenList.size() ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 54 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 55 | m_currentPosition=cp; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 56 | } |
57 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 58 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 59 | string getNextToken() |
60 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 61 | if(m_currentPosition >= 0 && m_currentPosition <= (m_vTokenList.size()-1)) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 62 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 63 | return m_vTokenList[m_currentPosition++]; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 64 | } |
65 | return ""; | ||||
66 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 67 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 68 | uint32_t getTokenNumber() |
69 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 70 | return m_tokenList.size(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 71 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 72 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 73 | string getToken(int position) |
74 | { | ||||
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 75 | if( position >=0 && position <m_vTokenList.size() ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 76 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 77 | return m_vTokenList[position]; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 78 | } |
79 | return ""; | ||||
80 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 81 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 82 | int getTokenPosition(string& token); |
83 | string getTokenString(int from , int to); | ||||
84 | string getTokenString(int from); | ||||
85 | bool doesTokenExist(string token); | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 86 | |
87 | |||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 88 | private: |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 89 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 90 | void makeToken(); |
91 | void insertToken(const string& token); | ||||
92 | void makeRestOfTheLine(); | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 93 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 94 | string m_seps; |
95 | string m_originalString; | ||||
96 | string m_firstToken; | ||||
97 | string m_restOfTheLine; | ||||
98 | std::list<string> m_tokenList; | ||||
99 | std::vector<string> m_vTokenList; | ||||
100 | uint32_t m_currentPosition; | ||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 101 | }; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 102 | |
103 | }//namespace nlsr | ||||
104 | #endif |