Further work on ini support

This commit is contained in:
Ingo Ruhnke 2010-08-10 00:18:14 +02:00
parent 38c31d465d
commit cacd778963
6 changed files with 71 additions and 32 deletions

View file

@ -218,9 +218,7 @@ CommandLineParser::init_ini(Options* opts)
("dpad-only", &opts->uinput_config.dpad_only)
("force-feedback", &opts->uinput_config.force_feedback)
("extra-devices", &opts->uinput_config.extra_devices)
// mimic_xpad()
// btn_map
// axis_map
// FIXME: mimic_xpad()
;
m_ini.section("ui-buttonmap", boost::bind(&CommandLineParser::set_ui_buttonmap, this, _1, _2));
@ -277,8 +275,8 @@ CommandLineParser::set_ui_axismap_from_string(const std::string& str)
}
else
{
set_axismap(str.substr(0, i),
str.substr(i+1, str.size()-i));
set_ui_axismap(str.substr(0, i),
str.substr(i+1, str.size()-i));
}
}
@ -789,17 +787,7 @@ CommandLineParser::set_ui_buttonmap(const std::string& name, const std::string&
void
CommandLineParser::set_axismap(const std::string& name, const std::string& value)
{
XboxAxis axis = string2axis(name);
AxisEvent event = AxisEvent::from_string(value);
if (axis != XBOX_AXIS_UNKNOWN)
{
m_options->uinput_config.axis_map[axis] = event;
}
else
{
throw std::runtime_error("Couldn't convert string \"" + value + "\" to ui-axis-mapping");
}
m_options->axis_map.push_back(AxisMapping::from_string(name, value));
}
void

View file

@ -671,7 +671,7 @@ int get_event_type(const std::string& name)
}
else
{
throw std::runtime_error("str2event(): unknown event type prefix: " + name);
throw std::runtime_error("get_event_type(): unknown event type prefix: " + name);
}
}

View file

@ -20,13 +20,6 @@
#include <sstream>
class INIPairSchema
{
public:
virtual ~INIPairSchema() {}
virtual std::string str() const = 0;
};
class INIPairSchemaBool : public INIPairSchema
{
private:
@ -128,7 +121,10 @@ private:
boost::function<void (const std::string&)> m_callback;
public:
INIPairSchemaCallback(boost::function<void (const std::string&)> callback) : m_callback(callback) {}
INIPairSchemaCallback(boost::function<void (const std::string&)> callback) :
m_callback(callback)
{}
void call(const std::string& value)
{
if (m_callback)
@ -142,7 +138,8 @@ public:
}
};
INISchemaSection::INISchemaSection()
INISchemaSection::INISchemaSection(boost::function<void (const std::string&, const std::string&)> callback) :
m_callback(callback)
{
}
@ -204,6 +201,20 @@ INISchemaSection::operator()(const std::string& name, boost::function<void (cons
return *this;
}
INIPairSchema*
INISchemaSection::get(const std::string& name) const
{
Schema::const_iterator i = m_schema.find(name);
if (i == m_schema.end())
{
return 0;
}
else
{
return i->second;
}
}
void
INISchemaSection::save(std::ostream& out)
{
@ -243,15 +254,15 @@ INISchema::section(const std::string& name,
delete i->second;
}
INISchemaSection* section = new INISchemaSection();
INISchemaSection* section = new INISchemaSection(callback);
m_sections.insert(std::pair<std::string, INISchemaSection*>(name, section));
return *section;
}
INISchemaSection*
INISchema::get_section(const std::string& name)
INISchema::get_section(const std::string& name) const
{
Sections::iterator i = m_sections.find(name);
Sections::const_iterator i = m_sections.find(name);
if (i != m_sections.end())
{
return i->second;

View file

@ -23,7 +23,14 @@
#include <string>
#include <boost/function.hpp>
class INIPairSchema;
class INIPairSchema
{
public:
INIPairSchema() {}
virtual ~INIPairSchema() {}
virtual std::string str() const =0;
virtual void call(const std::string& value) =0;
};
class INISchemaSection
{
@ -32,7 +39,10 @@ private:
Schema m_schema;
public:
INISchemaSection();
boost::function<void (const std::string&, const std::string&)> m_callback;
public:
INISchemaSection(boost::function<void (const std::string&, const std::string&)> callback);
~INISchemaSection();
INISchemaSection& operator()(const std::string& name, bool* value);
@ -41,6 +51,8 @@ public:
INISchemaSection& operator()(const std::string& name, std::string* value);
INISchemaSection& operator()(const std::string& name, boost::function<void (const std::string&)> callback);
INIPairSchema* get(const std::string& name) const;
void save(std::ostream& out);
private:
@ -67,7 +79,7 @@ public:
boost::function<void (const std::string&, const std::string&)> callback
= boost::function<void (const std::string&, const std::string&)>());
INISchemaSection* get_section(const std::string& name);
INISchemaSection* get_section(const std::string& name) const;
void save(std::ostream& out);

View file

@ -18,6 +18,8 @@
#include "ini_schema_builder.hpp"
#include "ini_schema.hpp"
INISchemaBuilder::INISchemaBuilder(const INISchema& schema) :
m_schema(schema)
{
@ -26,11 +28,36 @@ INISchemaBuilder::INISchemaBuilder(const INISchema& schema) :
void
INISchemaBuilder::send_section(const std::string& section)
{
m_current_section = section;
}
void
INISchemaBuilder::send_pair(const std::string& name, const std::string& value)
{
INISchemaSection* section = m_schema.get_section(m_current_section);
if (!section)
{
throw std::runtime_error("unknown section: '" + m_current_section + "'");
}
else
{
if (section->m_callback)
{
section->m_callback(name, value);
}
else
{
INIPairSchema* pair = section->get(name);
if (!pair)
{
throw std::runtime_error("unknown name: '" + name + "'");
}
else
{
pair->call(value);
}
}
}
}
/* EOF */

View file

@ -27,6 +27,7 @@ class INISchemaBuilder : public INIBuilder
{
private:
const INISchema& m_schema;
std::string m_current_section;
public:
INISchemaBuilder(const INISchema& schema);