Clean project structure.

This commit is contained in:
jmsgrogan 2023-01-17 10:13:25 +00:00
parent 78a4fa99ff
commit 947bf937fd
496 changed files with 206 additions and 137 deletions

View file

@ -0,0 +1,197 @@
#include "ByteUtils.h"
bool ByteUtils::MostSignificantBitIsOne(char c)
{
return c & (1 << 7);
}
ByteUtils::Word ByteUtils::GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
};
ByteUtils::Word ByteUtils::GetWordLastByte(const Word word)
{
return word & ByteUtils::WORD_LAST_BYTE;
}
unsigned char ByteUtils::getHigherNBits(unsigned char input, unsigned num)
{
return input >> (8 - num);
}
unsigned char ByteUtils::getByteN(uint32_t input, unsigned n)
{
return (input << 8*n) >> 24;
}
uint32_t ByteUtils::mirror(uint32_t byte, unsigned length)
{
uint32_t ret{0};
for(unsigned idx=0; idx<length; idx++)
{
if (getBitN(byte, length - 1 - idx))
{
ret |= (0x01 << idx);
}
}
return ret;
}
unsigned char ByteUtils::getLowerNBits(uint32_t input, unsigned num)
{
switch (num)
{
case 1:
return input & 0x01;
case 2:
return input & 0x03;
case 3:
return input & 0x07;
case 4:
return input & 0x0F;
case 5:
return input & 0x1F;
case 6:
return input & 0x3F;
case 7:
return input & 0x7F;
case 8:
return static_cast<unsigned char>(input);
default:
return 0;
}
}
unsigned char ByteUtils::getTwoBitsAtN(unsigned char input, unsigned n)
{
return (input & (0x03 << n)) >> n;
}
unsigned char ByteUtils::getMBitsAtN(unsigned char input, unsigned m, unsigned n)
{
switch (m)
{
case 1:
return (input & (0x01 << n)) >> n;
case 2:
return (input & (0x03 << n)) >> n;
case 3:
return (input & (0x07 << n)) >> n;
case 4:
return (input & (0x0F << n)) >> n;
case 5:
return (input & (0x1F << n)) >> n;
case 6:
return (input & (0x3F << n)) >> n;
case 7:
return (input & (0x7F << n)) >> n;
case 8:
return input;
default:
return 0;
}
}
bool ByteUtils::getBitN(uint32_t input, unsigned n)
{
return input & (1 << n);
}
unsigned char ByteUtils::getFromString(const std::string& string)
{
unsigned char ret{0};
if (string.length() < 8)
{
return ret;
}
for(unsigned idx=0; idx<8; idx++)
{
if (string[idx] == '1')
{
ret |= (0x01 << (7 - idx));
}
}
return ret;
}
std::string ByteUtils::toString(uint32_t input, unsigned length)
{
std::string ret;
if (length > 8)
{
unsigned overshoot = length - 8;
for(unsigned idx=0; idx<overshoot; idx++)
{
ret += getBitN(input, length - 1 - idx) ? '1' : '0';
}
ret += "-";
for(unsigned idx=0; idx<8; idx++)
{
ret += getBitN(input, 7 - idx) ? '1' : '0';
}
}
else
{
for(unsigned idx=0; idx<length; idx++)
{
ret += getBitN(input, 7 - idx) ? '1' : '0';
}
}
return ret;
}
void ByteUtils::ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize)
{
for(unsigned idx=0; idx<targetSize; idx++)
{
if (idx < size)
{
reverse[idx] = buffer[size - 1 -idx];
}
else
{
reverse[idx] = 0;
}
}
}
ByteUtils::Word ByteUtils::ToWord(char* buffer, bool reverse)
{
return ToType<Word>(buffer, reverse);
}
ByteUtils::DWord ByteUtils::ToDWord(char* buffer, bool reverse)
{
return ToType<DWord>(buffer, reverse);
}
ByteUtils::QWord ByteUtils::ToQWord(char* buffer, bool reverse)
{
return ToType<QWord>(buffer, reverse);
}
bool ByteUtils::Compare(char* buffer, const char* tag, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
if(tag[idx] != buffer[idx])
{
return false;
}
}
return true;
}
bool ByteUtils::CompareDWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, sizeof(DWord));
}
bool ByteUtils::CompareWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, sizeof(Word));
}

View file

@ -0,0 +1,72 @@
#pragma once
#include <cstring>
#include <stdint.h>
#include <string>
class ByteUtils
{
public:
using Word = int16_t;
using DWord = int32_t;
using QWord = int64_t;
static bool MostSignificantBitIsOne(char c);
static Word GetWordFirstBit(const Word word);
static Word GetWordLastByte(const Word word);
static unsigned char getByteN(uint32_t input, unsigned n);
static unsigned char getHigherNBits(unsigned char input, unsigned num);
static unsigned char getLowerNBits(uint32_t input, unsigned num);
static unsigned char getTwoBitsAtN(unsigned char input, unsigned n);
static unsigned char getMBitsAtN(unsigned char input, unsigned m, unsigned n);
static bool getBitN(uint32_t input, unsigned n);
static unsigned char getFromString(const std::string& string);
static std::string toString(uint32_t input, unsigned length = 8);
static uint32_t mirror(uint32_t input, unsigned length=0);
static void ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize);
template<typename T>
static T ToType(char* buffer, bool reverse = true)
{
T result {0};
if(reverse)
{
char reversed[sizeof(T)];
ReverseBuffer(buffer, reversed, sizeof(T), sizeof(T));
std::memcpy(&result, reversed, sizeof(T));
}
else
{
std::memcpy(&result, buffer, sizeof(T));
}
return result;
}
static Word ToWord(char* buffer, bool reverse = true);
static DWord ToDWord(char* buffer, bool reverse = true);
static QWord ToQWord(char* buffer, bool reverse = true);
static bool Compare(char* buffer, const char* tag, unsigned size);
static bool CompareDWords(char* buffer, const char* tag);
static bool CompareWords(char* buffer, const char* tag);
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const Word WORD_FIRST_BIT = static_cast<Word>(0x8000); // 1000 0000 - 0000 0000
static const Word WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
};

View file

@ -0,0 +1,209 @@
#include "StringUtils.h"
#include <locale>
#include <algorithm>
#include <sstream>
#include <iomanip>
bool StringUtils::isAlphaNumeric(char c)
{
return std::isalnum(c);
}
bool StringUtils::isSpace(char c)
{
return std::isspace(c);
}
bool StringUtils::isAlphabetical(char c)
{
return std::isalpha(c);
}
std::vector<unsigned char> StringUtils::toBytes(const std::string& input)
{
return {input.begin(), input.end()};
}
std::string StringUtils::toString(const std::vector<unsigned char>& bytes)
{
return {bytes.begin(), bytes.end()};
}
std::vector<std::string> StringUtils::toLines(const std::string& input)
{
auto result = std::vector<std::string>{};
auto ss = std::stringstream{input};
for (std::string line; std::getline(ss, line, '\n');)
{
result.push_back(line);
}
return result;
}
bool StringUtils::isWhitespaceOnly(const std::string& input)
{
if (input.empty())
{
return true;
}
else
{
return std::all_of(input.cbegin(), input.cend(), [](char c){ return std::isspace(c); });
}
}
std::size_t StringUtils::countFirstConsecutiveHits(const std::string& input, char c)
{
auto found_id = input.find(c);
if(found_id == std::string::npos)
{
return 0;
}
else
{
std::size_t count = 1;
for(std::size_t idx=found_id+1; idx<input.size(); idx++)
{
if(input[idx] == c)
{
count++;
}
else
{
return count;
}
}
return count;
}
}
std::string StringUtils::stripSurroundingWhitepsace(const std::string& input)
{
if (input.empty())
{
return {};
}
std::size_t first_nonspace = 0;
std::size_t last_nonspace = input.size() - 1;
for (std::size_t idx = 0; idx < input.size(); idx++)
{
if (!std::isspace(input[idx]))
{
first_nonspace = idx;
break;
}
}
if (first_nonspace == last_nonspace)
{
return {};
}
for (std::size_t idx = last_nonspace; idx > 0; idx--)
{
if (!std::isspace(input[idx]))
{
last_nonspace = idx;
break;
}
}
return input.substr(first_nonspace, last_nonspace-first_nonspace + 1);
}
std::vector<std::string> StringUtils::split(const std::string& input)
{
std::vector<std::string> substrings;
std::string working_string;
std::locale loc;
bool last_was_nonspace{ false };
for (auto c : input)
{
if (std::isspace(c, loc))
{
if (last_was_nonspace)
{
substrings.push_back(working_string);
working_string = "";
last_was_nonspace = false;
}
}
else
{
last_was_nonspace = true;
working_string += c;
}
}
if (!working_string.empty())
{
substrings.push_back(working_string);
}
return substrings;
}
std::string StringUtils::toLower(const std::string& s)
{
std::string ret;
std::transform(s.begin(), s.end(), ret.begin(), [](unsigned char c){ return std::tolower(c); });
return ret;
}
std::string StringUtils::toPaddedString(unsigned numBytes, unsigned entry)
{
std::stringstream sstr;
sstr << std::setfill('0') << std::setw(numBytes) << entry;
return sstr.str();
}
std::string StringUtils::stripQuotes(const std::string& input)
{
if (input.size() < 3)
{
return input;
}
std::size_t start_index = 0;
std::size_t end_index = input.size()-1;
if (input[start_index] == '"')
{
start_index = 1;
}
if (input[end_index] == '"')
{
end_index = end_index - 1;
}
return input.substr(start_index, end_index - start_index + 1);
}
std::string StringUtils::removeUpTo(const std::string& input, const std::string& prefix)
{
std::size_t found = input.find(prefix);
if (found != std::string::npos)
{
return input.substr(found + prefix.size(), input.size()-found);
}
else
{
return input;
}
}
bool StringUtils::startsWith(const std::string& input, const std::string& prefix, bool ignoreWhitespace)
{
if(ignoreWhitespace)
{
const auto loc = input.find(prefix);
if (loc == std::string::npos)
{
return false;
}
else
{
return isWhitespaceOnly(input.substr(0, loc));
}
}
else
{
return input.find(prefix) == 0;
}
}

View file

@ -0,0 +1,48 @@
#pragma once
#include <string>
#include <vector>
class StringUtils
{
public:
static constexpr char LEFT_BRACKET = '<';
static constexpr char RIGHT_BRACKET = '>';
static constexpr char FORWARD_SLASH = '/';
static constexpr char BACK_SLASH = '\\';
static constexpr char QUESTION_MARK = '?';
static constexpr char EQUALS = '=';
static constexpr char DOUBLE_QUOTE = '"';
static constexpr char SINGLE_QUOTE = '\'';
static constexpr char COLON = ':';
static std::size_t countFirstConsecutiveHits(const std::string& input, char c);
static bool isAlphaNumeric(char c);
static bool isAlphabetical(char c);
static bool isSpace(char c);
static bool isWhitespaceOnly(const std::string& input);
static std::string removeUpTo(const std::string& input, const std::string& prefix);
static std::vector<std::string> split(const std::string& input);
static bool startsWith(const std::string& input, const std::string& prefix, bool ignoreWhitespace = false);
static std::string stripSurroundingWhitepsace(const std::string& input);
static std::string stripQuotes(const std::string& input);
static std::vector<unsigned char> toBytes(const std::string& input);
static std::string toLower(const std::string& s);
static std::vector<std::string> toLines(const std::string& input);
static std::string toPaddedString(unsigned numBytes, unsigned entry);
static std::string toString(const std::vector<unsigned char>& bytes);
};

View file

@ -0,0 +1,41 @@
#include "UnicodeUtils.h"
#include "Win32BaseIncludes.h"
#include <vector>
#include <stdexcept>
std::string UnicodeUtils::utf16ToUtf8String(const std::wstring& input)
{
if (input.empty())
{
return std::string();
}
#ifdef _WIN32
const auto size = ::WideCharToMultiByte(CP_UTF8, 0, &input[0], static_cast<int>(input.size()), nullptr, 0, nullptr, nullptr);
std::string result(size, 0);
::WideCharToMultiByte(CP_UTF8, 0, &input[0], static_cast<int>(input.size()), &result[0], size, nullptr, nullptr);
return result;
#else
throw std::logic_error("Not implemented");
#endif
}
std::wstring UnicodeUtils::utf8ToUtf16WString(const std::string& input)
{
if (input.empty())
{
return std::wstring();
}
#ifdef _WIN32
const auto charsNeeded = ::MultiByteToWideChar(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
std::vector<wchar_t> buffer(charsNeeded);
const auto charsConverted = ::MultiByteToWideChar(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), &buffer[0], static_cast<int>(buffer.size()));
return std::wstring(&buffer[0], charsConverted);
#else
throw std::logic_error("Not implemented");
#endif
}

View file

@ -0,0 +1,11 @@
#pragma once
#include <string>
class UnicodeUtils
{
public:
static std::string utf16ToUtf8String(const std::wstring& input);
static std::wstring utf8ToUtf16WString(const std::string& input);
};