| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title SerializableFile.cpp | ||
| 3 | // \author dinkel | ||
| 4 | // \brief cpp file for SerializableFile | ||
| 5 | // | ||
| 6 | // \copyright | ||
| 7 | // Copyright 2009-2016, by the California Institute of Technology. | ||
| 8 | // ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 9 | // acknowledged. | ||
| 10 | // | ||
| 11 | // ====================================================================== | ||
| 12 | |||
| 13 | #include "Fw/SerializableFile/SerializableFile.hpp" | ||
| 14 | #include "Fw/Types/Assert.hpp" | ||
| 15 | #include "Os/File.hpp" | ||
| 16 | |||
| 17 | namespace Fw { | ||
| 18 | |||
| 19 | ✗ | SerializableFile::SerializableFile(MemAllocator* allocator, FwSizeType maxSerializedSize) | |
| 20 | ✗ | : m_allocator(allocator), | |
| 21 | ✗ | m_recoverable(false), // for compiler; not used | |
| 22 | ✗ | m_actualSize(maxSerializedSize), | |
| 23 | ✗ | m_buffer(static_cast<U8*>(this->m_allocator->allocate(0, m_actualSize, m_recoverable)), m_actualSize) { | |
| 24 | // assert if allocator returns smaller size | ||
| 25 | ✗ | FW_ASSERT(maxSerializedSize == m_actualSize, static_cast<FwAssertArgType>(maxSerializedSize), | |
| 26 | static_cast<FwAssertArgType>(m_actualSize)); | ||
| 27 | ✗ | FW_ASSERT(nullptr != m_buffer.getBuffAddr()); | |
| 28 | ✗ | } | |
| 29 | |||
| 30 | ✗ | SerializableFile::~SerializableFile() { | |
| 31 | ✗ | this->m_allocator->deallocate(0, this->m_buffer.getBuffAddr()); | |
| 32 | ✗ | } | |
| 33 | |||
| 34 | ✗ | SerializableFile::Status SerializableFile::load(const char* fileName, Serializable& serializable) { | |
| 35 | ✗ | Os::File file; | |
| 36 | Os::File::Status status; | ||
| 37 | ✗ | status = file.open(fileName, Os::File::OPEN_READ); | |
| 38 | ✗ | if (Os::File::OP_OK != status) { | |
| 39 | ✗ | return FILE_OPEN_ERROR; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | FwSizeType length = this->m_buffer.getCapacity(); | |
| 43 | ✗ | status = file.read(this->m_buffer.getBuffAddr(), length, Os::File::WaitType::NO_WAIT); | |
| 44 | ✗ | if (Os::File::OP_OK != status) { | |
| 45 | ✗ | file.close(); | |
| 46 | ✗ | return FILE_READ_ERROR; | |
| 47 | } | ||
| 48 | ✗ | file.close(); | |
| 49 | |||
| 50 | ✗ | this->reset(); | |
| 51 | SerializeStatus serStatus; | ||
| 52 | ✗ | serStatus = this->m_buffer.setBuffLen(length); | |
| 53 | ✗ | FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus); | |
| 54 | ✗ | serStatus = serializable.deserializeFrom(this->m_buffer); | |
| 55 | ✗ | if (FW_SERIALIZE_OK != serStatus) { | |
| 56 | ✗ | return DESERIALIZATION_ERROR; | |
| 57 | } | ||
| 58 | |||
| 59 | ✗ | return SerializableFile::OP_OK; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | ✗ | SerializableFile::Status SerializableFile::save(const char* fileName, Serializable& serializable) { | |
| 63 | ✗ | this->reset(); | |
| 64 | ✗ | SerializeStatus serStatus = serializable.serializeTo(this->m_buffer); | |
| 65 | ✗ | FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus); | |
| 66 | |||
| 67 | ✗ | Os::File file; | |
| 68 | Os::File::Status status; | ||
| 69 | ✗ | status = file.open(fileName, Os::File::OPEN_WRITE); | |
| 70 | ✗ | if (Os::File::OP_OK != status) { | |
| 71 | ✗ | return FILE_OPEN_ERROR; | |
| 72 | } | ||
| 73 | |||
| 74 | ✗ | FwSizeType length = this->m_buffer.getSize(); | |
| 75 | ✗ | status = file.write(this->m_buffer.getBuffAddr(), length); | |
| 76 | ✗ | if ((Os::File::OP_OK != status) || (length != this->m_buffer.getSize())) { | |
| 77 | ✗ | file.close(); | |
| 78 | ✗ | return FILE_WRITE_ERROR; | |
| 79 | } | ||
| 80 | |||
| 81 | ✗ | file.close(); | |
| 82 | |||
| 83 | ✗ | return SerializableFile::OP_OK; | |
| 84 | ✗ | } | |
| 85 | |||
| 86 | ✗ | void SerializableFile::reset() { | |
| 87 | ✗ | this->m_buffer.resetSer(); //!< reset to beginning of buffer to reuse for serialization | |
| 88 | ✗ | this->m_buffer.resetDeser(); //!< reset deserialization to beginning | |
| 89 | ✗ | } | |
| 90 | } // namespace Fw | ||
| 91 |