basic md5 test implementation
This commit is contained in:
parent
41d91e078e
commit
64100e34c1
@ -1,4 +1,7 @@
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <queue>
|
||||
@ -6,18 +9,18 @@
|
||||
namespace fs = std::filesystem;
|
||||
using std::vector, std::queue;
|
||||
|
||||
int validate(const fs::path path) {
|
||||
bool valid(const fs::path path) {
|
||||
if(!fs::exists(path))
|
||||
return 1;
|
||||
return false;
|
||||
|
||||
if (false)
|
||||
return 1;
|
||||
if (!fs::is_directory(path))
|
||||
return false;
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
vector<fs::path> find_all_files(const fs::path path) {
|
||||
if (validate(path) == 1)
|
||||
if (valid(path))
|
||||
return vector<fs::path>(0);
|
||||
|
||||
vector<fs::path> files;
|
||||
@ -25,11 +28,16 @@ vector<fs::path> find_all_files(const fs::path path) {
|
||||
folders_to_traverse.push(path);
|
||||
|
||||
while (!folders_to_traverse.empty()) {
|
||||
for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front()))
|
||||
try {
|
||||
for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front())) {
|
||||
if (!entry.is_directory())
|
||||
files.emplace_back(entry);
|
||||
else
|
||||
folders_to_traverse.push(entry);
|
||||
}
|
||||
} catch (std::exception &system_error) {
|
||||
system_error.what();
|
||||
}
|
||||
|
||||
folders_to_traverse.pop();
|
||||
}
|
||||
@ -39,7 +47,7 @@ vector<fs::path> find_all_files(const fs::path path) {
|
||||
}
|
||||
|
||||
vector<fs::path> find_all_folders(const fs::path path) {
|
||||
if (validate(path) == 1)
|
||||
if (valid(path))
|
||||
return vector<fs::path>(0);
|
||||
|
||||
vector<fs::path> folders;
|
||||
@ -61,7 +69,7 @@ vector<fs::path> find_all_folders(const fs::path path) {
|
||||
}
|
||||
|
||||
vector<fs::path> find_all_files_and_folders(const fs::path path) {
|
||||
if (validate(path) == 1)
|
||||
if (valid(path))
|
||||
return vector<fs::path>(0);
|
||||
|
||||
vector<fs::path> files_and_folders;
|
||||
|
@ -4,3 +4,4 @@
|
||||
std::vector<std::filesystem::path> find_all_files(const std::filesystem::path path);
|
||||
std::vector<std::filesystem::path> find_all_folders(const std::filesystem::path path);
|
||||
std::vector<std::filesystem::path> find_all_files_and_folders(const std::filesystem::path path);
|
||||
bool valid(std::filesystem::path path);
|
22
src/main.cpp
22
src/main.cpp
@ -2,9 +2,22 @@
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include "fs_reader.hpp"
|
||||
#include "openssl/md5.h"
|
||||
namespace fs = std::filesystem;
|
||||
using std::cout, std::vector;
|
||||
|
||||
void checksum_md5(const fs::path path) {
|
||||
unsigned char digest[16];
|
||||
MD5_CTX ctx;
|
||||
MD5_Init(&ctx);
|
||||
MD5_Update(&ctx, reinterpret_cast<unsigned char*>(const_cast<char*>(path.c_str())), path.string().size());
|
||||
MD5_Final(digest, &ctx);
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
printf("%02x", digest[i]);
|
||||
putchar ('\n');
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc == 1) {
|
||||
cout << "chksum: No path specified\n";
|
||||
@ -14,14 +27,17 @@ int main(int argc, char *argv[]) {
|
||||
cout << "chksum: Too many arguments\n";
|
||||
return 1;
|
||||
}
|
||||
if (!fs::exists(argv[1])) {
|
||||
if (!valid(argv[1])) {
|
||||
cout << "chksum: Not a valid path\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
vector<fs::path> files_and_folders = find_all_files(argv[1]);
|
||||
checksum_md5("hello world");
|
||||
return 0;
|
||||
|
||||
for (const fs::path &path : files_and_folders)
|
||||
vector<fs::path> files = find_all_files(argv[1]);
|
||||
|
||||
for (const fs::path &path : files)
|
||||
cout << path << "\n";
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user