diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fc44a1..75d1a83 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/ostd/Console.cpp b/src/ostd/Console.cpp new file mode 100644 index 0000000..75bc073 --- /dev/null +++ b/src/ostd/Console.cpp @@ -0,0 +1,51 @@ +#include + +namespace ostd +{ +#if defined _WIN32 + +#include + +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 +#include + +void Utils::clearConsole(void) +{ + if (!cur_term) + { + int result; + setupterm(NULL, STDOUT_FILENO, &result); + if (result <= 0) return; + } + + putp(tigetstr( "clear" )); +} + +#endif +} \ No newline at end of file diff --git a/src/ostd/FileSystem.cpp b/src/ostd/FileSystem.cpp new file mode 100644 index 0000000..280ead5 --- /dev/null +++ b/src/ostd/FileSystem.cpp @@ -0,0 +1,65 @@ +#include "Utils.hpp" + +namespace ostd +{ + std::vector Utils::listFilesInDirectory(const String& directoryPath) + { + std::vector 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 Utils::listDirectoriesInDirectory(const String& directoryPath) + { + std::vector 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 Utils::listDirectory(const String& directoryPath) + { + std::vector list; + for (const auto& file : std::filesystem::directory_iterator(directoryPath)) + list.push_back(file.path()); + return list; + } + + std::vector Utils::listFilesInDirectoryRecursive(const String& directoryPath) + { + std::vector 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 Utils::listDirectoriesInDirectoryRecursive(const String& directoryPath) + { + std::vector 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 Utils::listDirectoryRecursive(const String& directoryPath) + { + std::vector list; + for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath)) + list.push_back(file.path()); + return list; + } + +} \ No newline at end of file diff --git a/src/ostd/OutputHandlers.cpp b/src/ostd/OutputHandlers.cpp index 0a930f1..24749fd 100755 --- a/src/ostd/OutputHandlers.cpp +++ b/src/ostd/OutputHandlers.cpp @@ -181,6 +181,12 @@ namespace ostd std::cout << termcolor::reset; return *this; } + + IOutputHandler& ConsoleOutputHandler::clear(void) + { + Utils::clearConsole(); + return *this; + } diff --git a/src/ostd/Serial.cpp b/src/ostd/Serial.cpp index 1c3877f..55b4b42 100755 --- a/src/ostd/Serial.cpp +++ b/src/ostd/Serial.cpp @@ -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,40 +383,64 @@ 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 (!is_validAddr(addr, tTypeSize::ADDR + stream_size)) return false; - if (!w_Addr(addr, stream_size)) return false; - addr += tTypeSize::ADDR; + 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 - m_data.push_back(0); + 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) { uint32_t line_len = 32; @@ -386,152 +449,10 @@ namespace ostd power *= 2; 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 diff --git a/src/ostd/Serial.hpp b/src/ostd/Serial.hpp index 0810491..1a749d3 100755 --- a/src/ostd/Serial.hpp +++ b/src/ostd/Serial.hpp @@ -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 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 diff --git a/src/ostd/Utils.cpp b/src/ostd/Utils.cpp index be640ba..91a83c0 100755 --- a/src/ostd/Utils.cpp +++ b/src/ostd/Utils.cpp @@ -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 diff --git a/src/ostd/Utils.hpp b/src/ostd/Utils.hpp index 45aa39f..0c96091 100755 --- a/src/ostd/Utils.hpp +++ b/src/ostd/Utils.hpp @@ -3,6 +3,8 @@ #include +#include + 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 listFilesInDirectory(const String& directoryPath); + static std::vector listDirectoriesInDirectory(const String& directoryPath); + static std::vector listDirectory(const String& directoryPath); + static std::vector listFilesInDirectoryRecursive(const String& directoryPath); + static std::vector listDirectoriesInDirectoryRecursive(const String& directoryPath); + static std::vector 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: diff --git a/tools/build.nr b/tools/build.nr index 3755801..f754216 100755 --- a/tools/build.nr +++ b/tools/build.nr @@ -1 +1 @@ -1712 \ No newline at end of file +1716 \ No newline at end of file