Remove jstring class replacing it with the new base::string and new functions to manipulate strings.

This commit is contained in:
David Capello 2010-09-30 15:34:12 -03:00
parent 9f000741a3
commit 109d6a072f
29 changed files with 539 additions and 462 deletions

View File

@ -5,4 +5,7 @@ add_library(base-lib
convert_to.cpp
errno_string.cpp
mem_utils.cpp
mutex.cpp)
mutex.cpp
path.cpp
split_string.cpp
string.cpp)

155
src/base/path.cpp Normal file
View File

@ -0,0 +1,155 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/path.h"
#include <algorithm>
using namespace base;
#ifdef WIN32
const string::value_type base::path_separator = '\\';
#else
const string::value_type base::path_separator = '/';
#endif
bool base::is_path_separator(string::value_type chr)
{
return (chr == '\\' || chr == '/');
}
string base::get_file_path(const string& filename)
{
string::const_reverse_iterator rit;
string res;
for (rit=filename.rbegin(); rit!=filename.rend(); ++rit)
if (is_path_separator(*rit))
break;
if (rit != filename.rend()) {
++rit;
std::copy(filename.begin(), string::const_iterator(rit.base()),
std::back_inserter(res));
}
return res;
}
string base::get_file_name(const string& filename)
{
string::const_reverse_iterator rit;
string result;
for (rit=filename.rbegin(); rit!=filename.rend(); ++rit)
if (is_path_separator(*rit))
break;
std::copy(string::const_iterator(rit.base()), filename.end(),
std::back_inserter(result));
return result;
}
string base::get_file_extension(const string& filename)
{
string::const_reverse_iterator rit;
string result;
// search for the first dot from the end of the string
for (rit=filename.rbegin(); rit!=filename.rend(); ++rit) {
if (is_path_separator(*rit))
return result;
else if (*rit == '.')
break;
}
if (rit != filename.rend()) {
std::copy(string::const_iterator(rit.base()), filename.end(),
std::back_inserter(result));
}
return result;
}
string base::get_file_title(const string& filename)
{
string::const_reverse_iterator rit;
string::const_iterator last_dot = filename.end();
string result;
for (rit=filename.rbegin(); rit!=filename.rend(); ++rit) {
if (is_path_separator(*rit))
break;
else if (*rit == '.' && last_dot == filename.end())
last_dot = rit.base()-1;
}
for (string::const_iterator it(rit.base()); it!=filename.end(); ++it) {
if (it == last_dot)
break;
else
result.push_back(*it);
}
return result;
}
string base::join_path(const string& path, const string& file)
{
string result(path);
// Add a separator at the end if it is necessay
if (!result.empty() && !is_path_separator(*(result.end()-1)))
result.push_back(path_separator);
// Add the file
result += file;
return result;
}
string base::remove_path_separator(const string& path)
{
string result(path);
// Erase all trailing separators
while (!result.empty() && is_path_separator(*(result.end()-1)))
result.erase(result.end()-1);
return result;
}
string base::fix_path_separators(const string& filename)
{
string result(filename);
// Replace any separator with the system path separator.
std::replace_if(result.begin(), result.end(),
is_path_separator, path_separator);
return result;
}
bool base::has_file_extension(const string& filename, const string& csv_extensions)
{
if (!filename.empty()) {
string ext = string_to_lower(get_file_extension(filename));
int extsz = ext.size();
string::const_iterator p =
std::search(csv_extensions.begin(),
csv_extensions.end(),
ext.begin(), ext.end());
if ((p != csv_extensions.end()) &&
((p+extsz) == csv_extensions.end() || *(p+extsz) == ',') &&
(p == csv_extensions.begin() || *(p-1) == ','))
return true;
}
return false;
}

49
src/base/path.h Normal file
View File

@ -0,0 +1,49 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_PATH_H_INCLUDED
#define BASE_PATH_H_INCLUDED
#include "base/string.h"
namespace base {
// Default path separator (on Windows it is '\' and on Unix-like systems it is '/').
extern const string::value_type path_separator;
// Returns true if the given character is a valud path separator
// (any of '\' or '/' characters).
bool is_path_separator(string::value_type chr);
// Returns only the path (without the last trailing slash).
string get_file_path(const string& filename);
// Returns the file name with its extension, removing the path.
string get_file_name(const string& filename);
// Returns the extension of the file name (without the dot).
string get_file_extension(const string& filename);
// Returns the file name without path and without extension.
string get_file_title(const string& filename);
// Joins two paths or a path and a file name with a path-separator.
string join_path(const string& path, const string& file);
// Removes the trailing separator from the given path.
string remove_path_separator(const string& path);
// Replaces all separators with the system separator.
string fix_path_separators(const string& filename);
// Returns true if the filename contains one of the specified
// extensions. The cvs_extensions parameter must be a set of
// possible extensions separated by comma.
bool has_file_extension(const string& filename, const string& csv_extensions);
}
#endif

121
src/base/path_unittest.cpp Normal file
View File

@ -0,0 +1,121 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "tests/test.h"
#include "base/path.h"
using namespace base;
TEST(Path, IsPathSeparator)
{
EXPECT_TRUE (is_path_separator('\\'));
EXPECT_TRUE (is_path_separator('/'));
EXPECT_FALSE(is_path_separator('a'));
EXPECT_FALSE(is_path_separator('+'));
EXPECT_FALSE(is_path_separator(':'));
}
TEST(Path, GetFilePath)
{
EXPECT_EQ("C:\\foo", get_file_path("C:\\foo\\main.cpp"));
EXPECT_EQ("C:/foo", get_file_path("C:/foo/pack.tar.gz"));
EXPECT_EQ(".", get_file_path("./main.cpp"));
EXPECT_EQ(".", get_file_path(".\\main.cpp"));
EXPECT_EQ("", get_file_path("\\main.cpp"));
EXPECT_EQ("", get_file_path("main.cpp"));
EXPECT_EQ("", get_file_path("main."));
EXPECT_EQ("", get_file_path("main"));
EXPECT_EQ("C:/foo", get_file_path("C:/foo/"));
EXPECT_EQ("C:", get_file_path("C:\\"));
EXPECT_EQ("C:", get_file_path("C:\\.cpp"));
EXPECT_EQ("", get_file_path(".cpp"));
EXPECT_EQ("", get_file_path(""));
}
TEST(Path, GetFileName)
{
EXPECT_EQ("main.cpp", get_file_name("C:\\foo\\main.cpp"));
EXPECT_EQ("pack.tar.gz", get_file_name("C:/foo/pack.tar.gz"));
EXPECT_EQ("main.cpp", get_file_name("./main.cpp"));
EXPECT_EQ("main.cpp", get_file_name(".\\main.cpp"));
EXPECT_EQ("main.cpp", get_file_name("\\main.cpp"));
EXPECT_EQ("main.cpp", get_file_name("main.cpp"));
EXPECT_EQ("main.", get_file_name("main."));
EXPECT_EQ("main", get_file_name("main"));
EXPECT_EQ("", get_file_name("C:/foo/"));
EXPECT_EQ("", get_file_name("C:\\"));
EXPECT_EQ(".cpp", get_file_name("C:\\.cpp"));
EXPECT_EQ(".cpp", get_file_name(".cpp"));
EXPECT_EQ("", get_file_name(""));
}
TEST(Path, GetFileExtension)
{
EXPECT_EQ("cpp", get_file_extension("C:\\foo\\main.cpp"));
EXPECT_EQ("gz", get_file_extension("C:/foo/pack.tar.gz"));
EXPECT_EQ("cpp", get_file_extension("./main.cpp"));
EXPECT_EQ("cpp", get_file_extension(".\\main.cpp"));
EXPECT_EQ("cpp", get_file_extension("\\main.cpp"));
EXPECT_EQ("cpp", get_file_extension("main.cpp"));
EXPECT_EQ("", get_file_extension("main."));
EXPECT_EQ("", get_file_extension("main"));
EXPECT_EQ("", get_file_extension("C:/foo/"));
EXPECT_EQ("", get_file_extension("C:\\"));
EXPECT_EQ("cpp", get_file_extension("C:\\.cpp"));
EXPECT_EQ("cpp", get_file_extension(".cpp"));
EXPECT_EQ("", get_file_extension(""));
}
TEST(Path, GetFileTitle)
{
EXPECT_EQ("main", get_file_title("C:\\foo\\main.cpp"));
EXPECT_EQ("pack.tar", get_file_title("C:/foo/pack.tar.gz"));
EXPECT_EQ("main", get_file_title("./main.cpp"));
EXPECT_EQ("main", get_file_title(".\\main.cpp"));
EXPECT_EQ("main", get_file_title("\\main.cpp"));
EXPECT_EQ("main", get_file_title("main.cpp"));
EXPECT_EQ("main", get_file_title("main."));
EXPECT_EQ("main", get_file_title("main"));
EXPECT_EQ("", get_file_title("C:/foo/"));
EXPECT_EQ("", get_file_title("C:\\"));
EXPECT_EQ("", get_file_title("C:\\.cpp"));
EXPECT_EQ("", get_file_title(".cpp"));
EXPECT_EQ("", get_file_title(""));
}
TEST(Path, JoinPath)
{
base::string sep;
sep.push_back(path_separator);
EXPECT_EQ("", join_path("", ""));
EXPECT_EQ("fn", join_path("", "fn"));
EXPECT_EQ("/fn", join_path("/", "fn"));
EXPECT_EQ("/this"+sep+"fn", join_path("/this", "fn"));
EXPECT_EQ("C:\\path"+sep+"fn", join_path("C:\\path", "fn"));
EXPECT_EQ("C:\\path\\fn", join_path("C:\\path\\", "fn"));
}
TEST(Path, RemovePathSeparator)
{
EXPECT_EQ("C:\\foo", remove_path_separator("C:\\foo\\"));
EXPECT_EQ("C:/foo", remove_path_separator("C:/foo/"));
EXPECT_EQ("C:\\foo\\main.cpp", remove_path_separator("C:\\foo\\main.cpp"));
EXPECT_EQ("C:\\foo\\main.cpp", remove_path_separator("C:\\foo\\main.cpp/"));
}
TEST(Path, HasFileExtension)
{
EXPECT_TRUE (has_file_extension("hi.png", "png"));
EXPECT_FALSE(has_file_extension("hi.png", "pngg"));
EXPECT_FALSE(has_file_extension("hi.png", "ppng"));
EXPECT_TRUE (has_file_extension("hi.jpeg", "jpg,jpeg"));
EXPECT_TRUE (has_file_extension("hi.jpg", "jpg,jpeg"));
EXPECT_FALSE(has_file_extension("hi.ase", "jpg,jpeg"));
EXPECT_TRUE (has_file_extension("hi.ase", "jpg,jpeg,ase"));
EXPECT_TRUE (has_file_extension("hi.ase", "ase,jpg,jpeg"));
}

54
src/base/split_string.cpp Normal file
View File

@ -0,0 +1,54 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/split_string.h"
#include <algorithm>
namespace {
struct is_separator {
const base::string* separators;
is_separator(const base::string* seps) : separators(seps) {
}
bool operator()(base::string::value_type chr)
{
for (base::string::const_iterator
it = separators->begin(),
end = separators->end(); it != end; ++it) {
if (chr == *it)
return true;
}
return false;
}
};
}
void base::split_string(const base::string& string,
std::vector<base::string>& parts,
const base::string& separators)
{
size_t elements = 1 + std::count_if(string.begin(), string.end(), is_separator(&separators));
parts.reserve(elements);
size_t beg = 0, end;
while (true) {
end = string.find_first_of(separators, beg);
if (end != base::string::npos) {
parts.push_back(string.substr(beg, end - beg));
beg = end+1;
}
else {
parts.push_back(string.substr(beg));
break;
}
}
}

View File

@ -7,50 +7,14 @@
#ifndef BASE_SPLIT_STRING_H_INCLUDED
#define BASE_SPLIT_STRING_H_INCLUDED
#include "base/string.h"
#include <vector>
#include <algorithm>
namespace base {
namespace details {
template<class T>
struct is_separator {
const T* separators;
is_separator(const T* separators) : separators(separators) { }
bool operator()(typename T::value_type chr)
{
for (typename T::const_iterator
it = separators->begin(),
end = separators->end(); it != end; ++it) {
if (chr == *it)
return true;
}
return false;
}
};
}
template<class T>
void split_string(const T& string, std::vector<T>& parts, const T& separators)
{
size_t elements = 1 + std::count_if(string.begin(), string.end(), details::is_separator<T>(&separators));
parts.reserve(elements);
size_t beg = 0, end;
while (true) {
end = string.find_first_of(separators, beg);
if (end != T::npos) {
parts.push_back(string.substr(beg, end - beg));
beg = end+1;
}
else {
parts.push_back(string.substr(beg));
break;
}
}
}
void split_string(const base::string& string,
std::vector<base::string>& parts,
const base::string& separators);
}

View File

@ -13,24 +13,24 @@
TEST(SplitString, Empty)
{
std::vector<std::string> result;
base::split_string<std::string>("", result, ",");
std::vector<base::string> result;
base::split_string("", result, ",");
ASSERT_EQ(1, result.size());
EXPECT_EQ("", result[0]);
}
TEST(SplitString, NoSeparator)
{
std::vector<std::string> result;
base::split_string<std::string>("Hello,World", result, "");
std::vector<base::string> result;
base::split_string("Hello,World", result, "");
ASSERT_EQ(1, result.size());
EXPECT_EQ("Hello,World", result[0]);
}
TEST(SplitString, OneSeparator)
{
std::vector<std::string> result;
base::split_string<std::string>("Hello,World", result, ",");
std::vector<base::string> result;
base::split_string("Hello,World", result, ",");
ASSERT_EQ(2, result.size());
EXPECT_EQ("Hello", result[0]);
EXPECT_EQ("World", result[1]);
@ -38,8 +38,8 @@ TEST(SplitString, OneSeparator)
TEST(SplitString, MultipleSeparators)
{
std::vector<std::string> result;
base::split_string<std::string>("Hello,World", result, ",r");
std::vector<base::string> result;
base::split_string("Hello,World", result, ",r");
ASSERT_EQ(3, result.size());
EXPECT_EQ("Hello", result[0]);
EXPECT_EQ("Wo", result[1]);

32
src/base/string.cpp Normal file
View File

@ -0,0 +1,32 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/string.h"
#include <cctype>
using namespace base;
string base::string_to_lower(const string& original)
{
string result(original);
for (string::iterator it=result.begin(); it!=result.end(); ++it)
*it = std::tolower(*it);
return result;
}
string base::string_to_upper(const string& original)
{
string result(original);
for (string::iterator it=result.begin(); it!=result.end(); ++it)
*it = std::toupper(*it);
return result;
}

View File

@ -11,7 +11,10 @@
namespace base {
typedef std::string string;
typedef std::string string;
string string_to_lower(const string& original);
string string_to_upper(const string& original);
}

View File

@ -59,7 +59,7 @@ CheckArgs::CheckArgs(const std::vector<base::string>& args)
// The following argument should indicate the resolution
// in a format like: 320x240[x8]
std::vector<base::string> parts;
base::split_string<base::string>(args[i], parts, "x");
base::split_string(args[i], parts, "x");
switch (parts.size()) {
case 1:

View File

@ -68,7 +68,7 @@ void LoadMaskCommand::onExecute(Context* context)
{
CurrentSpriteWriter sprite(context);
jstring filename = m_filename;
base::string filename = m_filename;
if (context->is_ui_available()) {
filename = ase_file_selector("Load .msk File", filename, "msk");

View File

@ -338,7 +338,7 @@ static bool window_close_hook(JWidget widget, void *data)
static void load_command(JWidget widget)
{
Palette *palette;
jstring filename = ase_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col");
base::string filename = ase_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col");
if (!filename.empty()) {
palette = Palette::load(filename.c_str());
if (!palette) {
@ -353,7 +353,7 @@ static void load_command(JWidget widget)
static void save_command(JWidget widget)
{
jstring filename;
base::string filename;
int ret;
again:

View File

@ -150,8 +150,8 @@ static void save_sprite_in_background(Sprite* sprite, bool mark_as_saved)
static void save_as_dialog(Sprite* sprite, const char* dlg_title, bool mark_as_saved)
{
char exts[4096];
jstring filename;
jstring newfilename;
base::string filename;
base::string newfilename;
int ret;
filename = sprite->getFilename();
@ -309,7 +309,7 @@ bool SaveFileCopyAsCommand::onEnabled(Context* context)
void SaveFileCopyAsCommand::onExecute(Context* context)
{
CurrentSpriteWriter sprite(context);
jstring old_filename = sprite->getFilename();
base::string old_filename = sprite->getFilename();
// show "Save As" dialog
save_as_dialog(sprite, "Save Sprite Copy As", false);

View File

@ -63,7 +63,7 @@ bool SaveMaskCommand::onEnabled(Context* context)
void SaveMaskCommand::onExecute(Context* context)
{
const CurrentSpriteReader sprite(context);
jstring filename = "default.msk";
base::string filename = "default.msk";
int ret;
for (;;) {

View File

@ -68,7 +68,7 @@ void SpritePropertiesCommand::onExecute(Context* context)
{
JWidget name, type, size, frames, ok;
Button* speed;
jstring imgtype_text;
base::string imgtype_text;
char buf[256];
// Load the window widget

View File

@ -21,7 +21,6 @@
#include <allegro.h>
#include <vector>
#include "gui/jstring.h"
#include "gui/jwindow.h"
#include "app.h"
@ -38,7 +37,7 @@
#ifdef ALLEGRO_WINDOWS
static WNDPROC base_wnd_proc = NULL;
static std::vector<jstring>* dropped_files;
static std::vector<base::string>* dropped_files;
static Mutex* dropped_files_mutex = NULL;
static void subclass_hwnd();
@ -47,7 +46,7 @@ static LRESULT ase_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
void install_drop_files()
{
dropped_files = new std::vector<jstring>();
dropped_files = new std::vector<base::string>();
dropped_files_mutex = new Mutex();
subclass_hwnd();
@ -73,7 +72,7 @@ void check_for_dropped_files()
ScopedLock lock(*dropped_files_mutex);
if (!dropped_files->empty()) {
std::vector<jstring> files = *dropped_files;
std::vector<base::string> files = *dropped_files;
dropped_files->clear();
// open all files
@ -82,7 +81,7 @@ void check_for_dropped_files()
CommandsModule::instance()->get_command_by_name(CommandId::open_file);
Params params;
for (std::vector<jstring>::iterator
for (std::vector<base::string>::iterator
it = files.begin(); it != files.end(); ++it) {
params.set("filename", it->c_str());
UIContext::instance()->execute_command(cmd_open_file, &params);

View File

@ -55,8 +55,7 @@
#include <shlwapi.h>
#endif
#include "gui/jstring.h"
#include "base/path.h"
#include "core/file_system.h"
//////////////////////////////////////////////////////////////////////
@ -100,9 +99,9 @@
class FileItem : public IFileItem
{
public:
jstring keyname;
jstring filename;
jstring displayname;
base::string keyname;
base::string filename;
base::string displayname;
FileItem* parent;
FileItemList children;
unsigned int version;
@ -133,22 +132,22 @@ public:
bool isFolder() const;
bool isBrowsable() const;
jstring getKeyName() const;
jstring getFileName() const;
jstring getDisplayName() const;
base::string getKeyName() const;
base::string getFileName() const;
base::string getDisplayName() const;
IFileItem* getParent() const;
const FileItemList& getChildren();
bool hasExtension(const jstring& csv_extensions);
bool hasExtension(const base::string& csv_extensions);
BITMAP* getThumbnail();
void setThumbnail(BITMAP* thumbnail);
};
typedef std::map<jstring, FileItem*> FileItemMap;
typedef std::map<jstring, BITMAP*> ThumbnailMap;
typedef std::map<base::string, FileItem*> FileItemMap;
typedef std::map<base::string, BITMAP*> ThumbnailMap;
// the root of the file-system
static FileItem* rootitem = NULL;
@ -175,15 +174,15 @@ static unsigned int current_file_system_version = 0;
static LPITEMIDLIST clone_pidl(LPITEMIDLIST pidl);
static LPITEMIDLIST remove_last_pidl(LPITEMIDLIST pidl);
static void free_pidl(LPITEMIDLIST pidl);
static jstring get_key_for_pidl(LPITEMIDLIST pidl);
static base::string get_key_for_pidl(LPITEMIDLIST pidl);
static FileItem* get_fileitem_by_fullpidl(LPITEMIDLIST pidl, bool create_if_not);
static void put_fileitem(FileItem* fileitem);
#else
static FileItem* get_fileitem_by_path(const jstring& path, bool create_if_not);
static FileItem* get_fileitem_by_path(const base::string& path, bool create_if_not);
static void for_each_child_callback(const char *filename, int attrib, int param);
static jstring remove_backslash_if_needed(const jstring& filename);
static jstring get_key_for_filename(const jstring& filename);
static base::string remove_backslash_if_needed(const base::string& filename);
static base::string get_key_for_filename(const base::string& filename);
static void put_fileitem(FileItem* fileitem);
#endif
@ -325,7 +324,7 @@ IFileItem* FileSystemModule::getRootFileItem()
*
* @warning You have to call path.fix_separators() before.
*/
IFileItem* FileSystemModule::getFileItemFromPath(const jstring& path)
IFileItem* FileSystemModule::getFileItemFromPath(const base::string& path)
{
IFileItem* fileitem = NULL;
@ -359,7 +358,7 @@ IFileItem* FileSystemModule::getFileItemFromPath(const jstring& path)
}
#else
{
jstring buf = remove_backslash_if_needed(path);
base::string buf = remove_backslash_if_needed(path);
fileitem = get_fileitem_by_path(buf, true);
}
#endif
@ -369,11 +368,11 @@ IFileItem* FileSystemModule::getFileItemFromPath(const jstring& path)
return fileitem;
}
bool FileSystemModule::dirExists(const jstring& path)
bool FileSystemModule::dirExists(const base::string& path)
{
struct al_ffblk info;
int ret;
jstring path2 = path / "*.*";
base::string path2 = base::join_path(path, "*.*");
ret = al_findfirst(path2.c_str(), &info, FA_ALL);
al_findclose(&info);
@ -396,29 +395,29 @@ bool FileItem::isBrowsable() const
#ifdef USE_PIDLS
return IS_FOLDER(this)
&& (this->filename.extension() != "zip")
&& ((!this->filename.empty() && this->filename.front() != ':') ||
&& (base::get_file_extension(this->filename) != "zip")
&& ((!this->filename.empty() && (*this->filename.begin()) != ':') ||
(this->filename == MYPC_CSLID));
#else
return IS_FOLDER(this);
#endif
}
jstring FileItem::getKeyName() const
base::string FileItem::getKeyName() const
{
ASSERT(this->keyname != NOTINITIALIZED);
return this->keyname;
}
jstring FileItem::getFileName() const
base::string FileItem::getFileName() const
{
ASSERT(this->filename != NOTINITIALIZED);
return this->filename;
}
jstring FileItem::getDisplayName() const
base::string FileItem::getDisplayName() const
{
ASSERT(this->displayname != NOTINITIALIZED);
@ -568,11 +567,11 @@ const FileItemList& FileItem::getChildren()
return this->children;
}
bool FileItem::hasExtension(const jstring& csv_extensions)
bool FileItem::hasExtension(const base::string& csv_extensions)
{
ASSERT(this->filename != NOTINITIALIZED);
return this->filename.has_extension(csv_extensions);
return base::has_file_extension(this->filename, csv_extensions);
}
BITMAP* FileItem::getThumbnail()
@ -878,7 +877,7 @@ static void free_pidl(LPITEMIDLIST pidl)
shl_imalloc->Free(pidl);
}
static jstring get_key_for_pidl(LPITEMIDLIST pidl)
static base::string get_key_for_pidl(LPITEMIDLIST pidl)
{
#if 0
char *key = jmalloc(get_pidl_size(pidl)+1);
@ -1002,7 +1001,7 @@ static void put_fileitem(FileItem* fileitem)
// Allegro for_each_file: Portable
//////////////////////////////////////////////////////////////////////
static FileItem* get_fileitem_by_path(const jstring& path, bool create_if_not)
static FileItem* get_fileitem_by_path(const base::string& path, bool create_if_not)
{
if (path.empty())
return rootitem;
@ -1031,7 +1030,7 @@ static FileItem* get_fileitem_by_path(const jstring& path, bool create_if_not)
// get the parent
{
jstring parent_path = remove_backslash_if_needed(path.filepath() / "");
base::string parent_path = remove_backslash_if_needed(path.filepath() / "");
fileitem->parent = get_fileitem_by_path(parent_path, true);
}
@ -1073,9 +1072,9 @@ static void for_each_child_callback(const char *filename, int attrib, int param)
fileitem->insertChildSorted(child);
}
static jstring remove_backslash_if_needed(const jstring& filename)
static base::string remove_backslash_if_needed(const base::string& filename)
{
if (!filename.empty() && jstring::is_separator(filename.back())) {
if (!filename.empty() && base::string::is_separator(filename.back())) {
int len = filename.size();
#ifdef HAVE_DRIVES
// if the name is C:\ or something like that, the backslash isn't
@ -1087,16 +1086,16 @@ static jstring remove_backslash_if_needed(const jstring& filename)
if (len == 1)
return filename;
#endif
jstring tmp(filename);
base::string tmp(filename);
tmp.remove_separator();
return tmp;
}
return filename;
}
static jstring get_key_for_filename(const jstring& filename)
static base::string get_key_for_filename(const base::string& filename)
{
jstring buf(filename);
base::string buf(filename);
#if !defined CASE_SENSITIVE
buf.tolower();

View File

@ -19,8 +19,7 @@
#ifndef CORE_FILE_SYSTEM_H_INCLUDED
#define CORE_FILE_SYSTEM_H_INCLUDED
#include "gui/jbase.h"
#include "gui/jstring.h"
#include "base/string.h"
#include <vector>
@ -42,9 +41,9 @@ public:
void refresh();
IFileItem* getRootFileItem();
IFileItem* getFileItemFromPath(const jstring& path);
IFileItem* getFileItemFromPath(const base::string& path);
bool dirExists(const jstring& path);
bool dirExists(const base::string& path);
};
@ -56,14 +55,14 @@ public:
virtual bool isFolder() const = 0;
virtual bool isBrowsable() const = 0;
virtual jstring getKeyName() const = 0;
virtual jstring getFileName() const = 0;
virtual jstring getDisplayName() const = 0;
virtual base::string getKeyName() const = 0;
virtual base::string getFileName() const = 0;
virtual base::string getDisplayName() const = 0;
virtual IFileItem* getParent() const = 0;
virtual const FileItemList& getChildren() = 0;
virtual bool hasExtension(const jstring& csv_extensions) = 0;
virtual bool hasExtension(const base::string& csv_extensions) = 0;
virtual BITMAP* getThumbnail() = 0;
virtual void setThumbnail(BITMAP* thumbnail) = 0;

View File

@ -269,7 +269,7 @@ static FONT *my_load_font(const char *filename)
static void button_font_command(JWidget widget)
{
jstring filename =
base::string filename =
ase_file_selector("Open Font (TTF or Allegro bitmap format)",
get_config_string ("DrawText", "Font", ""),
"pcx,bmp,tga,lbm,ttf");

View File

@ -29,12 +29,13 @@
#include <allegro/internal/aintern.h>
#include <errno.h>
#include "base/bind.h"
#include "gui/jinete.h"
#include "app.h"
#include "base/bind.h"
#include "base/path.h"
#include "base/split_string.h"
#include "core/cfg.h"
#include "file/file.h"
#include "gui/jinete.h"
#include "modules/gfx.h"
#include "modules/gui.h"
#include "recent_files.h"
@ -83,15 +84,15 @@ static void on_exit_delete_navigation_history()
* - the 'core/file_system' routines.
* - the 'widgets/fileview' widget.
*/
jstring ase_file_selector(const jstring& message,
const jstring& init_path,
const jstring& exts)
base::string ase_file_selector(const base::string& message,
const base::string& init_path,
const base::string& exts)
{
static Frame* window = NULL;
Widget* fileview;
Widget* filename_entry;
ComboBox* filetype;
jstring result;
base::string result;
FileSystemModule::instance()->refresh();
@ -101,13 +102,13 @@ jstring ase_file_selector(const jstring& message,
}
// we have to find where the user should begin to browse files (start_folder)
jstring start_folder_path;
base::string start_folder_path;
IFileItem* start_folder = NULL;
// if init_path doesn't contain a path...
if (init_path.filepath().empty()) {
if (base::get_file_path(init_path).empty()) {
// get the saved `path' in the configuration file
jstring path = get_config_string("FileSelect", "CurrentDirectory", "");
base::string path = get_config_string("FileSelect", "CurrentDirectory", "");
start_folder = FileSystemModule::instance()->getFileItemFromPath(path);
// is the folder find?
@ -125,14 +126,14 @@ jstring ase_file_selector(const jstring& message,
path = tmp;
}
start_folder_path = path / init_path;
start_folder_path = base::join_path(path, init_path);
}
}
else {
// remove the filename
start_folder_path = init_path.filepath() / "";
start_folder_path = base::join_path(base::get_file_path(init_path), "");
}
start_folder_path.fix_separators();
start_folder_path = base::fix_path_separators(start_folder_path);
if (!start_folder)
start_folder = FileSystemModule::instance()->getFileItemFromPath(start_folder_path);
@ -209,15 +210,15 @@ jstring ase_file_selector(const jstring& message,
// fill file-type combo-box
filetype->removeAllItems();
std::vector<jstring> tokens;
std::vector<jstring>::iterator tok;
std::vector<base::string> tokens;
std::vector<base::string>::iterator tok;
exts.split(',', tokens);
base::split_string(exts, tokens, ",");
for (tok=tokens.begin(); tok!=tokens.end(); ++tok)
filetype->addItem(tok->c_str());
// file name entry field
filename_entry->setText(init_path.filename().c_str());
filename_entry->setText(base::get_file_name(init_path).c_str());
select_filetype_from_filename(window);
jentry_select_text(filename_entry, 0, -1);
@ -239,8 +240,8 @@ again:
IFileItem *folder = fileview_get_current_folder(fileview);
ASSERT(folder);
jstring fn = filename_entry->getText();
jstring buf;
base::string fn = filename_entry->getText();
base::string buf;
IFileItem* enter_folder = NULL;
// up a level?
@ -253,18 +254,18 @@ again:
// check if the user specified in "fn" a item of "fileview"
const FileItemList& children = fileview_get_filelist(fileview);
jstring fn2 = fn;
#ifdef ALLEGRO_WINDOWS
fn2.tolower();
base::string fn2 = fn;
#ifdef WIN32
fn2 = base::string_to_lower(fn2);
#endif
for (FileItemList::const_iterator
it=children.begin(); it!=children.end(); ++it) {
IFileItem* child = *it;
jstring child_name = child->getDisplayName();
base::string child_name = child->getDisplayName();
#ifdef ALLEGRO_WINDOWS
child_name.tolower();
#ifdef WIN32
child_name = base::string_to_lower(child_name);
#endif
if (child_name == fn2) {
enter_folder = *it;
@ -275,26 +276,26 @@ again:
if (!enter_folder) {
// does the file-name entry have separators?
if (jstring::is_separator(fn.front())) { // absolute path (UNIX style)
#ifdef ALLEGRO_WINDOWS
if (base::is_path_separator(*fn.begin())) { // absolute path (UNIX style)
#ifdef WIN32
// get the drive of the current folder
jstring drive = folder->getFileName();
base::string drive = folder->getFileName();
if (drive.size() >= 2 && drive[1] == ':') {
buf += drive[0];
buf += ':';
buf += fn;
}
else
buf = jstring("C:") / fn;
buf = base::join_path("C:", fn);
#else
buf = fn;
#endif
}
#ifdef ALLEGRO_WINDOWS
#ifdef WIN32
// does the file-name entry have colon?
else if (fn.find(':') != jstring::npos) { // absolute path on Windows
else if (fn.find(':') != base::string::npos) { // absolute path on Windows
if (fn.size() == 2 && fn[1] == ':') {
buf = fn / "";
buf = base::join_path(fn, "");
}
else {
buf = fn;
@ -303,9 +304,9 @@ again:
#endif
else {
buf = folder->getFileName();
buf /= fn;
buf = base::join_path(buf, fn);
}
buf.fix_separators();
buf = base::fix_path_separators(buf);
// we can check if 'buf' is a folder, so we have to enter in it
enter_folder = FileSystemModule::instance()->getFileItemFromPath(buf);
@ -335,7 +336,7 @@ again:
// does it not have extension? ...we should add the extension
// selected in the filetype combo-box
if (buf.extension().empty()) {
if (base::get_file_extension(buf).empty()) {
buf += '.';
buf += filetype->getItemText(filetype->getSelectedItem());
}
@ -344,7 +345,7 @@ again:
result = buf;
// save the path in the configuration file
jstring lastpath = folder->getKeyName();
base::string lastpath = folder->getKeyName();
set_config_string("FileSelect", "CurrentDirectory",
lastpath.c_str());
}
@ -386,7 +387,7 @@ static void update_location(JWidget window)
fileitem = reinterpret_cast<IFileItem*>(link->data);
// Indentation
jstring buf;
base::string buf;
for (int c=0; c<level; ++c)
buf += " ";
@ -415,7 +416,7 @@ static void update_location(JWidget window)
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end();
for (; it != end; ++it) {
// Get the path of the recent file
std::string path = jstring(*it).filepath();
base::string path = base::get_file_path(*it);
// Check if the path was not already included in the list
if (included.find(path) == included.end()) {
@ -566,7 +567,7 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
if (!fileitem->isFolder()) {
Frame* window = static_cast<Frame*>(widget->getRoot());
Widget* entry = window->findChild("filename");
jstring filename = fileitem->getFileName().filename();
base::string filename = base::get_file_name(fileitem->getFileName());
entry->setText(filename.c_str());
select_filetype_from_filename(window);
@ -613,7 +614,7 @@ static bool location_msg_proc(JWidget widget, JMessage msg)
// Maybe the user selected a recent file path
if (fileitem == NULL) {
jstring path = combobox->getItemText(itemIndex);
base::string path = combobox->getItemText(itemIndex);
if (!path.empty())
fileitem = FileSystemModule::instance()->getFileItemFromPath(path);
}
@ -677,7 +678,7 @@ static bool filename_msg_proc(JWidget widget, JMessage msg)
}
// String to be autocompleted
jstring left_part = widget->getText();
base::string left_part = widget->getText();
if (left_part.empty())
return false;
@ -689,9 +690,9 @@ static bool filename_msg_proc(JWidget widget, JMessage msg)
for (FileItemList::const_iterator
it=children.begin(); it!=children.end(); ++it) {
IFileItem* child = *it;
jstring child_name = child->getDisplayName();
base::string child_name = child->getDisplayName();
jstring::iterator it1, it2;
base::string::iterator it1, it2;
for (it1 = child_name.begin(), it2 = left_part.begin();
it1!=child_name.end() && it2!=left_part.end();

View File

@ -19,10 +19,10 @@
#ifndef DIALOGS_FILESEL_H_INCLUDED
#define DIALOGS_FILESEL_H_INCLUDED
#include "gui/jstring.h"
#include "base/string.h"
jstring ase_file_selector(const jstring& message,
const jstring& init_path,
const jstring& exts);
base::string ase_file_selector(const base::string& message,
const base::string& init_path,
const base::string& exts);
#endif

View File

@ -36,7 +36,6 @@ add_library(gui-lib
jsep.cpp
jslider.cpp
jstream.cpp
jstring.cpp
jsystem.cpp
jtextbox.cpp
jtheme.cpp

View File

@ -17,7 +17,6 @@
#include "gui/jmessage.h"
#include "gui/jrect.h"
#include "gui/jsystem.h"
#include "gui/jstring.h"
#include "gui/jtheme.h"
#include "gui/jwidget.h"
@ -586,7 +585,7 @@ static void entry_execute_cmd(JWidget widget, EntryCmd::Type cmd,
if (selbeg >= 0) {
// *cut* text!
if (cmd == EntryCmd::Cut) {
jstring buf = text.substr(selbeg, selend - selbeg + 1);
base::string buf = text.substr(selbeg, selend - selbeg + 1);
jclipboard_set_text(buf.c_str());
}
@ -630,7 +629,7 @@ static void entry_execute_cmd(JWidget widget, EntryCmd::Type cmd,
case EntryCmd::Copy:
if (selbeg >= 0) {
jstring buf = text.substr(selbeg, selend - selbeg + 1);
base::string buf = text.substr(selbeg, selend - selbeg + 1);
jclipboard_set_text(buf.c_str());
}
break;

View File

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

View File

@ -1,162 +0,0 @@
// ASE gui library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include <algorithm>
#include <iterator>
#include <cctype>
#include "allegro/base.h"
#include "gui/jstring.h"
#if defined ALLEGRO_WINDOWS
const char jstring::separator = '\\';
#else
const char jstring::separator = '/';
#endif
void jstring::tolower()
{
// std::transform(begin(), end(), std::back_inserter(res), std::tolower);
for (iterator it=begin(); it!=end(); ++it)
*it = std::tolower(*it);
}
void jstring::toupper()
{
for (iterator it=begin(); it!=end(); ++it)
*it = std::toupper(*it);
}
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(back()))
push_back(jstring::separator);
operator+=(component);
return *this;
}
void jstring::remove_separator()
{
while (!empty() && is_separator(back()))
erase(end()-1);
}
void jstring::fix_separators()
{
std::replace_if(begin(), end(), jstring::is_separator, jstring::separator);
}
bool jstring::has_extension(const jstring& csv_extensions) const
{
if (!empty()) {
jstring ext = extension();
ext.tolower();
int extsz = ext.size();
jstring::const_iterator p =
std::search(csv_extensions.begin(),
csv_extensions.end(),
ext.begin(), ext.end());
if ((p != csv_extensions.end()) &&
((p+extsz) == csv_extensions.end() || *(p+extsz) == ',') &&
(p == csv_extensions.begin() || *(p-1) == ','))
return true;
}
return false;
}

View File

@ -1,104 +0,0 @@
// ASE gui library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef GUI_JSTRING_H_INCLUDED
#define GUI_JSTRING_H_INCLUDED
#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)
{
}
char front() const { return *begin(); }
char back() const { return *(end()-1); }
void tolower();
void toupper();
jstring filepath() const;
jstring filename() const;
jstring extension() const;
jstring filetitle() const;
jstring operator/(const jstring& component) const;
jstring& operator/=(const jstring& component);
void remove_separator();
void fix_separators();
bool has_extension(const jstring& csv_extensions) const;
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);
}
if (!tok.empty())
result.push_back(tok);
}
static bool is_separator(char c) {
return (c == '\\' || c == '/');
}
};
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 // ASE_JINETE_STRING_H

View File

@ -1,33 +0,0 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2010 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tests/test.h"
#include "gui/jstring.h"
TEST(JString, has_extension)
{
EXPECT_TRUE (jstring("hi.png").has_extension("png"));
EXPECT_FALSE(jstring("hi.png").has_extension("pngg"));
EXPECT_FALSE(jstring("hi.png").has_extension("ppng"));
EXPECT_TRUE (jstring("hi.jpeg").has_extension("jpg,jpeg"));
EXPECT_TRUE (jstring("hi.jpg").has_extension("jpg,jpeg"));
EXPECT_FALSE(jstring("hi.ase").has_extension("jpg,jpeg"));
EXPECT_TRUE (jstring("hi.ase").has_extension("jpg,jpeg,ase"));
EXPECT_TRUE (jstring("hi.ase").has_extension("ase,jpg,jpeg"));
}

View File

@ -50,7 +50,7 @@ typedef struct FileView
bool req_valid;
int req_w, req_h;
IFileItem* selected;
jstring exts;
base::string exts;
/* incremental-search */
char isearch[256];
@ -88,7 +88,7 @@ static void openfile_bg(void *data);
static void monitor_thumbnail_generation(void *data);
static void monitor_free_thumbnail_generation(void *data);
JWidget fileview_new(IFileItem* start_folder, const jstring& exts)
JWidget fileview_new(IFileItem* start_folder, const base::string& exts)
{
Widget* widget = new Widget(fileview_type());
FileView* fileview = new FileView;

View File

@ -20,7 +20,7 @@
#define WIDGETS_FILEVIEW_H_INCLUDED
#include "gui/jbase.h"
#include "gui/jstring.h"
#include "base/string.h"
#include "core/file_system.h"
/* TODO use some JI_SIGNAL_USER */
@ -28,7 +28,7 @@
#define SIGNAL_FILEVIEW_FILE_ACCEPT 0x10007
#define SIGNAL_FILEVIEW_CURRENT_FOLDER_CHANGED 0x10008
JWidget fileview_new(IFileItem* start_folder, const jstring& exts);
JWidget fileview_new(IFileItem* start_folder, const base::string& exts);
int fileview_type();
IFileItem* fileview_get_current_folder(JWidget fileview);