|
|
#include "StructData.h"
|
|
|
#include <sstream>
|
|
|
#include <iomanip>
|
|
|
#include <ctime>
|
|
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
StructData::StructData()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
StructData::StructData(tagParam *data)
|
|
|
{
|
|
|
m_data.type = data->type;
|
|
|
m_data.datas.clear();
|
|
|
//m_data.datas.insert(data->datas.begin(), data->datas.end()); // std::map
|
|
|
std::copy(data->datas.begin(), data->datas.end(), std::back_inserter(m_data.datas));
|
|
|
}
|
|
|
|
|
|
Data* StructData::clone()
|
|
|
{
|
|
|
Data* data = new StructData(&m_data);
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
void StructData::copy(Data* data)
|
|
|
{
|
|
|
StructData *dt = dynamic_cast<StructData *>(data);
|
|
|
m_data.type = dt->getData().type;
|
|
|
m_data.datas.clear();
|
|
|
//m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map
|
|
|
std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas));
|
|
|
}
|
|
|
|
|
|
void StructData::copy(Data& data)
|
|
|
{
|
|
|
StructData *dt = dynamic_cast<StructData *>(&data);
|
|
|
m_data.type = dt->getData().type;
|
|
|
m_data.datas.clear();
|
|
|
//m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map
|
|
|
std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas));
|
|
|
}
|
|
|
|
|
|
int StructData::length()
|
|
|
{
|
|
|
return (5 + 14 * m_data.datas.size());
|
|
|
}
|
|
|
|
|
|
std::string StructData::toStr()
|
|
|
{
|
|
|
std::stringstream ss;
|
|
|
ss << "type:" << m_data.type << std::endl;
|
|
|
//std::map<std::string, int>::iterator iter = m_data.datas.begin(); // std::map
|
|
|
std::list<Param>::iterator iter = m_data.datas.begin();
|
|
|
while(iter != m_data.datas.end())
|
|
|
{
|
|
|
//ss << "name:" << iter->first << ", value:" << iter->second << std::endl; // std::map
|
|
|
ss << "name:" << (*iter).getName() << ", value:" << (*iter).getValue() << std::endl;
|
|
|
|
|
|
iter++;
|
|
|
}
|
|
|
return ss.str();
|
|
|
}
|
|
|
|
|
|
// 1. 第1个字节/共1字节: 类型
|
|
|
// 2. 第2,3,4,5字节/共4字节: 数量
|
|
|
// 3. 第6,7,8,9,10,11,12,13,14,15字节/共10字节:名称
|
|
|
// 4. 第16,17,18,19字节/共4字节:系数
|
|
|
// 重复3和4
|
|
|
std::string StructData::toByteStr()
|
|
|
{
|
|
|
std::string s;
|
|
|
|
|
|
s.append((char *)&m_data.type, 1);
|
|
|
// 修改数量为2字节
|
|
|
//unsigned int nCount = m_data.datas.size();
|
|
|
//s.append(Utils::intToByteStr(nCount));
|
|
|
unsigned short int nCount = m_data.datas.size();
|
|
|
s.append(Utils::shortToByteStr(nCount));
|
|
|
// std::map<std::string, int>::iterator iter = m_data.datas.begin(); // std::map
|
|
|
std::list<Param>::iterator iter = m_data.datas.begin();
|
|
|
while(iter != m_data.datas.end())
|
|
|
{
|
|
|
char name[11];
|
|
|
unsigned int value;
|
|
|
|
|
|
memset(name, 0, sizeof(name));
|
|
|
//memcpy(name, iter->first.c_str(), iter->first.length() <= 10 ? iter->first.length() : 10); // std::map
|
|
|
memcpy(name, (*iter).getName().c_str(), (*iter).getName().length() <= 10 ? (*iter).getName().length() : 10);
|
|
|
name[10] = '\0';
|
|
|
//value = iter->second; // std::map
|
|
|
value = (*iter).getValue();
|
|
|
s.append(name, 10);
|
|
|
s.append(Utils::intToByteStr(value));
|
|
|
|
|
|
iter++;
|
|
|
}
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
std::string StructData::toHexStr()
|
|
|
{
|
|
|
std::string s = toByteStr();
|
|
|
return Utils::strToHexStr(s);
|
|
|
}
|
|
|
|
|
|
bool StructData::fromByteStr(std::string str)
|
|
|
{
|
|
|
if(str.length() < 5)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
m_data.type = str.at(0);
|
|
|
std::string s;
|
|
|
s = str.substr(1, 4);
|
|
|
// 修改数量为2字节
|
|
|
//int nCount = Utils::byteStrToInt(s);
|
|
|
unsigned short int nCount = Utils::byteStrToShort(s);
|
|
|
// 1为类型字节数,2为数量字节数,14为 name + value 字节数
|
|
|
if(str.length() < (1 + 2 + 14 * nCount))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
for(unsigned short int i=0; i<nCount; i++)
|
|
|
{
|
|
|
std::string name = str.substr(1 + 2 + 14 * i, 10);
|
|
|
unsigned int value = Utils::byteStrToInt(str.substr(15 + 14 * i, 4));
|
|
|
//m_data.datas.insert(std::make_pair(name, value)); // std::map
|
|
|
Param param(name, value);
|
|
|
m_data.datas.push_back(param);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void StructData::setData(tagParam& data)
|
|
|
{
|
|
|
m_data.type = data.type;
|
|
|
m_data.datas.clear();
|
|
|
//m_data.datas.insert(data.datas.begin(), data.datas.end());
|
|
|
std::copy(data.datas.begin(), data.datas.end(), std::back_inserter(m_data.datas));
|
|
|
}
|