C++ API: add loadFile and saveFile

This commit is contained in:
Syping 2021-11-02 10:16:56 +01:00
parent af547bd89f
commit a396d014f7
2 changed files with 46 additions and 1 deletions

View File

@ -17,7 +17,9 @@
*****************************************************************************/
#include "RagePhoto.h"
#include <fstream>
#include <iostream>
#include <iterator>
#include <cstdlib>
#include <cstring>
@ -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<char>{ifs}, {});
ifs.close();
return load(sdata);
}
else {
m_data.error = static_cast<uint8_t>(Error::Uninitialised); // 0
return false;
}
}
RagePhoto::Error RagePhoto::error() const
{
return static_cast<Error>(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<uint8_t>(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)

View File

@ -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. */