Commit 4820371e authored by Oleg.Korshul's avatar Oleg.Korshul Committed by Alexander Trofimov

dom model

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@57646 954022d7-b5bf-4e40-9824-e11837661b57
parent d56ffea8
#include "LIBXML2_LIB/stdafx.h"
#include "./libxml2.h"
#include <list>
namespace XmlUtils
{
std::wstring CXmlNodeBase::GetXml()
{
std::wstring sXml = L"<";
sXml += m_sName;
std::map<std::string, std::string>::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 += L"='";
sXml += NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.c_str(), p->second.length(), TRUE);
sXml += L"'";
}
sXml += L">";
int nCount = m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
sXml += m_nodes[i]->GetXml();
}
sXml += m_sText;
sXml += L"</";
sXml += m_sName;
sXml += L">";
return sXml;
}
void CXmlNodeBase::GetXml(CStringWriter& oWriter)
{
oWriter.WriteString(L"<", 1);
oWriter.WriteString(m_sName);
std::map<std::string, std::string>::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(L"='", 2);
oWriter.WriteString(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(p->second.c_str(), p->second.length(), TRUE));
oWriter.WriteString(L"'", 1);
}
oWriter.WriteString(L">", 1);
int nCount = m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
m_nodes[i]->GetXml(oWriter);
}
oWriter.WriteString(m_sText);
oWriter.WriteString(L"</", 2);
oWriter.WriteString(m_sName);
oWriter.WriteString(L">", 1);
}
class CXmlDOMDocument : public IXmlDOMDocument, public CXmlLiteReader
{
public:
CXmlNodeBase* m_pNode;
private:
CXmlNodeBase* m_pCurrentNode;
std::list<CXmlNodeBase*> m_list;
public:
CXmlDOMDocument() : IXmlDOMDocument()
{
m_pNode = NULL;
m_pCurrentNode = NULL;
}
virtual ~CXmlDOMDocument()
{
}
void WriteElement()
{
if (NULL == m_pCurrentNode)
{
m_pNode = new CXmlNodeBase();
m_pNode->m_pDocument = this;
m_pNode->m_pDocument->AddRef();
m_pCurrentNode = m_pNode;
}
else
{
// m_pCurrentNode
CXmlNodeBase* pNewBase = new CXmlNodeBase();
pNewBase->m_pDocument = this;
pNewBase->m_pDocument->AddRef();
m_pCurrentNode->m_nodes.insert(m_pCurrentNode->m_nodes.end(), pNewBase);
m_pCurrentNode = pNewBase;
}
m_pCurrentNode->m_sName = GetName();
m_list.push_back(m_pCurrentNode);
if (GetAttributesCount() > 0)
{
MoveToFirstAttribute();
std::string sName = GetNameA();
while (!sName.empty())
{
m_pCurrentNode->m_attributes.insert(std::make_pair(sName, GetTextA()));
if ( !MoveToNextAttribute() )
break;
sName = GetNameA();
}
MoveToElement();
}
if (IsEmptyNode())
{
m_list.pop_back();
if (0 != m_list.size())
{
std::list<CXmlNodeBase*>::iterator iter = m_list.end();
--iter;
m_pCurrentNode = *iter;
}
else
{
m_pCurrentNode = m_pNode;
}
}
}
void Parse()
{
ReadNextNode();
WriteElement();
int nDepth = GetDepth();
if ( 0 == xmlTextReaderIsEmptyElement(reader) )
{
XmlNodeType eNodeType = XmlNodeType_None;
int nCurDepth = -1;
// 1 ,
while( TRUE )
{
if ( 0 == xmlTextReaderRead(reader) )
break;
eNodeType = (XmlNodeType)xmlTextReaderNodeType(reader);
nCurDepth = GetDepth();
if ( eNodeType == XmlNodeType_Text || eNodeType == XmlNodeType_Whitespace || eNodeType == XmlNodeType_SIGNIFICANT_WHITESPACE )
m_pCurrentNode->m_sText += GetText();
else if (eNodeType == XmlNodeType_Element)
WriteElement();
else if (eNodeType == XmlNodeType_EndElement)
{
m_list.pop_back();
if (0 != m_list.size())
{
std::list<CXmlNodeBase*>::iterator iter = m_list.end();
--iter;
m_pCurrentNode = *iter;
}
else
{
m_pCurrentNode = m_pNode;
}
}
nCurDepth = GetDepth();
if ( nCurDepth <= nDepth )
break;
if ( XmlNodeType_EndElement == eNodeType && nCurDepth == nDepth )
break;
}
}
}
};
// CXmlNode
CXmlNode::CXmlNode(const CXmlNode& oSrc)
{
m_pBase = oSrc.m_pBase;
if (NULL != m_pBase)
m_pBase->AddRef();
}
std::wstring CXmlNode::private_GetXml()
{
if (NULL == m_pBase)
return L"";
return m_pBase->GetXml();
}
std::wstring CXmlNode::private_GetXmlFast()
{
if (NULL == m_pBase)
return L"";
CStringWriter oWriter;
m_pBase->GetXml(oWriter);
return oWriter.GetData();
}
bool CXmlNode::FromXmlFile(const std::wstring& sFile)
{
CXmlDOMDocument* m_pDocument = new CXmlDOMDocument();
BOOL bRes = m_pDocument->FromFile(sFile);
if (FALSE == bRes)
{
delete m_pDocument;
return false;
}
m_pDocument->Parse();
if (NULL == m_pDocument->m_pNode)
{
delete m_pDocument;
return false;
}
m_pBase = m_pDocument->m_pNode;
m_pBase->AddRef();
return true;
}
bool CXmlNode::FromXmlStringA(const std::string& sString)
{
CXmlDOMDocument* m_pDocument = new CXmlDOMDocument();
BOOL bRes = m_pDocument->FromStringA(sString);
if (FALSE == bRes)
{
delete m_pDocument;
return false;
}
m_pDocument->Parse();
if (NULL != m_pDocument->m_pNode)
{
delete m_pDocument;
return false;
}
m_pBase = m_pDocument->m_pNode;
m_pBase->AddRef();
return true;
}
bool CXmlNode::FromXmlString(const std::wstring& sString)
{
return FromXmlStringA(NSFile::CUtf8Converter::GetUtf8StringFromUnicode2(sString.c_str(), (LONG)sString.length()));
}
std::string CXmlNode::GetAttributeA(const std::string& sName, const std::string& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::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)
{
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()));
if (pFind == m_pBase->m_attributes.end())
return _default;
return pFind->second;
}
std::wstring CXmlNode::GetAttribute(const std::string& sName, const std::wstring& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::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);
}
std::wstring CXmlNode::GetAttribute(const std::wstring& sName, const std::wstring& _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()));
if (pFind == m_pBase->m_attributes.end())
return _default;
return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.c_str(), pFind->second.length(), TRUE);
}
int CXmlNode::GetAttributeInt(const std::string& sName, const int& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
return _default;
return atoi(pFind->second.c_str());
}
int CXmlNode::GetAttributeInt(const std::wstring& 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()));
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
return _default;
return atoi(pFind->second.c_str());
}
double CXmlNode::GetAttributeDouble(const std::string& sName, const double& _default)
{
if (!IsValid())
return _default;
std::map<std::string, std::string>::const_iterator pFind = m_pBase->m_attributes.find(sName);
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
return _default;
return atof(pFind->second.c_str());
}
double CXmlNode::GetAttributeDouble(const std::wstring& 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()));
if (pFind == m_pBase->m_attributes.end())
return _default;
if (0 == pFind->second.length())
return _default;
return atof(pFind->second.c_str());
}
std::wstring CXmlNode::GetText()
{
return (IsValid() ? m_pBase->m_sText : L"");
}
bool CXmlNode::IsValid()
{
return (NULL != m_pBase);
}
CXmlNode CXmlNode::GetNode(const std::wstring& sName)
{
CXmlNode oNode;
if (!IsValid())
return oNode;
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;
break;
}
}
return oNode;
}
CXmlNodes CXmlNode::GetNodes(const std::wstring& sName)
{
CXmlNodes oNodes;
int nCount = (int)m_pBase->m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
if (sName == m_pBase->m_nodes[i]->m_sName)
{
CXmlNode oNode;
oNode.m_pBase = m_pBase->m_nodes[i];
if (oNode.m_pBase)
oNode.m_pBase->AddRef();
oNodes.m_nodes.insert(oNodes.m_nodes.end(), oNode);
}
}
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)
{
oNodes = GetNodes(sName);
return (0 != oNodes.GetCount());
}
CXmlNode& CXmlNode::operator=(const CXmlNode& oSrc)
{
m_pBase = oSrc.m_pBase;
if (NULL != m_pBase)
m_pBase->AddRef();
return *this;
}
}
\ No newline at end of file
#pragma once
#ifdef _WIN32
#pragma comment(lib, "libxml2.lib")
#include "./win_build/config.h"
#include "win_build/Config.h"
#include "XML/include/libxml/xmlreader.h"
#include <windows.h>
#include "../Utils.h"
#include <vector>
#include <map>
#include <string>
#include "../../../../../DesktopEditor/common/File.h"
namespace XmlUtils
{
namespace NSFile
{
class CFile
{
public:
CFile()
{
m_hFileHandle = NULL;
m_lFileSize = 0;
m_lFilePosition = 0;
}
virtual ~CFile()
{
CloseFile();
}
virtual HRESULT OpenFile(CString FileName)
{
CloseFile();
HRESULT hRes = S_OK;
DWORD AccessMode = GENERIC_READ;
DWORD ShareMode = FILE_SHARE_READ;
DWORD Disposition = OPEN_EXISTING;
m_hFileHandle = ::CreateFile(FileName, AccessMode, ShareMode, NULL, Disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle)
hRes = S_FALSE;
else
class CStringWriter
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
private:
wchar_t* m_pData;
size_t m_lSize;
SetPosition(0);
}
wchar_t* m_pDataCur;
size_t m_lSizeCur;
return hRes;
}
int m_lBinaryFactor;
virtual HRESULT OpenFileRW(CString FileName)
public:
CStringWriter()
{
CloseFile();
m_pData = NULL;
m_lSize = 0;
HRESULT hRes = S_OK;
DWORD AccessMode = GENERIC_READ | GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_READ;
DWORD Disposition = OPEN_EXISTING;
m_hFileHandle = ::CreateFile(FileName, AccessMode, ShareMode, NULL, Disposition, 0, 0);
m_pDataCur = m_pData;
m_lSizeCur = m_lSize;
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle)
{
hRes = S_FALSE;
}
else
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
m_bInitTable = FALSE;
SetPosition(0);
m_lBinaryFactor = (((sizeof(wchar_t)) >> 1));
}
return hRes;
}
HRESULT ReadFile(BYTE* pData, DWORD nBytesToRead)
{
DWORD nBytesRead = 0;
if(NULL == pData)
return S_FALSE;
if(m_hFileHandle && (pData))
~CStringWriter()
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
}
return S_OK;
RELEASEMEM(m_pData);
}
HRESULT ReadFile2(BYTE* pData, DWORD nBytesToRead)
__forceinline void AddSize(size_t nSize)
{
DWORD nBytesRead = 0;
if(NULL == pData)
return S_FALSE;
if(m_hFileHandle && (pData))
if (NULL == m_pData)
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
m_lSize = max(nSize, 1000);
m_pData = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
for (size_t index = 0; index < nBytesToRead / 2; ++index)
{
BYTE temp = pData[index];
pData[index] = pData[nBytesToRead - index - 1];
pData[nBytesToRead - index - 1] = temp;
}
}
return S_OK;
}
HRESULT ReadFile3(void* pData, DWORD nBytesToRead)
{
DWORD nBytesRead = 0;
if(NULL == pData)
return S_FALSE;
if(m_hFileHandle && (pData))
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
}
return S_OK;
}
HRESULT WriteFile(void* pData, DWORD nBytesToWrite)
{
if(m_hFileHandle)
{
DWORD dwWritten = 0;
::WriteFile(m_hFileHandle, pData, nBytesToWrite, &dwWritten, NULL);
m_lFilePosition += nBytesToWrite;
}
return S_OK;
m_lSizeCur = 0;
m_pDataCur = m_pData;
return;
}
HRESULT WriteFile2(void* pData, DWORD nBytesToWrite)
if ((m_lSizeCur + nSize) > m_lSize)
{
if(m_hFileHandle)
{
BYTE* mem = new BYTE[nBytesToWrite];
memcpy(mem, pData, nBytesToWrite);
for (size_t index = 0; index < nBytesToWrite / 2; ++index)
while ((m_lSizeCur + nSize) > m_lSize)
{
BYTE temp = mem[index];
mem[index] = mem[nBytesToWrite - index - 1];
mem[nBytesToWrite - index - 1] = temp;
m_lSize *= 2;
}
DWORD dwWritten = 0;
::WriteFile(m_hFileHandle, (void*)mem, nBytesToWrite, &dwWritten, NULL);
m_lFilePosition += nBytesToWrite;
RELEASEARRAYOBJECTS(mem);
}
return S_OK;
}
HRESULT CreateFile(CString strFileName)
wchar_t* pRealloc = (wchar_t*)realloc(m_pData, m_lSize * sizeof(wchar_t));
if (NULL != pRealloc)
{
CloseFile();
DWORD AccessMode = GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_WRITE;
DWORD Disposition = CREATE_ALWAYS;
m_hFileHandle = ::CreateFile(strFileName, AccessMode, ShareMode, NULL, Disposition, FILE_ATTRIBUTE_NORMAL, NULL);
return SetPosition(0);
}
HRESULT SetPosition( ULONG64 nPos )
{
if (m_hFileHandle && nPos < (ULONG)m_lFileSize)
{
LARGE_INTEGER nTempPos;
nTempPos.QuadPart = nPos;
::SetFilePointer(m_hFileHandle, nTempPos.LowPart, &nTempPos.HighPart, FILE_BEGIN);
m_lFilePosition = nPos;
return S_OK;
//
m_pData = pRealloc;
m_pDataCur = m_pData + m_lSizeCur;
}
else
{
return (INVALID_HANDLE_VALUE == m_hFileHandle) ? S_FALSE : S_OK;
}
}
LONG64 GetPosition()
{
return m_lFilePosition;
}
HRESULT SkipBytes(ULONG64 nCount)
{
return SetPosition(m_lFilePosition + nCount);
}
HRESULT CloseFile()
{
m_lFileSize = 0;
m_lFilePosition = 0;
RELEASEHANDLE(m_hFileHandle);
return S_OK;
}
ULONG64 GetFileSize()
{
return m_lFileSize;
}
HRESULT WriteReserved(DWORD dwCount)
{
BYTE* buf = new BYTE[dwCount];
memset(buf, 0, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
RELEASEARRAYOBJECTS(buf);
return hr;
}
HRESULT WriteReserved2(DWORD dwCount)
{
BYTE* buf = new BYTE[dwCount];
memset(buf, 0xFF, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
RELEASEARRAYOBJECTS(buf);
return hr;
}
HRESULT WriteReservedTo(DWORD dwPoint)
{
if (m_lFilePosition >= dwPoint)
return S_OK;
DWORD dwCount = dwPoint - (DWORD)m_lFilePosition;
BYTE* buf = new BYTE[dwCount];
memset(buf, 0, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
RELEASEARRAYOBJECTS(buf);
return hr;
}
HRESULT SkipReservedTo(DWORD dwPoint)
{
if (m_lFilePosition >= dwPoint)
return S_OK;
wchar_t* pMalloc = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(wchar_t));
DWORD dwCount = dwPoint - (DWORD)m_lFilePosition;
return SkipBytes(dwCount);
free(m_pData);
m_pData = pMalloc;
m_pDataCur = m_pData + m_lSizeCur;
}
LONG GetProgress()
{
if (0 >= m_lFileSize)
return -1;
double dVal = (double)(100 * m_lFilePosition);
LONG lProgress = (LONG)(dVal / m_lFileSize);
return lProgress;
}
void WriteStringUTF8(CString& strXml)
{
int nLength = strXml.GetLength();
CStringA saStr;
#ifdef UNICODE
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, strXml.GetBuffer(), nLength + 1, saStr.GetBuffer(nLength*3 + 1), nLength*3, NULL, NULL);
saStr.ReleaseBuffer();
#else
wchar_t* pWStr = new wchar_t[nLength + 1];
if (!pWStr)
return;
// set end string
pWStr[nLength] = 0;
// Encoding ASCII to Unicode
MultiByteToWideChar(CP_ACP, 0, strXml, nLength, pWStr, nLength);
int nLengthW = (int)wcslen(pWStr);
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, pWStr, nLengthW + 1, saStr.GetBuffer(nLengthW*3 + 1), nLengthW*3, NULL, NULL);
saStr.ReleaseBuffer();
delete[] pWStr;
#endif
WriteFile((void*)saStr.GetBuffer(), saStr.GetLength());
}
protected:
HANDLE m_hFileHandle;
LONG64 m_lFileSize;
LONG64 m_lFilePosition;
};
}
#else //#ifdef _WIN32
#include "XML/include/libxml/xmlreader.h"
#include "../../Base/Base.h"
#include <stdlib.h>
#include <stdio.h>
#include "assert.h"
namespace NSFile
{
class CFile
{
public:
CFile()
{
m_pFileHandle = NULL;
m_lFileSize = 0;
m_lFilePosition = 0;
}
virtual ~CFile()
__forceinline void WriteString(const wchar_t* pString, const size_t& nLen)
{
CloseFile();
AddSize(nLen);
memcpy(m_pDataCur, pString, nLen << m_lBinaryFactor);
m_pDataCur += nLen;
m_lSizeCur += nLen;
}
virtual HRESULT OpenFile(CString FileName)
{
CloseFile();
#ifdef _WIN32
m_pFileHandle = _wfopen(FileName.c_str(), L"rb");
#else
std::string aMutliByteName = stringWstingToUtf8String (FileName);
m_pFileHandle = fopen(aMutliByteName.c_str(), "rb");
#endif
if (NULL == m_pFileHandle)
__forceinline void WriteString(const std::wstring& bsString)
{
throw std::exception();
WriteString(bsString.c_str(), (size_t)bsString.length());
}
// get buffer size
fseek(m_pFileHandle, 0, SEEK_END);
m_lFileSize = ftell(m_pFileHandle);
fseek(m_pFileHandle, 0, SEEK_SET);
return S_OK;
}
/*
virtual HRESULT OpenFileRW(CString FileName)
{
CloseFile();
HRESULT hRes = S_OK;
DWORD AccessMode = GENERIC_READ | GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_READ;
DWORD Disposition = OPEN_EXISTING;
m_hFileHandle = ::CreateFile(FileName, AccessMode, ShareMode, NULL, Disposition, 0, 0);
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle)
__forceinline void AddCharSafe(const TCHAR& _c)
{
hRes = S_FALSE;
AddSize(1);
*m_pDataCur++ = _c;
++m_lSizeCur;
}
else
__forceinline void AddChar2Safe(const TCHAR _c1, const TCHAR& _c2)
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
SetPosition(0);
}
return hRes;
AddSize(2);
*m_pDataCur++ = _c1;
*m_pDataCur++ = _c2;
m_lSizeCur += 2;
}
*/
HRESULT ReadFile(BYTE* pData, DWORD nBytesToRead)
inline void WriteEncodeXmlString(const std::wstring& _string)
{
if(NULL == pData)
return S_FALSE;
if(m_pFileHandle && (pData))
{
fseek(m_pFileHandle, m_lFilePosition, SEEK_SET);
size_t res = fread (pData, 1, nBytesToRead, m_pFileHandle);
m_lFilePosition += res;
if (res != nBytesToRead)
{
return S_FALSE;
}
}
return S_OK;
WriteEncodeXmlString(_string.c_str());
}
HRESULT ReadFile2(BYTE* pData, DWORD nBytesToRead)
inline void WriteEncodeXmlString(const wchar_t* pString)
{
if(NULL == pData)
return S_FALSE;
if (m_pFileHandle && (pData))
const wchar_t* pData = pString;
while (*pData != 0)
{
fseek(m_pFileHandle, m_lFilePosition, SEEK_SET);
BYTE _code = CheckCode(*pData);
size_t res = fread (pData, 1, nBytesToRead, m_pFileHandle);
m_lFilePosition += res;
for (size_t index = 0; index < nBytesToRead / 2; ++index)
switch (_code)
{
BYTE temp = pData[index];
pData[index] = pData[nBytesToRead - index - 1];
pData[nBytesToRead - index - 1] = temp;
}
}
return S_OK;
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;
}
HRESULT ReadFile3(void* pData, DWORD nBytesToRead)
{
if(NULL == pData)
return S_FALSE;
if (m_pFileHandle && (pData))
{
fseek(m_pFileHandle, m_lFilePosition, SEEK_SET);
size_t res = fread (pData, 1, nBytesToRead, m_pFileHandle);
m_lFilePosition += res;
++pData;
}
return S_OK;
}
HRESULT WriteFile(void* pData, DWORD nBytesToWrite)
__forceinline size_t GetCurSize()
{
DWORD dwWritten = 0;
if (m_pFileHandle)
{
dwWritten = fwrite (pData, 1, nBytesToWrite, m_pFileHandle);
m_lFilePosition += nBytesToWrite;
}
return (dwWritten == nBytesToWrite) ? S_OK : S_FALSE;
return m_lSizeCur;
}
HRESULT WriteFile2(void* pData, DWORD nBytesToWrite)
{
DWORD dwWritten = 0;
if(m_pFileHandle)
__forceinline void Write(CStringWriter& oWriter)
{
BYTE* mem = new BYTE[nBytesToWrite];
memcpy(mem, pData, nBytesToWrite);
WriteString(oWriter.m_pData, oWriter.m_lSizeCur);
}
for (size_t index = 0; index < nBytesToWrite / 2; ++index)
inline void Clear()
{
BYTE temp = mem[index];
mem[index] = mem[nBytesToWrite - index - 1];
mem[nBytesToWrite - index - 1] = temp;
}
RELEASEMEM(m_pData);
dwWritten = fwrite (pData, 1, nBytesToWrite, m_pFileHandle);
m_lFilePosition += nBytesToWrite;
delete [] mem;
}
return (dwWritten == nBytesToWrite) ? S_OK : S_FALSE;
}
m_pData = NULL;
m_lSize = 0;
HRESULT CreateFile(CString strFileName)
{
CloseFile();
#ifdef _WIN32
m_pFileHandle = _wfopen(strFileName.c_str(), L"w");
#else
std::string aMutliByteName = stringWstingToUtf8String (strFileName);
m_pFileHandle = fopen(aMutliByteName.c_str(), "w");
#endif
return SetPosition(0);
}
HRESULT SetPosition( ULONG64 nPos )
{
if (m_pFileHandle && nPos < (ULONG)m_lFileSize)
{
fseek(m_pFileHandle, nPos, SEEK_SET);
m_lFilePosition = nPos;
return S_OK;
}
else
{
return (NULL == m_pFileHandle) ? S_FALSE : S_OK;
}
}
LONG64 GetPosition()
{
return m_lFilePosition;
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
HRESULT SkipBytes(ULONG64 nCount)
inline void ClearNoAttack()
{
return SetPosition(m_lFilePosition + nCount);
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
HRESULT CloseFile()
{
m_lFileSize = 0;
m_lFilePosition = 0;
if (NULL != m_pFileHandle)
std::wstring GetData()
{
fclose (m_pFileHandle);
}
return S_OK;
std::wstring str(m_pData, (int)m_lSizeCur);
return str;
}
ULONG64 GetFileSize()
{
return m_lFileSize;
}
protected:
BYTE m_arTableUnicodes[65536];
BOOL m_bInitTable;
HRESULT WriteReserved(DWORD dwCount)
{
BYTE* buf = new BYTE[dwCount];
memset(buf, 0, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
delete [] buf;
return hr;
}
HRESULT WriteReserved2(DWORD dwCount)
{
BYTE* buf = new BYTE[dwCount];
memset(buf, 0xFF, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
delete [] buf;
return hr;
}
HRESULT WriteReservedTo(DWORD dwPoint)
protected:
BYTE CheckCode(const WCHAR& c)
{
if (m_lFilePosition >= dwPoint)
return S_OK;
DWORD dwCount = dwPoint - (DWORD)m_lFilePosition;
BYTE* buf = new BYTE[dwCount];
memset(buf, 0, (size_t)dwCount);
HRESULT hr = WriteFile(buf, dwCount);
delete [] buf;
return hr;
}
HRESULT SkipReservedTo(DWORD dwPoint)
if (!m_bInitTable)
{
if (m_lFilePosition >= dwPoint)
return S_OK;
memset(m_arTableUnicodes, 0, 65536);
m_arTableUnicodes[0x0009] = 1;
m_arTableUnicodes[0x000A] = 1;
m_arTableUnicodes[0x000D] = 1;
DWORD dwCount = dwPoint - (DWORD)m_lFilePosition;
return SkipBytes(dwCount);
}
memset(m_arTableUnicodes + 0x0020, 1, 0xD7FF - 0x0020 + 1);
memset(m_arTableUnicodes + 0xE000, 1, 0xFFFD - 0xE000 + 1);
LONG GetProgress()
{
if (0 >= m_lFileSize)
return -1;
m_arTableUnicodes['&'] = 2;
m_arTableUnicodes['\''] = 3;
m_arTableUnicodes['<'] = 4;
m_arTableUnicodes['>'] = 5;
m_arTableUnicodes['\"'] = 6;
double dVal = (double)(100 * m_lFilePosition);
LONG lProgress = (LONG)(dVal / m_lFileSize);
return lProgress;
m_bInitTable = TRUE;
}
void WriteStringUTF8(CString& strXml)
{
std::string utf8_str;
#ifdef UNICODE
// Encoding Unicode to UTF-8
utf8_str = stringWstingToUtf8String (strXml);
#else
utf8_str = strXml;
/*
wchar_t* pWStr = new wchar_t[nLength + 1];
if (!pWStr)
return;
// set end string
pWStr[nLength] = 0;
// Encoding ASCII to Unicode
MultiByteToWideChar(CP_ACP, 0, strXml, nLength, pWStr, nLength);
int nLengthW = (int)wcslen(pWStr);
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, pWStr, nLengthW + 1, saStr.GetBuffer(nLengthW*3 + 1), nLengthW*3, NULL, NULL);
saStr.ReleaseBuffer();
delete[] pWStr;
*/
#endif
WriteFile((void*)utf8_str.c_str(), utf8_str.size());
return m_arTableUnicodes[c];
}
protected:
FILE *m_pFileHandle;
long m_lFileSize;
long m_lFilePosition;
};
}
#endif //#ifdef _WIN32
typedef
enum XmlNodeType
......@@ -629,6 +268,7 @@ namespace NSFile
class CXmlLiteReader
{
protected:
xmlTextReaderPtr reader;
BYTE* m_pStream;
......@@ -646,10 +286,16 @@ namespace NSFile
delete []m_pStream;
}
xmlTextReaderPtr getNativeReader() { return reader; }
public:
inline void Clear()
{
if (NULL != m_pStream)
delete []m_pStream;
m_pStream = NULL;
m_lStreamLen = 0;
}
inline BOOL IsValid()
......@@ -657,30 +303,39 @@ namespace NSFile
return ( NULL != reader );
}
inline BOOL FromFile(CString& sFilePath)
inline BOOL FromFile(const std::wstring& sFilePath)
{
Clear();
NSFile::CFile oFile;
NSFile::CFileBinary oFile;
oFile.OpenFile(sFilePath);
m_lStreamLen = (int)oFile.GetFileSize();
m_lStreamLen = (LONG)oFile.GetFileSize();
m_pStream = new BYTE[m_lStreamLen];
oFile.ReadFile(m_pStream, (DWORD)m_lStreamLen);
DWORD dwRead = 0;
oFile.ReadFile(m_pStream, (DWORD)m_lStreamLen, dwRead);
oFile.CloseFile();
reader = xmlReaderForMemory((char*)m_pStream, m_lStreamLen, NULL, NULL, 0);
return TRUE;
}
inline BOOL FromString(CString& sXml)
inline BOOL FromString(const std::wstring& sXml)
{
Clear();
UnicodeToUtf8(sXml, m_pStream, m_lStreamLen);
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sXml.c_str(), sXml.length(), m_pStream, m_lStreamLen, false);
reader = xmlReaderForMemory((char*)m_pStream, m_lStreamLen, NULL, NULL, 0);
return TRUE;
}
inline BOOL FromStringA(const std::string& sXml)
{
Clear();
reader = xmlReaderForMemory((char*)sXml.c_str(), sXml.length(), NULL, NULL, 0);
return TRUE;
}
inline BOOL Read(XmlNodeType &oNodeType)
{
if ( !IsValid() )
......@@ -770,13 +425,32 @@ namespace NSFile
return TRUE;
}
inline const wchar_t* GetName()
inline std::wstring GetName()
{
if ( !IsValid() )
return NULL;
return L"";
xmlChar* pName = xmlTextReaderName(reader);
if (NULL == pName)
return L"";
std::wstring s = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pName, (LONG)strlen((const char*)pName));
delete [] pName;
return s;
}
inline std::string GetNameA()
{
if ( !IsValid() )
return "";
xmlChar* pName = xmlTextReaderName(reader);
return UnicodeFromUtf8(pName);
if (NULL == pName)
return "";
//return std::string((const char*)pName);
std::string s((const char*)pName);
delete [] pName;
return s;
}
inline int GetDepth()
{
......@@ -793,21 +467,39 @@ namespace NSFile
return xmlTextReaderIsEmptyElement(reader);
}
inline const WCHAR *GetText()
inline std::wstring GetText()
{
if ( !IsValid() )
return NULL;
xmlChar* pValue = xmlTextReaderValue(reader);
if (NULL == pValue)
return L"";
std::wstring s = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pValue, (LONG)strlen((const char*)pValue));
delete [] pValue;
return s;
}
inline std::string GetTextA()
{
if ( !IsValid() )
return NULL;
xmlChar* pValue = xmlTextReaderValue(reader);
return UnicodeFromUtf8(pValue);
if (NULL == pValue)
return "";
std::string s((const char*)pValue);
delete [] pValue;
return s;
}
inline CString GetText2()
inline std::wstring GetText2()
{
if ( !IsValid() )
return _T("");
// TO DO: ( CString)
CString sResult;
std::wstring sResult;
if ( xmlTextReaderIsEmptyElement(reader) )
return sResult;
......@@ -822,11 +514,11 @@ namespace NSFile
return sResult;
}
inline CString GetOuterXml()
inline std::wstring GetOuterXml()
{
return GetXml(false);
}
inline CString GetInnerXml()
inline std::wstring GetInnerXml()
{
return GetXml(true);
}
......@@ -860,10 +552,10 @@ namespace NSFile
return (BOOL)xmlTextReaderMoveToElement(reader);
}
private:
inline CString GetXml(bool bInner)
inline std::wstring GetXml(bool bInner)
{
if ( !IsValid() )
return _T("");
return L"";
CStringWriter oResult;
if(false == bInner)
......@@ -916,8 +608,8 @@ namespace NSFile
if(GetAttributesCount() > 0)
{
MoveToFirstAttribute();
CString sName = GetName();
while( !sName.IsEmpty() )
std::wstring sName = GetName();
while( !sName.empty() )
{
oResult.AddCharSafe(TCHAR(' '));
oResult.WriteEncodeXmlString(GetName());
......@@ -936,66 +628,159 @@ namespace NSFile
else
oResult.AddCharSafe(TCHAR('>'));
}
};
WCHAR* UnicodeFromUtf8(xmlChar* pValue)
class IXmlDOMDocument
{
BYTE* pBuffer = (BYTE*)pValue;
LONG lCount = strlen((char*)pValue);
LONG lLenght = 0;
private:
ULONG m_lRef;
WCHAR* pUnicodeString = new WCHAR[lCount + 1];
LONG lIndexUnicode = 0;
public:
IXmlDOMDocument()
{
m_lRef = 1;
}
virtual ~IXmlDOMDocument()
{
}
for (LONG lIndex = 0; lIndex < lCount; ++lIndex)
virtual ULONG AddRef()
{
if (0x00 == (0x80 & pBuffer[lIndex]))
++m_lRef;
return m_lRef;
}
virtual ULONG Release()
{
//strRes += (WCHAR)pBuffer[lIndex];
pUnicodeString[lIndexUnicode++] = (WCHAR)pBuffer[lIndex];
continue;
ULONG lReturn = --m_lRef;
if (0 == m_lRef)
delete this;
return lReturn;
}
else if (0x00 == (0x20 & pBuffer[lIndex]))
};
class CXmlNodeBase : public IXmlDOMDocument
{
WCHAR mem = (WCHAR)(((pBuffer[lIndex] & 0x1F) << 6) + (pBuffer[lIndex + 1] & 0x3F));
public:
IXmlDOMDocument* m_pDocument;
//strRes += mem;
pUnicodeString[lIndexUnicode++] = mem;
std::map<std::string, std::string> m_attributes;
std::vector<CXmlNodeBase*> m_nodes;
std::wstring m_sText;
std::wstring m_sName;
lIndex += 1;
public:
CXmlNodeBase() : IXmlDOMDocument()
{
m_pDocument = NULL;
m_sText = L"";
m_sName = L"";
}
else if (0x00 == (0x10 & pBuffer[lIndex]))
~CXmlNodeBase()
{
WCHAR mem = (WCHAR)(((pBuffer[lIndex] & 0x0F) << 12) + ((pBuffer[lIndex + 1] & 0x3F) << 6) + (pBuffer[lIndex + 2] & 0x3F));
if (NULL != m_pDocument)
m_pDocument->Release();
//strRes += mem;
pUnicodeString[lIndexUnicode++] = mem;
lIndex += 2;
int nCount = (int)m_nodes.size();
for (int i = 0; i < nCount; ++i)
{
CXmlNodeBase* pNode = m_nodes[i];
if (NULL != pNode)
delete pNode;
}
else
}
void AddRefDoc()
{
BYTE mem = pBuffer[lIndex];
//pUnicodeString[lIndexUnicode++] = mem;
if (NULL != m_pDocument)
m_pDocument->AddRef();
}
void ReleaseDoc()
{
if (NULL != m_pDocument)
m_pDocument->Release();
}
pUnicodeString[lIndexUnicode] = 0;
return pUnicodeString;
std::wstring GetXml();
void GetXml(CStringWriter& oWriter);
};
class CXmlNodes;
class CXmlNode
{
private:
CXmlNodeBase* m_pBase;
public:
CXmlNode()
{
m_pBase = NULL;
}
~CXmlNode()
{
if (NULL != m_pBase)
m_pBase->Release();
}
CXmlNode(const CXmlNode& oSrc);
void UnicodeToUtf8(CString& strXml, BYTE*& pBuffer, LONG& lLen)
public:
bool FromXmlFile(const std::wstring& sFile);
bool FromXmlStringA(const std::string& 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 = "");
std::wstring GetAttribute(const std::string& sName, const std::wstring& _default = L"");
std::wstring GetAttribute(const std::wstring& sName, const std::wstring& _default = L"");
int GetAttributeInt(const std::string& sName, const int& _default = 0);
int GetAttributeInt(const std::wstring& sName, const int& _default = 0);
double GetAttributeDouble(const std::string& sName, const double& _default = 0);
double GetAttributeDouble(const std::wstring& sName, const double& _default = 0);
std::wstring GetText();
bool IsValid();
CXmlNode GetNode(const std::wstring& sName);
CXmlNodes GetNodes(const std::wstring& sName);
bool GetNode(const std::wstring& sName, CXmlNode& oNode);
bool GetNodes(const std::wstring& sName, CXmlNodes& oNodes);
CXmlNode& operator=(const CXmlNode& oSrc);
public:
std::wstring private_GetXml();
std::wstring private_GetXmlFast();
};
class CXmlNodes
{
int nLength = strXml.GetLength();
private:
std::vector<CXmlNode> m_nodes;
pBuffer = new BYTE[nLength*3 + 1];
public:
CXmlNodes() : m_nodes()
{
}
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, strXml.GetBuffer(), nLength + 1, (LPSTR)pBuffer, nLength*3, NULL, NULL);
lLen = strlen((LPSTR)pBuffer);
int GetCount()
{
return (int)m_nodes.size();
}
};
bool GetAt(int nIndex, CXmlNode& oXmlNode)
{
if (nIndex < 0 && nIndex >= GetCount())
return false;
oXmlNode = m_nodes[nIndex];
return true;
}
friend class CXmlNode;
};
}
#if 0
......
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