From a396d014f710573910c97c73bd698c98fbc36e15 Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 2 Nov 2021 10:16:56 +0100 Subject: [PATCH] C++ API: add loadFile and saveFile --- src/RagePhoto.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/RagePhoto.h | 8 +++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 0100ecb..6a5212b 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -17,7 +17,9 @@ *****************************************************************************/ #include "RagePhoto.h" +#include #include +#include #include #include @@ -387,6 +389,20 @@ bool RagePhoto::load(const std::string &data) return load(data.data(), data.size()); } +bool RagePhoto::loadFile(const std::string &filename) +{ + std::ifstream ifs(filename, std::ios::in | std::ios::binary); + if (ifs.is_open()) { + std::string sdata(std::istreambuf_iterator{ifs}, {}); + ifs.close(); + return load(sdata); + } + else { + m_data.error = static_cast(Error::Uninitialised); // 0 + return false; + } +} + RagePhoto::Error RagePhoto::error() const { return static_cast(m_data.error); @@ -676,6 +692,29 @@ const std::string RagePhoto::save(bool *ok) return save(m_data.photoFormat, ok); } +bool RagePhoto::saveFile(const std::string &filename, uint32_t photoFormat) +{ + bool ok; + const std::string sdata = save(photoFormat, &ok); + if (ok) { + std::ofstream ofs(filename, std::ios::out | std::ios::binary | std::ios::trunc); + if (!ofs.is_open()) { + m_data.error = static_cast(Error::Uninitialised); // 0 + return false; + } + ofs << sdata; + ofs.close(); + return true; + } + else + return false; +} + +bool RagePhoto::saveFile(const std::string &filename) +{ + return saveFile(filename, m_data.photoFormat); +} + inline size_t RagePhoto::saveSize(RagePhotoData *ragePhotoData, uint32_t photoFormat) { if (photoFormat == PhotoFormat::GTA5) diff --git a/src/RagePhoto.h b/src/RagePhoto.h index 7cee575..cfafabc 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -86,7 +86,7 @@ public: TitleReadError = 26, /**< Title can't be read */ UnicodeHeaderError = 5, /**< Header can't be decoded */ UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */ - Uninitialised = 0, /**< Uninitialised */ + Uninitialised = 0, /**< Uninitialised, file access failed */ }; /** Photo Formats */ enum PhotoFormat : uint32_t { @@ -105,6 +105,10 @@ public: * \param data Photo data */ bool load(const std::string &data); + /** Loads a Photo from a file. + * \param filename File to load + */ + bool loadFile(const std::string &filename); Error error() const; /**< Returns the last error occurred. */ uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */ const std::string photo() const; /**< Returns the Photo JPEG data. */ @@ -132,6 +136,8 @@ public: * \param ok \p true when saved successfully */ const std::string save(bool *ok = nullptr); + bool saveFile(const std::string &filename, uint32_t photoFormat); /**< Saves a Photo to a file. */ + bool saveFile(const std::string &filename); /**< Saves a Photo to a file. */ static size_t saveSize(RagePhotoData *ragePhotoData, uint32_t photoFormat); /**< Returns the Photo save file size. */ static size_t saveSize(RagePhotoData *ragePhotoData); /**< Returns the Photo save file size. */ size_t saveSize(uint32_t photoFormat); /**< Returns the Photo save file size. */