[cstdstring] removal of Trim/TrimLeft/TrimRight
[vuplus_xbmc] / xbmc / utils / TextSearch.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "TextSearch.h"
22
23 using namespace std;
24
25 CTextSearch::CTextSearch(const CStdString &strSearchTerms, bool bCaseSensitive /* = false */, TextSearchDefault defaultSearchMode /* = SEARCH_DEFAULT_OR */)
26 {
27   m_bCaseSensitive = bCaseSensitive;
28   ExtractSearchTerms(strSearchTerms, defaultSearchMode);
29 }
30
31 CTextSearch::~CTextSearch(void)
32 {
33   m_AND.clear();
34   m_OR.clear();
35   m_NOT.clear();
36 }
37
38 bool CTextSearch::IsValid(void) const
39 {
40   return m_AND.size() > 0 || m_OR.size() > 0 || m_NOT.size() > 0;
41 }
42
43 bool CTextSearch::Search(const CStdString &strHaystack) const
44 {
45   if (strHaystack.empty() || !IsValid())
46     return false;
47
48   CStdString strSearch(strHaystack);
49   if (!m_bCaseSensitive)
50     strSearch = strSearch.ToLower();
51
52   /* check whether any of the NOT terms matches and return false if there's a match */
53   for (unsigned int iNotPtr = 0; iNotPtr < m_NOT.size(); iNotPtr++)
54   {
55     if (strSearch.Find(m_NOT.at(iNotPtr)) != -1)
56       return false;
57   }
58
59   /* check whether at least one of the OR terms matches and return false if there's no match found */
60   bool bFound(m_OR.size() == 0);
61   for (unsigned int iOrPtr = 0; iOrPtr < m_OR.size(); iOrPtr++)
62   {
63     if (strSearch.Find(m_OR.at(iOrPtr)) != -1)
64     {
65       bFound = true;
66       break;
67     }
68   }
69   if (!bFound)
70     return false;
71
72   /* check whether all of the AND terms match and return false if one of them wasn't found */
73   for (unsigned int iAndPtr = 0; iAndPtr < m_AND.size(); iAndPtr++)
74   {
75     if (strSearch.Find(m_AND[iAndPtr]) == -1)
76       return false;
77   }
78
79   /* all ok, return true */
80   return true;
81 }
82
83 void CTextSearch::GetAndCutNextTerm(CStdString &strSearchTerm, CStdString &strNextTerm)
84 {
85   CStdString strFindNext(" ");
86
87   if (StringUtils::EndsWith(strSearchTerm, "\""))
88   {
89     strSearchTerm.erase(0, 1);
90     strFindNext = "\"";
91   }
92
93   int iNextPos = strSearchTerm.Find(strFindNext);
94   if (iNextPos != -1)
95   {
96     strNextTerm = strSearchTerm.Left(iNextPos);
97     strSearchTerm.erase(0, iNextPos + 1);
98   }
99   else
100   {
101     strNextTerm = strSearchTerm;
102     strSearchTerm.clear();
103   }
104 }
105
106 void CTextSearch::ExtractSearchTerms(const CStdString &strSearchTerm, TextSearchDefault defaultSearchMode)
107 {
108   CStdString strParsedSearchTerm(strSearchTerm);
109   StringUtils::Trim(strParsedSearchTerm);
110
111   if (!m_bCaseSensitive)
112     strParsedSearchTerm = strParsedSearchTerm.ToLower();
113
114   bool bNextAND(defaultSearchMode == SEARCH_DEFAULT_AND);
115   bool bNextOR(defaultSearchMode == SEARCH_DEFAULT_OR);
116   bool bNextNOT(defaultSearchMode == SEARCH_DEFAULT_NOT);
117
118   while (strParsedSearchTerm.length() > 0)
119   {
120     StringUtils::TrimLeft(strParsedSearchTerm);
121
122     if (StringUtils::StartsWith(strParsedSearchTerm, "!") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "not"))
123     {
124       CStdString strDummy;
125       GetAndCutNextTerm(strParsedSearchTerm, strDummy);
126       bNextNOT = true;
127     }
128     else if (StringUtils::StartsWith(strParsedSearchTerm, "+") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "and"))
129     {
130       CStdString strDummy;
131       GetAndCutNextTerm(strParsedSearchTerm, strDummy);
132       bNextAND = true;
133     }
134     else if (StringUtils::StartsWith(strParsedSearchTerm, "|") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "or"))
135     {
136       CStdString strDummy;
137       GetAndCutNextTerm(strParsedSearchTerm, strDummy);
138       bNextOR = true;
139     }
140     else
141     {
142       CStdString strTerm;
143       GetAndCutNextTerm(strParsedSearchTerm, strTerm);
144       if (strTerm.length() > 0)
145       {
146         if (bNextAND)
147           m_AND.push_back(strTerm);
148         else if (bNextOR)
149           m_OR.push_back(strTerm);
150         else if (bNextNOT)
151           m_NOT.push_back(strTerm);
152       }
153       else
154       {
155         break;
156       }
157
158       bNextAND = (defaultSearchMode == SEARCH_DEFAULT_AND);
159       bNextOR  = (defaultSearchMode == SEARCH_DEFAULT_OR);
160       bNextNOT = (defaultSearchMode == SEARCH_DEFAULT_NOT);
161     }
162
163     StringUtils::TrimLeft(strParsedSearchTerm);
164   }
165 }