Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / ai_service / keyword.cpp
blobfbfd8b2ded5fba72208d1758fea946db89575ab8
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "stdpch.h"
20 #include "keyword.h"
22 //---------------------------------------------------------------------------------
23 // CKeywordSet
24 //---------------------------------------------------------------------------------
26 void CKeywordSet::clear()
28 _words.clear();
31 void CKeywordSet::addKeywords(const std::string &keywords)
33 // chunkify the input string into white-space separated words
34 std::string word, s=keywords;
35 while (AI_SHARE::stringToWordAndTail(s,word,s))
37 if (_words.size()<32)
38 _words.push_back(word);
39 else
40 nlwarning("Too many keywords for keyword set (limit to 32) - ignoring keyword: '%s'",word.c_str());
44 bool CKeywordSet::stringToMask(std::string s, CKeywordMask &result) const
46 bool ret = true;
47 std::string word;
48 while (AI_SHARE::stringToWordAndTail(s,word,s))
50 //look for the keyword in the word list
51 uint i;
52 for (i=0;i<_words.size();++i)
53 if (NLMISC::nlstricmp(word,_words[i])==0)
55 result._mask|=(1<<i);
56 break;
58 if (i==_words.size())
60 nlwarning("Unknown keyword: '%s' in mask description: '%s'",word.c_str(),s.c_str());
61 ret = false;
64 return ret;
67 bool CKeywordSet::stringToFilter(std::string s, CKeywordFilter &result) const
69 // CKeywordFilter result;
70 bool ret = true;
71 CKeywordMask *mask=&result._includeAny;
72 std::string word;
73 while (AI_SHARE::stringToWordAndTail(s,word,s))
75 if (word=="+" || word=="-")
77 // this is a + or - without ajoining word
78 mask=(word[0]=='+')? &result._includeAll: &result._notInclude;
80 else
82 if (word[0]=='+' || word[0]=='-')
84 // this is the case of the '+' or '-' immediately followed by the keyword
85 mask=(word[0]=='+')? &result._includeAll: &result._notInclude;
86 // prune the first character from the word so that we can drop through to common code
87 word=word.substr(1);
90 //look for the keyword in the word list
91 uint i;
92 for (i=0;i<_words.size();++i)
93 if (NLMISC::nlstricmp(word,_words[i])==0)
95 mask->_mask|=(1<<i);
96 break;
98 if (i==_words.size())
100 nlwarning("Unknown keyword: '%s'",word.c_str());
101 ret = false;
104 // reset the type for the next keyword
105 mask=&result._includeAny;
108 return ret;
111 std::string CKeywordSet::maskToString(CKeywordMask mask) const
113 std::string result;
114 for (uint i=0;i<_words.size();++i)
115 if (mask._mask&(1<<i))
116 result+=_words[i]+' ';
117 return result;
120 std::string CKeywordSet::filterToString(CKeywordFilter filter) const
122 std::string result;
123 for (uint i=0;i<_words.size();++i)
125 if (filter._includeAll._mask&(1<<i)) result+=std::string("+")+_words[i]+' ';
126 else if (filter._notInclude._mask&(1<<i)) result+=std::string("-")+_words[i]+' ';
127 if (filter._notInclude._mask&(1<<i)) result+=_words[i]+' ';
129 return result;
132 std::string CKeywordSet::toString()
134 std::string s;
135 for (uint i=0;i<_words.size();++i)
136 s+=_words[i]+' ';
137 return s;
140 //---------------------------------------------------------------------------------