1 Star 2 Fork 4

初雨团队 / CPPHelper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
XMLHelper.h 13.77 KB
一键复制 编辑 原始数据 按行查看 历史
mingkuang 提交于 2019-01-28 16:56 . 让REG_OPTION允许运行时调整。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
#pragma once
//#include <msxml.h>
#include <Windows.h>
//#include <rapidxml/rapidxml.hpp>
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
#include "Base.h"
#include "handle.h"
//#include <comdef.h>
//#include <atlcomcli.h>
//#include <atlstr.h>
//#include <BaseFunction.h>
//#include <StringHelper.h>
namespace rapidxml
{
typedef xml_document<wchar_t> XMLDocument;
typedef xml_node<wchar_t> XMLNote;
inline bool operator<(const xml_base<char>& Item1, const xml_base<char>& Item2)
{
/*if (Item1.m_name_size == Item2.m_name_size)
return StrCmpN(Item1.m_name, Item2.m_name, Item1.m_name_size) < 0;
else */
if (Item1.m_name_size < Item2.m_name_size)
return StrCmpNA(Item1.m_name, Item2.m_name, Item1.m_name_size) <= 0;
else
return StrCmpNA(Item1.m_name, Item2.m_name, Item2.m_name_size) < 0;
}
inline bool operator<(const xml_base<wchar_t>& Item1, const xml_base<wchar_t>& Item2)
{
/*if (Item1.m_name_size == Item2.m_name_size)
return StrCmpN(Item1.m_name, Item2.m_name, Item1.m_name_size) < 0;
else */
if (Item1.m_name_size < Item2.m_name_size)
return StrCmpNW(Item1.m_name, Item2.m_name, Item1.m_name_size) <= 0;
else
return StrCmpNW(Item1.m_name, Item2.m_name, Item2.m_name_size) < 0;
}
inline bool operator==(const xml_base<char>& Item1, const xml_base<char>& Item2)
{
return Item1.m_name_size == Item2.m_name_size&&StrCmpNA(Item1.m_name, Item2.m_name, Item1.m_name_size) == 0;
}
inline bool operator==(const xml_base<wchar_t>& Item1, const xml_base<wchar_t>& Item2)
{
return Item1.m_name_size == Item2.m_name_size&&StrCmpNW(Item1.m_name, Item2.m_name, Item1.m_name_size) == 0;
}
template<class Ch>
_Check_return_ _Success_(return !=nullptr)
inline xml_node<Ch>* XMLOpenNote(
_In_ xml_node<Ch>* pRootNote,
_In_z_ const Ch* SubPath
)
{
if (pRootNote == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
if (StrEmpty(SubPath))
return pRootNote;
if (auto NextPathName = ChTraitsCRT<Ch>::StringFindChar(SubPath, Ch('/')))
{
return XMLOpenNote(pRootNote->first_node(SubPath, NextPathName - SubPath), NextPathName + 1);
}
else
{
return pRootNote->first_node(SubPath);
}
}
template<class Ch>
_Check_return_ _Success_(return != nullptr)
inline xml_node<Ch>* XMLCreateNote(
_In_ xml_node<Ch>* pRootNote,
_In_z_ const Ch* NoteName,
_In_ DWORD CreateType
)
{
if (pRootNote == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
auto NextPathName = ChTraitsCRT<Ch>::StringFindChar(NoteName, Ch('/'));
auto chSubPath = NextPathName ? NextPathName - NoteName : ChTraitsCRT<Ch>::SafeStringLen(NoteName);
xml_node<Ch>* pChildNote = NULL;
switch (CreateType)
{
case OPEN_EXISTING:
case OPEN_ALWAYS:
pChildNote = pRootNote->first_node(NoteName, chSubPath);
if (CreateType == OPEN_EXISTING || pChildNote)
{
break;
}
case CREATE_ALWAYS:
pChildNote = pRootNote->pDocument->allocate_node(node_element, pRootNote->pDocument->allocate_string(NoteName, chSubPath + 1), NULL, chSubPath);
pRootNote->append_node(pChildNote);
break;
default:
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
break;
}
if (NextPathName == NULL || pChildNote == NULL)
{
return pChildNote;
}
else
{
NextPathName++;
return XMLCreateNote(pChildNote, NextPathName, CreateType);
}
}
template<class Ch>
_Check_return_ _Success_(return != nullptr)
inline xml_node<Ch>* XMLSetNoteValue(
_In_ xml_node<Ch>* RootNote,
_In_z_ const Ch* NoteName,
_In_z_ const Ch* NoteValue
)
{
auto pNote = XMLCreateNote(RootNote, NoteName, OPEN_ALWAYS);
if (pNote == NULL)
return NULL;
auto cbNoteValue = ChTraitsCRT<Ch>::SafeStringLen(NoteValue);
pNote->value(pNote->pDocument->allocate_string(NoteValue, cbNoteValue + 1), cbNoteValue);
if (auto pChild = pNote->first_node())
{
if (pChild->next_sibling() == 0 && pChild->type() == node_data)
{
pChild->value(pNote->value(), cbNoteValue);
}
}
return pNote;
}
_Check_return_ _Success_(return != nullptr)
inline xml_node<wchar_t>* XMLSetNoteValue(
_In_ xml_node<wchar_t>* RootNote,
_In_z_ const wchar_t* NoteName,
_In_ const FILETIME& NoteValue
)
{
auto pNote = XMLCreateNote(RootNote, NoteName, OPEN_ALWAYS);
if (pNote == NULL)
return NULL;
CStringW TimeStr;
TimeStr.Format(L"0x%.8X", NoteValue.dwHighDateTime);
XMLSetNoteValue(pNote, L"HIGHPART", TimeStr.GetBuffer());
TimeStr.Format(L"0x%.8X", NoteValue.dwLowDateTime);
XMLSetNoteValue(pNote, L"LOWPART", TimeStr.GetBuffer());
return pNote;
}
_Check_return_ _Success_(return != nullptr)
inline xml_node<char>* XMLSetNoteValue(
_In_ xml_node<char>* RootNote,
_In_z_ const char* NoteName,
_In_ const FILETIME& NoteValue
)
{
auto pNote = XMLCreateNote(RootNote, NoteName, OPEN_ALWAYS);
if (pNote == NULL)
return NULL;
CStringA TimeStr;
TimeStr.Format("0x%.8X", NoteValue.dwHighDateTime);
XMLSetNoteValue(pNote, "HIGHPART", TimeStr.GetBuffer());
TimeStr.Format("0x%.8X", NoteValue.dwLowDateTime);
XMLSetNoteValue(pNote, "LOWPART", TimeStr.GetBuffer());
return pNote;
}
template<class Ch>
_Check_return_
inline LSTATUS XMLCreateXMLDocumentByString(
_In_ CStringT< Ch, StrTraitATL< Ch, ChTraitsCRT< Ch > > > Str,
_In_ xml_document<Ch>* pDocument
)
{
return pDocument->Load(Str);
}
_Check_return_
inline LSTATUS XMLCreateXMLDocumentByData(
_In_reads_bytes_(cbData) const byte* pData,
_In_ DWORD cbData,
_In_ xml_document<wchar_t>* pDocument
)
{
CStringW Temp;
//自动检测编码标志
if (cbData >= sizeof(BomUTF16) && *(UINT16*)pData == *(UINT16*)BomUTF16)
{
//UTF16
//CodePage = CP_UTF8 + 1;
Temp.SetString((LPCWSTR)pData, cbData>>1);
}
else if (cbData >= sizeof(BomUTF8) && memcmp(BomUTF8, pData, sizeof(BomUTF8)) == 0)
{
//CodePage = CP_UTF8;
Temp = UTF8ToUnicode((char*)pData, cbData);
}
else if (cbData >= 2 && pData[1])
{
//CodePage = CP_UTF8;
Temp = UTF8ToUnicode((char*)pData, cbData);
}
else
{
//CodePage = CP_UTF8 + 1;
Temp.SetString((LPCWSTR)pData, cbData >> 1);
}
return pDocument->Load(Temp);
}
_Check_return_
inline LSTATUS XMLCreateXMLDocumentByData(
_In_reads_bytes_(cbData) const byte* pData,
_In_ DWORD cbData,
_In_ xml_document<wchar_t>* pDocument,
_In_ int CodePage
)
{
CStringW Temp;
switch (CodePage)
{
case CP_UTF8:
Temp = UTF8ToUnicode((char*)pData, cbData);
break;
case CP_UTF8 + 1:
Temp.SetString((LPCWSTR)pData, cbData >> 1);
break;
case CP_ACP:
Temp = CStringW((char*)pData, cbData);
default:
assert(0);
return 87;
break;
}
return pDocument->Load(Temp);
}
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLCreateXMLDocumentByData(
_In_reads_bytes_(cbData) const byte* pData,
_In_ DWORD cbData,
_In_ xml_document<char>* pDocument
)
{
CStringA Temp;
//自动检测编码标志
if (cbData >= sizeof(BomUTF16) && *(UINT16*)pData == *(UINT16*)BomUTF16)
{
//UTF16
Temp = Unicode2UTF8((LPCWSTR)pData, cbData >> 1);
}
else if (cbData >= sizeof(BomUTF8) && memcmp(BomUTF8, pData, sizeof(BomUTF8)) == 0)
{
//UTF8
Temp.SetString((char*)pData, cbData);
}
else if (cbData >= 2 && pData[1])
{
//UTF8
Temp.SetString((char*)pData, cbData);
}
else
{
//UTF16
Temp = Unicode2UTF8((LPCWSTR)pData, cbData >> 1);
}
return pDocument->Load(Temp);
}
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLCreateXMLDocumentByData(
_In_reads_bytes_(cbData) const byte* pData,
_In_ DWORD cbData,
_In_ xml_document<char>* pDocument,
_In_ int CodePage
)
{
CStringA Temp;
switch (CodePage)
{
case CP_UTF8:
Temp.SetString((char*)pData, cbData);
case CP_ACP:
Temp = Unicode2UTF8(CStringW((char*)pData, cbData));
break;
case CP_UTF8 + 1:
Temp = Unicode2UTF8((LPCWSTR)pData, cbData >> 1);
break;
default:
assert(0);
return ERROR_INVALID_PARAMETER;
break;
}
return pDocument->Load(Temp);
}
template<typename Ch>
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLCreateXMLDocumentByFile(
_In_z_ LPCWSTR FilePath,
_In_ xml_document<Ch>* pDocument
)
{
CHFile hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if (hFile.IsInvalid())
return GetLastError();
CHFile hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMap.IsInvalid())
return GetLastError();
auto pBase = (byte*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if (!pBase)
return GetLastError();
auto lStatus = XMLCreateXMLDocumentByData(pBase, GetFileSize(hFile, NULL), pDocument);
UnmapViewOfFile(pBase);
return lStatus;
}
template<class Ch>
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLOpenMultiNotes(
_In_ xml_node<Ch>* pRootNote,
_In_z_ const Ch* SubPath,
_Out_ std::vector<xml_node<Ch>*>& Notes
)
{
auto NextPathName = ChTraitsCRT<Ch>::StringFindChar(SubPath, Ch('/'));
auto chSubPath = NextPathName ? NextPathName - SubPath : ChTraitsCRT<Ch>::SafeStringLen(SubPath);
xml_base<Ch> Temp(SubPath, chSubPath);
if (NextPathName)
{
NextPathName++;
if (chSubPath == 1 && *SubPath == Ch('*'))
{
chSubPath = 0;
SubPath = NULL;
}
for (auto pItem = pRootNote->first_node(SubPath, chSubPath); pItem; pItem = pItem->next_sibling(SubPath, chSubPath))
{
XMLOpenMultiNotes(pItem, NextPathName, Notes);
}
}
else
{
for (auto pItem = pRootNote->first_node(SubPath, chSubPath); pItem; pItem = pItem->next_sibling(SubPath, chSubPath))
{
Notes.push_back(pItem);
}
}
return ERROR_SUCCESS;
}
template<class Ch>
inline std::vector<xml_node<Ch>*> XMLOpenMultiNotes(
_In_ xml_node<Ch>* pRootNote,
_In_z_ const Ch* SubPath
)
{
std::vector<xml_node<Ch>*> Notes;
Notes.reserve(100);
XMLOpenMultiNotes(pRootNote, SubPath, Notes);
return Notes;
}
template<class Ch>
_Check_return_ _Success_(return != nullptr)
inline xml_attribute<Ch>* XMLGetNoteAttribute(
_In_ xml_node<Ch>* Note,
_When_(chAttributeName == 0, _In_z_)
_When_(chAttributeName != 0, _In_reads_(chAttributeName)) const Ch* AttributeName,
_In_opt_ int chAttributeName
)
{
return Note->first_attribute(AttributeName, chAttributeName);
}
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteAttribute(
_In_ xml_node<wchar_t>* Note,
_In_z_ const wchar_t* AttributeName,
_Outptr_result_z_ LPBSTR pAttributeValue
)
{
auto pAttribute = XMLGetNoteAttribute(Note, AttributeName, ChTraitsCRT<wchar_t>::SafeStringLen(AttributeName));
if (pAttribute == NULL)
return ERROR_PATH_NOT_FOUND;
*pAttributeValue = SysAllocStringLen(pAttribute->value(), pAttribute->value_size());
return ERROR_SUCCESS;
}
template<class Ch>
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteAttribute(
_In_ xml_node<Ch>* Note,
_In_z_ const Ch* AttributeName,
_Out_ CStringT<Ch,StrTraitATL<Ch,ChTraitsCRT<Ch>>>& AttributeValue
)
{
auto pAttribute = XMLGetNoteAttribute(Note, AttributeName, ChTraitsCRT<Ch>::SafeStringLen(AttributeName));
if (pAttribute == NULL)
return ERROR_PATH_NOT_FOUND;
AttributeValue.SetString(pAttribute->value(), pAttribute->value_size());
//*pAttributeValue = SysAllocStringLen(, );
return ERROR_SUCCESS;
}
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteAttribute(
_In_ XMLNote* RootNote,
_In_z_ LPCWSTR Path,
_In_z_ LPCWSTR AttributeName,
_Outptr_result_z_ LPBSTR pAttributeValue
)
{
RootNote = XMLOpenNote(RootNote, Path);
if (RootNote == NULL)
return ERROR_PATH_NOT_FOUND;
return XMLGetNoteAttribute(RootNote, AttributeName, pAttributeValue);
}
template<class Ch>
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteAttribute(
_In_ xml_node<Ch>* RootNote,
_In_z_ const Ch* Path,
_In_z_ const Ch* AttributeName,
_Out_ CStringT< Ch, StrTraitATL< Ch, ChTraitsCRT< Ch > > >& AttributeValue)
{
RootNote = XMLOpenNote(RootNote, Path);
if (RootNote == NULL)
return ERROR_PATH_NOT_FOUND;
return XMLGetNoteAttribute(RootNote, AttributeName, AttributeValue);
}
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteValue(
_In_ XMLNote* RootNote,
_In_z_ LPCWSTR Path,
_Outptr_result_z_ BSTR* Value
)
{
RootNote = XMLOpenNote(RootNote, Path);
if (RootNote == NULL)
return ERROR_PATH_NOT_FOUND;
auto szValue = SysAllocStringLen(RootNote->value(), RootNote->value_size());
if (!szValue)
return ERROR_OUTOFMEMORY;
*Value = szValue;
return ERROR_SUCCESS;
}
template<class Ch>
_Check_return_ _Success_(return != S_OK)
inline LSTATUS XMLGetNoteValue(
_In_ xml_node<Ch>* RootNote,
_In_z_ const Ch* Path,
_Out_ CStringT< Ch, StrTraitATL< Ch, ChTraitsCRT< Ch > > >& Value)
{
RootNote = XMLOpenNote(RootNote, Path);
if (RootNote == NULL)
return ERROR_PATH_NOT_FOUND;
Value.SetString(RootNote->value(), RootNote->value_size());
return ERROR_SUCCESS;
}
}
C++
1
https://gitee.com/Chuyu-Team/CPPHelper.git
git@gitee.com:Chuyu-Team/CPPHelper.git
Chuyu-Team
CPPHelper
CPPHelper
master

搜索帮助