use C++ stream classes

This commit is contained in:
Syping 2021-09-17 08:17:25 +02:00
parent 5bdbb09c7a
commit 3c59996591
2 changed files with 28 additions and 83 deletions

View file

@ -17,7 +17,9 @@
*****************************************************************************/
#include "RagePhoto.h"
#include <fstream>
#include <iostream>
#include <iterator>
int main(int argc, char *argv[])
{
@ -32,65 +34,32 @@ int main(int argc, char *argv[])
RagePhoto ragePhoto;
// Read file
FILE *file = fopen(argv[1], "rb");
if (!file) {
std::cout << "Failed to open import file" << std::endl;
return -1;
std::ifstream ifs(argv[1], std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
std::cout << "Failed to open file: " << argv[1] << std::endl;
return 1;
}
const int fseek_end_value = fseek(file, 0, SEEK_END);
if (fseek_end_value == -1) {
fclose(file);
std::cout << "Failed to read file" << std::endl;
return -1;
}
const size_t file_size = ftell(file);
if (file_size == -1) {
fclose(file);
std::cout << "Failed to read file" << std::endl;
return -1;
}
const int fseek_set_value = fseek(file, 0, SEEK_SET);
if (fseek_set_value == -1) {
fclose(file);
std::cout << "Failed to read file" << std::endl;
return -1;
}
char *data = static_cast<char*>(malloc(file_size));
const size_t file_rsize = fread(data, 1, file_size, file);
if (file_size != file_rsize) {
fclose(file);
std::cout << "Failed to read file" << std::endl;
return -1;
}
fclose(file);
std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
// Load Photo
const bool loaded = ragePhoto.load(data, file_size);
// Deinitialise data after Photo loaded
free(data);
const bool loaded = ragePhoto.load(data);
if (!loaded) {
const RagePhoto::Error error = ragePhoto.error();
if (error <= RagePhoto::Error::PhotoReadError) {
if (ragePhoto.error() <= RagePhoto::Error::PhotoReadError) {
std::cout << "Failed to load photo" << std::endl;
return 1;
}
}
// Write jpeg
file = fopen(argv[2], "wb");
if (!file) {
std::cout << "Failed to open export file" << std::endl;
return -1;
}
const size_t written = fwrite(ragePhoto.photoData(), sizeof(char), ragePhoto.photoSize(), file);
fclose(file);
if (written != ragePhoto.photoSize()) {
std::cout << "Failed to write file" << std::endl;
return -1;
std::ofstream ofs(argv[2], std::ios::out | std::ios::binary | std::ios::trunc);
if (!ofs.is_open()) {
std::cout << "Failed to write file: " << argv[2] << std::endl;
return 1;
}
ofs << ragePhoto.photo();
ofs.close();
if (ragePhoto.format() == RagePhoto::PhotoFormat::GTA5)
std::cout << "GTA V Photo successfully exported" << std::endl;