json 解析
2016-07-06 15:26
447 查看
template<typename TPBName>
class CConfigJson
{
private:
bool m_isLoaded;
public:
bool isVailed() { return m_isLoaded; }
public:
CConfigJson()
{
m_pbMessage = NULL;
m_isLoaded = false;
}
~CConfigJson()
{
if (m_pbMessage != NULL)
{
delete m_pbMessage;
}
}
void clear()
{
if (m_pbMessage != NULL)
{
delete m_pbMessage;
m_pbMessage = NULL;
}
m_isLoaded = false;
}
bool load(const char* fileName)
{
if (fileName == NULL)
{
nlwarning("fileName empty");
clear();
return false;
}
std::string outString;
if (!readFileString(fileName, outString))
{
nlwarning("readFile error fileName = [%s]", fileName);
clear();
return false;
}
if (m_isLoaded)
{
m_pbMessage->Clear();
}
else
{
m_pbMessage = new TPBName();
}
if (!google::protobuf::JsonFormat::ParseFromString(outString, m_pbMessage))
{
nlwarning("pb format error!");
clear();
return false;
}
m_isLoaded = true;
return true;
}
TPBName* getConfig()
{
if (isVailed() == false)
{
return NULL;
}
return m_pbMessage;
}
private:
static bool readFileString(const char* fileName, std::string & outString)
{
std::string strfileName = fileName;
std::string filePath = NLMISC::CPath::lookup(strfileName);
std::ifstream inFile(filePath.c_str());
if (!inFile.is_open())
{
nlinfo("no such a file exits %s", fileName);
return false;
}
//只处理 win/linux utf-8 utf-16 utf-32的头,其他的谁用谁支持
std::string lineStr;
std::vector<std::string> tempStrVector;
if (!getline(inFile, lineStr))
{
nlwarning("the format of the file is error %s", fileName);
return false;
}
static unsigned char charBom[7] = { 0xEF, 0xBB, 0xBF, 0xE, 0x0, 0xFF, 0xFE };
std::size_t found = 0;
for (sint32 i = 0; i < (sint32)lineStr.length(); i++)
{
bool fbom = false;
for (sint32 j = 0; j < sizeof(charBom); j++)
{
if ((unsigned char)lineStr.c_str()[i] == charBom[j])
{
fbom = true;
break;
}
}
if (fbom)
{
found++;
}
else
{
break;
}
}
if (found < lineStr.length())
{
lineStr = lineStr.substr(found);
}
else
{
lineStr.clear();
}
found = lineStr.find_first_of('\r');
if (found != std::string::npos)
{
lineStr.erase(found);
}
outString += lineStr;
while (getline(inFile, lineStr))
{
found = lineStr.find_first_of('\r');
if (found != std::string::npos)
{
lineStr.erase(found);
}
outString += lineStr;
}
return true;
}
TPBName* m_pbMessage;//现new吧?
};
class CConfigJson
{
private:
bool m_isLoaded;
public:
bool isVailed() { return m_isLoaded; }
public:
CConfigJson()
{
m_pbMessage = NULL;
m_isLoaded = false;
}
~CConfigJson()
{
if (m_pbMessage != NULL)
{
delete m_pbMessage;
}
}
void clear()
{
if (m_pbMessage != NULL)
{
delete m_pbMessage;
m_pbMessage = NULL;
}
m_isLoaded = false;
}
bool load(const char* fileName)
{
if (fileName == NULL)
{
nlwarning("fileName empty");
clear();
return false;
}
std::string outString;
if (!readFileString(fileName, outString))
{
nlwarning("readFile error fileName = [%s]", fileName);
clear();
return false;
}
if (m_isLoaded)
{
m_pbMessage->Clear();
}
else
{
m_pbMessage = new TPBName();
}
if (!google::protobuf::JsonFormat::ParseFromString(outString, m_pbMessage))
{
nlwarning("pb format error!");
clear();
return false;
}
m_isLoaded = true;
return true;
}
TPBName* getConfig()
{
if (isVailed() == false)
{
return NULL;
}
return m_pbMessage;
}
private:
static bool readFileString(const char* fileName, std::string & outString)
{
std::string strfileName = fileName;
std::string filePath = NLMISC::CPath::lookup(strfileName);
std::ifstream inFile(filePath.c_str());
if (!inFile.is_open())
{
nlinfo("no such a file exits %s", fileName);
return false;
}
//只处理 win/linux utf-8 utf-16 utf-32的头,其他的谁用谁支持
std::string lineStr;
std::vector<std::string> tempStrVector;
if (!getline(inFile, lineStr))
{
nlwarning("the format of the file is error %s", fileName);
return false;
}
static unsigned char charBom[7] = { 0xEF, 0xBB, 0xBF, 0xE, 0x0, 0xFF, 0xFE };
std::size_t found = 0;
for (sint32 i = 0; i < (sint32)lineStr.length(); i++)
{
bool fbom = false;
for (sint32 j = 0; j < sizeof(charBom); j++)
{
if ((unsigned char)lineStr.c_str()[i] == charBom[j])
{
fbom = true;
break;
}
}
if (fbom)
{
found++;
}
else
{
break;
}
}
if (found < lineStr.length())
{
lineStr = lineStr.substr(found);
}
else
{
lineStr.clear();
}
found = lineStr.find_first_of('\r');
if (found != std::string::npos)
{
lineStr.erase(found);
}
outString += lineStr;
while (getline(inFile, lineStr))
{
found = lineStr.find_first_of('\r');
if (found != std::string::npos)
{
lineStr.erase(found);
}
outString += lineStr;
}
return true;
}
TPBName* m_pbMessage;//现new吧?
};
相关文章推荐
- js 程序执行与顺序实现详解
- JVM JSTAT命令的用法和参数讲解
- jsoup
- JavaScript 常用对象
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
- JavaScript强化教程——Native.js示例汇总
- JavaScript强化教程——Native.js示例汇总
- 关于js中alert弹出窗口换行!
- [置顶] js实现下拉菜单提交表单后停留在选中option
- js敏感词过滤
- js中闭包的理解二
- 浅析highchart
- js中的return
- js中闭包的理解一
- 小代码 HTML 每个按钮控制一个DIV js实现
- js正则表达式的使用详解
- browser js
- js数组查找
- JavaScript高级程序设计——第5章:引用类型
- web.xml配置异常处理界面