2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file stringfilter.cpp Searching and filtering using a stringterm. */
11 #include "string_func.h"
12 #include "strings_func.h"
13 #include "stringfilter_type.h"
16 #include "safeguards.h"
18 static const WChar STATE_WHITESPACE
= ' ';
19 static const WChar STATE_WORD
= 'w';
20 static const WChar STATE_QUOTE1
= '\'';
21 static const WChar STATE_QUOTE2
= '"';
24 * Set the term to filter on.
25 * @param str Filter term
27 void StringFilter::SetFilterTerm(const char *str
)
29 this->word_index
.clear();
30 this->word_index
.shrink_to_fit();
31 this->word_matches
= 0;
32 free(this->filter_buffer
);
34 assert(str
!= nullptr);
36 char *dest
= MallocT
<char>(strlen(str
) + 1);
37 this->filter_buffer
= dest
;
39 WChar state
= STATE_WHITESPACE
;
40 const char *pos
= str
;
41 WordState
*word
= nullptr;
45 len
= Utf8Decode(&c
, pos
);
47 if (c
== 0 || (state
== STATE_WORD
&& IsWhitespace(c
))) {
49 if (word
!= nullptr) {
53 state
= STATE_WHITESPACE
;
54 if (c
!= 0) continue; else break;
57 if (state
== STATE_WHITESPACE
) {
59 if (IsWhitespace(c
)) continue;
63 if (c
== STATE_QUOTE1
|| c
== STATE_QUOTE2
) {
68 } else if (state
== STATE_WORD
) {
76 if (word
== nullptr) {
77 word
= &this->word_index
.emplace_back(WordState
{ dest
, false });
80 memcpy(dest
, pos
, len
);
86 * Reset the matching state to process a new item.
88 void StringFilter::ResetState()
90 this->word_matches
= 0;
91 for (WordState
&ws
: this->word_index
) {
97 * Pass another text line from the current item to the filter.
99 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
100 * Before processing the next item you have to call ResetState().
102 * @param str Another line from the item.
104 void StringFilter::AddLine(const char *str
)
106 if (str
== nullptr) return;
108 bool match_case
= this->case_sensitive
!= nullptr && *this->case_sensitive
;
109 for (WordState
&ws
: this->word_index
) {
111 if ((match_case
? strstr(str
, ws
.start
) : strcasestr(str
, ws
.start
)) != nullptr) {
113 this->word_matches
++;
120 * Pass another text line from the current item to the filter.
122 * You can call this multiple times for a single item, if the filter shall apply to multiple things.
123 * Before processing the next item you have to call ResetState().
125 * @param str Another line from the item.
127 void StringFilter::AddLine(StringID str
)
129 char buffer
[DRAW_STRING_BUFFER
];
130 GetString(buffer
, str
, lastof(buffer
));