loading file content, skipping directories

This commit is contained in:
Andy Pack 2023-06-09 00:21:53 +01:00
parent 66fd83ac02
commit 3d63ff29a7
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
6 changed files with 44 additions and 1 deletions

View File

@ -1,6 +1,7 @@
add_executable(kc add_executable(kc
main.cpp main.cpp
fs/fs.cpp fs/fs.cpp
fs/FileEntry.cpp
logging.cpp logging.cpp
config.cpp config.cpp
) )

32
src/fs/FileEntry.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "FileEntry.hpp"
#include <fstream>
#include <iostream>
namespace kc {
bool FileEntry::content_loaded()
{
return !file_content.empty();
}
std::string FileEntry::load_content()
{
std::ifstream ifs(file_entry.path());
file_content.assign( (std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()) );
return file_content;
}
std::string FileEntry::get_content()
{
return file_content;
}
void FileEntry::clear_content()
{
file_content.clear();
file_content.shrink_to_fit();
}
}

View File

@ -12,7 +12,14 @@ class FileEntry {
fs::directory_entry file_entry; fs::directory_entry file_entry;
fs::path relative_path; fs::path relative_path;
bool content_loaded();
std::string load_content();
std::string get_content();
void clear_content();
private: private:
std::string file_content;
}; };
} }

View File

@ -12,6 +12,8 @@ std::vector<kc::FileEntry> kc::walk_dir(std::string dir)
for (auto const& dir_entry : fs::recursive_directory_iterator(base_path)) for (auto const& dir_entry : fs::recursive_directory_iterator(base_path))
{ {
if (dir_entry.is_directory()) continue;
auto excluded = false; auto excluded = false;
auto dir_entry_path = dir_entry.path(); auto dir_entry_path = dir_entry.path();
auto dir_entry_path_string = dir_entry_path.string(); auto dir_entry_path_string = dir_entry_path.string();

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <filesystem> #include <filesystem>
#include <vector>
#include "FileEntry.hpp" #include "FileEntry.hpp"

View File

@ -25,6 +25,6 @@ int main(int argc, const char *argv[]) {
auto env_path = (*config)["path"].as<std::string>(); auto env_path = (*config)["path"].as<std::string>();
BOOST_LOG_TRIVIAL(info) << "Loading knowledge base from " << env_path; BOOST_LOG_TRIVIAL(info) << "Loading knowledge base from " << env_path;
kc::walk_dir(env_path); auto entries = kc::walk_dir(env_path);
} }
} }