lxg

导航

 
//https://www.cnblogs.com/MenAngel/p/11552588.html
/*
psClient.xml
<psClient>
 <ServerNameList select="196.168.65.187">
  <ServerName>196.168.65.187</ServerName>
  <ServerName>localhost</ServerName>
 </ServerNameList>
 <UserLogin>
  <UserName>admin</UserName>
  <Password>5A7D46B23449E0A2</Password>
  <AutoSave>true</AutoSave>
 </UserLogin>
</psClient>
*/
/*
typedef struct _STLoginInfo
{
    bool bValid;
    QVector<QString> vecServerIps;
    QString strServerIp;
    QString strUserName;
    QString strPwd;
    bool    bAutoSave;
    _STLoginInfo()
    {
        initInfo();
    }
    void initInfo()
    {
        bValid = true;
        vecServerIps.clear();
        strServerIp = "";
        strUserName = "";
        strPwd = "";
        bAutoSave = false;
    }
}STLoginInfo;
*/
STLoginInfo readLoginXml()
{
    STLoginInfo loginInfo;
    if (!QFile::exists("psClient.xml"))
    {
        qFcInfo()<<"psClient.xml not exists";
        m_bExistFile = false;
        loginInfo.bValid = false;
        return  loginInfo;
    }
    QMutexLocker locker(&m_mutex);
    //构建文档
    rapidxml::file<> xmlFile("psClient.xml");
    rapidxml::xml_document<> doc;
    doc.parse<0>(xmlFile.data());
    //根节点
    rapidxml::xml_node<>* root = doc.first_node();
    rapidxml::xml_node<>* pNode = root->first_node();
    while (pNode)
    {
        std::string strRootName = pNode->name();
        if(strRootName == "ServerNameList")
        {
            rapidxml::xml_attribute<>* attr = pNode->first_attribute();
            rapidxml::xml_node<>* pNodeChld = pNode->first_node();
            while (pNodeChld)
            {
                loginInfo.vecServerIps.push_back(pNodeChld->value());
                pNodeChld = pNodeChld->next_sibling();
            }
            //ui->lineIpEdit->setCurrentText(attr->value());
        }
        else if(strRootName == "UserLogin")
        {
            rapidxml::xml_node<>* pNodeChld = pNode->first_node();
            while (pNodeChld)
            {
                std::string strName;
                strName = pNodeChld->name();
                if(strName == "UserName")
                {
                    loginInfo.strUserName = pNodeChld->value();
                }
                if(strName == "Password")
                {
#ifdef WIN32
                    std::string strPasswd = m_TeaCrypt.DeCrypt(pNodeChld->value());
                    loginInfo.strPwd = strPasswd.c_str();
#else
                    QByteArray byteArray;
                    byteArray = byteArray.fromHex(QByteArray(pNodeChld->value()));
                    byteArray = ComonUtils::getXorEncryptDecrypt(byteArray);
                    loginInfo.strPwd = byteArray.fromBase64(byteArray);
#endif
                }
                if(strName == "AutoSave" && pNodeChld->value() == std::string("true"))
                {
                    loginInfo.bAutoSave = true;
                }
                pNodeChld = pNodeChld->next_sibling();
            }
        }
        pNode = pNode->next_sibling();
    }
    doc.clear();
    return  loginInfo;
}
void wrieLoginXml(const STLoginInfo &loginInfo)
{
    QMutexLocker locker(&m_mutex);
    if(!m_bExistFile)
    {
        rapidxml::xml_document<> doc;  //构造一个空的xml文档
        rapidxml::xml_node<>* rot = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("setting.xml version='1.0' encoding='utf-8'"));//allocate_node分配一个节点,该节点类型为node_pi,对XML文件进行描,描述内容在allocate_string中
        doc.append_node(rot); //把该节点添加到doc中
        //添加根节点
        rapidxml::xml_node<>* node = doc.allocate_node(rapidxml::node_element, "psClient", NULL);
        rapidxml::xml_node<>* nameList = doc.allocate_node(rapidxml::node_element, "ServerNameList", NULL);
        node->append_node(nameList);
        rapidxml::xml_attribute<>* attr = doc.allocate_attribute("select", loginInfo.strServerIp.toStdString().c_str());
        nameList->append_attribute(attr);
        rapidxml::xml_node<>* serverName = doc.allocate_node(rapidxml::node_element, "ServerName", loginInfo.strServerIp.toStdString().c_str());
        nameList->append_node(serverName);
        //添加用户
        rapidxml::xml_node<>* user = doc.allocate_node(rapidxml::node_element, "UserLogin", NULL);
        node->append_node(user);
        if(loginInfo.bAutoSave)
        {
            rapidxml::xml_node<>* userName = doc.allocate_node(rapidxml::node_element, "UserName", loginInfo.strUserName.toStdString().c_str());
            rapidxml::xml_node<>* userPasswd = doc.allocate_node(rapidxml::node_element, "Password", m_TeaCrypt.EnCrypt(loginInfo.strPwd.toStdString().c_str()));
            rapidxml::xml_node<>* autoSave = doc.allocate_node(rapidxml::node_element, "AutoSave", "true");
            user->append_node(userName);
            user->append_node(userPasswd);
            user->append_node(autoSave);
        }
        else
        {
            rapidxml::xml_node<>* autoSave = doc.allocate_node(rapidxml::node_element, "AutoSave", "false");
            user->append_node(autoSave);
        }
        doc.append_node(node);
        std::ofstream pout("psClient.xml");
        pout << doc;
        doc.clear();
        return;
    }
    //构建文档
    rapidxml::file<> xmlFile("psClient.xml");
    rapidxml::xml_document<> doc;
    doc.parse<0>(xmlFile.data());
    //根节点
    rapidxml::xml_node<>* root = doc.first_node();
    rapidxml::xml_node<>* pNode = root->first_node();
    while (pNode)
    {
        std::string strRootName = pNode->name();
        if(strRootName == "ServerNameList")
        {
            rapidxml::xml_attribute<>* attr = pNode->first_attribute();
            attr->value(loginInfo.strServerIp.toStdString().c_str());
            rapidxml::xml_node<>* pNodeChld = pNode->first_node();
            int nServerCount = 0;
            bool bFindIp = false;
            while (pNodeChld)
            {
                nServerCount++;
                if(pNodeChld->value() == loginInfo.strServerIp.toStdString())
                {
                    bFindIp = true;
                    break;
                }
                pNodeChld = pNodeChld->next_sibling();
            }
            if(!bFindIp && nServerCount == 10)
            {
                pNode->remove_last_node();
            }
            if(!bFindIp)
            {
                rapidxml::xml_node<>* serverName = doc.allocate_node(rapidxml::node_element, "ServerName", loginInfo.strServerIp.toStdString().c_str());
                pNode->append_node(serverName);
            }
        }
        else if(strRootName == "UserLogin" && loginInfo.bAutoSave)
        {
            //先删除再增加
            pNode->remove_all_nodes();
            rapidxml::xml_node<>* userName = doc.allocate_node(rapidxml::node_element, "UserName", loginInfo.strUserName.toStdString().c_str());
#ifdef WIN32
            rapidxml::xml_node<>* userPasswd = doc.allocate_node(rapidxml::node_element, "Password", m_TeaCrypt.EnCrypt(loginInfo.strPwd.toStdString().c_str()));
#else
            QByteArray byteArray = loginInfo.strPwd.toUtf8().toBase64();
            QString strTmpPwd = ComonUtils::getXorEncryptDecrypt(byteArray).toHex();
            rapidxml::xml_node<>* userPasswd = doc.allocate_node(rapidxml::node_element, "Password", strTmpPwd.toStdString().c_str());
#endif
            rapidxml::xml_node<>* autoSave = doc.allocate_node(rapidxml::node_element, "AutoSave", "true");
            pNode->append_node(userName);
            pNode->append_node(userPasswd);
            pNode->append_node(autoSave);
        }
        else
        {
            pNode->remove_last_node();
            rapidxml::xml_node<>* autoSave = doc.allocate_node(rapidxml::node_element, "AutoSave", "false");
            pNode->append_node(autoSave);
        }
        pNode = pNode->next_sibling();
    }
    std::ofstream pout("psClient.xml");
    pout << doc;
    doc.clear();
}
 
posted on 2020-12-11 14:10  lxg_7105  阅读(295)  评论(0编辑  收藏  举报