Added new functionality to Serial class
This commit is contained in:
parent
068adbe282
commit
08daf0cea1
5 changed files with 53 additions and 35 deletions
|
|
@ -1 +1 @@
|
||||||
2091
|
2095
|
||||||
|
|
|
||||||
|
|
@ -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++)
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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(); }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue