xml 单例类

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[16];
    char m_outstr[256];
    //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 = { 0 };
        memset(this->m_Digest, 0, sizeof(this->m_Digest));
        memset(this->m_outstr, 0, 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[256] = { 0 };
        memset(byteData, 0, sizeof(byteData));
        int len = 0;
        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[0], m_Digest[1], m_Digest[2], m_Digest[3], m_Digest[4], m_Digest[5],
            m_Digest[6], m_Digest[7], m_Digest[8], m_Digest[9], m_Digest[10], m_Digest[11],
            m_Digest[12], m_Digest[13], m_Digest[14], m_Digest[15]);
        return true;
    }
    return false;
}

bool MD5JSON::setmd5(const char *data, char *buffer, int bufferLen)
{
    if (data && buffer && bufferLen)
    {
        md5_byte_t byteData[256] = { 0 };
        memset(byteData, 0, sizeof(byteData));
        int len = 0;
        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[0], m_Digest[1], m_Digest[2], m_Digest[3], m_Digest[4], m_Digest[5],
            m_Digest[6], m_Digest[7], m_Digest[8], m_Digest[9], m_Digest[10], m_Digest[11],
            m_Digest[12], m_Digest[13], m_Digest[14], m_Digest[15]);
        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[256] = { 0 };
        memset(byteData, 0, sizeof(byteData));
        while (data[datalen - 1])
        {
            byteData[datalen - 1] = data[datalen - 1];
            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 - 1);
        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[0], m_Digest[1], m_Digest[2], m_Digest[3], m_Digest[4], m_Digest[5],
            m_Digest[6], m_Digest[7], m_Digest[8], m_Digest[9], m_Digest[10], m_Digest[11],
            m_Digest[12], m_Digest[13], m_Digest[14], m_Digest[15]);
        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, 0, SEEK_END);
        int size = ftell(File);
        //rewind(File);
        //从0开始偏移到0的位置
        fseek(File, 0, SEEK_SET);
        //文件当前读写位置
        //fseek(File, 0, SEEK_CUR);

        buf = new char[size + 1];
        int read = fread_s(buf, size + 1, size, 1, 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, 1, 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 = 0; 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 = 0; 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 = 0; 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 0;
}

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 -1;
}

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();
    }
}
复制代码
posted @ 2019-11-02 09:11  YZFHKMS-X  阅读(272)  评论(0编辑  收藏  举报