search.cpp (3037B)
1 #include "search.h" 2 3 #include "patch.h" 4 5 namespace pluginLib::patchDB 6 { 7 namespace 8 { 9 std::string lowercase(const std::string& _src) 10 { 11 std::string str(_src); 12 for (char& i : str) 13 i = static_cast<char>(tolower(i)); 14 return str; 15 } 16 17 bool matchStringsIgnoreCase(const std::string& _test, const std::string& _search) 18 { 19 if (_search.empty()) 20 return true; 21 22 const auto t = lowercase(_test); 23 return t.find(_search) != std::string::npos; 24 } 25 /* 26 bool matchStrings(const std::string& _test, const std::string& _search) 27 { 28 if (_search.empty()) 29 return true; 30 31 return _test.find(_search) != std::string::npos; 32 } 33 */ 34 bool testTags(const Tags& _tags, const Tags& _search) 35 { 36 for (const auto& t : _search.getAdded()) 37 { 38 if (!_tags.containsAdded(t)) 39 return false; 40 } 41 42 for (const auto& t : _search.getRemoved()) 43 { 44 if (_tags.containsAdded(t)) 45 return false; 46 } 47 return true; 48 } 49 /* 50 bool matchDataSource(const DataSourceNode& _source, const DataSource& _search) 51 { 52 if (_source.hasParent() && matchDataSource(*_source.getParent(), _search)) 53 return true; 54 55 if (_search.type != SourceType::Invalid && _source.type != _search.type) 56 return false; 57 58 if (_search.bank != g_invalidBank && _source.bank != _search.bank) 59 return false; 60 61 if (!matchStrings(_source.name, _search.name)) 62 return false; 63 64 return true; 65 } 66 */ 67 bool matchDataSource(const DataSourceNode* _source, const DataSourceNodePtr& _search) 68 { 69 if (_source == _search.get()) 70 return true; 71 72 if (const auto& parent = _source->getParent()) 73 return matchDataSource(parent.get(), _search); 74 75 return false; 76 } 77 } 78 79 bool SearchRequest::match(const Patch& _patch) const 80 { 81 // datasource 82 83 const auto patchSource = _patch.source.lock(); 84 85 if(sourceNode) 86 { 87 if (!matchDataSource(patchSource.get(), sourceNode)) 88 return false; 89 } 90 else if(sourceType != SourceType::Invalid) 91 { 92 if(patchSource->type != sourceType) 93 return false; 94 } 95 96 // name 97 if (!matchStringsIgnoreCase(_patch.getName(), name)) 98 return false; 99 100 // if (program != g_invalidProgram && _patch.program != program) 101 // return false; 102 103 // tags, categories, ... 104 for (const auto& it : tags.get()) 105 { 106 const auto type = it.first; 107 const auto& t = it.second; 108 109 const auto& patchTags = _patch.getTags().get(type); 110 111 if (!testTags(patchTags, t)) 112 return false; 113 } 114 115 for (const auto& tagOfType : anyTagOfType) 116 { 117 if(_patch.getTags(tagOfType).empty()) 118 return false; 119 } 120 121 for (const auto& tagOfType : noTagOfType) 122 { 123 if(!_patch.getTags(tagOfType).empty()) 124 return false; 125 } 126 127 if (!customCompareFunc) 128 return true; 129 return customCompareFunc(_patch); 130 } 131 132 bool SearchRequest::isValid() const 133 { 134 return !name.empty() || !tags.empty() || sourceNode || patch; 135 } 136 137 bool SearchRequest::operator==(const SearchRequest& _r) const 138 { 139 return name == _r.name && tags == _r.tags && sourceNode == _r.sourceNode && patch == _r.patch && sourceType == _r.sourceType; 140 } 141 }