blob: 095e55d8ee4b964deb1ae249a3474f992e5ea859 [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
akmhoquec8a10f72014-04-25 18:42:55 -05007#include <ndn-cxx/face.hpp>
8
akmhoque53353462014-04-22 08:43:45 -05009#include "tokenizer.hpp"
10
11namespace nlsr {
12
13using namespace std;
14using namespace boost;
15
16void
17Tokenizer::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
35void
36Tokenizer::insertToken(const string& token)
37{
38 m_tokenList.push_back(token);
39 m_vTokenList.push_back(token);
40}
41
42uint32_t
43Tokenizer::getTokenPosition(string& token)
44{
akmhoquefdbddb12014-05-02 18:35:19 -050045 uint32_t pos = -1;
46 uint32_t i = 0;
akmhoque53353462014-04-22 08:43:45 -050047 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
63string
64Tokenizer::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 {
akmhoquefdbddb12014-05-02 18:35:19 -050070 for (uint32_t i = from; i <= to; i++)
akmhoque53353462014-04-22 08:43:45 -050071 {
72 returnString += m_seps;
73 returnString += m_vTokenList[i];
74 }
75 }
76 trim(returnString);
77 return returnString;
78}
79
80string
81Tokenizer::getTokenString(uint32_t from)
82{
83 return getTokenString(from, m_tokenList.size() - 1);
84}
85
86static bool
87tokenCompare(string& s1, string& s2)
88{
89 return s1 == s2;
90}
91
92void
93Tokenizer::makeRestOfTheLine()
94{
95 m_restOfTheLine = getTokenString(1);
96}
97
98bool
99Tokenizer::doesTokenExist(string token)
100{
101 std::list<string>::iterator it = std::find_if(m_tokenList.begin(),
102 m_tokenList.end(),
akmhoquec8a10f72014-04-25 18:42:55 -0500103 ndn::bind(&tokenCompare, _1 , token));
akmhoque53353462014-04-22 08:43:45 -0500104 if (it != m_tokenList.end())
105 {
106 return true;
107 }
108 return false;
109}
110
111}//namespace nlsr