修改配置获取逻辑,更加便于使用,简单测试可用

This commit is contained in:
nnhy 2015-10-15 04:40:19 +00:00
parent c81fbf420d
commit 64331f1e5a
2 changed files with 31 additions and 13 deletions

View File

@ -96,7 +96,7 @@ const ConfigBlock* ConfigBlock::Find(const char* name, bool fAppend) const
} }
// 更新块 // 更新块
bool ConfigBlock::Write(const void* addr, const ByteArray& bs) bool ConfigBlock::Write(Storage* storage, const void* addr, const ByteArray& bs)
{ {
if(bs.Length() > Size) return false; if(bs.Length() > Size) return false;
@ -104,25 +104,27 @@ bool ConfigBlock::Write(const void* addr, const ByteArray& bs)
// 先写入头部,然后写入数据 // 先写入头部,然后写入数据
uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Signature); uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Signature);
rs &= Device->Write((uint)addr, ByteArray((byte*)&Signature, len)); rs &= storage->Write((uint)addr, ByteArray((byte*)&Signature, len));
if(bs.Length() > 0) if(bs.Length() > 0)
rs &= Device->Write((uint)addr + len, ByteArray(bs.GetBuffer(), Size)); rs &= storage->Write((uint)addr + len, ByteArray(bs.GetBuffer(), Size));
return rs; return rs;
} }
// 废弃 // 废弃
bool ConfigBlock::Invalid(const char* name, const void* addr) bool ConfigBlock::Invalid(const char* name, const void* addr, Storage* storage)
{ {
return Set(name, ByteArray(0), addr); return Set(name, ByteArray(0), addr, storage);
} }
// 根据名称更新块 // 根据名称更新块
bool ConfigBlock::Set(const char* name, const ByteArray& bs, const void* addr) const void* ConfigBlock::Set(const char* name, const ByteArray& bs, const void* addr, Storage* storage)
{ {
if(name == NULL) return false; if(name == NULL) return NULL;
if(!addr) addr = BaseAddress; if(!addr) addr = BaseAddress;
if(!storage) storage = Device;
const ConfigBlock* cfg = (const ConfigBlock*)addr; const ConfigBlock* cfg = (const ConfigBlock*)addr;
if(cfg) cfg = cfg->Find(name, true); if(cfg) cfg = cfg->Find(name, true);
@ -131,11 +133,12 @@ bool ConfigBlock::Set(const char* name, const ByteArray& bs, const void* addr)
// 重新搞一个配置头,使用新的数据去重新初始化 // 重新搞一个配置头,使用新的数据去重新初始化
ConfigBlock header; ConfigBlock header;
header.Init(name, bs); header.Init(name, bs);
header.Write(storage, cfg, bs);
return header.Write(cfg, bs); return cfg->Data();
} }
return false; return NULL;
} }
// 获取配置数据 // 获取配置数据
@ -161,6 +164,19 @@ bool ConfigBlock::Get(const char* name, ByteArray& bs, const void* addr)
return false; return false;
} }
const void* ConfigBlock::Get(const char* name, const void* addr)
{
if(name == NULL) return NULL;
if(!addr) addr = BaseAddress;
const ConfigBlock* cfg = (const ConfigBlock*)addr;
if(cfg) cfg = cfg->Find(name, false);
if(cfg) return cfg->Data();
return NULL;
}
// 初始化 // 初始化
TConfig::TConfig() TConfig::TConfig()
{ {

View File

@ -29,14 +29,16 @@ public:
bool Init(const char* name, const ByteArray& bs); bool Init(const char* name, const ByteArray& bs);
const ConfigBlock* Find(const char* name, bool fAppend = false) const; const ConfigBlock* Find(const char* name, bool fAppend = false) const;
bool Write(const void* addr, const ByteArray& bs); bool Write(Storage* storage, const void* addr, const ByteArray& bs);
// 废弃 // 废弃
static bool Invalid(const char* name, const void* addr = NULL); static bool Invalid(const char* name, const void* addr = NULL, Storage* storage = NULL);
// 设置配置数据 // 设置配置数据
static bool Set(const char* name, const ByteArray& bs, const void* addr = NULL); static const void* Set(const char* name, const ByteArray& bs, const void* addr = NULL, Storage* storage = NULL);
// 获取配置数据 // 获取配置数据
static bool Get(const char* name, ByteArray& bs, const void* addr = NULL); static bool Get(const char* name, ByteArray& bs, const void* addr = NULL);
// 获取配置数据
static const void* Get(const char* name, const void* addr = NULL);
}; };
// 必须设定为1字节对齐否则offsetof会得到错误的位置 // 必须设定为1字节对齐否则offsetof会得到错误的位置