diff --git a/GCryptCLI/include/DataIngestionLayer.h b/GCryptCLI/include/DataIngestionLayer.h index e69de29..3fd24e6 100644 --- a/GCryptCLI/include/DataIngestionLayer.h +++ b/GCryptCLI/include/DataIngestionLayer.h @@ -0,0 +1,28 @@ +#ifndef GCRYPTCLI_DATAINGESTIONLAYER_H +#define GCRYPTCLI_DATAINGESTIONLAYER_H + +#include +#include +#include + +namespace IO { + + // This class is used to read in data. + class DataIngestionLayer { + public: + //! Will initialize the ingestion + static void Init(); + + private: + static std::istream* in; + + // We have to hold on to a reference to a filestream, + // even if we're always just reading from in. + // We still have to CLOSE the file handle afterwards! + static std::ifstream ifs; + static std::istringstream iss; + }; +} + +#endif + diff --git a/GCryptCLI/src/DataIngestionLayer.cpp b/GCryptCLI/src/DataIngestionLayer.cpp index e69de29..27f6b54 100644 --- a/GCryptCLI/src/DataIngestionLayer.cpp +++ b/GCryptCLI/src/DataIngestionLayer.cpp @@ -0,0 +1,72 @@ +#include "DataIngestionLayer.h" +#include "CommandlineInterface.h" +#include "Configuration.h" +#include +#include + +using namespace IO; + +void DataIngestionLayer::Init() { + + // Set our istream + switch (Configuration::inputFrom) { + + // Are we reading from stdin? + case Configuration::INPUT_FROM::STDIN: + + // Redirect our istream to stdin + in = &std::cin; + break; + + // Are we reading from a file? + case Configuration::INPUT_FROM::FILE: + + // Open the file + ifs.open( + Configuration::inputFilename, + std::ios::in | std::ios::binary + ); + + // A little bit of error handling + if (!ifs.good()) { + throw std::runtime_error( "Unable to open infilestream!"); + } + + // Redirect our istream to this infilestream + in = &ifs; + break; + + // Are we reading from a parameter? + case Configuration::INPUT_FROM::PARAMETER: + + // Create an instringstream with our parameter + iss = std::istringstream( + CommandlineInterface::Get()["--intext"].GetString() + ); + + // Redirect our istream t this instringstream + in = &iss; + + break; + } + + // Temporary + std::size_t n_last_bytes_read = 0; + char buf[64]; + do { + memset(buf, 0, sizeof(buf)); + in->read(buf, sizeof(buf)); + n_last_bytes_read = in->gcount(); + std::cout << buf; + + } while(n_last_bytes_read == 64); + + + return; +} + + +std::istream* DataIngestionLayer::in; +std::ifstream DataIngestionLayer::ifs; +std::istringstream DataIngestionLayer::iss; + diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index a3dd30d..672566c 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -3,6 +3,8 @@ #include "KeyManager.h" #include "ModuleGenerateKey.h" +#include "DataIngestionLayer.h" + int main(int argc, char* const* argv) { // Init cli args @@ -14,6 +16,8 @@ int main(int argc, char* const* argv) { // Prepare the key KeyManager::PrepareKey(); + IO::DataIngestionLayer::Init(); + // Launch our module switch (Configuration::activeModule) { case Configuration::MODULE::GENERATE_KEY: