Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement recursive depth specifier #16

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions include/qCheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

#include <cstddef>
#include <filesystem>
#include <optional>
#include <vector>

#include <getopt.h>

struct Settings
{
std::vector<std::filesystem::path> InputFiles;
std::size_t Threads = 2;
bool Verbose = true;
bool Check = false;
bool Recursive = false;
std::size_t Threads = 2;
std::intmax_t RecursiveDepth = 0;
bool Verbose = true;
bool Check = false;
};

extern const char* Usage;

const static struct option CommandOptions[] = {
{"threads", required_argument, nullptr, 't'},
{"check", no_argument, nullptr, 'c'},
{"recursive", no_argument, nullptr, 'r'},
{"recursive", optional_argument, nullptr, 'r'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, '\0'},
};
Expand Down
41 changes: 30 additions & 11 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace
{

void ProcessInputPath(Settings& CurSettings, const std::filesystem::path& Path)
void ProcessInputPath(
Settings& CurSettings, const std::filesystem::path& Path,
std::intmax_t RecursiveDepth = 0)
{
if( !std::filesystem::exists(Path) )
{
Expand All @@ -35,19 +37,16 @@ void ProcessInputPath(Settings& CurSettings, const std::filesystem::path& Path)
}
// Add .sfv files to check recursively
else if(
CurSettings.Recursive
(RecursiveDepth != 0)
&& std::filesystem::is_directory(Path, CurError) )
{
for( const std::filesystem::directory_entry& DirectoryEntry :
std::filesystem::directory_iterator(Path) )
{
ProcessInputPath(CurSettings, DirectoryEntry.path());
ProcessInputPath(
CurSettings, DirectoryEntry.path(), RecursiveDepth - 1);
}
}
else
{
std::fprintf(stderr, "Error opening path: %s\n", Path.c_str());
}
}
else
{
Expand Down Expand Up @@ -79,7 +78,7 @@ int main(int argc, char* argv[])
}
// Parse Arguments
while(
(Opt = getopt_long(argc, argv, "t:crh", CommandOptions, &OptionIndex))
(Opt = getopt_long(argc, argv, "t:cr::h", CommandOptions, &OptionIndex))
!= -1 )
{
switch( Opt )
Expand All @@ -104,7 +103,27 @@ int main(int argc, char* argv[])
}
case 'r':
{
CurSettings.Recursive = true;
// Endless recursion when no optional argument specified
CurSettings.RecursiveDepth = -1;

// Try to parse optional argument
if( optarg != nullptr )
{
std::size_t RecursiveDepth;
const auto ParseResult = std::from_chars<std::size_t>(
optarg, optarg + std::strlen(optarg), RecursiveDepth);
if( *ParseResult.ptr != '\0' || ParseResult.ec != std::errc() )
{
std::fprintf(
stdout, "Invalid recursive depth count \"%s\"\n",
optarg);
return EXIT_FAILURE;
}

CurSettings.RecursiveDepth
= static_cast<std::intmax_t>(RecursiveDepth);
}

break;
}
case 'h':
Expand All @@ -122,7 +141,7 @@ int main(int argc, char* argv[])

// Recursive generation of sfv files is not implemented at the moment
// - Tue Feb 4 07:53:09 PM PST 2025
if( (!CurSettings.Check) && (CurSettings.Recursive) )
if( (!CurSettings.Check) && (CurSettings.RecursiveDepth != 0) )
{
std::puts("Recursive generation not implemented.");
return EXIT_FAILURE;
Expand All @@ -131,7 +150,7 @@ int main(int argc, char* argv[])
for( std::intmax_t i = 0; i < argc; ++i )
{
const std::filesystem::path CurPath(argv[i]);
ProcessInputPath(CurSettings, CurPath);
ProcessInputPath(CurSettings, CurPath, CurSettings.RecursiveDepth);
}

return CurSettings.Check ? CheckSFVs(CurSettings)
Expand Down
10 changes: 6 additions & 4 deletions source/qCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <charconv>
#include <fstream>
#include <optional>
#include <set>
#include <span>
#include <thread>
Expand All @@ -16,10 +17,11 @@
const char* Usage
= "qCheck - Wunkolo <[email protected]>\n"
"Usage: qCheck [Options]... [Files]...\n"
" -t, --threads Number of checker threads in parallel\n"
" -c, --check Verify all input as .sfv files\n"
" -r, --recursive Recursively traverse subdirectories\n"
" -h, --help Show this help message\n";
" -t , --threads Number of checker threads in parallel\n"
" -c , --check Verify all input as .sfv files\n"
" -r#, --recursive=# Recursively traverse subdirectories\n"
" Optionally limited to the specified depth\n"
" -h , --help Show this help message\n";

static std::optional<std::uint32_t>
ChecksumFile(const std::filesystem::path& Path)
Expand Down