Skip to content

Commit

Permalink
Merge pull request #27 from rhasler1/main
Browse files Browse the repository at this point in the history
Add example: Cipher
  • Loading branch information
gkthiruvathukal authored Jul 16, 2024
2 parents 93df0fa + 3856c0d commit b005b1e
Show file tree
Hide file tree
Showing 25 changed files with 85,386 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ add_subdirectory(montecarlo)
add_subdirectory(wordcloud)
add_subdirectory(technical_analysis)
add_subdirectory(bigmatrix)
add_subdirectory(cipher)
add_subdirectory(transitive_closure)
13 changes: 13 additions & 0 deletions cipher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# UnoAPI:CMakeLists-targetlibraries:begin

add_executable(cipher main.cpp scramble.cpp file_ops.cpp timestamps.cpp)
target_link_libraries(cipher spdlog::spdlog CLI11::CLI11)

enable_testing()
add_executable(cipher_tests test.cpp scramble.cpp file_ops.cpp)
target_link_libraries(cipher_tests gtest_main fmt::fmt spdlog::spdlog CLI11::CLI11)
include(GoogleTest)
gtest_discover_tests(cipher_tests)

# UnoAPI: CMakeLists-targetlibraries:end
18 changes: 18 additions & 0 deletions cipher/file_ops.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "file_ops.h"

// If the file cannot be opened, then the function will notify the caller
// by returning 1.
int read_from_binary(std::vector<std::byte> & buffer, std::string path_to_file)
{
std::ifstream input(path_to_file, std::ios::binary);
if (!input.is_open()) {
spdlog::error("Error opening file to read from: {}", path_to_file);
return 1;
}
std::uintmax_t size = std::filesystem::file_size(path_to_file);
buffer.resize(size);
// TODO: See if it is possible to read binary data from file without casting.
input.read(reinterpret_cast<char*>(buffer.data()), size);
input.close();
return 0;
}
31 changes: 31 additions & 0 deletions cipher/file_ops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef FILEOPS_H
#define FILEOPS_H

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#include <fstream>
#include <filesystem>

int read_from_binary(
std::vector<std::byte> & buffer,
std::string path_to_file
);

// If the file cannot be opened, then the function will notify the caller
// by returning 1.
template <class Writable>
int write_to_binary(Writable & buffer, std::string path_to_file)
{
std::ofstream output(path_to_file, std::ios::binary);
if (!output.is_open()) {
spdlog::error("Error opening file to read from: {}", path_to_file);
return 1;
}
// TODO: See if it is possible to write binary data to file without casting.
output.write(reinterpret_cast<char*>(&buffer[0]), buffer.size());
output.close();
return 0;
}

#endif
Empty file added cipher/live-data/ciphertext.txt
Empty file.
Empty file added cipher/live-data/decrypted.txt
Empty file.
Loading

0 comments on commit b005b1e

Please sign in to comment.