Skip to content

Commit

Permalink
Added a new option to override size filters with a regex include filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen committed Nov 26, 2021
1 parent 2c01f19 commit 88c6fc9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.10)
project(GodotPckTool)

set(GODOT_PCK_TOOL_VERSION_MAJOR 1)
set(GODOT_PCK_TOOL_VERSION_MINOR 5)
set(GODOT_PCK_TOOL_VERSION_MINOR 6)

set(GODOT_PCK_TOOL_VERSION_STR "${GODOT_PCK_TOOL_VERSION_MAJOR}.${GODOT_PCK_TOOL_VERSION_MINOR}")

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ For example to find files containing "po" but no "zh":
godotpcktool -i '\.po' -e 'zh'
```

#### Overriding filters

If you need more complex filtering you can specify regular expressions with
`--include-override-filter` which makes any file matching any of those
regular expression be included in the operation, even if another filter
would cause the file to be excluded. For example you can use this to set
file size limits and then override those for specific type:
```sh
godotpcktool --min-size-filter 1000 --include-override-filter '\.txt'
```

### General info

In the long form multiple files may be included like this:
Expand Down
28 changes: 18 additions & 10 deletions src/FileFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ using namespace pcktool;
// ------------------------------------ //
bool FileFilter::Include(const PckFile::ContainedFile& file) const
{
if(file.Size < MinSizeLimit)
return false;

if(file.Size > MaxSizeLimit)
return false;
if(!OverridePatterns.empty()) {
for(const auto& pattern : OverridePatterns) {
if(std::regex_search(file.Path, pattern)) {
return true;
}
}
}

if(!IncludePatterns.empty()){
if(!IncludePatterns.empty()) {
bool matched = false;

for(const auto& pattern : IncludePatterns){
if(std::regex_search(file.Path, pattern)){
for(const auto& pattern : IncludePatterns) {
if(std::regex_search(file.Path, pattern)) {
matched = true;
break;
}
Expand All @@ -25,8 +27,14 @@ bool FileFilter::Include(const PckFile::ContainedFile& file) const
return false;
}

if(!ExcludePatterns.empty()){
for(const auto& pattern : ExcludePatterns){
if(file.Size < MinSizeLimit)
return false;

if(file.Size > MaxSizeLimit)
return false;

if(!ExcludePatterns.empty()) {
for(const auto& pattern : ExcludePatterns) {
if(std::regex_search(file.Path, pattern))
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions src/FileFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class FileFilter {
ExcludePatterns = filters;
}

void SetIncludeOverrideRegexes(const std::vector<std::regex>& filters)
{
OverridePatterns = filters;
}

private:
//! File is excluded if it is under this size
uint64_t MinSizeLimit = 0;
Expand All @@ -51,6 +56,10 @@ class FileFilter {
//! any file being checked) must not regex_search find a match in any regexes in this
//! vector, if they do, they are excluded
std::vector<std::regex> ExcludePatterns;

//! If non-empty then any files that pass this filter are included anyway, even if they
//! would fail another inclusion check
std::vector<std::regex> OverridePatterns;
};

} // namespace pcktool
9 changes: 9 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ int main(int argc, char* argv[])
"if both include and exclude are specified includes are handled first and "
"excludes exclude files that include filters passed through",
cxxopts::value<std::vector<std::string>>())
("include-override-filter", "Set regexes for files to include in operation even if "
"another filter might exclude it, doesn't affect inclusion of files not "
"matching this",
cxxopts::value<std::vector<std::string>>())
("q,quieter", "Don't output all processed files to keep output more compact")
("v,version", "Print version and quit")
("h,help", "Print help and quit")
Expand Down Expand Up @@ -157,6 +161,11 @@ int main(int argc, char* argv[])
ParseRegexList(result["exclude-regex-filter"].as<std::vector<std::string>>()));
}

if(result.count("include-override-filter")) {
filter.SetIncludeOverrideRegexes(
ParseRegexList(result["include-override-filter"].as<std::vector<std::string>>()));
}

if(result.count("quieter")) {
reducedVerbosity = true;
}
Expand Down

0 comments on commit 88c6fc9

Please sign in to comment.