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 单例类的更多相关文章

  1. java单例类/

    java单例类  一个类只能创建一个实例,那么这个类就是一个单例类 可以重写toString方法 输出想要输出的内容 可以重写equcal来比较想要比较的内容是否相等 对于final修饰的成员变量 一 ...

  2. iOS中编写单例类的心得

    单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...

  3. 如何防止JAVA反射对单例类的攻击?

    在我的上篇随笔中,我们知道了创建单例类有以下几种方式: (1).饿汉式; (2).懒汉式(.加同步锁的懒汉式.加双重校验锁的懒汉式.防止指令重排优化的懒汉式); (3).登记式单例模式; (4).静态 ...

  4. 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类

    static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...

  5. 设计模式(java) 单例模式 单例类

    ·单例类 单实例类,就是这个类只能创建一个对象,保证了对象实例的唯一性. 1.单例模式( Singleton Pattern) 是一个比较简单的模式, 其定义如下:Ensure a class has ...

  6. [转]单例模式——C++实现自动释放单例类的实例

    [转]单例模式——C++实现自动释放单例类的实例 http://www.cnblogs.com/wxxweb/archive/2011/04/15/2017088.html http://blog.s ...

  7. iOS - OC SingleClass 单例类

    前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 特点: 在内存中只有一个实例 提供一个全局的访问点 ...

  8. iOS - Swift SingleClass 单例类

    前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...

  9. Java单例类的简单实现

    对于java新手来说,单例类给我的印象挺深,之前一道web后台笔试题就是写单例类.*.*可惜当时不了解. 在大部分时候,我们将类的构造器定义成public访问权限,允许任何类自由创建该类的对象.但在某 ...

随机推荐

  1. CompletionService:批量执行异步任务

    之前有去了解CompletionService,当时没有做记录,现在再想使用却发现忘记了,果然好记性不如烂笔头. 原文比较简洁,通俗易懂. 转载-原文链接: https://segmentfault. ...

  2. Mysql 查询视图出现The user specified as a definer ('root'@'%') does not exist的问题

    今天服务器Mysql版本在5.7升级到8.0+之后,部分网站(老的)访问视图出现The user specified as a definer ('root'@'%') does not exist问 ...

  3. LNMP部署

    部署企业LNMP架构 源码包:nginx-* ; mysql-* ; php-* ; boost-* ; zend-loader-php5.6-linux-* ;yum软件: pcre-devel z ...

  4. 杭电多校第一场-B-Operation

    题目描述 There is an integer sequence a of length n and there are two kinds of operations:0 l r: select ...

  5. docker gitlab backup

    说明:下面命令中带有<your container name>字,是 gitlab 容器的名称,请按实际情况进行代替 在创建备份之前,你不需要停止任何东西 docker exec -t & ...

  6. jq-demo-阻止冒泡,阻止默认行为

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. delphi获得文件大小

    大概有这些方法可以获得文件大小 FileSizeByName(需要引用IdGlobal单元) GetFileSize FileSize(不能获得正在使用的文件大小) FileSeek TFileStr ...

  8. Java——单例模式初步

    1.7 单例模式初步 好书推荐:java与模式 1.7.1 什么是设计模式 设计模式是在大量的实践中总结和理论化之后优选的代码结构.编程风格.以及解决问题的思考方式.设计模式就像是经典的棋谱,不同的棋 ...

  9. 我们能从java的HelloWorld学到什么?

    这是每个Java程序员都知道的.虽然简单,但是从一个简单的问题可以引入更深的思考.在这篇文章中,我们将讨论这个简单的程序.如果能更多的帮到你,请留下宝贵的意见. HelloWorld.java pub ...

  10. 配置Dubbo Demo遇到的坑之一---找不到dubbo.xsd文件

    原文地址:https://blog.csdn.net/qq_36654870/article/details/80603302 1.dubbo.xsd文件不能读取 因为阿里http://code.al ...