diff --git a/Common/DocxFormat/Projects/DocxFormat2005.vcproj b/Common/DocxFormat/Projects/DocxFormat2005.vcproj index 1614ad8f3d..b23e686b0b 100644 --- a/Common/DocxFormat/Projects/DocxFormat2005.vcproj +++ b/Common/DocxFormat/Projects/DocxFormat2005.vcproj @@ -1349,6 +1349,62 @@ > + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Common/DocxFormat/Source/XML/Utils.h b/Common/DocxFormat/Source/XML/Utils.h index fa9111e190..954f700889 100644 --- a/Common/DocxFormat/Source/XML/Utils.h +++ b/Common/DocxFormat/Source/XML/Utils.h @@ -219,7 +219,7 @@ namespace XmlUtils sResult.Replace(_T("\""), _T(""")); 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_ } diff --git a/Common/DocxFormat/Source/XML/libxml2/libxml2.cpp b/Common/DocxFormat/Source/XML/libxml2/libxml2.cpp index a73e5eced5..4684b35797 100644 --- a/Common/DocxFormat/Source/XML/libxml2/libxml2.cpp +++ b/Common/DocxFormat/Source/XML/libxml2/libxml2.cpp @@ -1,22 +1,22 @@ -#include "LIBXML2_LIB/stdafx.h" +//#include "LIBXML2_LIB/stdafx.h" #include "./libxml2.h" #include namespace XmlUtils { - std::wstring CXmlNodeBase::GetXml() + CString CXmlNodeBase::GetXml() { - std::wstring sXml = L"<"; + CString sXml = L"<"; sXml += m_sName; - std::map::iterator p; + std::map::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::iterator p; + std::map::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,158 +271,238 @@ 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) - { - if (!IsValid()) - return _default; - - std::map::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::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::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::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::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::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::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::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() + 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(); + } - CXmlNode CXmlNode::GetNode(const std::wstring& sName) + 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::const_iterator pFind = m_pBase->m_attributes.find(sName); + if (pFind == m_pBase->m_attributes.end()) + return _default; + + return pFind->second; + } + CStringA CXmlNode::GetAttributeA(const CString& sName, const CStringA& _default) + { + if (!IsValid()) + return _default; + + std::map::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 pFind->second; + } + + CString CXmlNode::GetAttribute(const CStringA& sName, const CString& _default) + { + if (!IsValid()) + return _default; + + std::map::const_iterator pFind = m_pBase->m_attributes.find(sName); + if (pFind == m_pBase->m_attributes.end()) + return _default; + + return std_string2string(NSFile::CUtf8Converter::GetUnicodeFromCharPtr(pFind->second.GetString(), pFind->second.GetLength(), TRUE)); + } + CString CXmlNode::GetAttribute(const CString& sName, const CString& _default) + { + if (!IsValid()) + return _default; + + std::map::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 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 CStringA& sName, const int& _default) + { + if (!IsValid()) + return _default; + + std::map::const_iterator pFind = m_pBase->m_attributes.find(sName); + + if (pFind == m_pBase->m_attributes.end()) + return _default; + + if (0 == pFind->second.GetLength()) + return _default; + + return atoi(pFind->second.GetString()); + } + int CXmlNode::GetAttributeInt(const CString& sName, const int& _default) + { + if (!IsValid()) + return _default; + + std::map::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.GetLength()) + return _default; + + return atoi(pFind->second.GetString()); + } + int CXmlNode::ReadAttributeInt(const CString& str, const int& nDef) + { + return GetAttributeInt(str, nDef); + } + + double CXmlNode::GetAttributeDouble(const CStringA& sName, const double& _default) + { + if (!IsValid()) + return _default; + + std::map::const_iterator pFind = m_pBase->m_attributes.find(sName); + + if (pFind == m_pBase->m_attributes.end()) + return _default; + + if (0 == pFind->second.GetLength()) + return _default; + + return atof(pFind->second.GetString()); + } + double CXmlNode::GetAttributeDouble(const CString& sName, const double& _default) + { + if (!IsValid()) + return _default; + + std::map::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.GetLength()) + return _default; + + return atof(pFind->second.GetString()); + } + + CString CXmlNode::ReadNodeTextBase(const wchar_t* bsName) + { + return ReadNodeText(CString(bsName)); + } + CString CXmlNode::ReadNodeText(const CString& strName) + { + 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::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 diff --git a/Common/DocxFormat/Source/XML/libxml2/libxml2.h b/Common/DocxFormat/Source/XML/libxml2/libxml2.h index 441a8caa9d..405e9f856c 100644 --- a/Common/DocxFormat/Source/XML/libxml2/libxml2.h +++ b/Common/DocxFormat/Source/XML/libxml2/libxml2.h @@ -9,241 +9,240 @@ #include #include +#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; - class CStringWriter - { - private: - wchar_t* m_pData; - size_t m_lSize; + // wchar_t* m_pDataCur; + // size_t m_lSizeCur; - wchar_t* m_pDataCur; - size_t m_lSizeCur; + // int m_lBinaryFactor; - int m_lBinaryFactor; + //public: + // CStringWriter() + // { + // m_pData = NULL; + // m_lSize = 0; - public: - CStringWriter() - { - m_pData = NULL; - m_lSize = 0; + // m_pDataCur = m_pData; + // m_lSizeCur = m_lSize; - m_pDataCur = m_pData; - m_lSizeCur = m_lSize; + // m_bInitTable = FALSE; - m_bInitTable = FALSE; + // m_lBinaryFactor = (((sizeof(wchar_t)) >> 1)); + // } + // ~CStringWriter() + // { + // RELEASEMEM(m_pData); + // } - 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)); - 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; + // } - m_lSizeCur = 0; - m_pDataCur = m_pData; - return; - } + // if ((m_lSizeCur + nSize) > m_lSize) + // { + // while ((m_lSizeCur + nSize) > m_lSize) + // { + // m_lSize *= 2; + // } - 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)); - 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; + // } + // } + // } - free(m_pData); - m_pData = pMalloc; - m_pDataCur = m_pData + m_lSizeCur; - } - } - } + //public: - 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; + // } - 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 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); - 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; + // } + // } - 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 size_t GetCurSize() - { - return m_lSizeCur; - } + // AVSINLINE void Write(CStringWriter& oWriter) + // { + // WriteString(oWriter.m_pData, oWriter.m_lSizeCur); + // } - AVSINLINE void Write(CStringWriter& oWriter) - { - WriteString(oWriter.m_pData, oWriter.m_lSizeCur); - } + // inline void Clear() + // { + // RELEASEMEM(m_pData); - inline void Clear() - { - RELEASEMEM(m_pData); + // m_pData = NULL; + // m_lSize = 0; - m_pData = NULL; - m_lSize = 0; + // m_pDataCur = m_pData; + // m_lSizeCur = 0; + // } + // inline void ClearNoAttack() + // { + // m_pDataCur = m_pData; + // m_lSizeCur = 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; + // } - std::wstring GetData() - { - std::wstring str(m_pData, (int)m_lSizeCur); - return str; - } + //protected: + // BYTE m_arTableUnicodes[65536]; + // BOOL m_bInitTable; - 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; - 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); - 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]; - } - }; + // 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 m_attributes; + std::map m_attributes; std::vector 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 = ""); - - 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(); + void Clear(); + CString GetName(); + CString GetText(); - CXmlNode GetNode(const std::wstring& sName); - CXmlNodes GetNodes(const std::wstring& sName); + CString ReadAttributeBase(const wchar_t* bstrName); + template + void ReadAttributeBase(const wchar_t* bsName, T& value) + { + value = GetAttribute(CString(bsName)).GetString(); + } + CString ReadAttribute(const CString& strAttibuteName); + template + void ReadAllAttributes(T& strNames, T& strValues) + { + if (!IsValid()) + return; - bool GetNode(const std::wstring& sName, CXmlNode& oNode); - bool GetNodes(const std::wstring& sName, CXmlNodes& oNodes); + std::map::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 = ""); + + 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); + + double GetAttributeDouble(const CStringA& sName, const double& _default = 0); + double GetAttributeDouble(const CString& sName, const double& _default = 0); + + + CString ReadNodeTextBase(const wchar_t* bsName); + template + 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("")); + + template + void LoadArray(const CString& sName, std::vector& arList) + { + CXmlNodes oNodes; + if (GetNodes(sName, oNodes)) + { + int nCount = oNodes.GetCount(); + for (int i = 0; i < nCount; ++i) + { + CXmlNode oItem; + oNodes.GetAt(i, oItem); + + arList.reserve(arList.size() + 1); + arList[i].fromXML(oItem); + } + } + } + template + void LoadArray(const CString& sName, const CString& sSubName, std::vector& arList) + { + CXmlNode oNode; + if (GetNode(sName, oNode)) + oNode.LoadArray(sSubName, arList); + } + CXmlNode ReadNode(const CString& strNodeName); + CXmlNode ReadNodeNoNS(const CString& strNodeName); + + 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 diff --git a/Common/DocxFormat/Source/XML/stringcommon.cpp b/Common/DocxFormat/Source/XML/stringcommon.cpp new file mode 100644 index 0000000000..f4b765206f --- /dev/null +++ b/Common/DocxFormat/Source/XML/stringcommon.cpp @@ -0,0 +1,18 @@ +#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 diff --git a/Common/DocxFormat/Source/XML/stringcommon.h b/Common/DocxFormat/Source/XML/stringcommon.h new file mode 100644 index 0000000000..9d9c4be81a --- /dev/null +++ b/Common/DocxFormat/Source/XML/stringcommon.h @@ -0,0 +1,16 @@ +#ifndef XML_COMMON +#define XML_COMMON + +#include +#ifdef _WIN32 +#include +#include +#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 diff --git a/Common/DocxFormat/Source/XML/xmlutils.h b/Common/DocxFormat/Source/XML/xmlutils.h index 514b80490f..f841e51cee 100644 --- a/Common/DocxFormat/Source/XML/xmlutils.h +++ b/Common/DocxFormat/Source/XML/xmlutils.h @@ -1,8 +1,296 @@ #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(""); + } + 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(""); + } + 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 rename_namespace("XML") @@ -317,176 +605,6 @@ namespace XmlUtils 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; - - 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(""); - } - 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(""); - } - 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("\"")); - } - }; 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 {