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

@ -21,7 +21,9 @@
#include <gdkmm/general.h>
#include <gtkmm/messagedialog.h>
#include <RagePhoto.h>
#include <fstream>
#include <iostream>
#include <iterator>
PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
{
@ -33,38 +35,20 @@ void PhotoViewer::open_file(const char *filename)
p_image.clear();
RagePhoto ragePhoto;
// Read file
FILE *file = fopen(filename, "rb");
if (!file) {
Gtk::MessageDialog msg(*p_win, "Failed to read file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
Gtk::MessageDialog msg(*p_win, "Failed to open file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
msg.set_title("Open Photo");
msg.run();
return;
}
const int fseek_end_value = fseek(file, 0, SEEK_END);
if (fseek_end_value == -1) {
fclose(file);
return;
}
const size_t file_size = ftell(file);
if (file_size == -1) {
fclose(file);
return;
}
const int fseek_set_value = fseek(file, 0, SEEK_SET);
if (fseek_set_value == -1) {
fclose(file);
return;
}
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);
return;
}
fclose(file);
const bool loaded = ragePhoto.load(data, file_size);
free(data);
std::string data(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
// Load Photo
const bool loaded = ragePhoto.load(data);
if (!loaded) {
const RagePhoto::Error error = ragePhoto.error();
if (error <= RagePhoto::Error::PhotoReadError) {
@ -75,22 +59,14 @@ void PhotoViewer::open_file(const char *filename)
}
}
guchar *photoData = static_cast<guchar*>(malloc(ragePhoto.photoSize()));
if (!photoData)
return;
memcpy(photoData, ragePhoto.photoData(), ragePhoto.photoSize());
GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, photoData, static_cast<gsize>(ragePhoto.photoSize()), NULL);
gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
gdk_pixbuf_loader_close(loader, NULL);
p_image = Glib::wrap(c_pixbuf);
p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
free(photoData);
if (p_image)
set_size_request(p_image->get_width(), p_image->get_height());
}

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;