Allocator improvements, kernel C++ platform, streaming
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* stream.cpp
|
||||
* String stream/string writer
|
||||
* Copyright (c) 2025 Daniel Hammer
|
||||
*/
|
||||
|
||||
#include "Stream.hpp"
|
||||
#include <Memory/MemoryAllocator.hpp>
|
||||
#include <Terminal/terminal.hpp>
|
||||
#include <Libraries/string.hpp>
|
||||
|
||||
kcp::cstringstream::cstringstream()
|
||||
{
|
||||
this->string = nullptr;
|
||||
this->size = 0;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
|
||||
this->string = (const char *)Memory::g_allocator->Realloc((void *)this->string, this->size + 1);
|
||||
|
||||
if (!this->string)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
char* ref = (char *)&string[size];
|
||||
*ref = c;
|
||||
|
||||
ref++;
|
||||
*ref = '\0';
|
||||
|
||||
this->size++;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(char* str) {
|
||||
while (*str != '\0')
|
||||
{
|
||||
*this << *str;
|
||||
str++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(int num) {
|
||||
char* out_str = Lib::int2basestr(num, current_base);
|
||||
|
||||
*this << out_str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(uint32_t val) {
|
||||
char* out_str = Lib::uint2basestr(val, current_base);
|
||||
|
||||
*this << out_str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(uint64_t val) {
|
||||
char* out_str = Lib::u64_2_basestr(val, current_base);
|
||||
|
||||
*this << out_str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
kcp::cstringstream& kcp::cstringstream::operator<<(base nb)
|
||||
{
|
||||
current_base = nb;
|
||||
}
|
||||
|
||||
const char* kcp::cstringstream::str() {
|
||||
return this->string;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* stream.hpp
|
||||
* Streaming
|
||||
* Copyright (c) 2025 Daniel Hammer
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Kernel C++ Platform
|
||||
namespace kcp {
|
||||
enum base
|
||||
{
|
||||
oct = 8,
|
||||
dec = 10,
|
||||
hex = 16
|
||||
};
|
||||
|
||||
class cstringstream {
|
||||
const char* string;
|
||||
std::size_t size;
|
||||
|
||||
base current_base = base::dec;
|
||||
|
||||
public:
|
||||
cstringstream();
|
||||
|
||||
cstringstream& operator<<(char c);
|
||||
cstringstream& operator<<(char* str);
|
||||
|
||||
cstringstream& operator<<(int num);
|
||||
cstringstream& operator<<(uint32_t val);
|
||||
cstringstream& operator<<(uint64_t val);
|
||||
|
||||
cstringstream& operator<<(base nb);
|
||||
|
||||
const char* str();
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user