Implemented to-block methods of dataformatter

This commit is contained in:
Leonetienne 2022-05-26 23:22:04 +02:00
parent 6ac775105b
commit f6d646da55
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
3 changed files with 81 additions and 2 deletions

View File

@ -12,6 +12,7 @@ using namespace Leonetienne::GCrypt;
class ModuleDataFormatter { class ModuleDataFormatter {
public: public:
static std::string FormatBlock(const Block& block, const Configuration::IOBASE_FORMAT base); 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: private:

View File

@ -2,6 +2,7 @@
#include "Bases.h" #include "Bases.h"
#include <GeneralUtility/BaseConversion.h> #include <GeneralUtility/BaseConversion.h>
#include <StringTools/StringTools.h> #include <StringTools/StringTools.h>
#include <GCrypt/Util.h>
using namespace Leonetienne::GCrypt; using namespace Leonetienne::GCrypt;
using namespace Leonetienne::StringTools; using namespace Leonetienne::StringTools;
@ -55,10 +56,85 @@ std::string ModuleDataFormatter::FormatBlock(
); );
default: 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( std::string ModuleDataFormatter::Bin2CustomBase(
const std::string& bin, const std::string& bin,
const std::vector<std::string>& customSet, const std::vector<std::string>& customSet,
@ -94,6 +170,9 @@ std::string ModuleDataFormatter::CustomBase2Bin(
std::string binary = std::string binary =
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, std::string>(in_symbols, customSet, std::string("01")); 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 // Check that our string is of size BLOCK_SIZE
if (binary.length() != Block::BLOCK_SIZE_BITS) { 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?"); throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin received input, which does note translate to a bitstring of size BLOCK_SIZE. Huh?");

View File

@ -22,7 +22,6 @@ int main(int argc, char* const* argv) {
) )
<< std::endl; << std::endl;
return 0; return 0;
} }