added Utils::clearConsole(...) method,

better handling in serial::SerialIO class,
added FileSystem.cpp extension to ostd::Utils
This commit is contained in:
OmniaX-dev 2023-11-19 19:27:36 +01:00
parent cfcb262541
commit bdb6ed78a9
9 changed files with 263 additions and 275 deletions

View file

@ -79,9 +79,11 @@ list(APPEND OGFX_SOURCE_FILES
list(APPEND OSTD_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/src/ostd/BaseObject.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Color.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Console.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/DataFile.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Errors.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/File.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/FileSystem.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Geometry.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Logger.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Logic.cpp
@ -132,6 +134,7 @@ add_compile_options(-O3 -m32 -MMD -MP -Wall -ggdb -fsanitize=address)
if (UNIX)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
target_link_libraries(${OMNIA_GFX_LIB} PUBLIC X11 GL)
target_link_libraries(${OMNIA_STD_LIB} PUBLIC tinfo)
endif (UNIX)
target_link_libraries(${OMNIA_GFX_LIB} PUBLIC sfml-system sfml-window sfml-graphics)

51
src/ostd/Console.cpp Normal file
View file

@ -0,0 +1,51 @@
#include <Utils.hpp>
namespace ostd
{
#if defined _WIN32
#include <windows.h>
void Utils::clearConsole(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOut;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE)
return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi))
return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(hStdOut, (TCHAR) ' ', cellCount, homeCoords, &count))
return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount, homeCoords, &count))
return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
#include <unistd.h>
#include <term.h>
void Utils::clearConsole(void)
{
if (!cur_term)
{
int result;
setupterm(NULL, STDOUT_FILENO, &result);
if (result <= 0) return;
}
putp(tigetstr( "clear" ));
}
#endif
}

65
src/ostd/FileSystem.cpp Normal file
View file

@ -0,0 +1,65 @@
#include "Utils.hpp"
namespace ostd
{
std::vector<std::filesystem::path> Utils::listFilesInDirectory(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
{
if (!std::filesystem::is_directory(file.path()))
list.push_back(file.path());
}
return list;
}
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectory(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
{
if (std::filesystem::is_directory(file.path()))
list.push_back(file.path());
}
return list;
}
std::vector<std::filesystem::path> Utils::listDirectory(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
list.push_back(file.path());
return list;
}
std::vector<std::filesystem::path> Utils::listFilesInDirectoryRecursive(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
{
if (!std::filesystem::is_directory(file.path()))
list.push_back(file.path());
}
return list;
}
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectoryRecursive(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
{
if (std::filesystem::is_directory(file.path()))
list.push_back(file.path());
}
return list;
}
std::vector<std::filesystem::path> Utils::listDirectoryRecursive(const String& directoryPath)
{
std::vector<std::filesystem::path> list;
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
list.push_back(file.path());
return list;
}
}

View file

@ -182,6 +182,12 @@ namespace ostd
return *this;
}
IOutputHandler& ConsoleOutputHandler::clear(void)
{
Utils::clearConsole();
return *this;
}

View file

@ -165,13 +165,6 @@ namespace ostd
return true;
}
// bool SerialIO::r_Data(StreamIndex addr, ISerializable& outData)
// {
// if (!is_validAddr(addr, outData.getMaxSerializedSize(), nullptr)) return false;
// return outData.deserialize(m_data, addr);
// }
bool SerialIO::r_ByteStream(StreamIndex addr, ByteStream& outStream)
{
if (!is_validAddr(addr, tTypeSize::ADDR)) return false;
@ -179,6 +172,7 @@ namespace ostd
if (!r_Addr(addr, stream_size)) return false;
addr += tTypeSize::ADDR;
if (!is_validAddr(addr, stream_size)) return false;
outStream.clear();
outStream.reserve(stream_size);
for (StreamIndex j = addr; j < addr + stream_size; j++)
outStream.push_back(m_data[j]);
@ -188,15 +182,54 @@ namespace ostd
bool SerialIO::r_ByteStream(StreamIndex addr, ByteStream& outStream, uint32_t size)
{
if (!is_validAddr(addr, size)) return false;
outStream.clear();
outStream.reserve(size);
for (StreamIndex j = addr; j < addr + size; j++)
outStream.push_back(m_data[j]);
return true;
}
bool SerialIO::r_String(StreamIndex addr, String& outString)
{
if (!is_validAddr(addr, tTypeSize::ADDR)) return false;
uint32_t stream_size;
if (!r_Addr(addr, stream_size)) return false;
addr += tTypeSize::ADDR;
if (!is_validAddr(addr, stream_size)) return false;
outString = "";
for (StreamIndex j = addr; j < addr + stream_size; j++)
outString += (char)(m_data[j]);
return true;
}
bool SerialIO::r_String(StreamIndex addr, String& outString, uint32_t size)
{
if (!is_validAddr(addr, size)) return false;
outString = "";
for (StreamIndex j = addr; j < addr + size; j++)
outString += (char)(m_data[j]);
return true;
}
bool SerialIO::r_NullTerminatedString(StreamIndex addr, String& outString)
{
outString = "";
int8_t c = 0;
if (!r_Byte(addr, c)) return false;
addr += tTypeSize::BYTE;
while (c != 0)
{
outString += (char)c;
if (!r_Byte(addr, c)) return false;
addr += tTypeSize::BYTE;
}
return true;
}
bool SerialIO::w_QWord(StreamIndex addr, QWord val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::QWORD)) return false;
if (isLittleEndian())
{
@ -226,6 +259,7 @@ namespace ostd
bool SerialIO::w_DWord(StreamIndex addr, DWord val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::DWORD)) return false;
if (isLittleEndian())
{
@ -247,6 +281,7 @@ namespace ostd
bool SerialIO::w_Word(StreamIndex addr, Word val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::WORD)) return false;
if (isLittleEndian())
{
@ -264,6 +299,7 @@ namespace ostd
bool SerialIO::w_Byte(StreamIndex addr, Byte val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::BYTE)) return false;
m_data[addr] = val;
return true;
@ -271,6 +307,7 @@ namespace ostd
bool SerialIO::w_Addr(StreamIndex addr, StreamIndex val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::ADDR)) return false;
if (isLittleEndian())
{
@ -292,6 +329,7 @@ namespace ostd
bool SerialIO::w_Float(StreamIndex addr, float val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::FLOAT)) return false;
__float_parser fp;
fp.val = val;
@ -315,6 +353,7 @@ namespace ostd
bool SerialIO::w_Double(StreamIndex addr, double val)
{
m_statuWriting = true;
if (!is_validAddr(addr, tTypeSize::DOUBLE)) return false;
__double_parser dp;
dp.val = val;
@ -344,39 +383,63 @@ namespace ostd
return true;
}
// bool SerialIO::w_Data(StreamIndex addr, const ISerializable& data)
// {
// if (!is_validAddr(addr, data.getMaxSerializedSize(), &memUsage)) return false;
// auto ser = data.serialize();
// if (ser.size() == 0 || !is_validAddr(addr, ser.size(), &memUsage)) return false;
// for (uint32_t i = 0; i < ser.size(); i++)
// m_data[addr + i] = ser[i];
// return true;
// }
bool SerialIO::w_ByteStream(StreamIndex addr, const ByteStream& stream)
bool SerialIO::w_ByteStream(StreamIndex addr, const ByteStream& stream, bool store_size)
{
m_statuWriting = true;
uint32_t stream_size = stream.size();
if (store_size)
{
if (!is_validAddr(addr, tTypeSize::ADDR + stream_size)) return false;
if (!w_Addr(addr, stream_size)) return false;
addr += tTypeSize::ADDR;
}
else if (!is_validAddr(addr, stream_size)) return false;
for (StreamIndex j = addr; j < addr + stream_size; j++)
m_data[j] = stream[j - addr];
return true;
}
bool SerialIO::w_String(StreamIndex addr, const String& str, bool store_size, bool null_terminate)
{
m_statuWriting = true;
auto stream = Utils::stringToByteStream(str);
uint32_t stream_size = stream.size();
if (store_size && !null_terminate)
{
if (!is_validAddr(addr, tTypeSize::ADDR + stream_size)) return false;
if (!w_Addr(addr, stream_size)) return false;
addr += tTypeSize::ADDR;
}
else if (!is_validAddr(addr, stream_size)) return false;
for (StreamIndex j = addr; j < addr + stream_size; j++)
m_data[j] = stream[j - addr];
addr += str.length() * tTypeSize::BYTE;
if (null_terminate)
if (!w_Byte(addr, 0x00)) return false;
return true;
}
bool SerialIO::is_validAddr(StreamIndex addr, StreamIndex offset)
{
if ((addr + offset - 1) >= m_data.size())
{
for (uint32_t i = 0; i <= 1024; i++) //TODO: This is temporary, find better way
if (isAutoResizeEnabled() && m_statuWriting)
{
for (uint32_t i = 0; i <= m_resizeAmount; i++)
m_data.push_back(0);
}
m_statuWriting = false;
return true;
}
else
{
m_statuWriting = false;
return false;
}
}
m_statuWriting = false;
return true;
}
void SerialIO::print(StreamIndex start, IOutputHandler& out)
{
@ -387,151 +450,9 @@ namespace ostd
Utils::printByteStream(m_data, start, line_len, power / line_len, out);
}
/*Byte ObjectField::getByte(String name)
bool SerialIO::saveToFile(const String& filePath)
{
return 0;
return Utils::saveByteStreamToFile(getData(), filePath);
}
UByte ObjectField::getUByte(String name)
{
return 0;
}
Word ObjectField::getWord(String name)
{
return 0;
}
UWord ObjectField::getUWord(String name)
{
return 0;
}
DWord ObjectField::getDWord(String name)
{
return 0;
}
UDWord ObjectField::getUDWord(String name)
{
return 0;
}
QWord ObjectField::getQWord(String name)
{
return 0;
}
UQWord ObjectField::getUQWord(String name)
{
return 0;
}
float ObjectField::getFloat(String name)
{
return 0;
}
double ObjectField::getDouble(String name)
{
return 0;
}
ObjectField ObjectField::getObject(String name)
{
return *this;
}
ByteStream OXDBuilder::stringToStream(String str)
{
if (str.length() == 0) return { 0 };
SerialIO sio(str.length() + tFieldType::QWord);
sio.w_QWord(0, str.length());
for (uint64_t i = 0; i < str.size(); i++)
{
char c = str[i];
sio.w_Byte(tFieldType::QWord + i, c);
}
return sio.getData();
}
bool OXDBuilder::Object::create(String name, uint64_t oid)
{
if (StringEditor(name).trim().str() == "") return false;
if (OXDBuilder::Object::s_open) return false;
OXDBuilder::Object::s_open = true;
OXDBuilder::Object::s_current = ObjectField();
}
ObjectField OXDBuilder::Object::get(void)
{
if (!OXDBuilder::Object::s_open) return ObjectField();
ObjectField obj = s_current;
OXDBuilder::Object::s_open = false;
OXDBuilder::Object::s_current = ObjectField();
return obj;
}
bool OXDBuilder::Object::newField_Byte(ObjectField& obj, String name, Byte value)
{
return false;
}
bool OXDBuilder::Object::newField_UByte(ObjectField& obj, String name, UByte value)
{
return false;
}
bool OXDBuilder::Object::newField_Word(ObjectField& obj, String name, Word value)
{
return false;
}
bool OXDBuilder::Object::newField_UWord(ObjectField& obj, String name, UWord value)
{
return false;
}
bool OXDBuilder::Object::newField_DWord(ObjectField& obj, String name, DWord value)
{
return false;
}
bool OXDBuilder::Object::newField_UDWord(ObjectField& obj, String name, UDWord value)
{
return false;
}
bool OXDBuilder::Object::newField_QWord(ObjectField& obj, String name, QWord value)
{
return false;
}
bool OXDBuilder::Object::newField_UQWord(ObjectField& obj, String name, UQWord value)
{
return false;
}
bool OXDBuilder::Object::newField_Float(ObjectField& obj, String name, float value)
{
return false;
}
bool OXDBuilder::Object::newField_Double(ObjectField& obj, String name, double value)
{
return false;
}
bool OXDBuilder::Object::newField_Object(ObjectField& obj, String name, ObjectField& value)
{
return false;
}*/
} // namespace serial
} // namesoace ox
} // namesoace ostd

View file

@ -31,9 +31,11 @@ namespace ostd
bool r_Addr(StreamIndex addr, StreamIndex& outVal);
bool r_Float(StreamIndex addr, float& outVal);
bool r_Double(StreamIndex addr, double& outVal);
//bool r_Data(StreamIndex addr, ISerializable& outData);
bool r_ByteStream(StreamIndex addr, ByteStream& outStream);
bool r_ByteStream(StreamIndex addr, ByteStream& outStream, uint32_t size);
bool r_String(StreamIndex addr, String& outString);
bool r_String(StreamIndex addr, String& outString, uint32_t size);
bool r_NullTerminatedString(StreamIndex addr, String& outString);
bool w_QWord(StreamIndex addr, QWord val);
bool w_DWord(StreamIndex addr, DWord val);
@ -42,124 +44,33 @@ namespace ostd
bool w_Addr(StreamIndex addr, StreamIndex val);
bool w_Float(StreamIndex addr, float val);
bool w_Double(StreamIndex addr, double val);
//bool w_Data(StreamIndex addr, const ISerializable& data);
bool w_ByteStream(StreamIndex addr, const ByteStream& stream);
bool w_ByteStream(StreamIndex addr, const ByteStream& stream, bool store_size = true);
bool w_String(StreamIndex addr, const String& str, bool store_size = false, bool null_terminate = true);
bool is_validAddr(StreamIndex addr, StreamIndex offset = 1);
inline uint64_t size(void) { return m_data.size(); }
inline ByteStream& getData(void) { return m_data; }
void print(StreamIndex start, IOutputHandler& out);
bool saveToFile(const String& filePath);
inline uint8_t getEndianness(void) const { return m_endianness; }
inline bool isLittleEndian(void) const { return m_endianness == tEndianness::LittleEndian; }
inline bool isBigEndian(void) const { return m_endianness == tEndianness::BigEndian; }
inline bool isAutoResizeEnabled(void) const { return m_autoResize; }
inline void enableAutoResize(bool val = true, int32_t resizeAmount = 1024) { m_autoResize = val; if (!val) return; m_resizeAmount = resizeAmount; }
private:
ByteStream m_data;
uint8_t m_endianness { tEndianness::BigEndian };
bool m_autoResize { true };
int32_t m_resizeAmount { 1024 };
bool m_statuWriting { false };
public:
inline static constexpr uint64_t DefaultMaxSize = 512;
};
/*struct tFieldType
{
inline static constexpr uint8_t Invalid = 0;
inline static constexpr uint8_t Object = 0xFF;
inline static constexpr uint8_t Stream = 0xFE;
inline static constexpr uint8_t Array = 0xFD;
inline static constexpr uint8_t Byte = 1;
inline static constexpr uint8_t Word = 2;
inline static constexpr uint8_t DWord = 4;
inline static constexpr uint8_t QWord = 8;
inline static constexpr uint8_t Float = 4;
inline static constexpr uint8_t Double = 8;
};
class RawField
{
public:
inline RawField(void) { m_type = tFieldType::Byte; m_data.push_back(0); }
inline RawField(uint8_t type, ByteStream data = { 0 }) { m_type = type; m_data = data; }
inline uint64_t size(void) const { return m_data.size(); }
inline uint8_t getType(void) const { return m_type; }
inline const ByteStream& getData(void) const { return m_data; }
inline ByteStream& getDataRW(void) { return m_data; }
inline void setType(uint8_t type) { m_type = type; }
inline void setData(ByteStream data) { m_data = data; }
protected:
uint8_t m_type;
ByteStream m_data;
};
class ObjectField
{
public: struct tFieldDescriptor
{
StreamIndex offset { 0 };
uint8_t type { tFieldType::Invalid };
};
public:
Byte getByte(String name);
UByte getUByte(String name);
Word getWord(String name);
UWord getUWord(String name);
DWord getDWord(String name);
UDWord getUDWord(String name);
QWord getQWord(String name);
UQWord getUQWord(String name);
float getFloat(String name);
double getDouble(String name);
ObjectField getObject(String name);
private:
ByteStream m_data;
std::unordered_map<String, tFieldDescriptor> m_fields;
friend class OXDBuilder;
};
class OXDBuilder
{
public:
// static bool newFile(String path);
// static bool writeFile(bool reset = true);
// static bool addObject(ObjectField& object);
// static void reset(void);
static ByteStream stringToStream(String str);
public: class Object
{
public:
static bool create(String name, uint64_t oid = 0);
static ObjectField get(void);
static bool newField_Byte(ObjectField& obj, String name, Byte value);
static bool newField_UByte(ObjectField& obj, String name, UByte value);
static bool newField_Word(ObjectField& obj, String name, Word value);
static bool newField_UWord(ObjectField& obj, String name, UWord value);
static bool newField_DWord(ObjectField& obj, String name, DWord value);
static bool newField_UDWord(ObjectField& obj, String name, UDWord value);
static bool newField_QWord(ObjectField& obj, String name, QWord value);
static bool newField_UQWord(ObjectField& obj, String name, UQWord value);
static bool newField_Float(ObjectField& obj, String name, float value);
static bool newField_Double(ObjectField& obj, String name, double value);
static bool newField_Object(ObjectField& obj, String name, ObjectField& value);
private:
inline static ObjectField s_current = ObjectField();
inline static bool s_open = false;
};
private:
inline static String s_filePath = "";
inline static bool s_open = false;
};*/
} //namespace serial
} // namespace ox
} // namespace ostd
#endif

View file

@ -817,4 +817,21 @@ namespace ostd
if (outStream.size() == 0) return false; //TODO: Error
return true;
}
ByteStream Utils::stringToByteStream(const String& data)
{
ByteStream bstream;
for (auto& c : data)
bstream.push_back((int8_t)c);
return bstream;
}
String Utils::byteStreamToString(const ByteStream& data)
{
StringEditor out_string = "";
for (int64_t i = 0; i < data.size(); i++)
{
if (data[i] == 0) break;
out_string.add((char)data[i]);
}
return out_string.str();
}
} // namespace ox

View file

@ -3,6 +3,8 @@
#include <ostd/Types.hpp>
#include <filesystem>
namespace ostd
{
enum class eTimeUnits
@ -155,8 +157,18 @@ namespace ostd
static void printByteStream(const ByteStream& data, StreamIndex start, uint8_t line_len, uint16_t n_rows, IOutputHandler& out, int32_t addrHighlight = -1, uint32_t highlightRange = 1, const String& title = "");
static bool saveByteStreamToFile(const ByteStream& stream, const String& filePath);
static bool loadByteStreamFromFile(const String& filePath, ByteStream& outStream);
static ByteStream stringToByteStream(const String& data);
static String byteStreamToString(const ByteStream& data);
static std::vector<std::filesystem::path> listFilesInDirectory(const String& directoryPath);
static std::vector<std::filesystem::path> listDirectoriesInDirectory(const String& directoryPath);
static std::vector<std::filesystem::path> listDirectory(const String& directoryPath);
static std::vector<std::filesystem::path> listFilesInDirectoryRecursive(const String& directoryPath);
static std::vector<std::filesystem::path> listDirectoriesInDirectoryRecursive(const String& directoryPath);
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
static int32_t solveIntegerExpression(const String& expr);
static void clearConsole(void);
private:
inline static uint64_t s_startTime_ms;
@ -334,6 +346,7 @@ namespace ostd
virtual IOutputHandler& bgcol(const Color& color) = 0;
virtual IOutputHandler& bgcol(String color) = 0;
virtual IOutputHandler& resetColors(void) = 0;
virtual IOutputHandler& clear(void) = 0;
};
class ConsoleOutputHandler : public IOutputHandler
@ -361,6 +374,7 @@ namespace ostd
IOutputHandler& nl(void) override;
IOutputHandler& flush(void) override;
IOutputHandler& reset(void) override;
IOutputHandler& clear(void) override;
};
class BufferedOutputHandler : public IOutputHandler
@ -387,7 +401,7 @@ namespace ostd
inline IOutputHandler& flush(void) override { return *this; }
inline IOutputHandler& reset(void) override { return *this; }
inline IOutputHandler& clear(void) { m_buffer = ""; return *this; }
inline IOutputHandler& clear(void) override { m_buffer = ""; return *this; }
inline const StringEditor& getBuffer(void) { return m_buffer; }
private:

View file

@ -1 +1 @@
1712
1716