配置块,调整查找模式

This commit is contained in:
nnhy 2015-10-22 01:27:17 +00:00
parent 7116f2a986
commit 170c6391c5
2 changed files with 14 additions and 19 deletions

View File

@ -79,25 +79,25 @@ bool ConfigBlock::Init(const char* name, const ByteArray& bs)
//--// //--//
// 循环查找配置块 // 循环查找配置块
const ConfigBlock* ConfigBlock::Find(const char* name, bool fAppend) const const ConfigBlock* ConfigBlock::Find(const char* name, uint addr, bool fAppend)
{ {
const ConfigBlock* ptr = this; const ConfigBlock* cfg = (const ConfigBlock*)addr;
uint slen = strlen(name); uint slen = strlen(name);
assert_param2(slen <= sizeof(ptr->Name), "配置段名称最大4个字符"); assert_param2(slen <= sizeof(cfg->Name), "配置段名称最大4个字符");
// 遍历链表,找到同名块 // 遍历链表,找到同名块
while(ptr->IsGoodBlock()) while(cfg->IsGoodBlock())
{ {
if(ptr->IsGoodData() && name && memcmp(name, ptr->Name, slen) == 0) if(cfg->IsGoodData() && name && memcmp(name, cfg->Name, slen) == 0)
{ {
return ptr; return cfg;
} }
ptr = ptr->Next(); cfg = cfg->Next();
} }
// 如果需要添加,返回最后一个非法块的地址 // 如果需要添加,返回最后一个非法块的地址
return fAppend ? ptr : NULL; return fAppend ? cfg : NULL;
} }
// 更新块 // 更新块
@ -136,9 +136,7 @@ const void* ConfigBlock::Set(const char* name, const ByteArray& bs, uint addr, S
if(!storage) storage = Device; if(!storage) storage = Device;
assert_param2(storage, "未指定配置段的存储设备"); assert_param2(storage, "未指定配置段的存储设备");
const ConfigBlock* cfg = (const ConfigBlock*)addr; const ConfigBlock* cfg = Find(name, addr, true);
if(cfg) cfg = cfg->Find(name, true);
if(cfg) if(cfg)
{ {
// 重新搞一个配置头,使用新的数据去重新初始化 // 重新搞一个配置头,使用新的数据去重新初始化
@ -158,14 +156,12 @@ bool ConfigBlock::Get(const char* name, ByteArray& bs, uint addr)
if(name == NULL) return false; if(name == NULL) return false;
if(!addr) addr = BaseAddress; if(!addr) addr = BaseAddress;
const ConfigBlock* cfg = (const ConfigBlock*)addr; const ConfigBlock* cfg = Find(name, addr, false);
if(cfg) cfg = cfg->Find(name, false);
if(cfg) if(cfg)
{ {
if(cfg->Size <= bs.Capacity()) if(cfg->Size <= bs.Capacity())
{ {
bs.Copy((byte*)cfg->Data(), 0, cfg->Size); bs.Copy(cfg->Data(), 0, cfg->Size);
bs.SetLength(cfg->Size); bs.SetLength(cfg->Size);
return true; return true;
@ -180,9 +176,7 @@ const void* ConfigBlock::Get(const char* name, uint addr)
if(name == NULL) return NULL; if(name == NULL) return NULL;
if(!addr) addr = BaseAddress; if(!addr) addr = BaseAddress;
const ConfigBlock* cfg = (const ConfigBlock*)addr; const ConfigBlock* cfg = Find(name, addr, false);
if(cfg) cfg = cfg->Find(name, false);
if(cfg) return cfg->Data(); if(cfg) return cfg->Data();
return NULL; return NULL;

View File

@ -28,9 +28,10 @@ public:
const void* Data() const; const void* Data() const;
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;
bool Write(Storage* storage, uint addr, const ByteArray& bs); bool Write(Storage* storage, uint addr, const ByteArray& bs);
// 查找
static const ConfigBlock* Find(const char* name, uint addr = NULL, bool fAppend = false);
// 废弃 // 废弃
static bool Invalid(const char* name, uint addr = NULL, Storage* storage = NULL); static bool Invalid(const char* name, uint addr = NULL, Storage* storage = NULL);
// 设置配置数据 // 设置配置数据