maint: cleanup string utility

This commit is contained in:
6arelyFuture 2023-06-07 11:26:24 +02:00
parent f3bc4e29c4
commit a292e5dc71
Signed by: Future
GPG Key ID: FA77F074E98D98A5
4 changed files with 24 additions and 30 deletions

View File

@ -1,5 +1,4 @@
![license](https://img.shields.io/github/license/diamante0018/master-tool.svg) ![license](https://img.shields.io/github/license/diamante0018/master-tool.svg)
[![build](https://github.com/diamante0018/master-tool/workflows/Build/badge.svg)](https://github.com/diamante0018/master-tool/actions) [![build](https://github.com/diamante0018/master-tool/workflows/Build/badge.svg)](https://github.com/diamante0018/master-tool/actions)
[![patreon](https://img.shields.io/badge/patreon-support-blue.svg?logo=patreon)](https://www.patreon.com/xlabsproject)
# X-Labs: Master Server Tool # AlterWare: Master Server Tool

View File

@ -14,7 +14,7 @@ namespace
{ {
utils::cryptography::ecc::key key; utils::cryptography::ecc::key key;
const char* master_address = "master.xlabs.dev"; const char* master_address = "master.alterware.dev";
std::uint16_t master_port = 20810; std::uint16_t master_port = 20810;
const char* to_kill = nullptr; const char* to_kill = nullptr;
const char* to_remove = nullptr; const char* to_remove = nullptr;
@ -78,7 +78,7 @@ namespace
void usage(const char* prog) void usage(const char* prog)
{ {
console::error("X Labs Master Tool\n" console::error("AlterWare Master Tool\n"
"Usage: %s OPTIONS\n" "Usage: %s OPTIONS\n"
" -master IP - set the target, default: '%s'\n" " -master IP - set the target, default: '%s'\n"
" -port PORT - set the port, default: '%hu'\n" " -port PORT - set the port, default: '%hu'\n"
@ -92,8 +92,8 @@ namespace
int main(const int argc, const char** argv) int main(const int argc, const char** argv)
{ {
console::set_title("X Labs Master Tool"); console::set_title("AlterWare Master Tool");
console::log("Starting X Labs Master Tool"); console::log("Starting AlterWare Master Tool");
// Parse command-line arguments // Parse command-line arguments
for (auto i = 1; i < argc; i++) for (auto i = 1; i < argc; i++)

View File

@ -49,9 +49,6 @@
#include <gsl/gsl> #include <gsl/gsl>
#ifdef _WIN32 #ifdef _WIN32
#pragma comment(lib, "ntdll.lib")
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "urlmon.lib" )
#pragma comment(lib, "iphlpapi.lib")
#endif #endif

View File

@ -1,20 +1,18 @@
#pragma once #pragma once
#include "memory.hpp" #include "memory.hpp"
#ifndef ARRAYSIZE
template <class Type, std::size_t n> template <class Type, std::size_t n>
std::size_t ARRAYSIZE(Type (&)[n]) { return n; } constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; }
#endif
namespace utils namespace utils
{ {
namespace string namespace string
{ {
template <std::size_t Buffers, std::size_t MinBufferSize> template <std::size_t buffers, std::size_t min_buffer_size>
class va_provider final class va_provider final
{ {
public: public:
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0"); static_assert(buffers != 0 && min_buffer_size != 0, "buffers and min_buffer_size mustn't be 0");
va_provider() : current_buffer_(0) va_provider() : current_buffer_(0)
{ {
@ -22,10 +20,10 @@ namespace utils
char* get(const char* format, va_list ap) char* get(const char* format, va_list ap)
{ {
++this->current_buffer_ %= ARRAYSIZE(this->string_pool_); ++this->current_buffer_ %= ARRAY_COUNT(this->string_pool_);
auto entry = &this->string_pool_[this->current_buffer_]; auto entry = &this->string_pool_[this->current_buffer_];
if (!entry->size || !entry->buffer) if (!entry->size_ || !entry->buffer_)
{ {
throw std::runtime_error("String pool not initialized"); throw std::runtime_error("String pool not initialized");
} }
@ -33,9 +31,9 @@ namespace utils
while (true) while (true)
{ {
#ifdef _WIN32 #ifdef _WIN32
const int res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); const int res = vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap);
#else #else
const int res = vsnprintf(entry->buffer, entry->size, format, ap); const int res = vsnprintf(entry->buffer_, entry->size_, format, ap);
#endif #endif
if (res > 0) break; // Success if (res > 0) break; // Success
@ -44,44 +42,44 @@ namespace utils
entry->double_size(); entry->double_size();
} }
return entry->buffer; return entry->buffer_;
} }
private: private:
class entry final class entry final
{ {
public: public:
explicit entry(const std::size_t _size = MinBufferSize) : size(_size), buffer(nullptr) explicit entry(const std::size_t size = min_buffer_size) : size_(size), buffer_(nullptr)
{ {
if (this->size < MinBufferSize) this->size = MinBufferSize; if (this->size_ < min_buffer_size) this->size_ = min_buffer_size;
this->allocate(); this->allocate();
} }
~entry() ~entry()
{ {
if (this->buffer) memory::get_allocator()->free(this->buffer); if (this->buffer_) memory::get_allocator()->free(this->buffer_);
this->size = 0; this->size_ = 0;
this->buffer = nullptr; this->buffer_ = nullptr;
} }
void allocate() void allocate()
{ {
if (this->buffer) memory::get_allocator()->free(this->buffer); if (this->buffer_) memory::get_allocator()->free(this->buffer_);
this->buffer = memory::get_allocator()->allocate_array<char>(this->size + 1); this->buffer_ = memory::get_allocator()->allocate_array<char>(this->size_ + 1);
} }
void double_size() void double_size()
{ {
this->size *= 2; this->size_ *= 2;
this->allocate(); this->allocate();
} }
std::size_t size; std::size_t size_;
char* buffer; char* buffer_;
}; };
std::size_t current_buffer_; std::size_t current_buffer_;
entry string_pool_[Buffers]; entry string_pool_[buffers];
}; };
const char* va(const char* fmt, ...); const char* va(const char* fmt, ...);