Commit cfe14287 authored by Sergey.Konovalov's avatar Sergey.Konovalov Committed by Alexander Trofimov

для сборки AscOfficeDocxFile2 с _USE_LIBXML2_READER_

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@58087 954022d7-b5bf-4e40-9824-e11837661b57
parent 57d80622
......@@ -1349,6 +1349,62 @@
>
</File>
</Filter>
<Filter
Name="Base"
>
<File
RelativePath="..\Source\Base\ASCString.h"
>
</File>
<File
RelativePath="..\Source\Base\Base.h"
>
</File>
<File
RelativePath="..\Source\Base\Nullable.h"
>
</File>
<File
RelativePath="..\Source\Base\SmartPtr.h"
>
</File>
<File
RelativePath="..\Source\Base\unicode_util.cpp"
>
</File>
<File
RelativePath="..\Source\Base\unicode_util.h"
>
</File>
</Filter>
<Filter
Name="XML"
>
<File
RelativePath="..\Source\XML\stringcommon.cpp"
>
</File>
<File
RelativePath="..\Source\XML\stringcommon.h"
>
</File>
<File
RelativePath="..\Source\XML\StringWriter.h"
>
</File>
<File
RelativePath="..\Source\XML\Utils.h"
>
</File>
<File
RelativePath="..\Source\XML\XmlSimple.h"
>
</File>
<File
RelativePath="..\Source\XML\xmlutils.h"
>
</File>
</Filter>
<File
RelativePath="..\Source\SystemUtility\FileSystem\Directory.cpp"
>
......
......@@ -219,7 +219,7 @@ namespace XmlUtils
sResult.Replace(_T("\""), _T("&quot;"));
return sResult;
}
#ifndef _USE_LIBXML2_READER_
//#ifndef _USE_LIBXML2_READER_
class CStringWriter
{
private:
......@@ -285,7 +285,7 @@ namespace XmlUtils
public:
AVSINLINE void WriteString(wchar_t* pString, size_t& nLen)
AVSINLINE void WriteString(wchar_t* pString, size_t nLen)
{
AddSize(nLen);
//memcpy(m_pDataCur, pString, nLen * sizeof(wchar_t));
......@@ -456,5 +456,5 @@ namespace XmlUtils
return m_arTableUnicodes[c];
}
};
#endif // ifndef _USE_LIBXML2_READER_
//#endif // ifndef _USE_LIBXML2_READER_
}
#include "LIBXML2_LIB/stdafx.h"
//#include "LIBXML2_LIB/stdafx.h"
#include "./libxml2.h"
#include <list>
namespace XmlUtils
{
std::wstring CXmlNodeBase::GetXml()
CString CXmlNodeBase::GetXml()
{
std::wstring sXml = L"<";
CString sXml = L"<";
sXml += m_sName;
std::map<std::string, std::string>::iterator p;
std::map<CStringA, CStringA>::iterator p;
for (p = m_attributes.begin(); p != m_attributes.end(); ++p)
{
sXml += L" ";
sXml += NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->first.c_str(), p->first.length(), TRUE);
sXml += std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->first.GetString(), p->first.GetLength(), TRUE));
sXml += L"='";
sXml += NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.c_str(), p->second.length(), TRUE);
sXml += std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.GetString(), p->second.GetLength(), TRUE));
sXml += L"'";
}
......@@ -42,13 +42,13 @@ namespace XmlUtils
oWriter.WriteString(L"<", 1);
oWriter.WriteString(m_sName);
std::map<std::string, std::string>::iterator p;
std::map<CStringA, CStringA>::iterator p;
for (p = m_attributes.begin(); p != m_attributes.end(); ++p)
{
oWriter.WriteString(L" ", 1);
oWriter.WriteString(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->first.c_str(), p->first.length(), TRUE));
oWriter.WriteString(std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->first.GetString(), p->first.GetLength(), TRUE)));
oWriter.WriteString(L"='", 2);
oWriter.WriteString(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.c_str(), p->second.length(), TRUE));
oWriter.WriteString(std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.GetString(), p->second.GetLength(), TRUE)));
oWriter.WriteString(L"'", 1);
}
......@@ -113,8 +113,8 @@ namespace XmlUtils
if (GetAttributesCount() > 0)
{
MoveToFirstAttribute();
std::string sName = GetNameA();
while (!sName.empty())
CStringA sName = GetNameA();
while (!sName.IsEmpty())
{
m_pCurrentNode->m_attributes.insert(std::make_pair(sName, GetTextA()));
......@@ -202,13 +202,13 @@ namespace XmlUtils
m_pBase->AddRef();
}
std::wstring CXmlNode::private_GetXml()
CString CXmlNode::private_GetXml()
{
if (NULL == m_pBase)
return L"";
return m_pBase->GetXml();
}
std::wstring CXmlNode::private_GetXmlFast()
CString CXmlNode::private_GetXmlFast()
{
if (NULL == m_pBase)
return L"";
......@@ -217,8 +217,9 @@ namespace XmlUtils
return oWriter.GetData();
}
bool CXmlNode::FromXmlFile(const std::wstring& sFile)
bool CXmlNode::FromXmlFile(const std::wstring& sFile, bool bRemoveRootNode)
{
//ignore bRemoveRootNode
CXmlDOMDocument* m_pDocument = new CXmlDOMDocument();
BOOL bRes = m_pDocument->FromFile(sFile);
......@@ -270,24 +271,54 @@ namespace XmlUtils
return FromXmlStringA(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sString.c_str(), (LONG)sString.length()));
}
std::string CXmlNode::GetAttributeA(const std::string& sName, const std::string& _default)
CString CXmlNode::GetText()
{
return (IsValid() ? m_pBase->m_sText : L"");
}
CString CXmlNode::GetName()
{
return (IsValid() ? m_pBase->m_sName : L"");
}
bool CXmlNode::IsValid()
{
return (NULL != m_pBase);
}
void CXmlNode::Clear()
{
if (NULL != m_pBase)
m_pBase->Release();
}
CString CXmlNode::ReadAttributeBase(const wchar_t* bstrName)
{
return GetAttribute(CString(bstrName));
}
CString CXmlNode::ReadAttribute(const CString& strAttibuteName)
{
return GetAttribute(strAttibuteName);
}
CStringA CXmlNode::GetAttributeA(const CStringA& sName, const CStringA& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
std::map<CStringA, CStringA>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
return pFind->second;
}
std::string CXmlNode::GetAttributeA(const std::wstring& sName, const std::string& _default)
CStringA CXmlNode::GetAttributeA(const CString& sName, const CStringA& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind =
m_pBase->m_attributes.find(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.c_str(), sName.length()));
std::map<CStringA, CStringA>::const_iterator pFind =
m_pBase->m_attributes.find(std_string2string(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.GetString(), sName.GetLength())));
if (pFind == m_pBase->m_attributes.end())
return _default;
......@@ -295,133 +326,183 @@ namespace XmlUtils
return pFind->second;
}
std::wstring CXmlNode::GetAttribute(const std::string& sName, const std::wstring& _default)
CString CXmlNode::GetAttribute(const CStringA& sName, const CString& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
std::map<CStringA, CStringA>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.c_str(), pFind->second.length(), TRUE);
return std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.GetString(), pFind->second.GetLength(), TRUE));
}
std::wstring CXmlNode::GetAttribute(const std::wstring& sName, const std::wstring& _default)
CString CXmlNode::GetAttribute(const CString& sName, const CString& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind =
m_pBase->m_attributes.find(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.c_str(), sName.length()));
std::map<CStringA, CStringA>::const_iterator pFind =
m_pBase->m_attributes.find(std_string2string(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.GetString(), sName.GetLength())));
if (pFind == m_pBase->m_attributes.end())
return _default;
return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.c_str(), pFind->second.length(), TRUE);
return std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.GetString(), pFind->second.GetLength(), TRUE));
}
CString CXmlNode::GetAttribute(const wchar_t* sName, const CString& _default)
{
return GetAttribute(CString(sName), _default);
}
int CXmlNode::GetAttributeInt(const std::string& sName, const int& _default)
int CXmlNode::GetAttributeInt(const CStringA& sName, const int& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
std::map<CStringA, CStringA>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
if (0 == pFind->second.GetLength())
return _default;
return atoi(pFind->second.c_str());
return atoi(pFind->second.GetString());
}
int CXmlNode::GetAttributeInt(const std::wstring& sName, const int& _default)
int CXmlNode::GetAttributeInt(const CString& sName, const int& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind =
m_pBase->m_attributes.find(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.c_str(), sName.length()));
std::map<CStringA, CStringA>::const_iterator pFind =
m_pBase->m_attributes.find(std_string2string(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.GetString(), sName.GetLength())));
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
if (0 == pFind->second.GetLength())
return _default;
return atoi(pFind->second.c_str());
return atoi(pFind->second.GetString());
}
int CXmlNode::ReadAttributeInt(const CString& str, const int& nDef)
{
return GetAttributeInt(str, nDef);
}
double CXmlNode::GetAttributeDouble(const std::string& sName, const double& _default)
double CXmlNode::GetAttributeDouble(const CStringA& sName, const double& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
std::map<CStringA, CStringA>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
if (0 == pFind->second.GetLength())
return _default;
return atof(pFind->second.c_str());
return atof(pFind->second.GetString());
}
double CXmlNode::GetAttributeDouble(const std::wstring& sName, const double& _default)
double CXmlNode::GetAttributeDouble(const CString& sName, const double& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind =
m_pBase->m_attributes.find(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.c_str(), (LONG)sName.length()));
std::map<CStringA, CStringA>::const_iterator pFind =
m_pBase->m_attributes.find(std_string2string(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sName.GetString(), (LONG)sName.GetLength())));
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
if (0 == pFind->second.GetLength())
return _default;
return atof(pFind->second.c_str());
return atof(pFind->second.GetString());
}
std::wstring CXmlNode::GetText()
CString CXmlNode::ReadNodeTextBase(const wchar_t* bsName)
{
return (IsValid() ? m_pBase->m_sText : L"");
return ReadNodeText(CString(bsName));
}
bool CXmlNode::IsValid()
CString CXmlNode::ReadNodeText(const CString& strName)
{
return (NULL != m_pBase);
return ReadValueString(strName);
}
CString CXmlNode::ReadValueString(const CString& sName, const CString& nDef)
{
CString sRes;
CXmlNode oTemp;
if(GetNode(sName, oTemp))
sRes = oTemp.GetText();
return sRes;
}
CXmlNode CXmlNode::GetNode(const std::wstring& sName)
CXmlNode CXmlNode::ReadNode(const CString& strNodeName)
{
CXmlNode oNode;
if (!IsValid())
return oNode;
int nCount = (int)m_pBase->m_nodes.size();
for (int i = 0; i < nCount; ++i)
GetNode(strNodeName, oNode);
return oNode;
}
CXmlNode CXmlNode::ReadNodeNoNS(const CString& strNodeName)
{
CXmlNodes oNodes;
if (GetNodes(_T("*"), oNodes))
{
if (sName == m_pBase->m_nodes[i]->m_sName)
int nCount = oNodes.GetCount();
for (int i = 0; i < nCount; ++i)
{
CXmlNodeBase* pBase = m_pBase->m_nodes[i];
pBase->AddRef();
oNode.m_pBase = pBase;
break;
CXmlNode node;
oNodes.GetAt(i, node);
if (strNodeName == GetNameNoNS(node.GetName()))
return node;
}
}
CXmlNode node;
return node;
}
CXmlNode CXmlNode::GetNode(const CString& sName)
{
CXmlNode oNode;
GetNode(sName, oNode);
return oNode;
}
CXmlNodes CXmlNode::GetNodes(const std::wstring& sName)
bool CXmlNode::GetNode(const CString& sName, CXmlNode& oNode)
{
bool bRes = false;
if (IsValid())
{
int nCount = (int)m_pBase->m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
if (sName == m_pBase->m_nodes[i]->m_sName)
{
CXmlNodeBase* pBase = m_pBase->m_nodes[i];
pBase->AddRef();
oNode.m_pBase = pBase;
bRes = true;
break;
}
}
}
return bRes;
}
CXmlNodes CXmlNode::GetNodes(const CString& sName)
{
CXmlNodes oNodes;
bool bGetAll = false;
if(_T("*") == sName)
bGetAll = true;
int nCount = (int)m_pBase->m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
if (sName == m_pBase->m_nodes[i]->m_sName)
if (bGetAll || sName == m_pBase->m_nodes[i]->m_sName)
{
CXmlNode oNode;
oNode.m_pBase = m_pBase->m_nodes[i];
......@@ -434,12 +515,7 @@ namespace XmlUtils
return oNodes;
}
bool CXmlNode::GetNode(const std::wstring& sName, CXmlNode& oNode)
{
oNode = GetNode(sName);
return oNode.IsValid();
}
bool CXmlNode::GetNodes(const std::wstring& sName, CXmlNodes& oNodes)
bool CXmlNode::GetNodes(const CString& sName, CXmlNodes& oNodes)
{
oNodes = GetNodes(sName);
return (0 != oNodes.GetCount());
......@@ -453,4 +529,18 @@ namespace XmlUtils
return *this;
}
CString CXmlNode::GetNamespace(const CString& strNodeName)
{
int nFind = strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return CString(_T(""));
return strNodeName.Mid(0, nFind);
}
CString CXmlNode::GetNameNoNS(const CString& strNodeName)
{
int nFind= strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return strNodeName;
return strNodeName.Mid(nFind + 1);
}
}
\ No newline at end of file
......@@ -9,241 +9,240 @@
#include <map>
#include <string>
#include "../../XML/stringcommon.h"
#include "../../../../../DesktopEditor/common/File.h"
#include "../Utils.h"
namespace XmlUtils
{
class CStringWriter
{
private:
wchar_t* m_pData;
size_t m_lSize;
wchar_t* m_pDataCur;
size_t m_lSizeCur;
int m_lBinaryFactor;
public:
CStringWriter()
{
m_pData = NULL;
m_lSize = 0;
m_pDataCur = m_pData;
m_lSizeCur = m_lSize;
m_bInitTable = FALSE;
m_lBinaryFactor = (((sizeof(wchar_t)) >> 1));
}
~CStringWriter()
{
RELEASEMEM(m_pData);
}
AVSINLINE void AddSize(size_t nSize)
{
if (NULL == m_pData)
{
m_lSize = max(nSize, 1000);
m_pData = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
m_lSizeCur = 0;
m_pDataCur = m_pData;
return;
}
if ((m_lSizeCur + nSize) > m_lSize)
{
while ((m_lSizeCur + nSize) > m_lSize)
{
m_lSize *= 2;
}
wchar_t* pRealloc = (wchar_t*)realloc(m_pData, m_lSize * sizeof(wchar_t));
if (NULL != pRealloc)
{
//
m_pData = pRealloc;
m_pDataCur = m_pData + m_lSizeCur;
}
else
{
wchar_t* pMalloc = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(wchar_t));
free(m_pData);
m_pData = pMalloc;
m_pDataCur = m_pData + m_lSizeCur;
}
}
}
public:
AVSINLINE void WriteString(const wchar_t* pString, const size_t& nLen)
{
AddSize(nLen);
memcpy(m_pDataCur, pString, nLen << m_lBinaryFactor);
m_pDataCur += nLen;
m_lSizeCur += nLen;
}
AVSINLINE void WriteString(const std::wstring& bsString)
{
WriteString(bsString.c_str(), (size_t)bsString.length());
}
AVSINLINE void AddCharSafe(const TCHAR& _c)
{
AddSize(1);
*m_pDataCur++ = _c;
++m_lSizeCur;
}
AVSINLINE void AddChar2Safe(const TCHAR _c1, const TCHAR& _c2)
{
AddSize(2);
*m_pDataCur++ = _c1;
*m_pDataCur++ = _c2;
m_lSizeCur += 2;
}
inline void WriteEncodeXmlString(const std::wstring& _string)
{
WriteEncodeXmlString(_string.c_str());
}
inline void WriteEncodeXmlString(const wchar_t* pString)
{
const wchar_t* pData = pString;
while (*pData != 0)
{
BYTE _code = CheckCode(*pData);
switch (_code)
{
case 1:
AddCharSafe(*pData);
break;
case 0:
AddCharSafe((WCHAR)' ');
break;
case 2:
AddSize(5);
*m_pDataCur++ = (WCHAR)('&');
*m_pDataCur++ = (WCHAR)('a');
*m_pDataCur++ = (WCHAR)('m');
*m_pDataCur++ = (WCHAR)('p');
*m_pDataCur++ = (WCHAR)(';');
m_lSizeCur += 5;
break;
case 3:
AddSize(6);
*m_pDataCur++ = (WCHAR)('&');
*m_pDataCur++ = (WCHAR)('a');
*m_pDataCur++ = (WCHAR)('p');
*m_pDataCur++ = (WCHAR)('o');
*m_pDataCur++ = (WCHAR)('s');
*m_pDataCur++ = (WCHAR)(';');
m_lSizeCur += 6;
break;
case 4:
AddSize(4);
*m_pDataCur++ = (WCHAR)('&');
*m_pDataCur++ = (WCHAR)('l');
*m_pDataCur++ = (WCHAR)('t');
*m_pDataCur++ = (WCHAR)(';');
m_lSizeCur += 4;
break;
case 5:
AddSize(4);
*m_pDataCur++ = (WCHAR)('&');
*m_pDataCur++ = (WCHAR)('g');
*m_pDataCur++ = (WCHAR)('t');
*m_pDataCur++ = (WCHAR)(';');
m_lSizeCur += 4;
break;
case 6:
AddSize(6);
*m_pDataCur++ = (WCHAR)('&');
*m_pDataCur++ = (WCHAR)('q');
*m_pDataCur++ = (WCHAR)('u');
*m_pDataCur++ = (WCHAR)('o');
*m_pDataCur++ = (WCHAR)('t');
*m_pDataCur++ = (WCHAR)(';');
m_lSizeCur += 6;
break;
default:
break;
}
++pData;
}
}
AVSINLINE size_t GetCurSize()
{
return m_lSizeCur;
}
AVSINLINE void Write(CStringWriter& oWriter)
{
WriteString(oWriter.m_pData, oWriter.m_lSizeCur);
}
inline void Clear()
{
RELEASEMEM(m_pData);
m_pData = NULL;
m_lSize = 0;
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
inline void ClearNoAttack()
{
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
std::wstring GetData()
{
std::wstring str(m_pData, (int)m_lSizeCur);
return str;
}
protected:
BYTE m_arTableUnicodes[65536];
BOOL m_bInitTable;
protected:
BYTE CheckCode(const WCHAR& c)
{
if (!m_bInitTable)
{
memset(m_arTableUnicodes, 0, 65536);
m_arTableUnicodes[0x0009] = 1;
m_arTableUnicodes[0x000A] = 1;
m_arTableUnicodes[0x000D] = 1;
memset(m_arTableUnicodes + 0x0020, 1, 0xD7FF - 0x0020 + 1);
memset(m_arTableUnicodes + 0xE000, 1, 0xFFFD - 0xE000 + 1);
m_arTableUnicodes['&'] = 2;
m_arTableUnicodes['\''] = 3;
m_arTableUnicodes['<'] = 4;
m_arTableUnicodes['>'] = 5;
m_arTableUnicodes['\"'] = 6;
m_bInitTable = TRUE;
}
return m_arTableUnicodes[c];
}
};
//class CStringWriter
//{
//private:
// wchar_t* m_pData;
// size_t m_lSize;
// wchar_t* m_pDataCur;
// size_t m_lSizeCur;
// int m_lBinaryFactor;
//public:
// CStringWriter()
// {
// m_pData = NULL;
// m_lSize = 0;
// m_pDataCur = m_pData;
// m_lSizeCur = m_lSize;
// m_bInitTable = FALSE;
// m_lBinaryFactor = (((sizeof(wchar_t)) >> 1));
// }
// ~CStringWriter()
// {
// RELEASEMEM(m_pData);
// }
// AVSINLINE void AddSize(size_t nSize)
// {
// if (NULL == m_pData)
// {
// m_lSize = max(nSize, 1000);
// m_pData = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
// m_lSizeCur = 0;
// m_pDataCur = m_pData;
// return;
// }
// if ((m_lSizeCur + nSize) > m_lSize)
// {
// while ((m_lSizeCur + nSize) > m_lSize)
// {
// m_lSize *= 2;
// }
// wchar_t* pRealloc = (wchar_t*)realloc(m_pData, m_lSize * sizeof(wchar_t));
// if (NULL != pRealloc)
// {
// //
// m_pData = pRealloc;
// m_pDataCur = m_pData + m_lSizeCur;
// }
// else
// {
// wchar_t* pMalloc = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
// memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(wchar_t));
// free(m_pData);
// m_pData = pMalloc;
// m_pDataCur = m_pData + m_lSizeCur;
// }
// }
// }
//public:
// AVSINLINE void WriteString(const wchar_t* pString, const size_t& nLen)
// {
// AddSize(nLen);
// memcpy(m_pDataCur, pString, nLen << m_lBinaryFactor);
// m_pDataCur += nLen;
// m_lSizeCur += nLen;
// }
// AVSINLINE void WriteString(const std::wstring& bsString)
// {
// WriteString(bsString.c_str(), (size_t)bsString.length());
// }
//
// AVSINLINE void AddCharSafe(const TCHAR& _c)
// {
// AddSize(1);
// *m_pDataCur++ = _c;
// ++m_lSizeCur;
// }
// AVSINLINE void AddChar2Safe(const TCHAR _c1, const TCHAR& _c2)
// {
// AddSize(2);
// *m_pDataCur++ = _c1;
// *m_pDataCur++ = _c2;
// m_lSizeCur += 2;
// }
// inline void WriteEncodeXmlString(const std::wstring& _string)
// {
// WriteEncodeXmlString(_string.c_str());
// }
// inline void WriteEncodeXmlString(const wchar_t* pString)
// {
// const wchar_t* pData = pString;
// while (*pData != 0)
// {
// BYTE _code = CheckCode(*pData);
// switch (_code)
// {
// case 1:
// AddCharSafe(*pData);
// break;
// case 0:
// AddCharSafe((WCHAR)' ');
// break;
// case 2:
// AddSize(5);
// *m_pDataCur++ = (WCHAR)('&');
// *m_pDataCur++ = (WCHAR)('a');
// *m_pDataCur++ = (WCHAR)('m');
// *m_pDataCur++ = (WCHAR)('p');
// *m_pDataCur++ = (WCHAR)(';');
// m_lSizeCur += 5;
// break;
// case 3:
// AddSize(6);
// *m_pDataCur++ = (WCHAR)('&');
// *m_pDataCur++ = (WCHAR)('a');
// *m_pDataCur++ = (WCHAR)('p');
// *m_pDataCur++ = (WCHAR)('o');
// *m_pDataCur++ = (WCHAR)('s');
// *m_pDataCur++ = (WCHAR)(';');
// m_lSizeCur += 6;
// break;
// case 4:
// AddSize(4);
// *m_pDataCur++ = (WCHAR)('&');
// *m_pDataCur++ = (WCHAR)('l');
// *m_pDataCur++ = (WCHAR)('t');
// *m_pDataCur++ = (WCHAR)(';');
// m_lSizeCur += 4;
// break;
// case 5:
// AddSize(4);
// *m_pDataCur++ = (WCHAR)('&');
// *m_pDataCur++ = (WCHAR)('g');
// *m_pDataCur++ = (WCHAR)('t');
// *m_pDataCur++ = (WCHAR)(';');
// m_lSizeCur += 4;
// break;
// case 6:
// AddSize(6);
// *m_pDataCur++ = (WCHAR)('&');
// *m_pDataCur++ = (WCHAR)('q');
// *m_pDataCur++ = (WCHAR)('u');
// *m_pDataCur++ = (WCHAR)('o');
// *m_pDataCur++ = (WCHAR)('t');
// *m_pDataCur++ = (WCHAR)(';');
// m_lSizeCur += 6;
// break;
// default:
// break;
// }
//
// ++pData;
// }
// }
// AVSINLINE size_t GetCurSize()
// {
// return m_lSizeCur;
// }
// AVSINLINE void Write(CStringWriter& oWriter)
// {
// WriteString(oWriter.m_pData, oWriter.m_lSizeCur);
// }
// inline void Clear()
// {
// RELEASEMEM(m_pData);
// m_pData = NULL;
// m_lSize = 0;
// m_pDataCur = m_pData;
// m_lSizeCur = 0;
// }
// inline void ClearNoAttack()
// {
// m_pDataCur = m_pData;
// m_lSizeCur = 0;
// }
// std::wstring GetData()
// {
// std::wstring str(m_pData, (int)m_lSizeCur);
// return str;
// }
//protected:
// BYTE m_arTableUnicodes[65536];
// BOOL m_bInitTable;
//protected:
// BYTE CheckCode(const WCHAR& c)
// {
// if (!m_bInitTable)
// {
// memset(m_arTableUnicodes, 0, 65536);
// m_arTableUnicodes[0x0009] = 1;
// m_arTableUnicodes[0x000A] = 1;
// m_arTableUnicodes[0x000D] = 1;
// memset(m_arTableUnicodes + 0x0020, 1, 0xD7FF - 0x0020 + 1);
// memset(m_arTableUnicodes + 0xE000, 1, 0xFFFD - 0xE000 + 1);
// m_arTableUnicodes['&'] = 2;
// m_arTableUnicodes['\''] = 3;
// m_arTableUnicodes['<'] = 4;
// m_arTableUnicodes['>'] = 5;
// m_arTableUnicodes['\"'] = 6;
// m_bInitTable = TRUE;
// }
// return m_arTableUnicodes[c];
// }
//};
typedef
enum XmlNodeType
......@@ -276,6 +275,8 @@ namespace XmlUtils
BYTE* m_pStream;
LONG m_lStreamLen;
std::wstring m_sTemp;
std::string m_sTempA;
public:
CXmlLiteReader()
......@@ -306,6 +307,10 @@ namespace XmlUtils
return ( NULL != reader );
}
inline BOOL FromFile(const wchar_t* sFilePath)
{
return FromFile(std::wstring(sFilePath));
}
inline BOOL FromFile(const std::wstring& sFilePath)
{
Clear();
......@@ -322,6 +327,10 @@ namespace XmlUtils
return TRUE;
}
inline BOOL FromString(const wchar_t* sXml)
{
return FromString(std::wstring(sXml));
}
inline BOOL FromString(const std::wstring& sXml)
{
Clear();
......@@ -360,7 +369,7 @@ namespace XmlUtils
while ( XmlNodeType_Element != oNodeType )
{
if (!xmlTextReaderRead(reader))
if (1 != xmlTextReaderRead(reader))
break;
oNodeType = (XmlNodeType)xmlTextReaderNodeType(reader);
......@@ -428,7 +437,7 @@ namespace XmlUtils
return TRUE;
}
inline std::wstring GetName()
inline const wchar_t* GetName()
{
if ( !IsValid() )
return L"";
......@@ -437,11 +446,11 @@ namespace XmlUtils
if (NULL == pName)
return L"";
std::wstring s = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pName, (LONG)strlen((const char*)pName));
m_sTemp = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pName, (LONG)strlen((const char*)pName));
delete [] pName;
return s;
return m_sTemp.c_str();
}
inline std::string GetNameA()
inline const char* GetNameA()
{
if ( !IsValid() )
return "";
......@@ -451,9 +460,9 @@ namespace XmlUtils
return "";
//return std::string((const char*)pName);
std::string s((const char*)pName);
m_sTempA = (const char*)pName;
delete [] pName;
return s;
return m_sTempA.c_str();
}
inline int GetDepth()
{
......@@ -470,7 +479,7 @@ namespace XmlUtils
return xmlTextReaderIsEmptyElement(reader);
}
inline std::wstring GetText()
inline const wchar_t* GetText()
{
if ( !IsValid() )
return NULL;
......@@ -479,11 +488,11 @@ namespace XmlUtils
if (NULL == pValue)
return L"";
std::wstring s = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pValue, (LONG)strlen((const char*)pValue));
m_sTemp = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pValue, (LONG)strlen((const char*)pValue));
delete [] pValue;
return s;
return m_sTemp.c_str();
}
inline std::string GetTextA()
inline const char* GetTextA()
{
if ( !IsValid() )
return NULL;
......@@ -492,17 +501,17 @@ namespace XmlUtils
if (NULL == pValue)
return "";
std::string s((const char*)pValue);
m_sTempA = (const char*)pValue;
delete [] pValue;
return s;
return m_sTempA.c_str();
}
inline std::wstring GetText2()
inline CString GetText2()
{
if ( !IsValid() )
return _T("");
std::wstring sResult;
CString sResult;
if ( xmlTextReaderIsEmptyElement(reader) )
return sResult;
......@@ -517,11 +526,11 @@ namespace XmlUtils
return sResult;
}
inline std::wstring GetOuterXml()
inline CString GetOuterXml()
{
return GetXml(false);
}
inline std::wstring GetInnerXml()
inline CString GetInnerXml()
{
return GetXml(true);
}
......@@ -555,7 +564,7 @@ namespace XmlUtils
return (BOOL)xmlTextReaderMoveToElement(reader);
}
private:
inline std::wstring GetXml(bool bInner)
inline CString GetXml(bool bInner)
{
if ( !IsValid() )
return L"";
......@@ -570,7 +579,7 @@ namespace XmlUtils
XmlNodeType eNodeType = XmlNodeType_None;
int nCurDepth = -1;
// 1 ,
//
while( TRUE )
{
if ( 0 == xmlTextReaderRead(reader) )
......@@ -585,7 +594,7 @@ namespace XmlUtils
WriteElement(oResult);
else if(eNodeType == XmlNodeType_EndElement)
{
if(false == bInner || nCurDepth != nDepth + 1)
if(false == bInner || nCurDepth != nDepth)
{
oResult.AddChar2Safe(TCHAR('<'), TCHAR('/'));
oResult.WriteEncodeXmlString(GetName());
......@@ -666,10 +675,10 @@ namespace XmlUtils
public:
IXmlDOMDocument* m_pDocument;
std::map<std::string, std::string> m_attributes;
std::map<CStringA, CStringA> m_attributes;
std::vector<CXmlNodeBase*> m_nodes;
std::wstring m_sText;
std::wstring m_sName;
CString m_sText;
CString m_sName;
public:
CXmlNodeBase() : IXmlDOMDocument()
......@@ -703,7 +712,7 @@ namespace XmlUtils
m_pDocument->Release();
}
std::wstring GetXml();
CString GetXml();
void GetXml(CStringWriter& oWriter);
};
......@@ -720,43 +729,114 @@ namespace XmlUtils
}
~CXmlNode()
{
if (NULL != m_pBase)
m_pBase->Release();
Clear();
}
CXmlNode(const CXmlNode& oSrc);
public:
bool FromXmlFile(const std::wstring& sFile);
bool FromXmlFile(const wchar_t* sFile, bool bRemoveRootNode = false)
{
return FromXmlFile(std::wstring(sFile), bRemoveRootNode);
}
bool FromXmlFile(const std::wstring& sFile, bool bRemoveRootNode = false);
bool FromXmlStringA(const std::string& sString);
bool FromXmlString(const wchar_t* sString)
{
return FromXmlFile(std::wstring(sString));
}
bool FromXmlString(const std::wstring& sString);
std::string GetAttributeA(const std::string& sName, const std::string& _default = "");
std::string GetAttributeA(const std::wstring& sName, const std::string& _default = "");
bool IsValid();
void Clear();
CString GetName();
CString GetText();
std::wstring GetAttribute(const std::string& sName, const std::wstring& _default = L"");
std::wstring GetAttribute(const std::wstring& sName, const std::wstring& _default = L"");
CString ReadAttributeBase(const wchar_t* bstrName);
template<typename T>
void ReadAttributeBase(const wchar_t* bsName, T& value)
{
value = GetAttribute(CString(bsName)).GetString();
}
CString ReadAttribute(const CString& strAttibuteName);
template<typename T>
void ReadAllAttributes(T& strNames, T& strValues)
{
if (!IsValid())
return;
int GetAttributeInt(const std::string& sName, const int& _default = 0);
int GetAttributeInt(const std::wstring& sName, const int& _default = 0);
std::map<CStringA, CStringA>::iterator p;
for (p = m_attributes.begin(); p != m_attributes.end(); ++p)
{
p->first.c_str(), p->first.length()
strNames.push_back(p->first);
strValues.push_back(p->second);
}
}
CStringA GetAttributeA(const CStringA& sName, const CStringA& _default = "");
CStringA GetAttributeA(const CString& sName, const CStringA& _default = "");
double GetAttributeDouble(const std::string& sName, const double& _default = 0);
double GetAttributeDouble(const std::wstring& sName, const double& _default = 0);
CString GetAttribute(const CStringA& sName, const CString& _default = L"");
CString GetAttribute(const CString& sName, const CString& _default = L"");
CString GetAttribute(const wchar_t* sName, const CString& _default = L"");
int GetAttributeInt(const CStringA& sName, const int& _default = 0);
int GetAttributeInt(const CString& sName, const int& _default = 0);
int ReadAttributeInt(const CString& str, const int& nDef = 0);
std::wstring GetText();
double GetAttributeDouble(const CStringA& sName, const double& _default = 0);
double GetAttributeDouble(const CString& sName, const double& _default = 0);
bool IsValid();
CString ReadNodeTextBase(const wchar_t* bsName);
template <typename T>
void ReadNodeValueBase(const wchar_t* bsName, T& value)
{
value = ReadNodeTextBase(bsName);
}
CString ReadNodeText(const CString& strName);
CString ReadValueString(const CString& sName, const CString& nDef = _T(""));
CXmlNode GetNode(const std::wstring& sName);
CXmlNodes GetNodes(const std::wstring& sName);
template <typename T>
void LoadArray(const CString& sName, std::vector<T>& arList)
{
CXmlNodes oNodes;
if (GetNodes(sName, oNodes))
{
int nCount = oNodes.GetCount();
for (int i = 0; i < nCount; ++i)
{
CXmlNode oItem;
oNodes.GetAt(i, oItem);
bool GetNode(const std::wstring& sName, CXmlNode& oNode);
bool GetNodes(const std::wstring& sName, CXmlNodes& oNodes);
arList.reserve(arList.size() + 1);
arList[i].fromXML(oItem);
}
}
}
template <typename T>
void LoadArray(const CString& sName, const CString& sSubName, std::vector<T>& arList)
{
CXmlNode oNode;
if (GetNode(sName, oNode))
oNode.LoadArray(sSubName, arList);
}
CXmlNode ReadNode(const CString& strNodeName);
CXmlNode ReadNodeNoNS(const CString& strNodeName);
CXmlNode& operator=(const CXmlNode& oSrc);
CXmlNode GetNode(const CString& sName);
CXmlNodes GetNodes(const CString& sName);
bool GetNode(const CString& sName, CXmlNode& oNode);
bool GetNodes(const CString& sName, CXmlNodes& oNodes);
CXmlNode& operator=(const CXmlNode& oSrc);
private:
CString GetNamespace(const CString& strNodeName);
CString GetNameNoNS(const CString& strNodeName);
public:
std::wstring private_GetXml();
std::wstring private_GetXmlFast();
CString private_GetXml();
CString private_GetXmlFast();
};
class CXmlNodes
......
#include "stringcommon.h"
std::wstring string2std_string(const CString& val)
{
return std::wstring(val.GetString());
}
std::string string2std_string(const CStringA& val)
{
return std::string(val.GetString());
}
CString std_string2string(const std::wstring& val)
{
return CString(val.c_str());
}
CStringA std_string2string(const std::string& val)
{
return CStringA(val.c_str());
}
\ No newline at end of file
#ifndef XML_COMMON
#define XML_COMMON
#include <string>
#ifdef _WIN32
#include <atlbase.h>
#include <atlstr.h>
#else
#include "../Base/ASCString.h"
#endif
std::wstring string2std_string(const CString& val);
std::string string2std_string(const CStringA& val);
CString std_string2string(const std::wstring& val);
CStringA std_string2string(const std::string& val);
#endif //XML_COMMON
\ No newline at end of file
#pragma once
#include "Utils.h"
#include "stringcommon.h"
#include "../Base/Base.h"
class CWCharWrapper
{
public:
CWCharWrapper():m_cwsString(NULL)
{
}
CWCharWrapper(const wchar_t* cwsString):m_cwsString(cwsString)
{
}
CWCharWrapper(const std::wstring& cwsString):m_cwsString(cwsString.c_str())
{
}
AVSINLINE const CWCharWrapper operator=(const wchar_t* cwsOther)
{
m_cwsString = cwsOther;
return *this;
}
AVSINLINE const bool operator==(const wchar_t* cwsOther)
{
if ( 0 == WStrCmp( m_cwsString, cwsOther ) )
return true;
return false;
}
AVSINLINE const bool operator!=(const wchar_t* cwsOther)
{
if ( 0 != WStrCmp( m_cwsString, cwsOther ) )
return true;
return false;
}
AVSINLINE const wchar_t operator[](const int nIndex) const
{
int nLen = GetLength();
if ( nIndex >= nLen )
return '\0';
return m_cwsString[nIndex];
}
AVSINLINE const bool IsNull() const
{
return (m_cwsString == NULL);
}
AVSINLINE const int GetLength() const
{
if ( NULL == m_cwsString )
return 0;
return (const int)wcslen( m_cwsString );
}
public:
static AVSINLINE int WStrCmp(const wchar_t* cwsStr1, const wchar_t* cwsStr2)
{
if ( NULL == cwsStr1 && NULL == cwsStr2 )
return 0;
else if ( NULL == cwsStr1 || NULL == cwsStr2 )
return -1;
return wcscmp( cwsStr1, cwsStr2 );
}
public:
const wchar_t* m_cwsString;
};
AVSINLINE const bool operator==(const wchar_t* cwsStr1, const CWCharWrapper& cwsStr2)
{
if ( 0 == CWCharWrapper::WStrCmp( cwsStr2.m_cwsString, cwsStr1 ) )
return true;
return false;
}
AVSINLINE const bool operator!=(const wchar_t* cwsStr1, const CWCharWrapper& cwsStr2)
{
if ( 0 != CWCharWrapper::WStrCmp( cwsStr2.m_cwsString, cwsStr1 ) )
return true;
return false;
}
namespace XmlUtils
{
AVSINLINE CString GetNamespace(CString& strNodeName)
{
int nFind = strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return _T("");
return strNodeName.Mid(0, nFind);
}
AVSINLINE CString GetNameNoNS(const CString& strNodeName)
{
int nFind = strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return strNodeName;
return strNodeName.Mid(nFind + 1);
}
// classes
class CXmlWriter
{
CString m_str;
public:
CXmlWriter()
{
m_str.Empty();
}
CString GetXmlString()
{
return m_str;
}
void SetXmlString(const CString& strValue)
{
m_str = strValue;
}
BOOL SaveToFile(const CString& strFilePath, BOOL bEncodingToUTF8 = FALSE)
{
FILE* pFile = _tfopen(strFilePath, _T("wt"));
if (!pFile)
return FALSE;
CStringA str; str = m_str;
if (bEncodingToUTF8)
#ifdef _UNICODE
str = EncodingUnicodeToUTF8();
#else
str = EncodingASCIIToUTF8();
#endif
fprintf(pFile, str);
fclose(pFile);
return TRUE;
}
#ifdef _UNICODE
CStringA EncodingUnicodeToUTF8()
{
int nLength = m_str.GetLength();
// Encoding Unicode to UTF-8
CStringA saStr;
WideCharToMultiByte(CP_UTF8, 0, m_str.GetBuffer(), nLength + 1, saStr.GetBuffer(nLength*3 + 1), nLength*3, NULL, NULL);
saStr.ReleaseBuffer();
return saStr;
}
#else
CString EncodingASCIIToUTF8()
{
int nLength = m_str.GetLength();
wchar_t* pWStr = new wchar_t[nLength + 1];
if (!pWStr)
return _T("");
// set end string
pWStr[nLength] = 0;
// Encoding ASCII to Unicode
MultiByteToWideChar(CP_ACP, 0, m_str, nLength, pWStr, nLength);
int nLengthW = (int) wcslen(pWStr);
// Encoding Unicode to UTF-8
CString cStr;
WideCharToMultiByte(CP_UTF8, 0, pWStr, nLengthW + 1, cStr.GetBuffer(nLengthW*3 + 1), nLengthW*3, NULL, NULL);
cStr.ReleaseBuffer();
delete[] pWStr;
return cStr;
}
#endif
void WriteString(const CString& strValue)
{
m_str += strValue;
}
void WriteInteger(int Value, int Base = 10)
{
char str[33];
_itoa(Value, str, Base);
m_str += str;
}
void WriteDouble(double Value)
{
CString str;
str.Format(_T("%lf"), Value);
m_str += str;
}
void WriteBoolean(BOOL Value)
{
if (Value)
m_str += _T("true");
else
m_str += _T("false");
}
void WriteNodeBegin(const CString& strNodeName, BOOL bAttributed = FALSE)
{
m_str += _T("<") + strNodeName;
if (!bAttributed)
m_str += _T(">");
}
void WriteNodeEnd(const CString& strNodeName, BOOL bEmptyNode = FALSE, BOOL bEndNode = TRUE)
{
if (bEmptyNode)
{
if (bEndNode)
m_str += _T(" />");
else
m_str += _T(">");
}
else
m_str += _T("</") + strNodeName + _T(">");
}
void WriteNode(const CString& strNodeName, const CString& strNodeValue)
{
if (strNodeValue.GetLength() == 0)
m_str += _T("<") + strNodeName + _T("/>");
else
m_str += _T("<") + strNodeName + _T(">") + strNodeValue + _T("</") + strNodeName + _T(">");
}
void WriteNode(const CString& strNodeName, int nValue, int nBase = 10, const CString& strTextBeforeValue = _T(""), const CString& strTextAfterValue = _T(""))
{
WriteNodeBegin(strNodeName);
WriteString(strTextBeforeValue);
WriteInteger(nValue, nBase);
WriteString(strTextAfterValue);
WriteNodeEnd(strNodeName);
}
void WriteNode(const CString& strNodeName, double dValue)
{
WriteNodeBegin(strNodeName);
WriteDouble(dValue);
WriteNodeEnd(strNodeName);
}
void WriteAttribute(const CString& strAttributeName, const CString& strAttributeValue)
{
m_str += _T(" ") + strAttributeName + _T("=\"") + strAttributeValue + _T("\"");
}
void WriteAttribute(const CString& strAttributeName, int nValue, int nBase = 10, const CString& strTextBeforeValue = _T(""), const CString& strTextAfterValue = _T(""))
{
WriteString(_T(" ") + strAttributeName + _T("="));
WriteString(_T("\""));
WriteString(strTextBeforeValue);
WriteInteger(nValue, nBase);
WriteString(strTextAfterValue);
WriteString(_T("\""));
}
void WriteAttribute(const CString& strAttributeName, double dValue)
{
WriteString(_T(" ") + strAttributeName + _T("="));
WriteString(_T("\""));
WriteDouble(dValue);
WriteString(_T("\""));
}
};
}
//#define _USE_LIBXML2_READER_
#ifdef _USE_LIBXML2_READER_
#include "libxml2/libxml2.h"
#else
#ifdef _WIN32
#import <msxml3.dll> rename_namespace("XML")
......@@ -213,280 +501,110 @@ namespace XmlUtils
{
try
{
if (node == NULL)
return FALSE;
BSTR bstrType = type.AllocSysString();
node->put_dataType(bstrType);
SysFreeString(bstrType);
*variant = node->nodeTypedValue;
return TRUE;
}
catch (...)
{
}
return FALSE;
}
static CString GetNodeAttrib(XML::IXMLDOMNodeListPtr nodes, int index, const CString& attrib, const CString& def = _T(""))
{
try
{
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeAttrib(nodes->item[index], attrib, def);
return s;
}
catch (...)
{
}
return def;
}
static CString GetNodeText(XML::IXMLDOMNodeListPtr nodes, int index, const CString& def = _T(""))
{
try
{
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeText(nodes->item[index], def);
return s;
}
catch (...)
{
}
return def;
}
static CString GetNodeXml(XML::IXMLDOMNodeListPtr nodes, int index, const CString& def = _T(""))
{
try
{
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeXml(nodes->item[index], def);
return s;
}
catch (...)
{
}
return def;
}
static CString GetNodeValue(XML::IXMLDOMNodeListPtr nodes, int index, const CString& subnode, const CString& def = _T(""))
{
try
{
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeValue(nodes->item[index], subnode, def);
return s;
}
catch (...)
{
}
return def;
}
static BOOL GetNodeTextTyped(XML::IXMLDOMNodeListPtr nodes, int index, _variant_t* variant, const CString& type = _T("bin.base64"))
{
try
{
if (nodes == NULL || index < 0 || index >= nodes->length)
return FALSE;
return GetNodeTextTyped(nodes->item[index], variant, type);
}
catch (...)
{
}
return FALSE;
}
// classes
class CXmlWriter
{
CString m_str;
public:
CXmlWriter()
{
m_str.Empty();
}
CString GetXmlString()
{
return m_str;
}
void SetXmlString(const CString& strValue)
{
m_str = strValue;
}
BOOL SaveToFile(const CString& strFilePath, BOOL bEncodingToUTF8 = FALSE)
{
FILE* pFile = _tfopen(strFilePath, _T("wt"));
if (!pFile)
return FALSE;
CStringA str; str = m_str;
if (bEncodingToUTF8)
#ifdef _UNICODE
str = EncodingUnicodeToUTF8();
#else
str = EncodingASCIIToUTF8();
#endif
fprintf(pFile, str);
fclose(pFile);
return TRUE;
}
#ifdef _UNICODE
CStringA EncodingUnicodeToUTF8()
{
int nLength = m_str.GetLength();
// Encoding Unicode to UTF-8
CStringA saStr;
WideCharToMultiByte(CP_UTF8, 0, m_str.GetBuffer(), nLength + 1, saStr.GetBuffer(nLength*3 + 1), nLength*3, NULL, NULL);
saStr.ReleaseBuffer();
return saStr;
}
#else
CString EncodingASCIIToUTF8()
{
int nLength = m_str.GetLength();
wchar_t* pWStr = new wchar_t[nLength + 1];
if (!pWStr)
return _T("");
// set end string
pWStr[nLength] = 0;
// Encoding ASCII to Unicode
MultiByteToWideChar(CP_ACP, 0, m_str, nLength, pWStr, nLength);
int nLengthW = (int) wcslen(pWStr);
// Encoding Unicode to UTF-8
CString cStr;
WideCharToMultiByte(CP_UTF8, 0, pWStr, nLengthW + 1, cStr.GetBuffer(nLengthW*3 + 1), nLengthW*3, NULL, NULL);
cStr.ReleaseBuffer();
delete[] pWStr;
if (node == NULL)
return FALSE;
return cStr;
}
#endif
BSTR bstrType = type.AllocSysString();
node->put_dataType(bstrType);
void WriteString(const CString& strValue)
{
m_str += strValue;
SysFreeString(bstrType);
*variant = node->nodeTypedValue;
return TRUE;
}
void WriteInteger(int Value, int Base = 10)
catch (...)
{
char str[33];
_itoa(Value, str, Base);
m_str += str;
}
void WriteDouble(double Value)
return FALSE;
}
static CString GetNodeAttrib(XML::IXMLDOMNodeListPtr nodes, int index, const CString& attrib, const CString& def = _T(""))
{
try
{
CString str;
str.Format(_T("%lf"), Value);
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
m_str += str;
CString s = GetNodeAttrib(nodes->item[index], attrib, def);
return s;
}
void WriteBoolean(BOOL Value)
catch (...)
{
if (Value)
m_str += _T("true");
else
m_str += _T("false");
}
void WriteNodeBegin(const CString& strNodeName, BOOL bAttributed = FALSE)
return def;
}
static CString GetNodeText(XML::IXMLDOMNodeListPtr nodes, int index, const CString& def = _T(""))
{
try
{
m_str += _T("<") + strNodeName;
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
if (!bAttributed)
m_str += _T(">");
CString s = GetNodeText(nodes->item[index], def);
return s;
}
void WriteNodeEnd(const CString& strNodeName, BOOL bEmptyNode = FALSE, BOOL bEndNode = TRUE)
catch (...)
{
if (bEmptyNode)
{
if (bEndNode)
m_str += _T(" />");
else
m_str += _T(">");
}
else
m_str += _T("</") + strNodeName + _T(">");
}
void WriteNode(const CString& strNodeName, const CString& strNodeValue)
return def;
}
static CString GetNodeXml(XML::IXMLDOMNodeListPtr nodes, int index, const CString& def = _T(""))
{
try
{
if (strNodeValue.GetLength() == 0)
m_str += _T("<") + strNodeName + _T("/>");
else
m_str += _T("<") + strNodeName + _T(">") + strNodeValue + _T("</") + strNodeName + _T(">");
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeXml(nodes->item[index], def);
return s;
}
void WriteNode(const CString& strNodeName, int nValue, int nBase = 10, const CString& strTextBeforeValue = _T(""), const CString& strTextAfterValue = _T(""))
catch (...)
{
WriteNodeBegin(strNodeName);
WriteString(strTextBeforeValue);
WriteInteger(nValue, nBase);
WriteString(strTextAfterValue);
WriteNodeEnd(strNodeName);
}
void WriteNode(const CString& strNodeName, double dValue)
return def;
}
static CString GetNodeValue(XML::IXMLDOMNodeListPtr nodes, int index, const CString& subnode, const CString& def = _T(""))
{
try
{
WriteNodeBegin(strNodeName);
WriteDouble(dValue);
WriteNodeEnd(strNodeName);
if (nodes == NULL || index < 0 || index >= nodes->length)
return def;
CString s = GetNodeValue(nodes->item[index], subnode, def);
return s;
}
void WriteAttribute(const CString& strAttributeName, const CString& strAttributeValue)
catch (...)
{
m_str += _T(" ") + strAttributeName + _T("=\"") + strAttributeValue + _T("\"");
}
void WriteAttribute(const CString& strAttributeName, int nValue, int nBase = 10, const CString& strTextBeforeValue = _T(""), const CString& strTextAfterValue = _T(""))
return def;
}
static BOOL GetNodeTextTyped(XML::IXMLDOMNodeListPtr nodes, int index, _variant_t* variant, const CString& type = _T("bin.base64"))
{
try
{
WriteString(_T(" ") + strAttributeName + _T("="));
WriteString(_T("\""));
WriteString(strTextBeforeValue);
WriteInteger(nValue, nBase);
WriteString(strTextAfterValue);
WriteString(_T("\""));
if (nodes == NULL || index < 0 || index >= nodes->length)
return FALSE;
return GetNodeTextTyped(nodes->item[index], variant, type);
}
void WriteAttribute(const CString& strAttributeName, double dValue)
catch (...)
{
WriteString(_T(" ") + strAttributeName + _T("="));
WriteString(_T("\""));
WriteDouble(dValue);
WriteString(_T("\""));
}
};
return FALSE;
}
class CXmlReader
{
CString m_str;
......@@ -1450,21 +1568,6 @@ namespace XmlUtils
// xml
namespace XmlUtils
{
AVSINLINE CString GetNamespace(CString& strNodeName)
{
int nFind = strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return _T("");
return strNodeName.Mid(0, nFind);
}
AVSINLINE CString GetNameNoNS(const CString& strNodeName)
{
int nFind = strNodeName.Find(TCHAR(':'));
if (-1 == nFind)
return strNodeName;
return strNodeName.Mid(nFind + 1);
}
class IXmlNode
{
public:
......@@ -2691,105 +2794,6 @@ namespace XmlUtils
#endif // #ifdef _WIN32
#include "../Base/Base.h"
class CWCharWrapper
{
public:
CWCharWrapper():m_cwsString(NULL)
{
}
CWCharWrapper(const wchar_t* cwsString):m_cwsString(cwsString)
{
}
AVSINLINE const CWCharWrapper operator=(const wchar_t* cwsOther)
{
m_cwsString = cwsOther;
return *this;
}
AVSINLINE const bool operator==(const wchar_t* cwsOther)
{
if ( 0 == WStrCmp( m_cwsString, cwsOther ) )
return true;
return false;
}
AVSINLINE const bool operator!=(const wchar_t* cwsOther)
{
if ( 0 != WStrCmp( m_cwsString, cwsOther ) )
return true;
return false;
}
AVSINLINE const wchar_t operator[](const int nIndex) const
{
int nLen = GetLength();
if ( nIndex >= nLen )
return '\0';
return m_cwsString[nIndex];
}
AVSINLINE const bool IsNull() const
{
return (m_cwsString == NULL);
}
AVSINLINE const int GetLength() const
{
if ( NULL == m_cwsString )
return 0;
return (const int)wcslen( m_cwsString );
}
public:
static AVSINLINE int WStrCmp(const wchar_t* cwsStr1, const wchar_t* cwsStr2)
{
if ( NULL == cwsStr1 && NULL == cwsStr2 )
return 0;
else if ( NULL == cwsStr1 || NULL == cwsStr2 )
return -1;
return wcscmp( cwsStr1, cwsStr2 );
}
public:
const wchar_t* m_cwsString;
};
AVSINLINE const bool operator==(const wchar_t* cwsStr1, const CWCharWrapper& cwsStr2)
{
if ( 0 == CWCharWrapper::WStrCmp( cwsStr2.m_cwsString, cwsStr1 ) )
return true;
return false;
}
AVSINLINE const bool operator!=(const wchar_t* cwsStr1, const CWCharWrapper& cwsStr2)
{
if ( 0 != CWCharWrapper::WStrCmp( cwsStr2.m_cwsString, cwsStr1 ) )
return true;
return false;
}
//#define _USE_LIBXML2_READER_
#ifdef _USE_LIBXML2_READER_
#include "libxml2/libxml2.h"
#else
#ifdef _USE_XMLLITE_READER_
namespace XmlUtils
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment