| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2009 Jürgen Riegel <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#ifdef __GNUC__ |
| 26 |
# include <unistd.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <sstream> |
| 30 |
|
| 31 |
#include <App/Document.h> |
| 32 |
#include <App/DocumentObjectPy.h> |
| 33 |
#include <App/DocumentObject.h> |
| 34 |
#include <Base/Interpreter.h> |
| 35 |
#include <Base/Tools.h> |
| 36 |
#include "Selection.h" |
| 37 |
#include "SelectionFilter.h" |
| 38 |
#include "SelectionFilterPy.h" |
| 39 |
#include "SelectionObject.h" |
| 40 |
|
| 41 |
|
| 42 |
using namespace Gui; |
| 43 |
|
| 44 |
// suppress annoying warnings from generated source files |
| 45 |
#ifdef _MSC_VER |
| 46 |
# pragma warning(disable : 4003) |
| 47 |
# pragma warning(disable : 4018) |
| 48 |
# pragma warning(disable : 4065) |
| 49 |
# pragma warning(disable : 4335) // disable MAC file format warning on VC |
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
SelectionFilterGate::SelectionFilterGate(const char* filter) |
| 54 |
{ |
| 55 |
Filter = new SelectionFilter(filter); |
| 56 |
} |
| 57 |
|
| 58 |
SelectionFilterGate::SelectionFilterGate(SelectionFilter* filter) |
| 59 |
{ |
| 60 |
Filter = filter; |
| 61 |
} |
| 62 |
|
| 63 |
SelectionFilterGate::SelectionFilterGate() |
| 64 |
{ |
| 65 |
Filter = nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
SelectionFilterGate::~SelectionFilterGate() |
| 69 |
{ |
| 70 |
delete Filter; |
| 71 |
} |
| 72 |
|
| 73 |
bool SelectionFilterGate::allow(App::Document* /*pDoc*/, App::DocumentObject* pObj, const char* sSubName) |
| 74 |
{ |
| 75 |
return Filter->test(pObj, sSubName); |
| 76 |
} |
| 77 |
|
| 78 |
// ---------------------------------------------------------------------------- |
| 79 |
|
| 80 |
SelectionGatePython::SelectionGatePython(const Py::Object& obj) |
| 81 |
: gate(obj) |
| 82 |
{} |
| 83 |
|
| 84 |
SelectionGatePython::~SelectionGatePython() = default; |
| 85 |
|
| 86 |
bool SelectionGatePython::allow(App::Document* doc, App::DocumentObject* obj, const char* sub) |
| 87 |
{ |
| 88 |
Base::PyGILStateLocker lock; |
| 89 |
try { |
| 90 |
if (this->gate.hasAttr(std::string("allow"))) { |
| 91 |
Py::Callable method(this->gate.getAttr(std::string("allow"))); |
| 92 |
Py::Object pyDoc = Py::asObject(doc->getPyObject()); |
| 93 |
Py::Object pyObj = Py::asObject(obj->getPyObject()); |
| 94 |
Py::Object pySub = Py::None(); |
| 95 |
if (sub) { |
| 96 |
pySub = Py::String(sub); |
| 97 |
} |
| 98 |
Py::Tuple args(3); |
| 99 |
args.setItem(0, pyDoc); |
| 100 |
args.setItem(1, pyObj); |
| 101 |
args.setItem(2, pySub); |
| 102 |
Py::Boolean ok(method.apply(args)); |
| 103 |
return (bool)ok; |
| 104 |
} |
| 105 |
} |
| 106 |
catch (Py::Exception&) { |
| 107 |
Base::PyException e; // extract the Python error text |
| 108 |
e.reportException(); |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
// ---------------------------------------------------------------------------- |
| 115 |
|
| 116 |
SelectionFilterGatePython::SelectionFilterGatePython(SelectionFilterPy* obj) |
| 117 |
: filter(obj) |
| 118 |
{ |
| 119 |
Base::PyGILStateLocker lock; |
| 120 |
Py_INCREF(filter); |
| 121 |
} |
| 122 |
|
| 123 |
SelectionFilterGatePython::~SelectionFilterGatePython() |
| 124 |
{ |
| 125 |
Base::PyGILStateLocker lock; |
| 126 |
Py_DECREF(filter); |
| 127 |
} |
| 128 |
|
| 129 |
bool SelectionFilterGatePython::allow(App::Document*, App::DocumentObject* obj, const char* sub) |
| 130 |
{ |
| 131 |
return filter->filter.test(obj, sub); |
| 132 |
} |
| 133 |
|
| 134 |
// ---------------------------------------------------------------------------- |
| 135 |
|
| 136 |
SelectionFilter::SelectionFilter(const char* filter, App::DocumentObject* container) |
| 137 |
: container(container) |
| 138 |
{ |
| 139 |
setFilter(filter); |
| 140 |
} |
| 141 |
|
| 142 |
SelectionFilter::SelectionFilter(const std::string& filter, App::DocumentObject* container) |
| 143 |
: container(container) |
| 144 |
{ |
| 145 |
setFilter(filter.c_str()); |
| 146 |
} |
| 147 |
|
| 148 |
void SelectionFilter::setFilter(const char* filter) |
| 149 |
{ |
| 150 |
if (Base::Tools::isNullOrEmpty(filter)) { |
| 151 |
Ast.reset(); |
| 152 |
Filter.clear(); |
| 153 |
} |
| 154 |
else { |
| 155 |
Filter = filter; |
| 156 |
if (!parse()) { |
| 157 |
throw Base::ParserError(Errors.c_str()); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
SelectionFilter::~SelectionFilter() = default; |
| 163 |
|
| 164 |
bool SelectionFilter::match() |
| 165 |
{ |
| 166 |
if (!Ast) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
Result.clear(); |
| 170 |
|
| 171 |
for (const auto& it : Ast->Objects) { |
| 172 |
std::size_t min = 1; |
| 173 |
std::size_t max = 1; |
| 174 |
|
| 175 |
if (it->Slice) { |
| 176 |
min = it->Slice->Min; |
| 177 |
max = it->Slice->Max; |
| 178 |
} |
| 179 |
std::vector<Gui::SelectionObject> temp = container |
| 180 |
? Gui::Selection().getSelectionIn(container, it->ObjectType) |
| 181 |
: Gui::Selection().getSelectionEx(nullptr, it->ObjectType); |
| 182 |
|
| 183 |
// test if subnames present |
| 184 |
if (it->SubName.empty()) { |
| 185 |
// if no subnames the count of the object get tested |
| 186 |
if (temp.size() < min || temp.size() > max) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
} |
| 190 |
else { |
| 191 |
// if subnames present count all subs over the selected object of type |
| 192 |
std::size_t subCount = 0; |
| 193 |
for (const auto& it2 : temp) { |
| 194 |
const std::vector<std::string>& subNames = it2.getSubNames(); |
| 195 |
if (subNames.empty()) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
for (const auto& subName : subNames) { |
| 199 |
if (subName.find(it->SubName) != 0) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |
| 203 |
subCount += subNames.size(); |
| 204 |
} |
| 205 |
|
| 206 |
if (subCount < min || subCount > max) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
Result.push_back(temp); |
| 212 |
} |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
bool SelectionFilter::test(App::DocumentObject* pObj, const char* sSubName) |
| 217 |
{ |
| 218 |
if (!Ast) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
for (const auto& it : Ast->Objects) { |
| 223 |
if (pObj->isDerivedFrom(it->ObjectType)) { |
| 224 |
if (!sSubName) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
if (it->SubName.empty()) { |
| 228 |
return true; |
| 229 |
} |
| 230 |
if (std::string(sSubName).find(it->SubName) == 0) { |
| 231 |
return true; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
void SelectionFilter::addError(const char* e) |
| 239 |
{ |
| 240 |
Errors += e; |
| 241 |
Errors += '\n'; |
| 242 |
} |
| 243 |
|
| 244 |
// === Parser & Scanner stuff =============================================== |
| 245 |
|
| 246 |
// include the Scanner and the Parser for the filter language |
| 247 |
|
| 248 |
SelectionFilter* ActFilter = nullptr; |
| 249 |
Node_Block* TopBlock = nullptr; |
| 250 |
|
| 251 |
// error func |
| 252 |
void yyerror(const char* errorinfo) |
| 253 |
{ |
| 254 |
ActFilter->addError(errorinfo); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
// for VC9 (isatty and fileno not supported anymore) |
| 259 |
#ifdef _MSC_VER |
| 260 |
int isatty(int i) |
| 261 |
{ |
| 262 |
return _isatty(i); |
| 263 |
} |
| 264 |
int fileno(FILE* stream) |
| 265 |
{ |
| 266 |
return _fileno(stream); |
| 267 |
} |
| 268 |
#endif |
| 269 |
|
| 270 |
namespace SelectionParser |
| 271 |
{ |
| 272 |
|
| 273 |
/*! |
| 274 |
* \brief The StringFactory class |
| 275 |
* Helper class to record the created strings used by the parser. |
| 276 |
*/ |
| 277 |
class StringFactory |
| 278 |
{ |
| 279 |
std::list<std::unique_ptr<std::string>> data; |
| 280 |
std::size_t max_elements = 20; |
| 281 |
|
| 282 |
public: |
| 283 |
static StringFactory* instance() |
| 284 |
{ |
| 285 |
static auto inst = new StringFactory(); |
| 286 |
return inst; |
| 287 |
} |
| 288 |
std::string* make(const std::string& str) |
| 289 |
{ |
| 290 |
data.push_back(std::make_unique<std::string>(str)); |
| 291 |
return data.back().get(); |
| 292 |
} |
| 293 |
static std::string* New(const std::string& str) |
| 294 |
{ |
| 295 |
return StringFactory::instance()->make(str); |
| 296 |
} |
| 297 |
void clear() |
| 298 |
{ |
| 299 |
if (data.size() > max_elements) { |
| 300 |
data.clear(); |
| 301 |
} |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
// show the parser the lexer method |
| 306 |
#define yylex SelectionFilterlex |
| 307 |
int SelectionFilterlex(); |
| 308 |
|
| 309 |
// Parser, defined in SelectionFilter.y |
| 310 |
#include "SelectionFilter.tab.c" |
| 311 |
|
| 312 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 313 |
// Scanner, defined in SelectionFilter.l |
| 314 |
# if defined(__clang__) |
| 315 |
# pragma clang diagnostic push |
| 316 |
# pragma clang diagnostic ignored "-Wsign-compare" |
| 317 |
# pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 318 |
# elif defined(__GNUC__) |
| 319 |
# pragma GCC diagnostic push |
| 320 |
# pragma GCC diagnostic ignored "-Wsign-compare" |
| 321 |
# endif |
| 322 |
# include "SelectionFilter.lex.cpp" |
| 323 |
# if defined(__clang__) |
| 324 |
# pragma clang diagnostic pop |
| 325 |
# elif defined(__GNUC__) |
| 326 |
# pragma GCC diagnostic pop |
| 327 |
# endif |
| 328 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 329 |
|
| 330 |
class StringBufferCleaner |
| 331 |
{ |
| 332 |
public: |
| 333 |
explicit StringBufferCleaner(YY_BUFFER_STATE buffer) |
| 334 |
: my_string_buffer {buffer} |
| 335 |
{} |
| 336 |
~StringBufferCleaner() |
| 337 |
{ |
| 338 |
// free the scan buffer |
| 339 |
yy_delete_buffer(my_string_buffer); |
| 340 |
} |
| 341 |
|
| 342 |
StringBufferCleaner(const StringBufferCleaner&) = delete; |
| 343 |
StringBufferCleaner(StringBufferCleaner&&) = delete; |
| 344 |
StringBufferCleaner& operator=(const StringBufferCleaner&) = delete; |
| 345 |
StringBufferCleaner& operator=(StringBufferCleaner&&) = delete; |
| 346 |
|
| 347 |
private: |
| 348 |
YY_BUFFER_STATE my_string_buffer; |
| 349 |
}; |
| 350 |
|
| 351 |
} // namespace SelectionParser |
| 352 |
|
| 353 |
bool SelectionFilter::parse() |
| 354 |
{ |
| 355 |
Errors = ""; |
| 356 |
SelectionParser::YY_BUFFER_STATE my_string_buffer = SelectionParser::SelectionFilter_scan_string( |
| 357 |
Filter.c_str() |
| 358 |
); |
| 359 |
SelectionParser::StringBufferCleaner cleaner(my_string_buffer); |
| 360 |
// be aware that this parser is not reentrant! Don't use with Threats!!! |
| 361 |
assert(!ActFilter); |
| 362 |
ActFilter = this; |
| 363 |
/*int my_parse_result =*/SelectionParser::yyparse(); |
| 364 |
ActFilter = nullptr; |
| 365 |
Ast.reset(TopBlock); |
| 366 |
TopBlock = nullptr; |
| 367 |
SelectionParser::StringFactory::instance()->clear(); |
| 368 |
|
| 369 |
if (Errors.empty()) { |
| 370 |
return true; |
| 371 |
} |
| 372 |
else { |
| 373 |
return false; |
| 374 |
} |
| 375 |
} |