diff --git a/core/base/StrBuffer.cpp b/core/base/StrBuffer.cpp index b9ad2f0f..9395d2b6 100644 --- a/core/base/StrBuffer.cpp +++ b/core/base/StrBuffer.cpp @@ -24,6 +24,8 @@ #include "StrBuffer.hpp" +#include + namespace oatpp { namespace base { void StrBuffer::set(const void* data, v_int32 size, bool hasOwnData) { @@ -99,6 +101,25 @@ std::shared_ptr StrBuffer::createSharedConcatenated(const void* data1 return ptr; } +std::shared_ptr StrBuffer::loadFromFile(const char* filename) { + std::ifstream file (filename, std::ios::in|std::ios::binary|std::ios::ate); + if (file.is_open()) { + auto result = createShared((v_int32) file.tellg()); + file.seekg(0, std::ios::beg); + file.read((char*)result->getData(), result->getSize()); + file.close(); + return result; + + } + return nullptr; +} + +void StrBuffer::saveToFile(const char* filename) { + std::ofstream fs(filename, std::ios::out | std::ios::binary); + fs.write((const char*)m_data, m_size); + fs.close(); +} + p_char8 StrBuffer::getData() const { return m_data; } diff --git a/core/base/StrBuffer.hpp b/core/base/StrBuffer.hpp index 4951d4fc..5bfd5b01 100644 --- a/core/base/StrBuffer.hpp +++ b/core/base/StrBuffer.hpp @@ -99,6 +99,14 @@ public: return nullptr; } + /** + * Load data from file and store in StrBuffer. + * If file not found return nullptr + */ + static std::shared_ptr loadFromFile(const char* filename); + + void saveToFile(const char* filename); + p_char8 getData() const; v_int32 getSize() const;