TinyXml的读取和编辑以及多线程的锁

来源:互联网 发布:极速扑克 知乎 编辑:程序博客网 时间:2024/06/05 01:04

 

 

#include <iostream>

#include <pthread.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <dirent.h>

#include <errno.h>

#include <vector>

#include <algorithm>

#include <unistd.h>

#include "tinyxml/tinyxml.h" //本地下载了tinyxml源码

#include <stdexcept>

#include <map>

#include <fcntl.h>

#include <stdlib.h>

#define MACXML "mac.xml"

using namespace std;

 

static bool usingMacXml = false;

pthread_mutex_t mutexMac = PTHREAD_MUTEX_INITIALIZER;

 

struct mac_data{

    string mac;

    string account;

    string password;

    string status;

};

 

 

void setMacStatus(mac_data m,string status)

{

   pthread_mutex_lock( &mutexMac );

    while(true)

    {

        if(!usingMacXml)

        {

            usingMacXml = true;

 

            TiXmlDocument* macXml = new TiXmlDocument();

            while(!(macXml->LoadFile(MACXML)))

            {

            }

            TiXmlElement* rootElement = macXml->RootElement();  //MacList

            TiXmlElement* macElement = rootElement->FirstChildElement();  //Machine

 

            while ( macElement ) {

                TiXmlElement* nameElement = macElement->FirstChildElement();//获得Machine的Name元素

                TiXmlElement* accountElement = nameElement->NextSiblingElement(); //获得Machine的Password元素

                TiXmlElement* passwordElement = accountElement->NextSiblingElement(); //获得Machine的Account元素

                TiXmlElement* statusElement = passwordElement->NextSiblingElement(); //获得Machine的Status元素

                bool condition = m.mac ==nameElement->GetText() && m.account ==accountElement->GetText() && status !=statusElement->GetText();

                if(condition)

                {

                    statusElement->Clear();

                    TiXmlText * text = new TiXmlText(status );

                    statusElement->LinkEndChild( text );

                    break;

                }

                else

                    cout<<"No match "<<m.mac<<" "<<nameElement->GetText()<< " account:"<<m.account<<" "<<accountElement->GetText()<<" status:" <<m.status<<" " <<statusElement->GetText()<<endl;

 

                cout<<"condition is "<<condition<<endl;

                macElement = macElement->NextSiblingElement();

            }

            macXml->SaveFile(MACXML);

 

            delete macXml;

            usingMacXml = false;

            break;

        }

        else

            system("sleep 1");

    }

    pthread_mutex_unlock(&mutexMac);

}

 

原创粉丝点击