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