cppcheck cleanups and additional docs+testing.

This commit is contained in:
2023-10-21 23:33:22 -07:00
parent 9faed6a95b
commit 9a8dc08a4f
8 changed files with 191 additions and 76 deletions

View File

@@ -186,27 +186,25 @@ main(int argc, char *argv[])
auto args = flags->Args();
args.erase(args.begin());
auto result = commander.Run(command, args);
delete flags;
switch (result) {
case Subcommand::Status::OK:
std::cout << "[+] OK\n";
retc = 0;
break;
case Subcommand::Status::NotEnoughArgs:
delete flags;
usage(cerr, 1);
break;
case Subcommand::Status::Failed:
cerr << "[!] '"<< command << "' failed\n";
break;
case Subcommand::Status::CommandNotRegistered:
delete flags;
cerr << "[!] '" << command << "' not registered.\n";
usage(cerr, 1);
break;
default:
delete flags;
abort();
}

View File

@@ -20,24 +20,19 @@
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <cstdio>
#include <cstdlib>
#include <cstring>
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PROT_RW (PROT_WRITE|PROT_READ)
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ios>
#include <scsl/Arena.h>
#define PROT_RW (PROT_WRITE|PROT_READ)
namespace scsl {
@@ -74,9 +69,6 @@ Arena::SetAlloc(size_t allocSize)
this->arenaType = ArenaType::Alloc;
this->size = allocSize;
this->store = new uint8_t[allocSize];
if (this->store == nullptr) {
return -1;
}
this->Clear();
return 0;
@@ -221,16 +213,15 @@ Arena::Destroy()
this->arenaType = ArenaType::Uninit;
this->size = 0;
this->store = nullptr;
return;
}
std::ostream &
operator<<(std::ostream &os, Arena &arena)
{
auto cursor = arena.Start();
auto *cursor = arena.Start();
char cursorString[33] = {0};
snprintf(cursorString, 32, "%#016llx",
(long long unsigned int) cursor);
(long long unsigned int)(cursor));
os << "Arena<";
switch (arena.Type()) {
@@ -250,7 +241,7 @@ operator<<(std::ostream &os, Arena &arena)
os << "unknown (this is a bug)";
}
os << ">@0x";
os << std::hex << (uintptr_t) &arena;
os << std::hex << static_cast<void *>(&arena);
os << std::dec;
os << ",store<" << arena.Size() << "B>@";
os << std::hex << cursorString;

View File

@@ -83,13 +83,46 @@ Buffer::Buffer(const char *data)
}
Buffer::Buffer(const std::string s)
Buffer::Buffer(const std::string& s)
: contents(nullptr), length(0), capacity(0), autoTrim(true)
{
this->Append(s);
}
Buffer::~Buffer()
{
this->Reclaim();
}
uint8_t *
Buffer::Contents() const
{
return this->contents;
}
std::string
Buffer::ToString() const
{
return std::string((const char *)(this->contents));
}
size_t
Buffer::Length() const
{
return this->length;
}
size_t
Buffer::Capacity() const
{
return this->capacity;
}
bool
Buffer::Append(const char *s)
{
@@ -100,7 +133,7 @@ Buffer::Append(const char *s)
bool
Buffer::Append(const std::string s)
Buffer::Append(const std::string &s)
{
return this->Append((const uint8_t *) s.c_str(), s.size());
}
@@ -156,7 +189,7 @@ Buffer::Insert(const size_t index, const char *s)
bool
Buffer::Insert(const size_t index, const std::string s)
Buffer::Insert(const size_t index, const std::string &s)
{
return this->Insert(index, (const uint8_t *) s.c_str(), s.size());
}
@@ -240,6 +273,28 @@ Buffer::Trim()
return 0;
}
void
Buffer::DisableAutoTrim()
{
this->autoTrim = false;
}
void
Buffer::EnableAutoTrim()
{
this->autoTrim = true;
}
bool
Buffer::AutoTrimIsEnabled()
{
return this->autoTrim;
}
void
Buffer::Clear()
{
@@ -328,7 +383,11 @@ Buffer::shiftRight(size_t offset, size_t delta)
resized = true;
}
if (this->length == 0) return 0;
if (this->length < offset) {
for (size_t i = this->length; i < offset; i++) {
this->contents[i] = ' ';
}
}
memmove(this->contents + (offset + delta), this->contents + offset,

View File

@@ -46,7 +46,6 @@ Subcommand::Run(std::vector<std::string> args)
}
Commander::Commander()
: cmap()
{
this->cmap.clear();
}
@@ -72,7 +71,7 @@ Commander::Run(std::string command, std::vector<std::string> args)
}
auto scmd = this->cmap[command];
return scmd->Run(args);
return scmd->Run(std::move(args));
}

View File

@@ -208,11 +208,11 @@ Flags::parseArg(int argc, char **argv, int &index)
std::string arg(argv[index]);
string::TrimWhitespace(arg);
index++;
if (!std::regex_search(arg, isFlag)) {
return ParseStatus::EndOfFlags;
}
index++;
if (this->flags.count(arg) == 0) {
if (arg == "-h" || arg == "--help") {
Usage(std::cout, 0);