Added new functionality to Serial class

This commit is contained in:
OmniaX-Dev 2026-06-08 06:24:43 +02:00
parent 068adbe282
commit 08daf0cea1
5 changed files with 53 additions and 35 deletions

View file

@ -1 +1 @@
2091 2095

View file

@ -1,21 +1,21 @@
/* /*
OmniaFramework - A collection of useful functionality OmniaFramework - A collection of useful functionality
Copyright (C) 2026 OmniaX-Dev Copyright (C) 2026 OmniaX-Dev
This file is part of OmniaFramework. This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful, OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>. along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "Memory.hpp" #include "Memory.hpp"
@ -23,7 +23,7 @@
namespace ostd namespace ostd
{ {
void Memory::printByteStream(const ByteStream& data, StreamIndex start, u8 line_len, u16 n_rows, OutputHandlerBase& out, i32 addrHighlight, u32 highlightRange, const String& title) void Memory::printByteStream(std::span<const i8> data, StreamIndex start, u8 line_len, u16 n_rows, OutputHandlerBase& out, i32 addrHighlight, u32 highlightRange, const String& title)
{ {
StreamIndex end = start + (n_rows * line_len); StreamIndex end = start + (n_rows * line_len);
if (end > data.size()) end = data.size(); if (end > data.size()) end = data.size();
@ -89,13 +89,13 @@ namespace ostd
out.nl().fg(ConsoleColors::BrightBlue).p(String::duplicateChar('=', linew)).nl().reset(); out.nl().fg(ConsoleColors::BrightBlue).p(String::duplicateChar('=', linew)).nl().reset();
} }
bool Memory::saveByteStreamToFile(const ByteStream& stream, const String& filePath) bool Memory::saveByteStreamToFile(std::span<const i8> stream, const String& filePath)
{ {
std::ofstream writeFile; std::ofstream writeFile;
writeFile.open(filePath.cpp_str(), std::ios::out | std::ios::binary); writeFile.open(filePath.cpp_str(), std::ios::out | std::ios::binary);
writeFile.write((char*)(&stream[0]), stream.size()); writeFile.write((char*)(stream.data()), stream.size());
writeFile.close(); writeFile.close();
return true; return writeFile.good();
} }
bool Memory::loadByteStreamFromFile(const String& filePath, ByteStream& outStream) bool Memory::loadByteStreamFromFile(const String& filePath, ByteStream& outStream)
@ -117,7 +117,7 @@ namespace ostd
return bstream; return bstream;
} }
String Memory::byteStreamToString(const ByteStream& data) String Memory::byteStreamToString(std::span<const i8> data)
{ {
String out_string = ""; String out_string = "";
for (i64 i = 0; i < data.size(); i++) for (i64 i = 0; i < data.size(); i++)

View file

@ -1,21 +1,21 @@
/* /*
OmniaFramework - A collection of useful functionality OmniaFramework - A collection of useful functionality
Copyright (C) 2026 OmniaX-Dev Copyright (C) 2026 OmniaX-Dev
This file is part of OmniaFramework. This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful, OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>. along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
@ -32,11 +32,11 @@ namespace ostd
class Memory class Memory
{ {
public: public:
static void printByteStream(const ByteStream& data, StreamIndex start, u8 line_len, u16 n_rows, OutputHandlerBase& out, i32 addrHighlight = -1, u32 highlightRange = 1, const String& title = ""); static void printByteStream(std::span<const i8> data, StreamIndex start, u8 line_len, u16 n_rows, OutputHandlerBase& out, i32 addrHighlight = -1, u32 highlightRange = 1, const String& title = "");
static bool saveByteStreamToFile(const ByteStream& stream, const String& filePath); static bool saveByteStreamToFile(std::span<const i8> stream, const String& filePath);
static bool loadByteStreamFromFile(const String& filePath, ByteStream& outStream); static bool loadByteStreamFromFile(const String& filePath, ByteStream& outStream);
static ByteStream stringToByteStream(const String& data); static ByteStream stringToByteStream(const String& data);
static String byteStreamToString(const ByteStream& data); static String byteStreamToString(std::span<const i8> data);
//Array helpers //Array helpers
template<typename T> template<typename T>

View file

@ -1,4 +1,5 @@
#include "Serial.hpp" #include "Serial.hpp"
#include "Memory.hpp"
namespace ostd namespace ostd
{ {
@ -41,6 +42,14 @@ namespace ostd
Serial::Serial(StreamView copyData, serial::Endianness endianness) : read(*this), write(*this)
{
if (construct(copyData.size(), endianness, false) && isValid())
{
std::ranges::copy(copyData, m_data.data().begin());
}
}
bool Serial::construct(u64 size, serial::Endianness endianness, bool preInitialize, i8 initialValue) bool Serial::construct(u64 size, serial::Endianness endianness, bool preInitialize, i8 initialValue)
{ {
if (m_data.isValid()) return false; if (m_data.isValid()) return false;
@ -63,6 +72,12 @@ namespace ostd
return (addr + size - 1) < m_data.size(); return (addr + size - 1) < m_data.size();
} }
bool Serial::writeToFile(const String& filePath) const
{
if (!isValid()) return false;
return Memory::saveByteStreamToFile(m_data.data(), filePath);
}

View file

@ -21,6 +21,7 @@
#pragma once #pragma once
#include <ostd/string/String.hpp> #include <ostd/string/String.hpp>
// #include <ostd/io/Memory.hpp>
#include <span> #include <span>
namespace ostd namespace ostd
@ -307,8 +308,10 @@ namespace ostd
public: public:
inline Serial(void) : read(*this), write(*this) { } inline Serial(void) : read(*this), write(*this) { }
inline Serial(u64 size, serial::Endianness endianness = serial::Endianness::LittleEndian, bool preInitialize = false, i8 initialValue = 0) : read(*this), write(*this) { construct(size, endianness, preInitialize, initialValue); } inline Serial(u64 size, serial::Endianness endianness = serial::Endianness::LittleEndian, bool preInitialize = false, i8 initialValue = 0) : read(*this), write(*this) { construct(size, endianness, preInitialize, initialValue); }
Serial(StreamView copyData, serial::Endianness endianness = serial::Endianness::LittleEndian);
bool construct(u64 size, serial::Endianness endianness = serial::Endianness::LittleEndian, bool preInitialize = false, i8 initialValue = 0); bool construct(u64 size, serial::Endianness endianness = serial::Endianness::LittleEndian, bool preInitialize = false, i8 initialValue = 0);
bool isValidAddress(u64 addr, u32 size = 1) const; bool isValidAddress(u64 addr, u32 size = 1) const;
bool writeToFile(const String& filePath) const;
inline bool isValid(void) const { return m_data.isValid(); } inline bool isValid(void) const { return m_data.isValid(); }
inline bool isInvalid(void) const { return !isValid(); } inline bool isInvalid(void) const { return !isValid(); }