JSON 单例类
MD5JSON.h
#pragma once
#include "include/json/json.h"
#include "include/md5/md5.h"
#include "xml/tinyxml/tinystr.h"
#include "xml/tinyxml/tinyxml.h"
#include <string>
using namespace std; #pragma comment(lib,"md5_JSON\\include\\bin\\json_vc71_libmtd.lib")
#define MJ MD5JSON::getInstance()
#define JSON Json
#define XM XML::getInstance() // //多线程调试 (/MTd)
class MD5JSON
{
//md5
md5_state_t m_context;
unsigned char m_Digest[];
char m_outstr[];
//json
char *buf;
Json::Reader reader;
Json::Value root;
string m_jsonfilename;
Json::Value jsonItem;
public:
///////////////////////// MD5
static MD5JSON *getInstance(); bool setmd5(const char *data); bool setmd5(const char *data, char *buffer, int bufferLen); bool setmd5(const char *data, int datalen); bool setmd5(string data); bool setmd5(string data, char *buffer, int bufferLen); void print(); void print(char *data); void print(string data); void print(const char *data); char *getmd5() { return m_outstr; } public:
///////////////////////// JSON
bool createJSONFile(const char *filename);
bool createJSONFile(string filename);
string getFileName() { return m_jsonfilename; }
Json::Value getRoot() { return this->root; }
Json::Value getItem() { return this->jsonItem; }
bool readJSONFile(const char *filename);
bool readJSONFile(string filename);
//直接写入文件
bool insertData(const char *data);
//json 方法写入并baocun
bool insertData();
//如果相同则覆盖
//==================================================
bool addChild(const char *key,const char *value);
bool addChild(string key, string value);
//MD5JSON::insertData(); 内部已经调用
void addChildEnd();
//================================================== char *getJSONData() { return buf; }
//在加载文件时此函数就已经调用
bool parseJSON();
//这里只有在一个root下才有效
int get_int(const char *key);
string get_string(const char *key);
unsigned int get_uint(const char *key);
double get_double(const char *key);
bool get_bool(const char *key);
const char *get_const_string(const char *key);
//清楚整个文件数据
void clear(); //遍历
void ergodicObject(Json::Value::Members &object);
void ergodicObject(Json::Value &root, Json::Value::Members &object);
//数组遍历
void ergodicArray(Json::Value &root);
private:
//f30992da54715e5a0c4a7eaf29889641 //vico MD5JSON()
{
this->m_context = { };
memset(this->m_Digest, , sizeof(this->m_Digest));
memset(this->m_outstr, , sizeof(this->m_outstr));
}
~MD5JSON()
{
if (buf)
{
delete[] buf;
buf = NULL;
}
}
}; class XML
{
string name;
TiXmlDocument* Doc;
public:
static XML *getInstance();
string setXMLName(string name);
bool createXML();
bool createXML(string name);
bool loadXML(string name);
//Accout Password
bool insert(string A, string P);
bool save();
bool saveToNewFile(string name);
//node 除非你知道在做什么
bool addChild(string node, string key, string value);
//Accout AccoutValue Password PasswordValue
bool addChild(string A, string AV,string P,string PV); private:
XML();
~XML();
};
MD5JSON.cpp
#include "MD5JSON.h" ///////////////////////// MD5
MD5JSON *MD5JSON::getInstance()
{
static MD5JSON md5json;
return &md5json;
} bool MD5JSON::setmd5(const char *data)
{
if (data)
{
md5_byte_t byteData[] = { };
memset(byteData, , sizeof(byteData));
int len = ;
while (data[len])
{
byteData[len] = data[len];
len++;
}
/*
pms->abcd[1] = /*0xefcdab89 // T_MASK ^ 0x10325476;
pms->abcd[2] = /*0x98badcfe // T_MASK ^ 0x67452301;
*/
md5_init(&m_context);
md5_append(&m_context, byteData, len);
md5_finish(&m_context, m_Digest); sprintf_s(m_outstr,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[]);
return true;
}
return false;
} bool MD5JSON::setmd5(const char *data, char *buffer, int bufferLen)
{
if (data && buffer && bufferLen)
{
md5_byte_t byteData[] = { };
memset(byteData, , sizeof(byteData));
int len = ;
while (data[len])
{
byteData[len] = data[len];
len++;
}
/*
pms->abcd[1] = /*0xefcdab89 // T_MASK ^ 0x10325476;
pms->abcd[2] = /*0x98badcfe // T_MASK ^ 0x67452301;
*/
md5_init(&m_context);
md5_append(&m_context, byteData, len);
md5_finish(&m_context, m_Digest); sprintf_s(m_outstr,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[]);
strcpy_s(buffer, bufferLen, m_outstr);
return true;
}
return false;
} bool MD5JSON::setmd5(const char *data, int datalen)
{
if (data && datalen)
{
md5_byte_t byteData[] = { };
memset(byteData, , sizeof(byteData));
while (data[datalen - ])
{
byteData[datalen - ] = data[datalen - ];
datalen--;
}
/*
pms->abcd[1] = /*0xefcdab89 // T_MASK ^ 0x10325476;
pms->abcd[2] = /*0x98badcfe // T_MASK ^ 0x67452301;
*/
md5_init(&m_context);
md5_append(&m_context, byteData, datalen - );
md5_finish(&m_context, m_Digest); sprintf_s(m_outstr,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[], m_Digest[],
m_Digest[], m_Digest[], m_Digest[], m_Digest[]);
return true;
}
return false;
} bool MD5JSON::setmd5(string data)
{
return MD5JSON::setmd5(data.c_str());
} bool MD5JSON::setmd5(string data, char *buffer, int bufferLen)
{
return MD5JSON::setmd5(data.c_str(),buffer,bufferLen);
} void MD5JSON::print()
{
printf("%s\n", m_outstr);
} void MD5JSON::print(char *data)
{
printf("%s\n", data);
} void MD5JSON::print(string data)
{
printf("%s\n", data.c_str());
} void MD5JSON::print(const char *data)
{
printf("%s\n", data);
} ///////////////////////// JSON
bool MD5JSON::createJSONFile(const char *filename)
{
m_jsonfilename = filename;
FILE *File = NULL;
fopen_s(&File, filename, "wb");
if (File)
{
fclose(File);
File = NULL;
return true;
}
return false;
} bool MD5JSON::createJSONFile(string filename)
{
return MD5JSON::createJSONFile(filename.c_str());
} bool MD5JSON::readJSONFile(const char *filename)
{ m_jsonfilename = filename;
FILE *File = NULL;
fopen_s(&File, filename, "rb");
if (File)
{
rewind(File);
//从0开始偏移到最后
fseek(File, , SEEK_END);
int size = ftell(File);
//rewind(File);
//从0开始偏移到0的位置
fseek(File, , SEEK_SET);
//文件当前读写位置
//fseek(File, 0, SEEK_CUR); buf = new char[size + ];
int read = fread_s(buf, size + , size, , File);
rewind(File); fclose(File);
File = NULL;
return MD5JSON::parseJSON();
}
return false;
} bool MD5JSON::readJSONFile(string filename)
{
return MD5JSON::readJSONFile(filename.c_str());
} bool MD5JSON::insertData(const char *data)
{
FILE *File = NULL;
fopen_s(&File, this->m_jsonfilename.c_str(), "wb+");
if (File)
{
int len = strlen(data);
fwrite(data, len, , File);
fclose(File);
File = NULL;
return true;
}
return false;
} bool MD5JSON::insertData()
{
return MJ->insertData(this->root.toStyledString().c_str());
} bool MD5JSON::addChild(const char *key, const char *value)
{
if (key && value)
{
jsonItem[key] = value;
return true;
}
return false;
} bool MD5JSON::addChild(string key, string value)
{
return MD5JSON::addChild(key.c_str(), value.c_str());
} void MD5JSON::addChildEnd()
{
this->root.append(jsonItem);
MD5JSON::insertData();
} bool MD5JSON::parseJSON()
{
if (this->reader.parse(MJ->getJSONData(), this->root))
{
Json::Value::Members member = this->root.getMemberNames();
MD5JSON::ergodicObject(this->root,member); //int size_i = member.size();
//for (int i = 0; i < size_i; i++)
//{
// if (this->root[member[i].c_str()].isObject())
// {
// Json::Value::Members temp = this->root[member[i].c_str()].getMemberNames();
// MD5JSON::ergodicObject(this->root[member[i].c_str()],temp);
// int size_j = temp.size();
// for (int j = 0; j < size_j; j++)
// {
// if ((this->root[member[i].c_str()])[temp[j].c_str()].isObject())
// {
// Json::Value::Members tmp = (this->root[member[i].c_str()])[temp[j].c_str()].getMemberNames();
// MD5JSON::ergodicObject(this->root[member[i].c_str()][temp[j].c_str()],tmp);
// int size_k = tmp.size();
// for (int k = 0; k < size_k; k++)
// {
// if ((this->root[member[i].c_str()])[temp[j].c_str()][tmp[k].c_str()].isObject())
// {
// Json::Value::Members tm = ((this->root[member[i].c_str()])[temp[j].c_str()])[tmp[k].c_str()].getMemberNames();
// MD5JSON::ergodicObject(this->root[member[i].c_str()][temp[j].c_str()][tmp[k].c_str()], tm);
// int size_n = tm.size();
// for (int n = 0; n < size_n; n++)
// {
// if ((this->root[member[i].c_str()])[temp[j].c_str()][tmp[k].c_str()][tm[n].c_str()].isObject())
// {
// Json::Value::Members t = (((this->root[member[i].c_str()])[temp[j].c_str()])[tmp[k].c_str()])[tm[n].c_str()].getMemberNames();
// MD5JSON::ergodicObject(this->root[member[i].c_str()][temp[j].c_str()][tmp[k].c_str()][tm[n].c_str()], t);
// }
// }
// }
// }
// }
// }
// }
//}
return true;
}
return false;
} void MD5JSON::ergodicObject(Json::Value::Members &object)
{
int size = object.size();
for (int i = ; i < size; i++)
{
std::cout << object[i] << std::endl;
}
} void MD5JSON::ergodicObject(Json::Value &root, Json::Value::Members &object)
{
int size = object.size();
for (int i = ; i < size; i++)
{
if (root[object[i].c_str()].isArray())
{
std::cout << "Key = " << object[i] << " , Value = Array"<< std::endl; //遇到数组对象一直进行递归
MD5JSON::ergodicArray(root[object[i].c_str()]);
}
else if (root[object[i].c_str()].isBool())
{
std::cout << "Key = " << object[i] << " , Value = " << root[object[i].c_str()].asBool() << std::endl;
}
else if (root[object[i].c_str()].isDouble())
{
std::cout << "Key = " << object[i] << " , Value = " << root[object[i].c_str()].asDouble() << std::endl;
}
else if (root[object[i].c_str()].isInt())
{
std::cout << "Key = " << object[i] << " , Value = " << root[object[i].c_str()].asInt() << std::endl;
}
else if (root[object[i].c_str()].isString())
{
std::cout << "Key = " << object[i] << " , Value = ";
printf("%s\n", root[object[i].c_str()].asString().c_str()); }
else if (root[object[i].c_str()].isObject())
{
std::cout << "Key = " << object[i] << " , Value = Object"<< std::endl; //遇到对象一直进行递归
Json::Value::Members tmp = root[object[i].c_str()].getMemberNames();
MD5JSON::ergodicObject(root[object[i].c_str()], tmp);
}
}
} void MD5JSON::ergodicArray(Json::Value &root)
{
int size = root.size();
for (int i = ; i < size; i++)
{
string str = root[i].toStyledString();
//printf("%s\n", str.c_str());
Json::Value::Members tmp = root[i].getMemberNames();
MD5JSON::ergodicObject(root[i], tmp);
}
} int MD5JSON::get_int(const char *key)
{
if (root[key].isInt())
return root[key].asInt();
return ;
} string MD5JSON::get_string(const char *key)
{
if (root[key].isString())
return root[key].asString();
string str;
return str;
} unsigned int MD5JSON::get_uint(const char *key)
{
if (root[key].isUInt())
return root[key].asUInt();
return -;
} double MD5JSON::get_double(const char *key)
{
if (root[key].isDouble())
return root[key].asDouble();
return -1.0;
} bool MD5JSON::get_bool(const char *key)
{
if (root[key].isBool())
return root[key].asBool();
return false;
} const char *MD5JSON::get_const_string(const char *key)
{
if (root[key].isString())
return root[key].asCString();
return NULL;
} void MD5JSON::clear()
{
FILE *File = NULL;
fopen_s(&File, this->m_jsonfilename.c_str(), "rb");
if (File)
{
fclose(File);
File = NULL;
string head("del ");
head += this->m_jsonfilename;
int c = system(head.c_str());
if (!c)
{
MJ->createJSONFile(this->m_jsonfilename.c_str());
delete[] buf;
buf = NULL;
return;
}
}
MJ->createJSONFile(this->m_jsonfilename.c_str());
delete[] buf;
buf = NULL;
} ///////////////////////////////////////////////////////// XML XML *XML::getInstance()
{
static XML xml;
return &xml;
} string XML::setXMLName(string name)
{
this->name.clear();
return this->name = name;
} bool XML::createXML()
{
//加载成功
if (!Doc->LoadFile(this->name.c_str()))
{
return Doc->SaveFile(this->name.c_str());
}
return false;
} bool XML::createXML(string name)
{
//加载成功
if (!Doc->LoadFile(name.c_str()))
{
return Doc->SaveFile(name.c_str());
}
return false;
} bool XML::loadXML(string name)
{
XML::createXML(name); TiXmlElement* e = NULL;
for (e = Doc->FirstChildElement("Node"); e != NULL; e = e->NextSiblingElement())
{
if (e)
{
std::cout << "Account : " << e->Attribute("Account") << ", Password : " << e->Attribute("Password") << std::endl;
}
}
return false;
} bool XML::insert(string A, string P)
{
TiXmlElement* e = new TiXmlElement("Node");
e->SetAttribute("Account",A.c_str());//得到玩家账号
e->SetAttribute("Password", P.c_str());//得到玩家密码
Doc->LinkEndChild(e);//存入文档
//保存到文档
return Doc->SaveFile(this->name.c_str());
} bool XML::save()
{
return Doc->SaveFile(this->name.c_str());
} bool XML::saveToNewFile(string name)
{
return Doc->SaveFile(name.c_str());
} bool XML::addChild(string node, string key, string value)
{
TiXmlElement* e = new TiXmlElement(node.c_str());
e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
Doc->LinkEndChild(e);//存入文档
//保存到文档
return Doc->SaveFile(this->name.c_str());
} bool XML::addChild(string A, string AV, string P, string PV)
{
TiXmlElement* e = new TiXmlElement("Node");
e->SetAttribute(A.c_str(), AV.c_str());
e->SetAttribute(P.c_str(), PV.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
//e->SetAttribute(key.c_str(), value.c_str());
Doc->LinkEndChild(e);//存入文档
//保存到文档
return Doc->SaveFile(this->name.c_str());
} XML::XML()
{
name = "XML_FILE.xml";
Doc = new TiXmlDocument();
} XML::~XML()
{
if (Doc)
{
delete Doc;
Doc = NULL;
}
if (!name.empty())
{
name.clear();
}
}
JSON 单例类的更多相关文章
- java单例类/
java单例类 一个类只能创建一个实例,那么这个类就是一个单例类 可以重写toString方法 输出想要输出的内容 可以重写equcal来比较想要比较的内容是否相等 对于final修饰的成员变量 一 ...
- iOS中编写单例类的心得
单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...
- 如何防止JAVA反射对单例类的攻击?
在我的上篇随笔中,我们知道了创建单例类有以下几种方式: (1).饿汉式; (2).懒汉式(.加同步锁的懒汉式.加双重校验锁的懒汉式.防止指令重排优化的懒汉式); (3).登记式单例模式; (4).静态 ...
- 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类
static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...
- 设计模式(java) 单例模式 单例类
·单例类 单实例类,就是这个类只能创建一个对象,保证了对象实例的唯一性. 1.单例模式( Singleton Pattern) 是一个比较简单的模式, 其定义如下:Ensure a class has ...
- [转]单例模式——C++实现自动释放单例类的实例
[转]单例模式——C++实现自动释放单例类的实例 http://www.cnblogs.com/wxxweb/archive/2011/04/15/2017088.html http://blog.s ...
- iOS - OC SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 特点: 在内存中只有一个实例 提供一个全局的访问点 ...
- iOS - Swift SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...
- Java单例类的简单实现
对于java新手来说,单例类给我的印象挺深,之前一道web后台笔试题就是写单例类.*.*可惜当时不了解. 在大部分时候,我们将类的构造器定义成public访问权限,允许任何类自由创建该类的对象.但在某 ...
随机推荐
- 要素选择变化事件 IActiveViewEvents_SelectionChanged
void IDockableWindowDef.OnCreate(object hook) { m_application = hook as IApplication; m_hookHelper = ...
- $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别
一.$router和$route的区别 $router : 是路由操作对象,只写对象$route : 路由信息对象,只读对象 例子://$router操作 路由跳转 this.$router.push ...
- 第一类和第二类Stirling数
做了老是忘…… 实际问题: 找维基百科.百度百科…… 第一类Stirling数 n个元素构成m个圆排列 S(n,m)=S(n-1,m-1)+(n-1)*S(n-1,m) 初始 S(0,0)=1 S(n ...
- wpf 绑定除数据上下文外的属性
例如: listview 绑定了一个windows.datacontext.一个集合,那么其中一个item想绑定windows.datacontext.A属性怎么办: 通过查找祖先的方法:
- Apache 环境变量配置
在path 中加入 C:\__S_D_K__\AndroidApache\apache-ant-1.9.14\bin 我的路径在C盘
- bzoj1042题解
[题意分析] 有q个询问,每次询问求一个只有四个物品的背包方案数. [解题思路] 先计算出所有面值硬币的无限背包方案数. 考虑容斥,每个限制表示选取大于di个面值ci的硬币,总方案数=0限制方案数-1 ...
- NX二次开发-UFUN设置环境变量UF_set_variable
NX9+VS2012 #include <uf.h> #include <stdio.h> UF_initialize(); //UFUN方式 //设置环境变量 int a = ...
- NX二次开发-NXOPEN更改工程图视图名字baseView1->SetName("LSY");
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- spring-helloworld (1)
目录 一.eclipse安装springsource-tools插件 二.新建maven工程,引入spring配置 三.添加helloworld类 四.使用springsource-tools插件 创 ...
- 谈谈E语言
基于中国文化底蕴的编程语言, 绝对不是E语言那个样子. 基于中文的编程,必将是计算机届的一次原子爆炸!