blob: e326c2ad53051143184647b8c347351dc5dea411 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#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
9namespace nlsr {
10
11using namespace std;
12using namespace boost;
13
14void
15Tokenizer::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
33void
34Tokenizer::insertToken(const string& token)
35{
36 m_tokenList.push_back(token);
37 m_vTokenList.push_back(token);
38}
39
40uint32_t
41Tokenizer::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
61string
62Tokenizer::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
78string
79Tokenizer::getTokenString(uint32_t from)
80{
81 return getTokenString(from, m_tokenList.size() - 1);
82}
83
84static bool
85tokenCompare(string& s1, string& s2)
86{
87 return s1 == s2;
88}
89
90void
91Tokenizer::makeRestOfTheLine()
92{
93 m_restOfTheLine = getTokenString(1);
94}
95
96bool
97Tokenizer::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