akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <boost/tokenizer.hpp> |
| 3 | #include <boost/algorithm/string.hpp> |
| 4 | #include <string> |
| 5 | #include <algorithm> |
| 6 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 7 | #include <ndn-cxx/face.hpp> |
| 8 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 9 | #include "tokenizer.hpp" |
| 10 | |
| 11 | namespace nlsr { |
| 12 | |
| 13 | using namespace std; |
| 14 | using namespace boost; |
| 15 | |
| 16 | void |
| 17 | Tokenizer::makeToken() |
| 18 | { |
| 19 | char_separator<char> sep(m_seps.c_str()); |
| 20 | tokenizer<char_separator<char> >tokens(m_originalString, sep); |
| 21 | tokenizer<char_separator<char> >::iterator tok_iter = tokens.begin(); |
| 22 | for (; tok_iter != tokens.end(); ++tok_iter) |
| 23 | { |
| 24 | string oneToken(*tok_iter); |
| 25 | trim(oneToken); |
| 26 | if (!oneToken.empty()) |
| 27 | { |
| 28 | insertToken(oneToken); |
| 29 | } |
| 30 | } |
| 31 | m_firstToken = m_vTokenList[0]; |
| 32 | makeRestOfTheLine(); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | Tokenizer::insertToken(const string& token) |
| 37 | { |
| 38 | m_tokenList.push_back(token); |
| 39 | m_vTokenList.push_back(token); |
| 40 | } |
| 41 | |
| 42 | uint32_t |
| 43 | Tokenizer::getTokenPosition(string& token) |
| 44 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 45 | uint32_t pos = -1; |
| 46 | uint32_t i = 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | for (std::list<string>::iterator it = m_tokenList.begin(); |
| 48 | it != m_tokenList.end(); it++) |
| 49 | { |
| 50 | if ((*it) == token) |
| 51 | { |
| 52 | break; |
| 53 | } |
| 54 | i++; |
| 55 | } |
| 56 | if (i < m_tokenList.size()) |
| 57 | { |
| 58 | pos = i; |
| 59 | } |
| 60 | return pos; |
| 61 | } |
| 62 | |
| 63 | string |
| 64 | Tokenizer::getTokenString(uint32_t from , uint32_t to) |
| 65 | { |
| 66 | string returnString = ""; |
| 67 | if ((to < m_tokenList.size()) && |
| 68 | (to >= from && to < m_tokenList.size())) |
| 69 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 70 | for (uint32_t i = from; i <= to; i++) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | { |
| 72 | returnString += m_seps; |
| 73 | returnString += m_vTokenList[i]; |
| 74 | } |
| 75 | } |
| 76 | trim(returnString); |
| 77 | return returnString; |
| 78 | } |
| 79 | |
| 80 | string |
| 81 | Tokenizer::getTokenString(uint32_t from) |
| 82 | { |
| 83 | return getTokenString(from, m_tokenList.size() - 1); |
| 84 | } |
| 85 | |
| 86 | static bool |
| 87 | tokenCompare(string& s1, string& s2) |
| 88 | { |
| 89 | return s1 == s2; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | Tokenizer::makeRestOfTheLine() |
| 94 | { |
| 95 | m_restOfTheLine = getTokenString(1); |
| 96 | } |
| 97 | |
| 98 | bool |
| 99 | Tokenizer::doesTokenExist(string token) |
| 100 | { |
| 101 | std::list<string>::iterator it = std::find_if(m_tokenList.begin(), |
| 102 | m_tokenList.end(), |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 103 | ndn::bind(&tokenCompare, _1 , token)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | if (it != m_tokenList.end()) |
| 105 | { |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | }//namespace nlsr |