highlighter application  1.1
HCE project utils : highlighter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
SearchStringParser.cpp
Go to the documentation of this file.
1 #include "Algorithms.hpp"
2 #include "SearchStringParser.hpp"
4 
5 using namespace HCE::exception;
6 
7 void SearchStringParser::setSearchString(std::vector<std::pair<SearchType, std::string> > &sVec, SearchType searchType, std::string &searchString)
8 {
9  if(searchString.length() > 0)
10  {
11  sVec.push_back(std::pair<SearchType, std::string>(searchType, searchString));
12  searchString = "";
13  }
14 }
15 
16 std::vector<std::pair<SearchType, std::string> > SearchStringParser::parseSearchString(const std::string &searchString)
17 {
18  std::vector<std::pair<SearchType, std::string> > ret;
19  SearchType localSearchType = SearchType::ST_SINGLE;
20  std::string localSearchStr;
21  for(unsigned int i = 0; i < searchString.length(); i++)
22  {
23  if(searchString[i] == '"')
24  {
25  if(localSearchType == SearchType::ST_SINGLE)
26  {
27  setSearchString(ret, localSearchType, localSearchStr);
28  localSearchType = SearchType::ST_STRICKT;
29  }
30  else if(localSearchType == SearchType::ST_STRICKT)
31  {
32  setSearchString(ret, localSearchType, localSearchStr);
33  localSearchType = SearchType::ST_SINGLE;
34  }
35  else if(localSearchType == SearchType::ST_NO_STRICKT)
36  {
37  throw SearchStringParseException("BAD SEARCH SEQUENCE");
38  }
39  }
40  else if(searchString[i] == '\'')
41  {
42  if(localSearchType == SearchType::ST_SINGLE)
43  {
44  setSearchString(ret, localSearchType, localSearchStr);
45  localSearchType = SearchType::ST_NO_STRICKT;
46  }
47  else if(localSearchType == SearchType::ST_STRICKT)
48  {
49  throw SearchStringParseException("BAD SEARCH SEQUENCE");
50  }
51  else if(localSearchType == SearchType::ST_NO_STRICKT)
52  {
53  setSearchString(ret, localSearchType, localSearchStr);
54  localSearchType = SearchType::ST_SINGLE;
55  }
56  }
57  else if(searchString[i] == ' ')
58  {
59  if(localSearchType == SearchType::ST_SINGLE)
60  {
61  setSearchString(ret, localSearchType, localSearchStr);
62  }
63  else
64  {
65  localSearchStr += searchString[i];
66  }
67  }
68  else
69  {
70  localSearchStr += searchString[i];
71  }
72  }
73  setSearchString(ret, localSearchType, localSearchStr);
74  return ret;
75 }
76 
77 std::vector<std::vector<std::pair<SearchType, std::string> > > SearchStringParser::parse(const std::string &searchString, const std::string &algorithmSeparator)
78 {
79  std::vector<std::vector<std::pair<SearchType, std::string> > > ret;
80  if(!searchString.empty())
81  {
82  std::vector<std::string> algorithmStrings = HCE::algorithms::stringSplitter(searchString, algorithmSeparator, true);
83  for(unsigned int i = 0; i < algorithmStrings.size(); i++)
84  {
85  if(algorithmStrings[i].empty())
86  {
87  ret.push_back(std::vector<std::pair<SearchType, std::string> >());
88  continue;
89  }
90  try
91  {
92  ret.push_back(parseSearchString(algorithmStrings[i]));
93  }
94  catch(SearchStringParseException &excp)
95  {
96  ret.push_back(std::vector<std::pair<SearchType, std::string> >());
97  }
98  }
99  }
100  return ret;
101 }