XML helpers added

This commit is contained in:
Simon Rozman 2016-03-17 12:25:22 +01:00
parent c0dbc0caf2
commit b927b467a3
6 changed files with 250 additions and 4 deletions

View File

@ -27,11 +27,13 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\src\xml.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\wxex\appbar.h" />
<ClInclude Include="..\include\wxex\common.h" />
<ClInclude Include="..\include\wxex\comutils.h" />
<ClInclude Include="..\include\wxex\xml.h" />
<ClInclude Include="..\src\stdafx.h" />
</ItemGroup>
<ItemGroup>

View File

@ -28,6 +28,9 @@
<ClCompile Include="..\src\comutils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\xml.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\stdafx.h">
@ -42,6 +45,9 @@
<ClInclude Include="..\include\wxex\comutils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\wxex\xml.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\locale\wxExtend.pot">

View File

@ -83,6 +83,7 @@ inline bool wxGetDoWndAnimation()
/// \param[in] dwRemove Set of extended styles to remove.
/// \param[in] dwAdd Set of extended styles to add.
/// \param[in] nFlags Additional SWP_ flags to pass to SetWindowPos(). If zero, SetWindowPos() is not called.
///
/// \returns
/// - true when the window extended style was modified
/// - false if the window extended style was not neccessary

116
include/wxex/xml.h Normal file
View File

@ -0,0 +1,116 @@
/*
Copyright 2016 Amebis
This file is part of wxExtend.
wxExtend is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
wxExtend is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wxExtend. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "common.h"
#include <wx/string.h>
#include <wx/xml/xml.h>
///
/// Escapes text string for XML insertion
///
/// \param[in] str Text string
/// \returns Escaped string
///
inline wxString wxXmlEscapeText(_In_ const wxString& str)
{
wxString escaped;
escaped.reserve(str.length());
for (wxString::const_iterator i = str.begin(); i != str.end(); ++i) {
const wxChar c = *i;
switch (c) {
case wxS('<'):
escaped.append(wxS("&lt;"));
break;
case wxS('>'):
escaped.append(wxS("&gt;"));
break;
case wxS('&'):
escaped.append(wxS("&amp;"));
break;
case wxS('\r'):
escaped.append(wxS("&#xD;"));
break;
default:
escaped.append(c);
}
}
return escaped;
}
///
/// Escapes attribute value string for XML insertion
///
/// \param[in] str Attribute value
///
/// \returns Escaped string
///
inline wxString wxXmlEscapeAttr(_In_ const wxString& str)
{
wxString escaped;
escaped.reserve(str.length());
for (wxString::const_iterator i = str.begin(); i != str.end(); ++i) {
const wxChar c = *i;
switch (c) {
case wxS('<'):
escaped.append(wxS("&lt;"));
break;
case wxS('>'):
escaped.append(wxS("&gt;"));
break;
case wxS('&'):
escaped.append(wxS("&amp;"));
break;
case wxS('\r'):
escaped.append(wxS("&#xD;"));
break;
case wxS('"'):
escaped.append(wxS("&quot;"));
break;
case wxS('\t'):
escaped.append(wxS("&#x9;"));
break;
case wxS('\n'):
escaped.append(wxS("&#xA;"));
break;
default:
escaped.append(c);
}
}
return escaped;
}
///
/// Calculates hash of the node and all its children
///
/// \param[in] hash Handle of a hash object
/// \param[in] node Root node
///
///
void WXEXTEND_API wxXmlHashNode(_In_ HCRYPTHASH hash, const wxXmlNode *node);

View File

@ -25,5 +25,6 @@
#include "../include/wxex/appbar.h"
#include "../include/wxex/comutils.h"
#include "../include/wxex/xml.h"
#include "../include/wxex/common.h"

120
src/xml.cpp Normal file
View File

@ -0,0 +1,120 @@
/*
Copyright 2015-2016 Amebis
This file is part of wxExtend.
wxExtend is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
wxExtend is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wxExtend. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
static inline BOOL CryptHashData(__in HCRYPTHASH hHash, __in const wxString &str, __in DWORD dwFlags)
{
const wxScopedCharBuffer buf(str.ToUTF8());
return ::CryptHashData(hHash, (const BYTE*)buf.data(), buf.length(), dwFlags);
}
void WXEXTEND_API wxXmlHashNode(_In_ HCRYPTHASH hash, const wxXmlNode *node)
{
wxASSERT_MSG(node, wxT("invalid parameter"));
switch (node->GetType()) {
case wxXML_ELEMENT_NODE:
{
{
static const BYTE element_in [] = "<";
// Hash the open tag.
wxVERIFY(::CryptHashData(hash, element_in, _countof(element_in) - 1, 0));
wxVERIFY(::CryptHashData(hash, node->GetName(), 0));
for (wxXmlAttribute *attr = node->GetAttributes(); attr; attr = attr->GetNext()) {
static const BYTE attrib_sep[] = " ";
wxVERIFY(::CryptHashData(hash, attrib_sep, _countof(attrib_sep) - 1, 0));
wxVERIFY(::CryptHashData(hash, attr->GetName(), 0));
wxString value = attr->GetValue();
if (!value.IsEmpty()) {
static const BYTE
attrval_in [] = "=\"",
attrval_out[] = "\"";
wxVERIFY(::CryptHashData(hash, attrval_in, _countof(attrval_in) - 1, 0));
wxVERIFY(::CryptHashData(hash, wxXmlEscapeAttr(value), 0));
wxVERIFY(::CryptHashData(hash, attrval_out, _countof(attrval_out) - 1, 0));
}
}
}
wxXmlNode *child = node->GetChildren();
if (child) {
static const BYTE
element_out [] = ">",
elemclose_in[] = "</";
// Hash the open tag closing.
wxVERIFY(::CryptHashData(hash, element_out, _countof(element_out) - 1, 0));
// Hash the children.
for (; child; child = child->GetNext())
wxXmlHashNode(hash, child);
// Hash the closing tag.
wxVERIFY(::CryptHashData(hash, elemclose_in, _countof(elemclose_in) - 1, 0));
wxVERIFY(::CryptHashData(hash, node->GetName(), 0));
wxVERIFY(::CryptHashData(hash, element_out, _countof(element_out) - 1, 0));
} else {
static const BYTE element_out [] = "/>";
// Hash the childless element tag closing.
wxVERIFY(::CryptHashData(hash, element_out, _countof(element_out) - 1, 0));
}
break;
}
case wxXML_TEXT_NODE:
{
wxVERIFY(::CryptHashData(hash, wxXmlEscapeText(node->GetContent()), 0));
break;
}
case wxXML_CDATA_SECTION_NODE:
{
static const BYTE
cdata_in [] = "<![CDATA[",
cdata_out[] = "]]>";
wxVERIFY(::CryptHashData(hash, cdata_in, _countof(cdata_in) - 1, 0));
wxVERIFY(::CryptHashData(hash, node->GetContent(), 0));
wxVERIFY(::CryptHashData(hash, cdata_out, _countof(cdata_out) - 1, 0));
break;
}
case wxXML_COMMENT_NODE:
{
wxVERIFY(::CryptHashData(hash, node->GetContent(), 0));
break;
}
case wxXML_DOCUMENT_NODE:
{
// Hash the children.
for (wxXmlNode *child = node->GetChildren(); child; child = child->GetNext())
wxXmlHashNode(hash, child);
}
}
}