Implemented to-block methods of dataformatter
This commit is contained in:
parent
6ac775105b
commit
f6d646da55
@ -12,6 +12,7 @@ using namespace Leonetienne::GCrypt;
|
||||
class ModuleDataFormatter {
|
||||
public:
|
||||
static std::string FormatBlock(const Block& block, const Configuration::IOBASE_FORMAT base);
|
||||
static Block StringToBlock(const std::string& str, const Configuration::IOBASE_FORMAT base);
|
||||
|
||||
|
||||
private:
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "Bases.h"
|
||||
#include <GeneralUtility/BaseConversion.h>
|
||||
#include <StringTools/StringTools.h>
|
||||
#include <GCrypt/Util.h>
|
||||
|
||||
using namespace Leonetienne::GCrypt;
|
||||
using namespace Leonetienne::StringTools;
|
||||
@ -55,10 +56,85 @@ std::string ModuleDataFormatter::FormatBlock(
|
||||
);
|
||||
|
||||
default:
|
||||
throw std::invalid_argument("Iobase now found! Oh no. Anyway.");
|
||||
throw std::invalid_argument("FormatBlock(): Iobase now found! Oh no. Anyway.");
|
||||
}
|
||||
}
|
||||
|
||||
Block ModuleDataFormatter::StringToBlock(
|
||||
const std::string& str,
|
||||
const Configuration::IOBASE_FORMAT base
|
||||
) {
|
||||
|
||||
Block b;
|
||||
|
||||
switch (base) {
|
||||
case Configuration::IOBASE_FORMAT::BASE_BYTES:
|
||||
b.FromByteString(str);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_2:
|
||||
b.FromBinaryString(str);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_8:
|
||||
b.FromBinaryString(
|
||||
CustomBase2Bin(
|
||||
str,
|
||||
BASE_8
|
||||
)
|
||||
);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_10:
|
||||
b.FromBinaryString(
|
||||
CustomBase2Bin(
|
||||
str,
|
||||
BASE_10
|
||||
)
|
||||
);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_16:
|
||||
b.FromHexString(str);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_64:
|
||||
b.FromBinaryString(
|
||||
CustomBase2Bin(
|
||||
str,
|
||||
BASE_64
|
||||
)
|
||||
);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_UWU:
|
||||
b.FromBinaryString(
|
||||
CustomBase2Bin(
|
||||
str,
|
||||
BASE_UWU,
|
||||
" "
|
||||
)
|
||||
);
|
||||
break;
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_UGH:
|
||||
b.FromBinaryString(
|
||||
CustomBase2Bin(
|
||||
str,
|
||||
BASE_UGH,
|
||||
" "
|
||||
)
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw std::invalid_argument("StringToBlock(): Iobase now found! Oh no. Anyway.");
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
std::string ModuleDataFormatter::Bin2CustomBase(
|
||||
const std::string& bin,
|
||||
const std::vector<std::string>& customSet,
|
||||
@ -94,6 +170,9 @@ std::string ModuleDataFormatter::CustomBase2Bin(
|
||||
std::string binary =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, std::string>(in_symbols, customSet, std::string("01"));
|
||||
|
||||
// Pad to BLOCK_SIZE
|
||||
binary = PadStringToLength(binary, Block::BLOCK_SIZE_BITS, '0', false);
|
||||
|
||||
// Check that our string is of size BLOCK_SIZE
|
||||
if (binary.length() != Block::BLOCK_SIZE_BITS) {
|
||||
throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin received input, which does note translate to a bitstring of size BLOCK_SIZE. Huh?");
|
||||
|
@ -22,7 +22,6 @@ int main(int argc, char* const* argv) {
|
||||
)
|
||||
<< std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user