Modify FileOp structure to use std::string and std::vector<> instead of raw pointers and JList.

This commit is contained in:
David Capello 2010-11-06 11:59:03 -03:00
parent 6ff0fc038b
commit d4f67cdb73
14 changed files with 77 additions and 102 deletions

View File

@ -150,8 +150,8 @@ void OpenFileCommand::onExecute(Context* context)
bool unrecent = false;
if (fop) {
if (fop->error) {
console.printf(fop->error);
if (fop->has_error()) {
console.printf(fop->error.c_str());
fop_free(fop);
unrecent = true;
@ -180,14 +180,14 @@ void OpenFileCommand::onExecute(Context* context)
thread.join();
// Show any error
if (fop->error)
console.printf(fop->error);
if (fop->has_error())
console.printf(fop->error.c_str());
Sprite *sprite = fop->sprite;
if (sprite) {
UIContext* context = UIContext::instance();
App::instance()->getRecentFiles()->addRecentFile(fop->filename);
App::instance()->getRecentFiles()->addRecentFile(fop->filename.c_str());
context->add_sprite(sprite);
set_sprite_in_more_reliable_editor(sprite);

View File

@ -121,9 +121,9 @@ static void save_sprite_in_background(Sprite* sprite, bool mark_as_saved)
thread.join();
/* show any error */
if (fop->error) {
if (fop->has_error()) {
Console console;
console.printf(fop->error);
console.printf(fop->error.c_str());
}
/* no error? */
else {

View File

@ -135,7 +135,7 @@ static bool load_ASE(FileOp *fop)
int c, frame;
FILE *f;
f = fopen(fop->filename, "rb");
f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -290,7 +290,7 @@ static bool save_ASE(FileOp *fop)
int frame;
FILE *f;
f = fopen(fop->filename, "wb");
f = fopen(fop->filename.c_str(), "wb");
if (!f)
return false;

View File

@ -577,7 +577,7 @@ static bool load_BMP(FileOp *fop)
unsigned long biSize;
int type, format;
f = fopen(fop->filename, "rb");
f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -717,7 +717,7 @@ static bool save_BMP(FileOp *fop)
bfSize = 54 + biSizeImage; /* header + image data */
}
f = fopen(fop->filename, "wb");
f = fopen(fop->filename.c_str(), "wb");
if (!f) {
fop_error(fop, "Error creating file.\n");
return false;

View File

@ -21,15 +21,13 @@
#include <allegro.h>
#include <string.h>
#include "gui/jalert.h"
#include "gui/jlist.h"
#include "app.h"
#include "base/mutex.h"
#include "base/scoped_lock.h"
#include "console.h"
#include "file/file.h"
#include "file/format_options.h"
#include "gui/jalert.h"
#include "modules/gui.h"
#include "modules/palettes.h"
#include "raster/raster.h"
@ -112,9 +110,9 @@ Sprite *sprite_load(const char *filename)
fop_operate(fop);
fop_done(fop);
if (fop->error) {
if (fop->has_error()) {
Console console;
console.printf(fop->error);
console.printf(fop->error.c_str());
}
sprite = fop->sprite;
@ -134,12 +132,12 @@ int sprite_save(Sprite *sprite)
fop_operate(fop);
fop_done(fop);
if (fop->error) {
if (fop->has_error()) {
Console console;
console.printf(fop->error);
console.printf(fop->error.c_str());
}
ret = (fop->error == NULL) ? 0: -1;
ret = (!fop->has_error() ? 0: -1);
fop_free(fop);
return ret;
@ -180,7 +178,7 @@ FileOp *fop_to_load_sprite(const char *filename, int flags)
fop_prepare_for_sequence(fop);
/* per now, we want load just one file */
jlist_append(fop->seq.filename_list, jstrdup(filename));
fop->seq.filename_list.push_back(filename);
/* don't load the sequence (just the one file/one frame) */
if (!(flags & FILE_LOAD_SEQUENCE_NONE)) {
@ -203,8 +201,7 @@ FileOp *fop_to_load_sprite(const char *filename, int flags)
break;
/* add this file name to the list */
jlist_append(fop->seq.filename_list,
jstrdup(buf));
fop->seq.filename_list.push_back(buf);
}
}
@ -212,26 +209,26 @@ FileOp *fop_to_load_sprite(const char *filename, int flags)
if ((flags & FILE_LOAD_SEQUENCE_ASK) &&
App::instance()->isGui()) {
/* really want load all files? */
if ((jlist_length(fop->seq.filename_list) > 1) &&
if ((fop->seq.filename_list.size() > 1) &&
(jalert("Notice"
"<<Possible animation with:"
"<<%s"
"<<Load the sequence of bitmaps?"
"||&Agree||&Skip",
get_filename(filename)) != 1)) {
/* if the user replies "Skip", we need just one file name (the
first one) */
while (jlist_length(fop->seq.filename_list) > 1) {
JLink link = jlist_last(fop->seq.filename_list);
jfree(link->data);
jlist_delete_link(fop->seq.filename_list, link);
if (fop->seq.filename_list.size() > 1) {
fop->seq.filename_list.erase(fop->seq.filename_list.begin()+1,
fop->seq.filename_list.end());
}
}
}
}
}
else
fop->filename = jstrdup(filename);
fop->filename = filename;
/* load just one frame */
if (flags & FILE_LOAD_ONE_FRAME)
@ -390,8 +387,7 @@ FileOp *fop_to_save_sprite(Sprite *sprite)
/* to save one frame */
if (fop->sprite->getTotalFrames() == 1) {
jlist_append(fop->seq.filename_list,
jstrdup(fop->sprite->getFilename()));
fop->seq.filename_list.push_back(fop->sprite->getFilename());
}
/* to save more frames */
else {
@ -410,12 +406,12 @@ FileOp *fop_to_save_sprite(Sprite *sprite)
for (frame=0; frame<fop->sprite->getTotalFrames(); frame++) {
/* get the name for this frame */
usprintf(buf, "%s%0*d%s", left, width, start_from+frame, right);
jlist_append(fop->seq.filename_list, jstrdup(buf));
fop->seq.filename_list.push_back(buf);
}
}
}
else
fop->filename = jstrdup(fop->sprite->getFilename());
fop->filename = fop->sprite->getFilename();
/* configure output format? */
if (fop->format->get_options != NULL) {
@ -452,10 +448,9 @@ void fop_operate(FileOp *fop)
fop->format != NULL &&
fop->format->load != NULL) {
/* load a sequence */
if (fop->seq.filename_list != NULL) {
if (fop->is_sequence()) {
int frame, frames, image_index = 0;
Image *old_image;
JLink link;
bool loadres;
/* default palette */
@ -481,7 +476,7 @@ void fop_operate(FileOp *fop)
} while (0)
/* load the sequence */
frames = jlist_length(fop->seq.filename_list);
frames = fop->seq.filename_list.size();
frame = 0;
old_image = NULL;
@ -489,14 +484,16 @@ void fop_operate(FileOp *fop)
fop->seq.progress_offset = 0.0f;
fop->seq.progress_fraction = 1.0f / (float)frames;
JI_LIST_FOR_EACH(fop->seq.filename_list, link) {
fop->filename = reinterpret_cast<char*>(link->data);
std::vector<std::string>::iterator it = fop->seq.filename_list.begin();
std::vector<std::string>::iterator end = fop->seq.filename_list.end();
for (; it != end; ++it) {
fop->filename = it->c_str();
/* call the "load" procedure to read the first bitmap */
loadres = (*fop->format->load)(fop);
if (!loadres) {
fop_error(fop, "Error loading frame %d from file \"%s\"\n",
frame+1, fop->filename);
frame+1, fop->filename.c_str());
}
/* for the first frame... */
@ -551,7 +548,7 @@ void fop_operate(FileOp *fop)
frame++;
fop->seq.progress_offset += fop->seq.progress_fraction;
}
fop->filename = jstrdup((char*)jlist_first_data(fop->seq.filename_list));
fop->filename = *fop->seq.filename_list.begin();
// Final setup
if (fop->sprite != NULL) {
@ -571,7 +568,7 @@ void fop_operate(FileOp *fop)
/* call the "load" procedure */
if (!(*fop->format->load)(fop))
fop_error(fop, "Error loading sprite from file \"%s\"\n",
fop->filename);
fop->filename.c_str());
}
if (fop->sprite != NULL) {
@ -582,10 +579,10 @@ void fop_operate(FileOp *fop)
}
/* set the filename */
if (fop->seq.filename_list)
fop->sprite->setFilename(reinterpret_cast<char*>(jlist_first_data(fop->seq.filename_list)));
if (fop->is_sequence())
fop->sprite->setFilename(fop->seq.filename_list.begin()->c_str());
else
fop->sprite->setFilename(fop->filename);
fop->sprite->setFilename(fop->filename.c_str());
// Quantize a palette for RGB images
if (fop->sprite->getImgType() == IMAGE_RGB)
@ -599,7 +596,7 @@ void fop_operate(FileOp *fop)
fop->format != NULL &&
fop->format->save != NULL) {
/* save a sequence */
if (fop->seq.filename_list != NULL) {
if (fop->is_sequence()) {
ASSERT(fop->format->flags & FILE_SUPPORT_SEQUENCES);
/* create a temporary bitmap */
@ -625,20 +622,18 @@ void fop_operate(FileOp *fop)
->copyColorsTo(fop->seq.palette);
/* setup the filename to be used */
fop->filename = reinterpret_cast<char*>
(jlist_nth_data(fop->seq.filename_list,
fop->sprite->getCurrentFrame()));
fop->filename = fop->seq.filename_list[fop->sprite->getCurrentFrame()];
/* call the "save" procedure... did it fail? */
if (!(*fop->format->save)(fop)) {
fop_error(fop, "Error saving frame %d in the file \"%s\"\n",
fop->sprite->getCurrentFrame()+1, fop->filename);
fop->sprite->getCurrentFrame()+1, fop->filename.c_str());
break;
}
fop->seq.progress_offset += fop->seq.progress_fraction;
}
fop->filename = jstrdup(reinterpret_cast<char*>(jlist_first_data(fop->seq.filename_list)));
fop->filename = *fop->seq.filename_list.begin();
// Destroy the image
image_free(fop->seq.image);
@ -655,7 +650,7 @@ void fop_operate(FileOp *fop)
/* call the "save" procedure */
if (!(*fop->format->save)(fop))
fop_error(fop, "Error saving the sprite in the file \"%s\"\n",
fop->filename);
fop->filename.c_str());
}
}
@ -682,22 +677,6 @@ void fop_stop(FileOp *fop)
void fop_free(FileOp *fop)
{
if (fop->filename)
jfree(fop->filename);
if (fop->error)
jfree(fop->error);
if (fop->seq.filename_list) {
JLink link;
/* free old filenames strings */
JI_LIST_FOR_EACH(fop->seq.filename_list, link)
jfree(link->data);
jlist_free(fop->seq.filename_list);
}
if (fop->seq.palette != NULL)
delete fop->seq.palette;
@ -784,20 +763,10 @@ void fop_error(FileOp *fop, const char *format, ...)
uvszprintf(buf_error, sizeof(buf_error), format, ap);
va_end(ap);
// Concatenate the new error
{
ScopedLock lock(*fop->mutex);
// Concatenate old errors with the new one
if (fop->error) {
char *old_error = fop->error;
fop->error = reinterpret_cast<char*>(jmalloc(ustrsizez(old_error) + ustrsizez(buf_error) + 1));
ustrcpy(fop->error, old_error);
ustrcat(fop->error, buf_error);
jfree(old_error);
}
/* first error */
else
fop->error = jstrdup(buf_error);
fop->error += buf_error;
}
}
@ -807,7 +776,7 @@ void fop_progress(FileOp *fop, float progress)
ScopedLock lock(*fop->mutex);
if (fop->seq.filename_list != NULL) {
if (fop->is_sequence()) {
fop->progress =
fop->seq.progress_offset +
fop->seq.progress_fraction*progress;
@ -858,16 +827,13 @@ static FileOp *fop_new(FileOpType type)
fop->type = type;
fop->format = NULL;
fop->sprite = NULL;
fop->filename = NULL;
fop->mutex = new Mutex();
fop->progress = 0.0f;
fop->error = NULL;
fop->done = false;
fop->stop = false;
fop->oneframe = false;
fop->seq.filename_list = NULL;
fop->seq.palette = NULL;
fop->seq.image = NULL;
fop->seq.progress_offset = 0.0f;
@ -881,7 +847,6 @@ static FileOp *fop_new(FileOpType type)
static void fop_prepare_for_sequence(FileOp *fop)
{
fop->seq.filename_list = jlist_new();
fop->seq.palette = new Palette(0, 256);
fop->seq.format_options = NULL;
}

View File

@ -19,8 +19,9 @@
#ifndef FILE_FILE_H_INCLUDED
#define FILE_FILE_H_INCLUDED
#include "gui/jbase.h"
#include <stdio.h>
#include <vector>
#include <string>
#define FILE_SUPPORT_RGB (1<<0)
#define FILE_SUPPORT_RGBA (1<<1)
@ -77,12 +78,12 @@ struct FileOp
FileOpType type; /* operation type: 0=load, 1=save */
FileFormat* format;
Sprite* sprite; /* loaded sprite, or sprite to be saved */
char* filename; /* file-name to load/save */
std::string filename; /* file-name to load/save */
/* shared fields between threads */
Mutex* mutex; /* mutex to access to the next two fields */
float progress; /* progress (1.0 is ready) */
char* error; /* error string */
std::string error; /* error string */
bool done : 1; /* true if the operation finished */
bool stop : 1; /* force the break of the operation */
bool oneframe : 1; /* load just one frame (in formats
@ -91,7 +92,7 @@ struct FileOp
/* data for sequences */
struct {
JList filename_list; /* all file names to load/save */
std::vector<std::string> filename_list; /* all file names to load/save */
Palette* palette; /* palette of the sequence */
Image* image; /* image to be saved/loaded */
/* for the progress bar */
@ -104,6 +105,15 @@ struct FileOp
Cel* last_cel;
FormatOptions* format_options;
} seq;
bool has_error() const {
return !this->error.empty();
}
bool is_sequence() const {
return !this->seq.filename_list.empty();
}
};
/* available extensions for each load/save operation */

View File

@ -72,7 +72,7 @@ static bool load_FLI(FileOp *fop)
FILE *f;
/* open the file to read in binary mode */
f = fopen(fop->filename, "rb");
f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -241,7 +241,7 @@ static bool save_FLI(FileOp *fop)
fli_header.oframe1 = fli_header.oframe2 = 0;
/* open the file to write in binary mode */
f = fopen(fop->filename, "wb");
f = fopen(fop->filename.c_str(), "wb");
if (!f)
return false;

View File

@ -87,7 +87,7 @@ static bool load_GIF(FileOp *fop)
bool ret = false;
int i, c;
gif = gif_load_animation(fop->filename,
gif = gif_load_animation(fop->filename.c_str(),
reinterpret_cast<void(*)(void*,float)>(fop_progress), fop);
if (!gif) {
fop_error(fop, "Error loading GIF file.\n");
@ -439,7 +439,7 @@ static bool save_GIF(FileOp *fop)
opal = npal;
}
ret = gif_save_animation(fop->filename, gif,
ret = gif_save_animation(fop->filename.c_str(), gif,
reinterpret_cast<void(*)(void*,float)>(fop_progress), fop);
gif_destroy_animation(gif);
return ret == 0 ? true: false;

View File

@ -76,7 +76,7 @@ struct BITMAPINFOHEADER
static bool load_ICO(FileOp *fop)
{
FILE* f = fopen(fop->filename, "rb");
FILE* f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -229,7 +229,7 @@ static bool save_ICO(FileOp *fop)
int c, x, y, b, m, v;
int num = sprite->getTotalFrames();
FILE* f = fopen(fop->filename, "wb");
FILE* f = fopen(fop->filename.c_str(), "wb");
if (!f)
return false;

View File

@ -89,7 +89,7 @@ static bool load_JPEG(FileOp *fop)
JDIMENSION buffer_height;
int c;
file = fopen(fop->filename, "rb");
file = fopen(fop->filename.c_str(), "rb");
if (!file)
return false;
@ -232,7 +232,7 @@ static bool save_JPEG(FileOp *fop)
int c;
/* Open the file for write in it. */
file = fopen(fop->filename, "wb");
file = fopen(fop->filename.c_str(), "wb");
if (!file) {
fop_error(fop, "Error creating file.\n");
return false;

View File

@ -52,7 +52,7 @@ static bool load_PCX(FileOp *fop)
int x, y;
char ch = 0;
f = fopen(fop->filename, "rb");
f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -187,7 +187,7 @@ static bool save_PCX(FileOp *fop)
char runchar;
char ch = 0;
f = fopen(fop->filename, "wb");
f = fopen(fop->filename.c_str(), "wb");
if (!f) {
fop_error(fop, "Error creating file.\n");
return false;

View File

@ -67,7 +67,7 @@ static bool load_PNG(FileOp *fop)
int imgtype;
FILE *fp;
fp = fopen(fop->filename, "rb");
fp = fopen(fop->filename.c_str(), "rb");
if (!fp)
return false;
@ -303,7 +303,7 @@ static bool save_PNG(FileOp *fop)
FILE *fp;
/* open the file */
fp = fopen(fop->filename, "wb");
fp = fopen(fop->filename.c_str(), "wb");
if (fp == NULL)
return false;

View File

@ -199,7 +199,7 @@ static bool load_TGA(FileOp *fop)
FILE *f;
int type;
f = fopen(fop->filename, "rb");
f = fopen(fop->filename.c_str(), "rb");
if (!f)
return false;
@ -405,7 +405,7 @@ static bool save_TGA(FileOp *fop)
bool need_pal = (image->imgtype == IMAGE_INDEXED)? true: false;
FILE *f;
f = fopen(fop->filename, "wb");
f = fopen(fop->filename.c_str(), "wb");
if (!f) {
fop_error(fop, "Error creating file.\n");
return false;

View File

@ -714,7 +714,7 @@ static bool fileview_generate_thumbnail(JWidget widget, IFileItem* fileitem)
if (!fop)
return true;
if (fop->error) {
if (fop->has_error()) {
fop_free(fop);
}
else {