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

View file

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