C++ API: add loadFile and saveFile
This commit is contained in:
parent
af547bd89f
commit
a396d014f7
|
@ -17,7 +17,9 @@
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include "RagePhoto.h"
|
#include "RagePhoto.h"
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
@ -387,6 +389,20 @@ bool RagePhoto::load(const std::string &data)
|
||||||
return load(data.data(), data.size());
|
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
|
RagePhoto::Error RagePhoto::error() const
|
||||||
{
|
{
|
||||||
return static_cast<Error>(m_data.error);
|
return static_cast<Error>(m_data.error);
|
||||||
|
@ -676,6 +692,29 @@ const std::string RagePhoto::save(bool *ok)
|
||||||
return save(m_data.photoFormat, 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)
|
inline size_t RagePhoto::saveSize(RagePhotoData *ragePhotoData, uint32_t photoFormat)
|
||||||
{
|
{
|
||||||
if (photoFormat == PhotoFormat::GTA5)
|
if (photoFormat == PhotoFormat::GTA5)
|
||||||
|
|
|
@ -86,7 +86,7 @@ public:
|
||||||
TitleReadError = 26, /**< Title can't be read */
|
TitleReadError = 26, /**< Title can't be read */
|
||||||
UnicodeHeaderError = 5, /**< Header can't be decoded */
|
UnicodeHeaderError = 5, /**< Header can't be decoded */
|
||||||
UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */
|
UnicodeInitError = 4, /**< Failed to initialise Unicode decoder */
|
||||||
Uninitialised = 0, /**< Uninitialised */
|
Uninitialised = 0, /**< Uninitialised, file access failed */
|
||||||
};
|
};
|
||||||
/** Photo Formats */
|
/** Photo Formats */
|
||||||
enum PhotoFormat : uint32_t {
|
enum PhotoFormat : uint32_t {
|
||||||
|
@ -105,6 +105,10 @@ public:
|
||||||
* \param data Photo data
|
* \param data Photo data
|
||||||
*/
|
*/
|
||||||
bool load(const std::string &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. */
|
Error error() const; /**< Returns the last error occurred. */
|
||||||
uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */
|
uint32_t format() const; /**< Returns the Photo Format (GTA V or RDR 2). */
|
||||||
const std::string photo() const; /**< Returns the Photo JPEG data. */
|
const std::string photo() const; /**< Returns the Photo JPEG data. */
|
||||||
|
@ -132,6 +136,8 @@ public:
|
||||||
* \param ok \p true when saved successfully
|
* \param ok \p true when saved successfully
|
||||||
*/
|
*/
|
||||||
const std::string save(bool *ok = nullptr);
|
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, uint32_t photoFormat); /**< Returns the Photo save file size. */
|
||||||
static size_t saveSize(RagePhotoData *ragePhotoData); /**< 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. */
|
size_t saveSize(uint32_t photoFormat); /**< Returns the Photo save file size. */
|
||||||
|
|
Loading…
Reference in New Issue