Implement unified istream for ingestion layer

This commit is contained in:
Leonetienne 2022-05-27 18:26:48 +02:00
parent 5df625e610
commit 87987f6fe2
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
3 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#ifndef GCRYPTCLI_DATAINGESTIONLAYER_H
#define GCRYPTCLI_DATAINGESTIONLAYER_H
#include <istream>
#include <fstream>
#include <sstream>
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

View File

@ -0,0 +1,72 @@
#include "DataIngestionLayer.h"
#include "CommandlineInterface.h"
#include "Configuration.h"
#include <iostream>
#include <cstring>
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;

View File

@ -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: