使用字符串替代字符指针操作,未测试
This commit is contained in:
parent
a37cbd0f20
commit
bef7df0d7c
54
Config.cpp
54
Config.cpp
|
@ -29,7 +29,7 @@ struct ConfigBlock
|
||||||
const void* Data() const;
|
const void* Data() const;
|
||||||
uint CopyTo(Buffer& bs) const;
|
uint CopyTo(Buffer& bs) const;
|
||||||
|
|
||||||
bool Init(const char* name, const Buffer& bs);
|
bool Init(const String& name, const Buffer& bs);
|
||||||
bool Write(const Storage& storage, uint addr, const Buffer& bs);
|
bool Write(const Storage& storage, uint addr, const Buffer& bs);
|
||||||
bool Remove(const Storage& storage, uint addr);
|
bool Remove(const Storage& storage, uint addr);
|
||||||
};
|
};
|
||||||
|
@ -73,28 +73,19 @@ uint ConfigBlock::CopyTo(Buffer& bs) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造一个新的配置块
|
// 构造一个新的配置块
|
||||||
bool ConfigBlock::Init(const char* name, const Buffer& bs)
|
bool ConfigBlock::Init(const String& name, const Buffer& bs)
|
||||||
{
|
{
|
||||||
if(name == nullptr) return false;
|
if(!name) return false;
|
||||||
//assert_param2(name, "配置块名称不能为空");
|
|
||||||
|
|
||||||
TS("ConfigBlock::Init");
|
TS("ConfigBlock::Init");
|
||||||
|
|
||||||
uint slen = strlen(name);
|
if(name.Length() > sizeof(Name)) return false;
|
||||||
if(slen > sizeof(Name)) return false;
|
|
||||||
|
|
||||||
if(slen > ArrayLength(Name)) slen = ArrayLength(Name);
|
|
||||||
|
|
||||||
Buffer ns(Name, ArrayLength(Name));
|
|
||||||
// 无论如何,把整个名称区域清空
|
|
||||||
ns.Clear();
|
|
||||||
|
|
||||||
// 配置块的大小,只有第一次能够修改,以后即使废弃也不能修改,仅仅清空名称
|
// 配置块的大小,只有第一次能够修改,以后即使废弃也不能修改,仅仅清空名称
|
||||||
if(bs.Length() > 0)
|
if(bs.Length() > 0)
|
||||||
{
|
{
|
||||||
Size = bs.Length();
|
Size = bs.Length();
|
||||||
ns.SetLength(slen);
|
name.CopyTo(0, Name, -1);
|
||||||
ns = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Hash = GetHash();
|
Hash = GetHash();
|
||||||
|
@ -169,26 +160,23 @@ bool CheckSignature(const Storage& st, uint& addr, bool create)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 循环查找配置块
|
// 循环查找配置块
|
||||||
const ConfigBlock* FindBlock(const Storage& st, uint addr, const char* name)
|
const ConfigBlock* FindBlock(const Storage& st, uint addr, const String& name)
|
||||||
{
|
{
|
||||||
TS("Config::Find");
|
TS("Config::Find");
|
||||||
|
|
||||||
if(name == nullptr) return nullptr;
|
if(!name) return nullptr;
|
||||||
//assert_param2(name, "配置段名称不能为空");
|
|
||||||
|
|
||||||
if(!CheckSignature(st, addr, false)) return nullptr;
|
if(!CheckSignature(st, addr, false)) return nullptr;
|
||||||
|
|
||||||
// 第一个配置块
|
// 第一个配置块
|
||||||
auto cfg = (const ConfigBlock*)addr;
|
auto cfg = (const ConfigBlock*)addr;
|
||||||
|
|
||||||
uint slen = strlen(name);
|
if(name.Length() > sizeof(cfg->Name)) return nullptr;
|
||||||
if(slen > sizeof(cfg->Name)) return nullptr;
|
|
||||||
//assert_param2(slen <= sizeof(cfg->Name), "配置段名称最大4个字符");
|
|
||||||
|
|
||||||
// 遍历链表,找到同名块
|
// 遍历链表,找到同名块
|
||||||
while(cfg->Valid())
|
while(cfg->Valid())
|
||||||
{
|
{
|
||||||
if(cfg->Name[0] && memcmp(name, cfg->Name, slen) == 0) return cfg;
|
if(cfg->Name[0] && name == cfg->Name) return cfg;
|
||||||
|
|
||||||
cfg = cfg->Next();
|
cfg = cfg->Next();
|
||||||
}
|
}
|
||||||
|
@ -218,7 +206,7 @@ const ConfigBlock* NewBlock(const Storage& st, uint addr, int size)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 循环查找配置块
|
// 循环查找配置块
|
||||||
const void* Config::Find(const char* name) const
|
const void* Config::Find(const String& name) const
|
||||||
{
|
{
|
||||||
return FindBlock(Device, Address, name);
|
return FindBlock(Device, Address, name);
|
||||||
}
|
}
|
||||||
|
@ -240,12 +228,12 @@ const void* Config::New(int size) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除
|
// 删除
|
||||||
bool Config::Remove(const char* name) const
|
bool Config::Remove(const String& name) const
|
||||||
{
|
{
|
||||||
//return Set(name, ByteArray(0));
|
//return Set(name, ByteArray(0));
|
||||||
TS("Config::Remove");
|
TS("Config::Remove");
|
||||||
|
|
||||||
if(name == nullptr) return nullptr;
|
if(!name) return false;
|
||||||
|
|
||||||
auto cfg = FindBlock(Device, Address, name);
|
auto cfg = FindBlock(Device, Address, name);
|
||||||
if(!cfg) return false;
|
if(!cfg) return false;
|
||||||
|
@ -258,13 +246,11 @@ bool Config::Remove(const char* name) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据名称更新块
|
// 根据名称更新块
|
||||||
const void* Config::Set(const char* name, const Buffer& bs) const
|
const void* Config::Set(const String& name, const Buffer& bs) const
|
||||||
{
|
{
|
||||||
TS("Config::Set");
|
TS("Config::Set");
|
||||||
|
|
||||||
if(name == nullptr) return nullptr;
|
if(!name) return nullptr;
|
||||||
//assert_param2(name, "配置块名称不能为空");
|
|
||||||
//assert_param2(Device, "未指定配置段的存储设备");
|
|
||||||
|
|
||||||
auto cfg = FindBlock(Device, Address, name);
|
auto cfg = FindBlock(Device, Address, name);
|
||||||
if(!cfg) cfg = NewBlock(Device, Address, bs.Length());
|
if(!cfg) cfg = NewBlock(Device, Address, bs.Length());
|
||||||
|
@ -279,12 +265,11 @@ const void* Config::Set(const char* name, const Buffer& bs) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取配置数据
|
// 获取配置数据
|
||||||
bool Config::Get(const char* name, Buffer& bs) const
|
bool Config::Get(const String& name, Buffer& bs) const
|
||||||
{
|
{
|
||||||
TS("Config::Get");
|
TS("Config::Get");
|
||||||
|
|
||||||
if(name == nullptr) return false;
|
if(!name) return false;
|
||||||
//assert_param2(name, "配置块名称不能为空");
|
|
||||||
|
|
||||||
auto cfg = FindBlock(Device, Address, name);
|
auto cfg = FindBlock(Device, Address, name);
|
||||||
if(!cfg) return false;
|
if(!cfg) return false;
|
||||||
|
@ -292,12 +277,11 @@ bool Config::Get(const char* name, Buffer& bs) const
|
||||||
return cfg->CopyTo(bs) > 0;
|
return cfg->CopyTo(bs) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const void* Config::Get(const char* name) const
|
const void* Config::Get(const String& name) const
|
||||||
{
|
{
|
||||||
TS("Config::GetByName");
|
TS("Config::GetByName");
|
||||||
|
|
||||||
if(name == nullptr) return nullptr;
|
if(!name) return nullptr;
|
||||||
//assert_param2(name, "配置块名称不能为空");
|
|
||||||
|
|
||||||
auto cfg = FindBlock(Device, Address, name);
|
auto cfg = FindBlock(Device, Address, name);
|
||||||
if(cfg && cfg->Size) return cfg->Data();
|
if(cfg && cfg->Size) return cfg->Data();
|
||||||
|
@ -306,7 +290,7 @@ const void* Config::Get(const char* name) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/*// 获取配置数据,如果不存在则覆盖
|
/*// 获取配置数据,如果不存在则覆盖
|
||||||
bool Config::GetOrSet(const char* name, Buffer& bs) const
|
bool Config::GetOrSet(const String& name, Buffer& bs) const
|
||||||
{
|
{
|
||||||
TS("Config::GetOrSet");
|
TS("Config::GetOrSet");
|
||||||
|
|
||||||
|
|
10
Config.h
10
Config.h
|
@ -19,19 +19,19 @@ public:
|
||||||
Config(const Storage& st, uint addr, uint size);
|
Config(const Storage& st, uint addr, uint size);
|
||||||
|
|
||||||
// 查找。size不为0时表示要查找该大小的合适配置块
|
// 查找。size不为0时表示要查找该大小的合适配置块
|
||||||
const void* Find(const char* name) const;
|
const void* Find(const String& name) const;
|
||||||
// 创建一个指定大小的配置块
|
// 创建一个指定大小的配置块
|
||||||
const void* New(int size) const;
|
const void* New(int size) const;
|
||||||
// 删除。仅清空名称,并不删除数据区
|
// 删除。仅清空名称,并不删除数据区
|
||||||
bool Remove(const char* name) const;
|
bool Remove(const String& name) const;
|
||||||
// 设置配置数据
|
// 设置配置数据
|
||||||
const void* Set(const char* name, const Buffer& bs) const;
|
const void* Set(const String& name, const Buffer& bs) const;
|
||||||
// 获取配置数据
|
// 获取配置数据
|
||||||
bool Get(const char* name, Buffer& bs) const;
|
bool Get(const String& name, Buffer& bs) const;
|
||||||
// 获取配置数据,如果不存在则覆盖
|
// 获取配置数据,如果不存在则覆盖
|
||||||
//bool GetOrSet(const char* name, Buffer& bs) const;
|
//bool GetOrSet(const char* name, Buffer& bs) const;
|
||||||
// 获取配置数据
|
// 获取配置数据
|
||||||
const void* Get(const char* name) const;
|
const void* Get(const String& name) const;
|
||||||
|
|
||||||
// 当前
|
// 当前
|
||||||
static const Config* Current;
|
static const Config* Current;
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#include "Platform\stm32.h"
|
#include "Platform\stm32.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// 仅用于调试使用的一些函数实现,RTM不需要
|
// 仅用于调试使用的一些函数实现,RTM不需要
|
||||||
|
|
||||||
#define MEM_DEBUG DEBUG
|
#define MEM_DEBUG DEBUG
|
||||||
|
@ -313,6 +315,7 @@ void ShowFault(uint exception)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
#if defined ( __CC_ARM )
|
||||||
# include <rw/_defs.h>
|
# include <rw/_defs.h>
|
||||||
|
|
||||||
// 发行版不允许抛出异常以及显示异常信息,这将极大减小使用C++标准库所带来的固件膨胀
|
// 发行版不允许抛出异常以及显示异常信息,这将极大减小使用C++标准库所带来的固件膨胀
|
||||||
|
@ -324,5 +327,6 @@ void _RWSTD_EXPORT __rw_throw (int, ...)
|
||||||
}
|
}
|
||||||
|
|
||||||
_RWSTD_NAMESPACE_END // __rw
|
_RWSTD_NAMESPACE_END // __rw
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -44,9 +44,9 @@ public:
|
||||||
virtual void Register(TransportHandler handler, void* param = nullptr);
|
virtual void Register(TransportHandler handler, void* param = nullptr);
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
virtual const char* ToString() const { return "ITransport"; }
|
virtual String ToString() const { return String("ITransport"); }
|
||||||
#else
|
#else
|
||||||
virtual const char* ToString() const { return ""; }
|
virtual String ToString() const { return String(""); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -76,7 +76,7 @@ public:
|
||||||
|
|
||||||
virtual void Set(ITransport* port);
|
virtual void Set(ITransport* port);
|
||||||
|
|
||||||
virtual const char* ToString() const { return "PackPort"; }
|
virtual String ToString() const { return String("PackPort"); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool OnOpen();
|
virtual bool OnOpen();
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
uint32_t HSE_VALUE = 8000000;
|
unsigned int HSE_VALUE = 8000000;
|
||||||
uint32_t SystemCoreClock = 72000000;
|
uint32_t SystemCoreClock = 72000000;
|
||||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||||
|
|
||||||
|
|
|
@ -358,9 +358,10 @@ void TinyController::ShowMessage(const TinyMessage& msg, bool send, const ITrans
|
||||||
if(st)
|
if(st)
|
||||||
{
|
{
|
||||||
msg_printf("Mac=");
|
msg_printf("Mac=");
|
||||||
if(strcmp(port->ToString(), "R24") == 0)
|
auto name = port->ToString();
|
||||||
|
if(name == "R24")
|
||||||
ByteArray(st, 5).Show();
|
ByteArray(st, 5).Show();
|
||||||
else if(strcmp(port->ToString(), "ShunCom") == 0)
|
else if(name == "ShunCom")
|
||||||
ByteArray(st, 2).Show();
|
ByteArray(st, 2).Show();
|
||||||
else
|
else
|
||||||
ByteArray(st, 6).Show();
|
ByteArray(st, 6).Show();
|
||||||
|
|
Loading…
Reference in New Issue