fs_reader mostly works

slight changes to datatypes and return values
is able to print out all files and folders in specified directory
This commit is contained in:
AustrianToast 2024-02-14 16:07:48 +01:00
parent 60aac542cd
commit 76d12f91b1
No known key found for this signature in database
GPG Key ID: 8086574D3AAF2453
3 changed files with 41 additions and 15 deletions

View File

@ -10,24 +10,41 @@ namespace fs = std::filesystem;
// Rinse and Repeat
// Avoid recursion if possible
std::vector<std::string> find_all_files(std::string filesystem_path) {
for (const auto & entry : fs::directory_iterator(filesystem_path)) {
std::cout << entry.path() << "\n";
std::vector<fs::path> find_all_files(std::string filesystem_path) {
std::vector<fs::path> files = {"test"};
for (const fs::directory_entry & entry : fs::directory_iterator(filesystem_path)) {
if (entry.is_directory()) {
continue;
}
return std::vector<std::string>(0) = {};
files.insert(files.begin()+files.size(), entry.path());
}
files.shrink_to_fit();
return files;
}
std::vector<std::string> find_all_folders(std::string filesystem_path) {
return std::vector<std::string>(0) = {};
std::vector<fs::path> find_all_folders(std::string filesystem_path) {
std::vector<fs::path> folders = {};
for (const fs::directory_entry & entry : fs::directory_iterator(filesystem_path)) {
if (entry.is_directory()) {
folders.insert(folders.begin()+folders.size(), entry.path());
}
}
folders.shrink_to_fit();
return folders;
}
std::vector<std::string> find_all_files_and_folders(std::string filesystem_path) {
std::vector<std::string> files = find_all_files(filesystem_path);
std::vector<std::string> folders = find_all_folders(filesystem_path);
std::vector<std::string> files_and_folders = {};
std::vector<fs::path> find_all_files_and_folders(std::string filesystem_path) {
std::vector<fs::path> files = find_all_files(filesystem_path);
std::vector<fs::path> folders = find_all_folders(filesystem_path);
std::vector<fs::path> files_and_folders = {};
files_and_folders.reserve(files.size() + folders.size());
files_and_folders.insert( files_and_folders.end(), files.begin(), files.end() );
files_and_folders.insert( files_and_folders.end(), folders.begin(), folders.end() );
return std::vector<std::string>(0) = {};
return files_and_folders;
}

View File

@ -1,6 +1,8 @@
#include <string>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
std::vector<std::string> find_all_files(std::string filesystem_path);
std::vector<std::string> find_all_folders(std::string filesystem_path);
std::vector<std::string> find_all_files_and_folders(std::string filesystem_path);
std::vector<fs::path> find_all_files(std::string filesystem_path);
std::vector<fs::path> find_all_folders(std::string filesystem_path);
std::vector<fs::path> find_all_files_and_folders(std::string filesystem_path);

View File

@ -1,7 +1,14 @@
#include <iostream>
#include <string>
#include "fs_reader.h"
int main(int argc, char *argv[]) {
(void)find_all_files_and_folders(argv[1]);
std::vector<fs::path> files_and_folders = find_all_files_and_folders(argv[1]);
for (size_t index = 0; index < files_and_folders.size(); index++) {
std::cout << "index " + std::to_string(index) + ": " + files_and_folders.at(index).string() << std::endl;
}
files_and_folders.clear();
return 0;
}