Added jstring. The file-selector now uses jstrings.

This commit is contained in:
David Capello 2008-10-11 20:41:27 +00:00
parent 17dee030f5
commit 9bf40439d4
16 changed files with 361 additions and 112 deletions

View File

@ -1,5 +1,10 @@
2008-10-11 David A. Capello <davidcapello@gmail.com>
* src/dialogs/filesel.cpp (ase_file_selector): Now it uses
"jstring" instead of "char*".
* src/jinete/jstring.h (jstring): Added.
* src/raster/undoable.cpp (Undoable::set_constant_frame_rate): Added.
(Undoable::set_frame_length): Renamed to set_frame_duration.

View File

@ -25,7 +25,7 @@
/* general information */
#define PACKAGE "ASE"
#define VERSION "0.6b2"
#define VERSION "0.6"
#define WEBSITE "http://www.aseprite.org/"
#define COPYRIGHT "Copyright (C) 2001-2008 David A. Capello"

View File

@ -147,6 +147,7 @@ COMMON_SOURCES = \
src/jinete/jsep.cpp \
src/jinete/jslider.cpp \
src/jinete/jstream.cpp \
src/jinete/jstring.cpp \
src/jinete/jsystem.cpp \
src/jinete/jtextbox.cpp \
src/jinete/jtheme.cpp \

View File

@ -38,15 +38,13 @@ static void cmd_load_mask_execute(const char *argument)
{
/* get current sprite */
Sprite *sprite = current_sprite;
char *filename = ase_file_selector(_("Load .msk File"), "", "msk");
if (filename) {
Mask *mask = load_msk_file(filename);
jstring filename = ase_file_selector(_("Load .msk File"), "", "msk");
if (!filename.empty()) {
Mask *mask = load_msk_file(filename.c_str());
if (!mask) {
jalert("%s<<%s<<%s||%s",
_("Error"), _("Error loading .msk file"),
filename, _("&Close"));
jfree(filename);
filename.c_str(), _("&Close"));
return;
}
@ -61,7 +59,6 @@ static void cmd_load_mask_execute(const char *argument)
sprite_generate_mask_boundaries(sprite);
update_screen_for_sprite(sprite);
jfree(filename);
}
}

View File

@ -142,7 +142,7 @@ static void monitor_free(void *_data)
*/
static void cmd_open_file_execute(const char *argument)
{
char *filename;
jstring filename;
/* interactive */
if (!argument) {
@ -152,11 +152,11 @@ static void cmd_open_file_execute(const char *argument)
}
/* load the file specified in the argument */
else {
filename = (char *)argument;
filename = argument;
}
if (filename) {
FileOp *fop = fop_to_load_sprite(filename, FILE_LOAD_SEQUENCE_ASK);
if (!filename.empty()) {
FileOp *fop = fop_to_load_sprite(filename.c_str(), FILE_LOAD_SEQUENCE_ASK);
if (fop) {
if (fop->error) {
@ -173,7 +173,7 @@ static void cmd_open_file_execute(const char *argument)
data->thread = thread;
data->alert_window = jalert_new(PACKAGE
"<<Loading file:<<%s||&Cancel",
get_filename(filename));
get_filename(filename.c_str()));
/* add a monitor to check the loading (FileOp) progress */
data->monitor = add_gui_monitor(monitor_openfile_bg,
@ -224,9 +224,6 @@ static void cmd_open_file_execute(const char *argument)
else {
/* do nothing (the user cancelled or something like that) */
}
if (filename != argument)
jfree(filename);
}
}

View File

@ -284,11 +284,9 @@ static void select_all_command(JWidget widget)
static void load_command(JWidget widget)
{
Palette *palette;
char *filename;
filename = ase_file_selector(_("Load Palette"), "", "pcx,bmp,tga,lbm,col");
if (filename) {
palette = palette_load(filename);
jstring filename = ase_file_selector(_("Load Palette"), "", "pcx,bmp,tga,lbm,col");
if (!filename.empty()) {
palette = palette_load(filename.c_str());
if (!palette) {
jalert(_("Error<<Loading palette file||&Close"));
}
@ -296,39 +294,33 @@ static void load_command(JWidget widget)
set_new_palette(palette);
jfree(palette);
}
jfree(filename);
}
}
static void save_command(JWidget widget)
{
char *filename;
jstring filename;
int ret;
again:
filename = ase_file_selector(_("Save Palette"), "", "pcx,bmp,tga,col");
if (filename) {
if (exists(filename)) {
if (!filename.empty()) {
if (exists(filename.c_str())) {
ret = jalert("%s<<%s<<%s||%s",
_("Warning"),
_("File exists, overwrite it?"),
get_filename(filename),
get_filename(filename.c_str()),
_("&Yes||&No||&Cancel"));
if (ret == 2) {
jfree(filename);
if (ret == 2)
goto again;
}
else if (ret != 1) {
jfree(filename);
else if (ret != 1)
return;
}
}
if (palette_save(paledit_get_palette(palette_editor), filename))
if (palette_save(paledit_get_palette(palette_editor),
filename.c_str()))
jalert(_("Error<<Saving palette file||&Close"));
jfree(filename);
}
}

View File

@ -187,28 +187,28 @@ static bool cmd_save_file_as_enabled(const char *argument)
static void cmd_save_file_as_execute(const char *argument)
{
Sprite *sprite = current_sprite;
char filename[4096];
char exts[4096];
char *newfilename;
jstring filename;
jstring newfilename;
int ret;
ustrcpy(filename, sprite->filename);
filename = sprite->filename;
get_writable_extensions(exts, sizeof(exts));
for (;;) {
newfilename = ase_file_selector(_("Save Sprite"), filename, exts);
if (!newfilename)
if (newfilename.empty())
return;
ustrcpy(filename, newfilename);
jfree(newfilename);
filename = newfilename;
/* does the file exist? */
if (exists(filename)) {
if (exists(filename.c_str())) {
/* ask if the user wants overwrite the file? */
ret = jalert("%s<<%s<<%s||%s||%s||%s",
_("Warning"),
_("File exists, overwrite it?"),
get_filename(filename),
get_filename(filename.c_str()),
_("&Yes"), _("&No"), _("&Cancel"));
}
else
@ -224,7 +224,7 @@ static void cmd_save_file_as_execute(const char *argument)
/* "no": we must back to select other file-name */
}
sprite_set_filename(sprite, filename);
sprite_set_filename(sprite, filename.c_str());
app_realloc_sprite_list();
save_sprite_in_background(sprite);

View File

@ -42,20 +42,20 @@ static void cmd_save_mask_execute(const char *argument)
{
/* get current sprite */
Sprite *sprite = current_sprite;
char *filename = "default.msk";
jstring filename = "default.msk";
int ret;
for (;;) {
filename = ase_file_selector(_("Save .msk File"), filename, "msk");
if (!filename)
if (filename.empty())
return;
/* does the file exist? */
if (exists(filename)) {
if (exists(filename.c_str())) {
/* ask if the user wants overwrite the file? */
ret = jalert("%s<<%s<<%s||%s||%s||%s",
_("Warning"), _("File exists, overwrite it?"),
get_filename(filename),
get_filename(filename.c_str()),
_("&Yes"), _("&No"), _("&Cancel"));
}
else
@ -71,10 +71,10 @@ static void cmd_save_mask_execute(const char *argument)
/* "no": we must back to select other file-name */
}
if (save_msk_file(sprite->mask, filename) != 0)
if (save_msk_file(sprite->mask, filename.c_str()) != 0)
jalert("%s<<%s<<%s||%s",
_("Error"), _("Error saving .msk file"),
filename, _("&Close"));
filename.c_str(), _("&Close"));
}
Command cmd_save_mask = {

View File

@ -43,7 +43,7 @@ static void cmd_sprite_properties_execute(const char *argument)
{
JWidget window, killer, name, type, size, frames, speed, ok;
Sprite *sprite = current_sprite;
char *imgtype_text;
jstring imgtype_text;
char buf[256];
/* load the window widget */
@ -82,7 +82,7 @@ static void cmd_sprite_properties_execute(const char *argument)
jwidget_set_text(name, sprite->filename);
/* color mode */
jwidget_set_text(type, imgtype_text);
jwidget_set_text(type, imgtype_text.c_str());
/* sprite size (width and height) */
usprintf(buf, "%dx%d (", sprite->w, sprite->h);

View File

@ -108,7 +108,7 @@ struct FileItem
#endif
};
/* the root of the file-system */
// the root of the file-system
static FileItem *rootitem = NULL;
static HashTable *hash_fileitems = NULL;
static unsigned int current_file_system_version = 0;

View File

@ -278,15 +278,14 @@ static FONT *my_load_font(const char *filename)
static void button_font_command(JWidget widget)
{
char *filename;
jstring filename =
ase_file_selector(_("Open Font (TTF or Allegro bitmap format)"),
get_config_string ("DrawText", "Font", ""),
"pcx,bmp,tga,lbm,ttf");
filename = ase_file_selector(_("Open Font (TTF or Allegro bitmap format)"),
get_config_string ("DrawText", "Font", ""),
"pcx,bmp,tga,lbm,ttf");
if (filename) {
set_config_string("DrawText", "Font", filename);
if (!filename.empty()) {
set_config_string("DrawText", "Font", filename.c_str());
update_button_text();
jfree(filename);
}
}

View File

@ -18,7 +18,9 @@
#include "config.h"
#include <assert.h>
#include <cassert>
#include <vector>
#include <allegro.h>
#include <allegro/internal/aintern.h>
#include <errno.h>
@ -69,9 +71,9 @@ static bool filetype_msg_proc(JWidget widget, JMessage msg);
* - the 'core/file_system' routines.
* - the 'widgets/fileview' widget.
*/
char *ase_file_selector(const char *message,
const char *init_path,
const char *exts)
jstring ase_file_selector(const jstring& message,
const jstring& init_path,
const jstring& exts)
{
static JWidget window = NULL;
FileItem *start_folder = NULL;
@ -79,9 +81,7 @@ char *ase_file_selector(const char *message,
JWidget goback, goforward, goup;
JWidget filename_entry;
JWidget filetype;
char buf[512];
char *result = NULL;
char *tok;
jstring result;
file_system_refresh();
@ -92,44 +92,41 @@ char *ase_file_selector(const char *message,
navigation_history);
}
/* 'buf' will contain the start folder path */
ustrcpy(buf, init_path);
// use the current path
jstring start_folder_path;
/* use the current path */
if (get_filename(buf) == buf) {
char path[512];
// if init_path doesn't contain a path...
if (init_path.filepath().empty()) {
// get the saved `path' in the configuration file
jstring path = get_config_string("FileSelect", "CurrentDirectory", "");
start_folder = get_fileitem_from_path(path.c_str());
/* get saved `path' */
ustrcpy(path, get_config_string("FileSelect", "CurrentDirectory", ""));
start_folder = get_fileitem_from_path(path);
// is the folder find?
if (!start_folder) {
/* if the `path' doesn't exist */
if ((!ugetat(path, 0)) || (!ji_dir_exists(path))) {
/* try to get current `path' */
// if the `path' doesn't exist...
if (path.empty() || (!ji_dir_exists(path.c_str()))) {
// we can get the current `path' from the system
#ifdef HAVE_DRIVES
int drive = _al_getdrive();
#else
int drive = 0;
#endif
_al_getdcwd(drive, path, sizeof(path) - ucwidth(OTHER_PATH_SEPARATOR));
char tmp[1024];
_al_getdcwd(drive, tmp, sizeof(tmp) - ucwidth(OTHER_PATH_SEPARATOR));
path = tmp;
}
fix_filename_case(path);
fix_filename_slashes(path);
put_backslash(path);
ustrcat(path, buf);
ustrcpy(buf, path);
start_folder_path = path / init_path;
}
}
else {
/* remove the filename */
*get_filename(buf) = 0;
// remove the filename
start_folder_path = init_path.filepath() / "";
}
start_folder_path.fix_separators();
if (!start_folder)
start_folder = get_fileitem_from_path(buf);
start_folder = get_fileitem_from_path(start_folder_path.c_str());
if (!window) {
JWidget view, location;
@ -159,7 +156,7 @@ char *ase_file_selector(const char *message,
jbutton_add_command(goup, goup_command);
view = jview_new();
fileview = fileview_new(start_folder, exts);
fileview = fileview_new(start_folder, exts.c_str());
jwidget_add_hook(fileview, -1, fileview_msg_proc, NULL);
jwidget_add_hook(location, -1, location_msg_proc, NULL);
@ -196,20 +193,21 @@ char *ase_file_selector(const char *message,
/* fill file-type combo-box */
jcombobox_clear(filetype);
ustrcpy(buf, exts);
for (tok = ustrtok(buf, ",");
tok != NULL;
tok = ustrtok(NULL, ",")) {
jcombobox_add_string(filetype, tok, NULL);
}
std::vector<jstring> tokens;
std::vector<jstring>::iterator tok;
exts.split(',', tokens);
for (tok=tokens.begin(); tok!=tokens.end(); ++tok)
jcombobox_add_string(filetype, tok->c_str(), NULL);
/* file name entry field */
filename_entry = jwidget_find_name(window, "filename");
jwidget_set_text(filename_entry, get_filename(init_path));
jwidget_set_text(filename_entry, init_path.filename().c_str());
select_filetype_from_filename(window);
/* setup the title of the window */
jwidget_set_text(window, message);
jwidget_set_text(window, message.c_str());
/* get the ok-button */
ok = jwidget_find_name(window, "ok");
@ -221,26 +219,22 @@ char *ase_file_selector(const char *message,
jwindow_open_fg(window);
if (jwindow_get_killer(window) == ok ||
jwindow_get_killer(window) == fileview) {
char *p;
/* open the selected file */
FileItem *folder = fileview_get_current_folder(fileview);
assert(folder != NULL);
ustrcpy(buf, fileitem_get_filename(folder));
put_backslash(buf);
ustrcat(buf, jwidget_get_text(filename_entry));
jstring buf = fileitem_get_filename(folder);
buf /= jwidget_get_text(filename_entry);
/* does it not have extension? ...we should add the extension
selected in the filetype combo-box */
p = get_extension(buf);
if (!p || *p == 0) {
ustrcat(buf, ".");
ustrcat(buf, jcombobox_get_selected_string(filetype));
if (buf.extension().empty()) {
buf += '.';
buf += jcombobox_get_selected_string(filetype);
}
/* duplicate the buffer to return a new string */
result = jstrdup(buf);
result = buf;
/* save the path in the configuration file */
set_config_string("FileSelect", "CurrentDirectory",

View File

@ -19,9 +19,11 @@
#ifndef DIALOGS_FILESEL_H
#define DIALOGS_FILESEL_H
char *ase_file_selector(const char *message,
const char *init_path,
const char *exts);
#include "jinete/jstring.h"
jstring ase_file_selector(const jstring& message,
const jstring& init_path,
const jstring& exts);
#endif /* DIALOGS_FILESEL_H */

View File

@ -62,6 +62,7 @@
#include "jinete/jsep.h"
#include "jinete/jslider.h"
#include "jinete/jstream.h"
#include "jinete/jstring.h"
#include "jinete/jsystem.h"
#include "jinete/jtextbox.h"
#include "jinete/jtheme.h"

142
src/jinete/jstring.cpp Normal file
View File

@ -0,0 +1,142 @@
/* Jinete - a GUI library
* Copyright (C) 2003-2008 David A. Capello.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the Jinete nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <algorithm>
#include "jinete/jstring.h"
#if defined ALLEGRO_WINDOWS
const char jstring::separator = '\\';
#else
const char jstring::separator = '/';
#endif
jstring jstring::filepath() const
{
const_reverse_iterator rit;
jstring res;
for (rit=rbegin(); rit!=rend(); ++rit)
if (is_separator(*rit))
break;
if (rit != rend()) {
++rit;
std::copy(begin(), const_iterator(rit.base()),
std::back_inserter(res));
}
return res;
}
jstring jstring::filename() const
{
const_reverse_iterator rit;
jstring res;
for (rit=rbegin(); rit!=rend(); ++rit)
if (is_separator(*rit))
break;
std::copy(const_iterator(rit.base()), end(),
std::back_inserter(res));
return res;
}
jstring jstring::extension() const
{
const_reverse_iterator rit;
jstring res;
// search for the first dot from the end of the string
for (rit=rbegin(); rit!=rend(); ++rit) {
if (is_separator(*rit))
return res;
else if (*rit == '.')
break;
}
if (rit != rend()) {
std::copy(const_iterator(rit.base()), end(),
std::back_inserter(res));
}
return res;
}
jstring jstring::filetitle() const
{
const_reverse_iterator rit;
const_iterator last_dot = end();
jstring res;
for (rit=rbegin(); rit!=rend(); ++rit) {
if (is_separator(*rit))
break;
else if (*rit == '.' && last_dot == end())
last_dot = rit.base()-1;
}
for (const_iterator it(rit.base()); it!=end(); ++it) {
if (it == last_dot)
break;
else
res.push_back(*it);
}
return res;
}
jstring jstring::operator/(const jstring& component) const
{
jstring res(*this);
res /= component;
return res;
}
/**
* Adds a the file name @a component in the path (this string)
* separated with a slash.
*/
jstring& jstring::operator/=(const jstring& component)
{
if (!empty() && !is_separator(*(end()-1)))
push_back(jstring::separator);
operator+=(component);
return *this;
}
void jstring::fix_separators()
{
std::replace_if(begin(), end(), jstring::is_separator, jstring::separator);
}

119
src/jinete/jstring.h Normal file
View File

@ -0,0 +1,119 @@
/* Jinete - a GUI library
* Copyright (C) 2003-2008 David A. Capello.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the Jinete nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JINETE_STRING_H
#define JINETE_STRING_H
#include <string>
/**
* A string of characters.
*/
class jstring : public std::basic_string<char>
{
public:
static const char separator;
jstring() {
}
explicit jstring(int length)
: std::string(length, static_cast<char>(0))
{
}
jstring(const jstring& str)
: std::string(str)
{
}
jstring(const std::string& str)
: std::string(str)
{
}
jstring(const char* str)
: std::string(str)
{
}
jstring(const char* str, int length)
: std::string(str, length)
{
}
static bool is_separator(char c) {
return (c == '\\' || c == '/');
}
jstring filepath() const;
jstring filename() const;
jstring extension() const;
jstring filetitle() const;
jstring operator/(const jstring& component) const;
jstring& operator/=(const jstring& component);
void fix_separators();
template<typename List>
void split(value_type separator, List& result) const
{
jstring tok;
tok.reserve(size());
for (const_iterator it=begin(); it!=end(); ++it) {
if (*it == separator) {
result.push_back(tok);
tok.clear();
}
else
tok.push_back(*it);
}
}
};
inline jstring operator+(const jstring& _s1, const char* _s2)
{
jstring _res(_s1);
_res.append(jstring(_s2));
return _res;
}
inline jstring operator+(const char* _s1, const jstring& _s2)
{
jstring _res(_s1);
_res.append(_s2);
return _res;
}
#endif /* JINETE_STRING_H */