Whitespace and pointer cleanup.

This commit is contained in:
jmsgrogan 2021-03-29 21:31:24 +01:00
parent 6fc0b8dca8
commit a03eb9599f
32 changed files with 441 additions and 468 deletions

View file

@ -4,78 +4,78 @@
class ByteUtils
{
public:
using Word = int;
using DWord = int;
using Word = int;
using DWord = int;
static int GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
};
static int GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
};
static int GetWordLastByte(const Word word)
{
return word & ByteUtils::WORD_LAST_BYTE;
}
static int GetWordLastByte(const Word word)
{
return word & ByteUtils::WORD_LAST_BYTE;
}
static void ReverseBuffer(char* buffer, char* reverse, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
reverse[idx] = buffer[size - 1 -idx];
}
}
static void ReverseBuffer(char* buffer, char* reverse, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
reverse[idx] = buffer[size - 1 -idx];
}
}
static int ToInt(char* buffer, const unsigned size, bool reverse = true)
{
int result;
if(reverse)
{
std::string reversed;
ReverseBuffer(buffer, reversed.data(), size);
std::memcpy(&result, reversed.data(), sizeof(int));
}
else
{
std::memcpy(&result, buffer, sizeof(int));
}
return result;
}
static int ToInt(char* buffer, const unsigned size, bool reverse = true)
{
int result;
if(reverse)
{
std::string reversed;
ReverseBuffer(buffer, reversed.data(), size);
std::memcpy(&result, reversed.data(), sizeof(int));
}
else
{
std::memcpy(&result, buffer, sizeof(int));
}
return result;
}
static Word ToWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_WORD, reverse);
}
static Word ToWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_WORD, reverse);
}
static DWord ToDWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_DWORD, reverse);
}
static DWord ToDWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_DWORD, reverse);
}
static bool Compare(char* buffer, const char* tag, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
if(tag[idx] != buffer[idx])
{
return false;
}
}
return true;
}
static bool Compare(char* buffer, const char* tag, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
if(tag[idx] != buffer[idx])
{
return false;
}
}
return true;
}
static bool CompareDWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_DWORD);
}
static bool CompareDWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_DWORD);
}
static bool CompareWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_WORD);
}
static bool CompareWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_WORD);
}
static const int BYTES_PER_WORD = 2;
static const int BYTES_PER_DWORD = 4;
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const int WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
static const int WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
static const int BYTES_PER_WORD = 2;
static const int BYTES_PER_DWORD = 4;
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const int WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
static const int WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
};

View file

@ -4,14 +4,14 @@
bool StringUtils::IsAlphaNumeric(char c)
{
std::locale loc;
return std::isalnum(c, loc);
std::locale loc;
return std::isalnum(c, loc);
}
bool StringUtils::IsSpace(char c)
{
std::locale loc;
return std::isspace(c, loc);
std::locale loc;
return std::isspace(c, loc);
}
std::string StringUtils::ToLower(const std::string& s)

View file

@ -5,16 +5,16 @@
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 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 bool IsAlphaNumeric(char c);
static bool IsSpace(char c);
static std::string ToLower(const std::string& s);
static bool IsAlphaNumeric(char c);
static bool IsSpace(char c);
static std::string ToLower(const std::string& s);
};

View file

@ -1,10 +1,10 @@
#include "HttpResponse.h"
HttpResponse::HttpResponse()
:mHttpVersion("1.1"),
mResponseCode("200 OK"),
mContentType("text/plain"),
mBody()
:mHttpVersion("1.1"),
mResponseCode("200 OK"),
mContentType("text/plain"),
mBody()
{
}
@ -16,23 +16,23 @@ HttpResponse::~HttpResponse()
void HttpResponse::SetBody(const std::string& body)
{
mBody = body;
mBody = body;
}
unsigned HttpResponse::GetBodyLength()
unsigned HttpResponse::GetBodyLength() const
{
return unsigned(mBody.length());
return unsigned(mBody.length());
}
std::string HttpResponse::GetHeaderString()
std::string HttpResponse::GetHeaderString() const
{
std::string header = "HTTP/" + mHttpVersion + " " + mResponseCode + "\n";
header += "Content-Type: " + mContentType + "\n";
header += "Content-Length: " + std::to_string(GetBodyLength()) + "\n";
return header;
std::string header = "HTTP/" + mHttpVersion + " " + mResponseCode + "\n";
header += "Content-Type: " + mContentType + "\n";
header += "Content-Length: " + std::to_string(GetBodyLength()) + "\n";
return header;
}
std::string HttpResponse::ToString()
std::string HttpResponse::ToString() const
{
return GetHeaderString() + "\n\n" + mBody;
return GetHeaderString() + "\n\n" + mBody;
}

View file

@ -5,22 +5,22 @@
class HttpResponse
{
std::string mHttpVersion;
std::string mResponseCode;
std::string mContentType;
std::string mBody;
std::string mHttpVersion;
std::string mResponseCode;
std::string mContentType;
std::string mBody;
public:
HttpResponse();
HttpResponse();
~HttpResponse();
~HttpResponse();
void SetBody(const std::string& body);
void SetBody(const std::string& body);
unsigned GetBodyLength();
unsigned GetBodyLength() const;
std::string GetHeaderString();
std::string GetHeaderString() const;
std::string ToString();
std::string ToString() const;
};