directory walking working

This commit is contained in:
Andy Pack 2023-06-09 00:02:11 +01:00
parent e6a5581cea
commit 66fd83ac02
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
11 changed files with 154 additions and 97 deletions

View File

@ -17,7 +17,7 @@ project(kc
LANGUAGES LANGUAGES
CXX) CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

View File

@ -1,4 +1,9 @@
add_executable(kc main.cpp) add_executable(kc
main.cpp
fs/fs.cpp
logging.cpp
config.cpp
)
FetchContent_Declare( FetchContent_Declare(
Boost Boost

View File

@ -1,68 +1,43 @@
#include "config.hpp"
#include <memory> #include <memory>
#include <fstream> #include <fstream>
#include <iostream>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
using namespace std; std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
namespace po = boost::program_options;
void on_age(int age)
{ {
BOOST_LOG_TRIVIAL(info) << "On age: " << age; try {
} po::options_description desc("Config");
shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
{
int def;
po::options_description desc("Allowed options");
desc.add_options() desc.add_options()
("help", "produce help message") ("help", "produce help message")
("compression,c", po::value<int>(), "set compression level") ("path,p", po::value<std::string>()->default_value("."), "set root path of knowledge base")
("def,d", po::value<int>(&def)->default_value(10), "set def level") ("config", po::value<std::string>()->default_value("kc.ini"), "config file location")
("abc", po::value<int>()->default_value(25), "set abc level")
("phone", po::value<std::vector<std::string>>()->multitoken()->zero_tokens()->composing(), "Phone")
("age,a", po::value<int>()->notifier(on_age), "age")
("config", po::value<std::string>(), "Config file")
; ;
po::options_description other("Other");
desc.add_options()
("other-option,o", po::value<int>(), "another option")
;
// Hidden options, will be allowed both on command line and
// in config file, but will not be shown to the user.
po::options_description hidden("Hidden options");
hidden.add_options()
("input-file", po::value< vector<string> >(), "input file");
po::positional_options_description p;
p.add("phone", -1);
po::options_description cmdline_options; po::options_description cmdline_options;
cmdline_options.add(desc).add(other).add(hidden); cmdline_options.add(desc);
po::options_description config_file_options; po::options_description config_file_options;
config_file_options.add(desc).add(hidden); config_file_options.add(desc);
po::options_description visible("Allowed options");
visible.add(desc).add(other);
//////////// ////////////
// PARSE // PARSE
//////////// ////////////
auto vm = make_shared<po::variables_map>(); auto vm = std::make_shared<po::variables_map>();
po::store(po::command_line_parser(argc, argv) po::store(po::command_line_parser(argc, argv)
.options(desc) .options(cmdline_options)
.positional(p)
.run(), .run(),
*vm); *vm);
if (vm->count("config")) if (vm->count("config"))
{ {
BOOST_LOG_TRIVIAL(info) << "Attempting file config load"; auto config_path = (*vm)["config"].as<std::string>();
std::ifstream ifs{(*vm)["config"].as<std::string>().c_str()}; BOOST_LOG_TRIVIAL(info) << "Attempting file config load for " << config_path;
std::ifstream ifs{config_path.c_str()};
if (ifs) { if (ifs) {
BOOST_LOG_TRIVIAL(info) << "File opened, loading..."; BOOST_LOG_TRIVIAL(info) << "File opened, loading...";
po::store(po::parse_config_file(ifs, config_file_options), *vm); po::store(po::parse_config_file(ifs, config_file_options), *vm);
@ -71,22 +46,16 @@ shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
po::notify(*vm); po::notify(*vm);
if (vm->count("help")) { if (vm->count("help")) {
BOOST_LOG_TRIVIAL(info) << desc; std::cout << desc;
return nullptr; return nullptr;
} }
if (vm->count("compression")) {
BOOST_LOG_TRIVIAL(info) << "Compression level was set to " << (*vm)["compression"].as<int>() << ".";
} else {
BOOST_LOG_TRIVIAL(info) << "Compression level was not set.";
}
if (vm->count("def")) {
BOOST_LOG_TRIVIAL(info) << "def level was set to " << (*vm)["def"].as<int>() << ".";
} else {
BOOST_LOG_TRIVIAL(info) << "def level was not set.";
}
return vm; return vm;
}
catch (const po::error &ex)
{
BOOST_LOG_TRIVIAL(error) << ex.what();
}
} }

8
src/config.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
namespace po = boost::program_options;
std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[]);

1
src/const.hpp Normal file
View File

@ -0,0 +1 @@
#pragma once

18
src/fs/FileEntry.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
namespace kc {
class FileEntry {
public:
fs::directory_entry file_entry;
fs::path relative_path;
private:
};
}

39
src/fs/fs.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "fs.hpp"
#include <fstream>
#include <iostream>
static const std::string exclusions[] = {".git", ".obsidian"};
std::vector<kc::FileEntry> kc::walk_dir(std::string dir)
{
auto matched = std::vector<kc::FileEntry>();
auto base_path = fs::path(dir);
for (auto const& dir_entry : fs::recursive_directory_iterator(base_path))
{
auto excluded = false;
auto dir_entry_path = dir_entry.path();
auto dir_entry_path_string = dir_entry_path.string();
for (auto const& exclusion: exclusions)
{
if (dir_entry_path_string.contains(exclusion))
{
excluded = true;
}
}
if (!excluded)
{
auto entry = kc::FileEntry();
entry.file_entry = dir_entry;
entry.relative_path = fs::relative(dir_entry_path, base_path);
matched.push_back(entry);
}
}
return matched;
}

14
src/fs/fs.hpp Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <string>
#include <filesystem>
#include "FileEntry.hpp"
namespace fs = std::filesystem;
namespace kc {
std::vector<kc::FileEntry> walk_dir(std::string dir);
}

View File

@ -1,3 +1,5 @@
#include "logging.hpp"
#include <boost/log/core.hpp> #include <boost/log/core.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp> #include <boost/log/expressions.hpp>

3
src/logging.hpp Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void init_logging();

View File

@ -4,13 +4,13 @@
#include <fstream> #include <fstream>
#include <memory> #include <memory>
#include "logging.cpp" #include "const.hpp"
#include "config.cpp" #include "logging.hpp"
#include "config.hpp"
using namespace std; #include "fs/fs.hpp"
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
try{
init_logging(); init_logging();
BOOST_LOG_TRIVIAL(info) << "================================"; BOOST_LOG_TRIVIAL(info) << "================================";
@ -22,11 +22,9 @@ int main(int argc, const char *argv[]) {
if(config) if(config)
{ {
BOOST_LOG_TRIVIAL(info) << "hello world"; auto env_path = (*config)["path"].as<std::string>();
} BOOST_LOG_TRIVIAL(info) << "Loading knowledge base from " << env_path;
}
catch (const po::error &ex) kc::walk_dir(env_path);
{
BOOST_LOG_TRIVIAL(error) << ex.what();
} }
} }