* Add functions for constructing a settings file from messages and settings

templates.
* Prepare saving of such generated config files. Actually writing them out isn't
  yet done however.
* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42805 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz 2011-10-07 10:15:57 +00:00
parent fe8708f308
commit 4890c4a265
2 changed files with 200 additions and 9 deletions

View File

@ -14,6 +14,7 @@
#include <fs_interface.h>
#include <Path.h>
#include <PathMonitor.h>
#include <String.h>
#include <stdio.h>
#include <stdlib.h>
@ -214,7 +215,7 @@ Settings::_ConvertFromDriverParameter(const driver_parameter& parameter,
for (int32 j = 0; j < parameter.parameter_count; j++) {
status = _ConvertFromDriverParameter(parameter.parameters[j],
settingsTemplate->sub_template, subMessage);
if (status < B_OK)
if (status != B_OK)
break;
const settings_template* parentValueTemplate
@ -245,7 +246,7 @@ Settings::_ConvertFromDriverSettings(const driver_settings& settings,
// ignore unknown entries
continue;
}
if (status < B_OK)
if (status != B_OK)
return status;
}
@ -259,7 +260,7 @@ Settings::_ConvertFromDriverSettings(const char* name,
{
BPath path;
status_t status = _GetPath(name, path);
if (status < B_OK)
if (status != B_OK)
return status;
void* handle = load_driver_settings(path.Path());
@ -277,24 +278,164 @@ Settings::_ConvertFromDriverSettings(const char* name,
}
status_t
Settings::_AppendSettings(const settings_template* settingsTemplate,
BString& settings, const BMessage& message, const char* name,
type_code type, int32 count, const char* settingName)
{
const settings_template* valueTemplate
= _FindSettingsTemplate(settingsTemplate, name);
if (valueTemplate == NULL) {
fprintf(stderr, "unknown field %s\n", name);
return B_BAD_VALUE;
}
if (valueTemplate->type != type) {
fprintf(stderr, "field type mismatch %s\n", name);
return B_BAD_VALUE;
}
if (settingName == NULL)
settingName = name;
if (type != B_MESSAGE_TYPE) {
settings.Append("\n");
settings.Append(settingName);
settings.Append("\t");
}
for (int32 valueIndex = 0; valueIndex < count; valueIndex++) {
if (valueIndex > 0 && type != B_MESSAGE_TYPE)
settings.Append(" ");
switch (type) {
case B_BOOL_TYPE:
{
bool value;
status_t result = message.FindBool(name, valueIndex, &value);
if (result != B_OK)
return result;
settings.Append(value ? "true" : "false");
break;
}
case B_STRING_TYPE:
{
const char* value = NULL;
status_t result = message.FindString(name, valueIndex, &value);
if (result != B_OK)
return result;
settings.Append(value);
break;
}
case B_INT32_TYPE:
{
int32 value;
status_t result = message.FindInt32(name, valueIndex, &value);
if (result != B_OK)
return result;
char buffer[100];
snprintf(buffer, sizeof(buffer), "%"B_PRId32, value);
settings.Append(buffer, sizeof(buffer));
break;
}
case B_MESSAGE_TYPE:
{
BMessage subMessage;
status_t result = message.FindMessage(name, valueIndex,
&subMessage);
if (result != B_OK)
return result;
const settings_template* parentValueTemplate
= _FindParentValueTemplate(valueTemplate);
if (parentValueTemplate != NULL) {
_AppendSettings(valueTemplate->sub_template, settings,
subMessage, parentValueTemplate->name,
parentValueTemplate->type, 1, name);
subMessage.RemoveName(parentValueTemplate->name);
}
BString subSettings;
_ConvertToDriverSettings(valueTemplate->sub_template,
subSettings, subMessage);
subSettings.ReplaceAll("\n", "\n\t");
subSettings.RemoveFirst("\n");
settings.Append(" {\n");
settings.Append(subSettings);
settings.Append("\n}");
}
}
}
return B_OK;
}
status_t
Settings::_ConvertToDriverSettings(const settings_template* settingsTemplate,
BString& settings, const BMessage& message)
{
int32 index = 0;
char *name = NULL;
type_code type;
int32 count = 0;
while (message.GetInfo(B_ANY_TYPE, index++, &name, &type, &count) == B_OK) {
status_t result = _AppendSettings(settingsTemplate, settings, message,
name, type, count);
if (result != B_OK)
return result;
}
return B_OK;
}
status_t
Settings::_ConvertToDriverSettings(const char* name,
const settings_template* settingsTemplate, const BMessage& message)
{
BPath path;
status_t status = _GetPath(name, path);
if (status != B_OK)
return status;
BString settings;
status = _ConvertToDriverSettings(settingsTemplate, settings, message);
if (status == B_OK) {
settings.RemoveFirst("\n");
// TODO: actually write the settings.String() out into the file
}
return status;
}
status_t
Settings::_Load(const char* name, uint32* _type)
{
status_t status = B_ENTRY_NOT_FOUND;
if (name == NULL || !strcmp(name, "interfaces")) {
if (name == NULL || strcmp(name, "interfaces") == 0) {
status = _ConvertFromDriverSettings("interfaces", kInterfacesTemplate,
fInterfaces);
if (status == B_OK && _type != NULL)
*_type = kMsgInterfaceSettingsUpdated;
}
if (name == NULL || !strcmp(name, "wireless_networks")) {
if (name == NULL || strcmp(name, "wireless_networks") == 0) {
status = _ConvertFromDriverSettings("wireless_networks",
kNetworksTemplate, fNetworks);
if (status == B_OK && _type != NULL)
*_type = kMsgInterfaceSettingsUpdated;
}
if (name == NULL || !strcmp(name, "services")) {
if (name == NULL || strcmp(name, "services") == 0) {
status = _ConvertFromDriverSettings("services", kServicesTemplate,
fServices);
if (status == B_OK && _type != NULL)
@ -305,12 +446,34 @@ Settings::_Load(const char* name, uint32* _type)
}
status_t
Settings::_Save(const char* name)
{
status_t status = B_ENTRY_NOT_FOUND;
if (name == NULL || strcmp(name, "interfaces") == 0) {
status = _ConvertToDriverSettings("interfaces", kInterfacesTemplate,
fInterfaces);
}
if (name == NULL || strcmp(name, "wireless_networks") == 0) {
status = _ConvertToDriverSettings("wireless_networks",
kNetworksTemplate, fNetworks);
}
if (name == NULL || strcmp(name, "services") == 0) {
status = _ConvertToDriverSettings("services", kServicesTemplate,
fServices);
}
return status;
}
status_t
Settings::_StartWatching(const char* name, const BMessenger& target)
{
BPath path;
status_t status = _GetPath(name, path);
if (status < B_OK)
if (status != B_OK)
return status;
return BPrivate::BPathMonitor::StartWatching(path.Path(), B_WATCH_STAT,
@ -352,8 +515,8 @@ Settings::Update(BMessage* message)
{
const char* pathName;
int32 opcode;
if (message->FindInt32("opcode", &opcode) < B_OK
|| message->FindString("path", &pathName) < B_OK)
if (message->FindInt32("opcode", &opcode) != B_OK
|| message->FindString("path", &pathName) != B_OK)
return B_BAD_VALUE;
BPath settingsFolderPath;
@ -412,6 +575,17 @@ Settings::GetNextNetwork(uint32& cookie, BMessage& network)
}
status_t
Settings::AddNetwork(const BMessage& network)
{
status_t result = fNetworks.AddMessage("network", &network);
if (result != B_OK)
return result;
return _Save("wireless_networks");
}
status_t
Settings::GetNextService(uint32& cookie, BMessage& service)
{

View File

@ -25,8 +25,11 @@ public:
status_t GetNextInterface(uint32& cookie,
BMessage& interface);
status_t GetNextNetwork(uint32& cookie,
BMessage& network);
status_t AddNetwork(const BMessage& network);
status_t GetNextService(uint32& cookie,
BMessage& service);
const BMessage& Services() const;
@ -39,6 +42,7 @@ public:
private:
status_t _Load(const char* name = NULL,
uint32* _type = NULL);
status_t _Save(const char* name = NULL);
status_t _GetPath(const char* name, BPath& path);
status_t _StartWatching(const char* name,
@ -64,6 +68,19 @@ private:
const settings_template* settingsTemplate,
BMessage& message);
status_t _AppendSettings(
const settings_template* settingsTemplate,
BString& settings, const BMessage& message,
const char* name, type_code type,
int32 count,
const char* settingName = NULL);
status_t _ConvertToDriverSettings(
const settings_template* settingsTemplate,
BString& settings, const BMessage& message);
status_t _ConvertToDriverSettings(const char* path,
const settings_template* settingsTemplate,
const BMessage& message);
bool _IsWatching(const BMessenger& target) const
{ return fListener == target; }
bool _IsWatching() const