Implement unified istream for ingestion layer
This commit is contained in:
parent
5df625e610
commit
87987f6fe2
@ -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
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -3,6 +3,8 @@
|
|||||||
#include "KeyManager.h"
|
#include "KeyManager.h"
|
||||||
#include "ModuleGenerateKey.h"
|
#include "ModuleGenerateKey.h"
|
||||||
|
|
||||||
|
#include "DataIngestionLayer.h"
|
||||||
|
|
||||||
int main(int argc, char* const* argv) {
|
int main(int argc, char* const* argv) {
|
||||||
|
|
||||||
// Init cli args
|
// Init cli args
|
||||||
@ -14,6 +16,8 @@ int main(int argc, char* const* argv) {
|
|||||||
// Prepare the key
|
// Prepare the key
|
||||||
KeyManager::PrepareKey();
|
KeyManager::PrepareKey();
|
||||||
|
|
||||||
|
IO::DataIngestionLayer::Init();
|
||||||
|
|
||||||
// Launch our module
|
// Launch our module
|
||||||
switch (Configuration::activeModule) {
|
switch (Configuration::activeModule) {
|
||||||
case Configuration::MODULE::GENERATE_KEY:
|
case Configuration::MODULE::GENERATE_KEY:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user