⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wp_exmldoc.cpp

📁 电力故障信息采集,主要是针对南自的保护装置,这个程序用在Linux操作系统下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "WP_EXMLDoc.h"#include "WP_EXMLNode.h"#include "WP_Utility.h"# if defined (WIN32)#include <io.h># else#include <sys/io.h>#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <ctype.h>inline int _stricmp(const char* src, const char* dst){    char c1, c2;    while (*src && *dst)    {        c1 = (*src>='A'&&*src<='Z') ? *src+('a'-'A') : *src;        c2 = (*dst>='A'&&*dst<='Z') ? *dst+('a'-'A') : *dst;        if (c1 > c2)            return 1;        else if (c1 < c2)	     return -1;        src++;        dst++;    }    if (0 != *src)        return 1;    else if (0 != *dst)        return -1;    return 0;}#endifWP_EXMLDoc::WP_EXMLDoc(){    m_pRoot = NULL;    m_nFomatTABs = 0;}WP_EXMLDoc::~WP_EXMLDoc(){    if (m_pRoot != NULL)        delete m_pRoot;    m_pRoot = NULL;}void WP_EXMLDoc::SetXMLVerAndEncode(const char* pszVer, const char* pszEncode){    m_strVerXML = "<?xml version=\"";    m_strVerXML+= pszVer;    m_strVerXML+= "\" encoding=\"";    m_strVerXML+= pszEncode;    m_strVerXML+= "\"?>";}void WP_EXMLDoc::GetXML(std::string &strXML){    int nFormatTabs = 0;    strXML = "";    strXML = m_strVerXML;    strXML += m_pRoot->GetXML(nFormatTabs);}const char* WP_EXMLDoc::GetXML(){    int nFormatTabs = 0;    std::string strXML = m_strVerXML + m_pRoot->GetXML(nFormatTabs);    return strXML.c_str();}void WP_EXMLDoc::ToXML(){    int nFormatTabs = 0;    m_pRoot->ToXML(nFormatTabs);}void WP_EXMLDoc::SetRootNode(WP_EXMLNode *pNode){    if (m_pRoot)    {        delete m_pRoot;    }    m_pRoot = pNode;}WP_EXMLNode *WP_EXMLDoc::GetRootNode(){    return m_pRoot;}bool WP_EXMLDoc::LoadXML(const char *szXML){    if (m_pRoot)    {        delete m_pRoot;        m_pRoot = NULL;    }    if (! ParseXML(szXML))    {	 printf("解析错误!\n");        delete m_pRoot;        m_pRoot = NULL;        while (m_stackNode.size())            m_stackNode.pop();	 printf("解析错误退出!\n");        return false;    }    return true;}bool WP_EXMLDoc::LoadXMLFromFile(const char *szFile){    if (m_pRoot)    {        delete m_pRoot;        m_pRoot = NULL;    }    int nRead;    FILE *hXML;    char *szXMLData;    hXML = fopen(szFile, "r");    if (! hXML)    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::LoadXMLFromFile(): FAIL, fopen(%s)\n", szFile ));        return false;    }#if defined (WIN32)    szXMLData = new char[filelength(fileno(hXML))+1];    nRead = fread(szXMLData, 1, filelength(fileno(hXML)), hXML);#else    struct stat file_stat;    fstat(fileno(hXML), &file_stat);    szXMLData = new char[file_stat.st_size+1];    nRead = fread(szXMLData, 1, file_stat.st_size, hXML);#endif    szXMLData[nRead] = 0;    fclose(hXML);    if (! LoadXML(szXMLData))    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::LoadXML(): FAIL\n" ));                return false;    }EXML_LOG_MSG (( "EXML: WP_EXMLDoc::LoadXML(): SUCCESS\n" ));    delete szXMLData;    return true;}bool WP_EXMLDoc::SaveXMLToFile(const char* szFile){    FILE *hXML;    std::string strXML;    hXML = fopen(szFile, "w");    if (! hXML)    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::SaveXMLToFile(): FAIL, fopen(%s)\n", szFile ));        return false;    }    GetXML(strXML);    if (strXML.length() != fwrite(strXML.c_str(), 1, strXML.length(), hXML))    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::SaveXMLToFile(): FAIL, fwrite(%s)\n", szFile ));        fclose(hXML);        return false;    }    fclose(hXML);    return true;}bool WP_EXMLDoc::ParseXML(const char*& pszPre){    skip_unused_char(pszPre);    if (*pszPre != '\0' && *pszPre++ != '<') // 找不到节点起始标志!    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, Can't find the opened loop sign(<) for XML's Version: %s\n", pszPre ));        return false;    }    // 已找到节点起始标志'<',并已跳过    switch (*pszPre)    {        case '?': // 跳过版本检查            m_strVerXML += "<";            while (*pszPre!='>' && *pszPre!='<' && *pszPre!='\0')            {                m_strVerXML += *pszPre;                pszPre++;            }            if (*pszPre++ !='>')            {                EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, Can't find the closed loop sign(>) for XML's Version: %s\n", pszPre ));                return false;            }            m_strVerXML += '>';            break;            case '!': // 跳过注释				/*                while (*pszPre!='>' && *pszPre!='\0')                    pszPre++;                if (*pszPre++ !='>')                {                    EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, Can't find the closed loop sign(>) for XML's Comment: %s\n", pszPre ));                    return false;                }*/				pszPre = strstr(pszPre,"-->");				if(pszPre == NULL)				{                    EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, Can't find the closed loop sign(-->) for XML's Comment\n" ));                    return false;				}				pszPre += strlen("-->");                break;                case '/': // 节点闭环标志: </XXX>                    pszPre++;                    if (! CheckNodeName(pszPre))                    {                        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, CheckNodeName()\n" ));                        return false;                    }                    break;        default:            if (! PickXMLNode(pszPre))            {                EXML_LOG_MSG (( "EXML: WP_EXMLDoc::ParseXML(): FAIL, PickXMLNode()\n" ));                return false;            }            break;    }    skip_unused_char(pszPre);    if ('\0' == *pszPre) // 解析结束        return m_stackNode.empty();    return ParseXML(pszPre);}bool WP_EXMLDoc::CheckNodeName(const char*& pszPre){    std::string strNameDst;    std::string strNameSrc;    WP_EXMLNode *pEXMLNode;    if (! PickNodeName(pszPre, strNameDst))    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::CheckNodeName(): FAIL, PickNodeName()\n" ));        return false;    }    // 节点名无结束标志'>'    skip_unused_char(pszPre);    if ('>' != *pszPre++)    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::CheckNodeName(): FAIL, Can't find the closed loop sign(>) for XML's Node: %s\n", pszPre ));        return false;    }    if (0 == m_stackNode.size()) // 完整匹配失败    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::CheckNodeName(): FAIL, Node(name:%s, not root) can't match XML's Node\n", strNameDst.c_str() ));        return false;    }    pEXMLNode = m_stackNode.top();    m_stackNode.pop();    strNameSrc = pEXMLNode->GetName();    if (0 != strNameSrc.compare(strNameDst.c_str()))    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::CheckNodeName(): FAIL, Node'name(%s) isn't equal to Node'name(%s) in stack\n", strNameDst.c_str(), strNameSrc.c_str() ));    }    return (0 == strNameSrc.compare(strNameDst.c_str()));}bool WP_EXMLDoc::PickXMLNode(const char*& pszPre){    std::string strName;    WP_EXMLNode* pEXMLNode;    WP_EXMLNode* pTempNode;    // 提取节点名    if (! PickNodeName(pszPre, strName))    {        EXML_LOG_MSG (( "EXML: WP_EXMLDoc::PickXMLNode(): FAIL, PickNodeName()\n" ));        return false;    }    pEXMLNode = new WP_EXMLNode;    pEXMLNode->SetName(strName.c_str());    if (NULL == m_pRoot) // 作为文档的根节点    {        m_pRoot = pEXMLNode;    }    else // 作为文档的一个子节点    {        // m_stackNode.size() != 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -