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. 多个串的最长公共子串 SPOJ - LCS2 后缀自动机

    题意: 求多个串的最长公共子串 这里用的是O(n)的后缀自动机写法 我后缀数组的专题有nlog(n)写法的 题解: 对于其中的一个串建立后缀自动机 然后对于后缀自动机上面的每一个节点求出每一个节点最长 ...

  2. PHP中的闭包小谈

    接触PHP一段时间以来,我一直以为这是一种基于函数式编程的语言是没有闭包这种东西的,但事实上却颠覆了我的想法,PHP竟然有闭包,下面我们一起来接触一下PHP的所谓的闭包. 根据PHP官网的定义来看,闭 ...

  3. 框架_mybatis1

    mybatis框架是实现与数据之间交互 入门: 创建数据库环境 创建实体类与数据库对应字段 实现Serializable 创建接口定义方法 创建主配置方法: <?xml version=&quo ...

  4. Arrays 001

    1.1 Array Initalization First of all, we need know Java arrays is static. When the array is initiali ...

  5. vs2010用iis5作为调试服务器从而允许非本机电脑访问项目网站

    工作的时候经常遇见这2种情况 1,和设备端的同事调程序,但是他们却不能访问vs自带的web服务器 2,写好的程序在vs中运行一点问题都没有,一发布到iis就问题一大堆 后来在终于有了一个比较好的解决办 ...

  6. ES6模块化使用遇到的问题

    前言 最近在学习ES6模块化时,遇到了一些问题,通过查询相关资料得以解决,本篇随笔详细记录了解决方法. 具体内容 以下定义一个模块common.js 在test.html中引入上述定义的模块 运行上述 ...

  7. Vue.js 样式绑定(1)

    demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  8. Jmeter-【JSON Extractor】-响应结果中三级key取值

    一.请求返回样式 二.取第三个option 三.查看结果

  9. Oracle实现行转列+Mybatis

    1.需求 报表需要动态展示某几个公司分别在几个月内销售额情况(前端表头月份是动态的,月时间段是前端参数来选择的,最大为12个月), 页面展示如下 Oracle数据库中数据如下: 可以看到一个公司的月份 ...

  10. javascript追加节点

    追加节点 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...