Add simpleini to replace Allegro 4 configuration routines

This commit is contained in:
David Capello 2014-10-16 21:27:25 -03:00
parent 432dd33281
commit 2b2d0f34b2
12 changed files with 195 additions and 44 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "third_party/simpleini"]
path = third_party/simpleini
url = git@github.com:aseprite/simpleini.git

View File

@ -88,6 +88,7 @@ set(LIBJPEG_DIR ${CMAKE_SOURCE_DIR}/third_party/jpeg)
set(LIBPNG_DIR ${CMAKE_SOURCE_DIR}/third_party/libpng)
set(LOADPNG_DIR ${CMAKE_SOURCE_DIR}/third_party/loadpng)
set(MONGOOSE_DIR ${CMAKE_SOURCE_DIR}/third_party/mongoose)
set(SIMPLEINI_DIR ${CMAKE_SOURCE_DIR}/third_party/simpleini)
set(TINYXML_DIR ${CMAKE_SOURCE_DIR}/third_party/tinyxml)
set(ZLIB_DIR ${CMAKE_SOURCE_DIR}/third_party/zlib)

View File

@ -25,6 +25,7 @@ endif(MSVC)
set(aseprite_libraries
app-lib
fixmath-lib
cfg-lib
css-lib
doc-lib
raster-lib
@ -134,6 +135,9 @@ else()
include_directories(${LOADPNG_DIR})
endif()
set(libs3rdparty simpleini ${libs3rdparty})
include_directories(${SIMPLEINI_DIR})
######################################################################
# Add C++11 support only for our code (avoid Allegro)
@ -195,6 +199,7 @@ endif()
# Aseprite Libraries (in preferred order to be built)
add_subdirectory(base)
add_subdirectory(cfg)
add_subdirectory(css)
add_subdirectory(doc)
add_subdirectory(filters)

View File

@ -22,7 +22,7 @@ because they don't depend on any other component.
## Level 1
* [cfg](cfg/) (base, allegro): Library to handle configuration/settings/user preferences.
* [cfg](cfg/) (base): Library to load/save .ini files.
* [gen](gen/) (base): Helper utility to generate C++ files from different XMLs.
* [net](net/) (base): Networking library to send HTTP requests.
* [raster](raster/) (base, gfx): Library to handle graphics entities like sprites, images, frames.

View File

@ -1,5 +1,5 @@
/* Aseprite
* Copyright (C) 2001-2013 David Capello
* Copyright (C) 2001-2014 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
@ -197,7 +197,7 @@ private:
}
void onLocateConfigFile() {
app::launcher::open_folder(app::get_config_file());
app::launcher::open_folder(app::main_config_filename());
}
ISettings* m_settings;

View File

@ -1,5 +1,5 @@
/* Aseprite
* Copyright (C) 2001-2013 David Capello
* Copyright (C) 2001-2014 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
@ -29,6 +29,7 @@
#include "app/file/format_options.h"
#include "app/find_widget.h"
#include "app/ini_file.h"
#include "app/ini_file.h"
#include "app/load_widget.h"
#include "base/file_handle.h"
#include "base/memory.h"

View File

@ -1,5 +1,5 @@
/* Aseprite
* Copyright (C) 2001-2013 David Capello
* Copyright (C) 2001-2014 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
@ -23,20 +23,22 @@
#include "app/ini_file.h"
#include "app/resource_finder.h"
#include "base/split_string.h"
#include "base/string.h"
#include "cfg/cfg.h"
#ifndef WIN32
#include "base/fs.h"
#endif
#include <allegro/config.h>
#include <allegro/file.h>
#include <allegro/unicode.h>
#include <vector>
namespace app {
using namespace gfx;
static std::string g_configFilename;
static std::vector<cfg::CfgFile*> g_configs;
ConfigModule::ConfigModule()
{
@ -61,59 +63,118 @@ ConfigModule::ConfigModule()
ConfigModule::~ConfigModule()
{
flush_config_file();
for (auto cfg : g_configs)
delete cfg;
g_configs.clear();
}
std::string get_config_file()
//////////////////////////////////////////////////////////////////////
// Allegro-like API to handle .ini configuration files
void push_config_state()
{
g_configs.push_back(new cfg::CfgFile());
}
void pop_config_state()
{
ASSERT(!g_configs.empty());
delete g_configs.back();
g_configs.erase(--g_configs.end());
}
void flush_config_file()
{
ASSERT(!g_configs.empty());
g_configs.back()->save();
}
void set_config_file(const char* filename)
{
if (g_configs.empty())
g_configs.push_back(new cfg::CfgFile());
g_configs.back()->load(filename);
}
std::string main_config_filename()
{
return g_configFilename;
}
bool get_config_bool(const char *section, const char *name, bool value)
const char* get_config_string(const char* section, const char* name, const char* value)
{
const char *got = get_config_string(section, name, value ? "yes": "no");
return (got &&
(ustricmp(got, "yes") == 0 ||
ustricmp(got, "true") == 0 ||
ustricmp(got, "1") == 0)) ? true: false;
return g_configs.back()->getValue(section, name, value);
}
void set_config_bool(const char *section, const char *name, bool value)
void set_config_string(const char* section, const char* name, const char* value)
{
set_config_string(section, name, value ? "yes": "no");
g_configs.back()->setValue(section, name, value);
}
Rect get_config_rect(const char *section, const char *name, const Rect& rect)
int get_config_int(const char* section, const char* name, int value)
{
return g_configs.back()->getIntValue(section, name, value);
}
void set_config_int(const char* section, const char* name, int value)
{
g_configs.back()->setIntValue(section, name, value);
}
float get_config_float(const char* section, const char* name, float value)
{
return g_configs.back()->getDoubleValue(section, name, value);
}
void set_config_float(const char* section, const char* name, float value)
{
g_configs.back()->setDoubleValue(section, name, value);
}
bool get_config_bool(const char* section, const char* name, bool value)
{
return g_configs.back()->getBoolValue(section, name, value);
}
void set_config_bool(const char* section, const char* name, bool value)
{
g_configs.back()->setBoolValue(section, name, value);
}
Rect get_config_rect(const char* section, const char* name, const Rect& rect)
{
Rect rect2(rect);
char **argv;
int argc;
argv = get_config_argv(section, name, &argc);
if (argv && argc == 4) {
rect2.x = ustrtol(argv[0], NULL, 10);
rect2.y = ustrtol(argv[1], NULL, 10);
rect2.w = ustrtol(argv[2], NULL, 10);
rect2.h = ustrtol(argv[3], NULL, 10);
const char* value = get_config_string(section, name, "");
if (value) {
std::vector<std::string> parts;
base::split_string(value, parts, " ");
if (parts.size() == 4) {
rect2.x = strtol(parts[0].c_str(), NULL, 10);
rect2.y = strtol(parts[1].c_str(), NULL, 10);
rect2.w = strtol(parts[2].c_str(), NULL, 10);
rect2.h = strtol(parts[3].c_str(), NULL, 10);
}
}
return rect2;
}
void set_config_rect(const char *section, const char *name, const Rect& rect)
void set_config_rect(const char* section, const char* name, const Rect& rect)
{
char buf[128];
uszprintf(buf, sizeof(buf), "%d %d %d %d",
rect.x, rect.y, rect.w, rect.h);
sprintf(buf, "%d %d %d %d", rect.x, rect.y, rect.w, rect.h);
set_config_string(section, name, buf);
}
app::Color get_config_color(const char *section, const char *name, const app::Color& value)
app::Color get_config_color(const char* section, const char* name, const app::Color& value)
{
return app::Color::fromString(get_config_string(section, name, value.toString().c_str()));
}
void set_config_color(const char *section, const char *name, const app::Color& value)
void set_config_color(const char* section, const char* name, const app::Color& value)
{
set_config_string(section, name, value.toString().c_str());
}

View File

@ -20,7 +20,6 @@
#define APP_INI_FILE_H_INCLUDED
#pragma once
#include <allegro/config.h>
#include "gfx/rect.h"
#include "app/color.h"
@ -32,16 +31,30 @@ namespace app {
~ConfigModule();
};
std::string get_config_file();
void push_config_state();
void pop_config_state();
void flush_config_file();
void set_config_file(const char* filename);
bool get_config_bool(const char *section, const char *name, bool value);
void set_config_bool(const char *section, const char *name, bool value);
std::string main_config_filename();
gfx::Rect get_config_rect(const char *section, const char *name, const gfx::Rect& rect);
void set_config_rect(const char *section, const char *name, const gfx::Rect& rect);
const char* get_config_string(const char* section, const char* name, const char* value);
void set_config_string(const char* section, const char* name, const char* value);
app::Color get_config_color(const char *section, const char *name, const app::Color& value);
void set_config_color(const char *section, const char *name, const app::Color& value);
int get_config_int(const char* section, const char* name, int value);
void set_config_int(const char* section, const char* name, int value);
float get_config_float(const char* section, const char* name, float value);
void set_config_float(const char* section, const char* name, float value);
bool get_config_bool(const char* section, const char* name, bool value);
void set_config_bool(const char* section, const char* name, bool value);
gfx::Rect get_config_rect(const char* section, const char* name, const gfx::Rect& rect);
void set_config_rect(const char* section, const char* name, const gfx::Rect& rect);
app::Color get_config_color(const char* section, const char* name, const app::Color& value);
void set_config_color(const char* section, const char* name, const app::Color& value);
} // namespace app

View File

@ -0,0 +1,65 @@
/* Aseprite
* Copyright (C) 2001-2014 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 "app/ini_file.h"
#include "base/fs.h"
using namespace app;
TEST(IniFile, Basic)
{
if (base::is_file("_test.ini"))
base::delete_file("_test.ini");
set_config_file("_test.ini");
EXPECT_EQ(false, get_config_bool("A", "a", false));
EXPECT_EQ(true, get_config_bool("A", "b", true));
EXPECT_EQ(0, get_config_bool("B", "a", 0));
EXPECT_EQ(1, get_config_bool("B", "b", 1));
set_config_bool("A", "a", true);
set_config_bool("A", "b", false);
set_config_int("B", "a", 2);
set_config_int("B", "b", 3);
EXPECT_EQ(true, get_config_bool("A", "a", false));
EXPECT_EQ(false, get_config_bool("A", "b", true));
EXPECT_EQ(2, get_config_int("B", "a", 0));
EXPECT_EQ(3, get_config_int("B", "b", 1));
}
TEST(IniFile, PushPop)
{
if (base::is_file("_a.ini")) base::delete_file("_a.ini");
if (base::is_file("_b.ini")) base::delete_file("_b.ini");
set_config_file("_a.ini");
set_config_int("A", "a", 32);
push_config_state();
set_config_file("_b.ini");
set_config_int("A", "a", 64);
EXPECT_EQ(64, get_config_int("A", "a", 0));
pop_config_state();
EXPECT_EQ(32, get_config_int("A", "a", 0));
}

View File

@ -1,5 +1,5 @@
/* Aseprite
* Copyright (C) 2001-2013 David Capello
* Copyright (C) 2001-2014 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
@ -31,7 +31,6 @@
#include "app/app.h"
#include "app/color_utils.h"
#include "app/console.h"
#include "app/ini_file.h"
#include "app/modules/gfx.h"
#include "app/modules/gui.h"
#include "app/modules/palettes.h"

View File

@ -43,3 +43,5 @@ endif()
if(ENABLE_WEBSERVER)
add_subdirectory(mongoose)
endif()
add_subdirectory(simpleini)

1
third_party/simpleini vendored Submodule

@ -0,0 +1 @@
Subproject commit d4a436a8adfbc7f611313803d776e6c1f48dad91