summaryrefslogtreecommitdiff
path: root/plugins/Quotes/src/XMLEngineMI.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2016-02-25 13:36:37 +0000
committerGeorge Hazan <george.hazan@gmail.com>2016-02-25 13:36:37 +0000
commit42095fb5cb7228f9dfb94965988029fd7f47b793 (patch)
tree6185ec5d7450fb474ed80b0ce95d0d1c0e0b26a8 /plugins/Quotes/src/XMLEngineMI.cpp
parent8e891173c74ceaa0964f32be247f380fd5f8bcd1 (diff)
Quotes: major code cleaning
git-svn-id: http://svn.miranda-ng.org/main/trunk@16335 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Quotes/src/XMLEngineMI.cpp')
-rw-r--r--plugins/Quotes/src/XMLEngineMI.cpp228
1 files changed, 85 insertions, 143 deletions
diff --git a/plugins/Quotes/src/XMLEngineMI.cpp b/plugins/Quotes/src/XMLEngineMI.cpp
index 6cd1ca5863..93d87ce87c 100644
--- a/plugins/Quotes/src/XMLEngineMI.cpp
+++ b/plugins/Quotes/src/XMLEngineMI.cpp
@@ -1,125 +1,91 @@
#include "StdAfx.h"
-namespace
+class CXMLNodeMI : public IXMLNode, private boost::noncopyable
{
- class CXMLNodeMI : public IXMLNode,
- private boost::noncopyable
- {
- public:
- typedef boost::shared_ptr<IXMLNode> TXMLNodePtr;
+public:
+ typedef boost::shared_ptr<IXMLNode> TXMLNodePtr;
- public:
- explicit CXMLNodeMI(HXML hXMl, bool bDestroy = false) : m_hXML(hXMl), m_bDestroy(bDestroy)
- {
- assert(m_hXML);
- }
+public:
+ explicit CXMLNodeMI(HXML hXMl, bool bDestroy = false) : m_hXML(hXMl), m_bDestroy(bDestroy)
+ {
+ assert(m_hXML);
+ }
- virtual ~CXMLNodeMI()
- {
- if (m_bDestroy)
- {
- xmlDestroyNode(m_hXML);
- }
- }
+ virtual ~CXMLNodeMI()
+ {
+ if (m_bDestroy)
+ xmlDestroyNode(m_hXML);
+ }
- virtual size_t GetChildCount()const
- {
- return xmlGetChildCount(m_hXML);
- }
+ virtual size_t GetChildCount()const
+ {
+ return xmlGetChildCount(m_hXML);
+ }
- virtual TXMLNodePtr GetChildNode(size_t nIndex)const
- {
- HXML h = xmlGetChild(m_hXML, (int)nIndex);
- if (h)
- {
- return TXMLNodePtr(new CXMLNodeMI(h));
- }
- else
- {
- return TXMLNodePtr();
- }
- }
+ virtual TXMLNodePtr GetChildNode(size_t nIndex)const
+ {
+ HXML h = xmlGetChild(m_hXML, (int)nIndex);
+ if (h)
+ return TXMLNodePtr(new CXMLNodeMI(h));
- virtual tstring GetText()const
- {
- tstring sResult;
- LPCTSTR psz = xmlGetText(m_hXML);
- if (psz)
- {
- sResult = psz;
- }
+ return TXMLNodePtr();
+ }
- return sResult;
- }
+ virtual tstring GetText()const
+ {
+ tstring sResult;
+ LPCTSTR psz = xmlGetText(m_hXML);
+ if (psz)
+ sResult = psz;
- virtual tstring GetName()const
- {
- tstring sResult;
- LPCTSTR psz = xmlGetName(m_hXML);
- if (psz)
- {
- sResult = psz;
- }
+ return sResult;
+ }
- return sResult;
- }
+ virtual tstring GetName()const
+ {
+ tstring sResult;
+ LPCTSTR psz = xmlGetName(m_hXML);
+ if (psz)
+ sResult = psz;
- virtual bool AddChild(const TXMLNodePtr& pNode)
- {
- CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get());
- if (pXML)
- {
- xmlAddChild2(pXML->m_hXML, m_hXML);
- pXML->m_bDestroy = false;
- return true;
- }
- else
- {
- return false;
- }
- }
+ return sResult;
+ }
- virtual bool AddAttribute(const tstring& rsName, const tstring& rsValue)
- {
- xmlAddAttr(m_hXML, rsName.c_str(), rsValue.c_str());
+ virtual bool AddChild(const TXMLNodePtr& pNode)
+ {
+ CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get());
+ if (pXML) {
+ xmlAddChild2(pXML->m_hXML, m_hXML);
+ pXML->m_bDestroy = false;
return true;
}
- virtual tstring GetAttributeValue(const tstring& rsAttrName)
- {
- LPCTSTR pszValue = xmlGetAttrValue(m_hXML, rsAttrName.c_str());
- return ((NULL != pszValue) ? tstring(pszValue) : tstring());
- }
+ return false;
+ }
- virtual void Write(tostream& o)const
- {
- // struct safe_string
- // {
- // safe_string(LPTSTR p):m_p(p){}
- // ~safe_string(){xmlFree(m_p);}
- //
- // LPTSTR m_p;
- // };
- //
- // struct mir_safe_string
- // {
- // mir_safe_string(LPSTR p) : m_p(p){}
- // ~mir_safe_string(){mir_free(m_p);}
- //
- // LPSTR m_p;
- // };
-
-
- safe_string<TCHAR> ss(xmlToString(m_hXML, NULL));
- if (ss.m_p)
- o << (char*)T2Utf(ss.m_p);
- }
+ virtual bool AddAttribute(const tstring& rsName, const tstring& rsValue)
+ {
+ xmlAddAttr(m_hXML, rsName.c_str(), rsValue.c_str());
+ return true;
+ }
- private:
- HXML m_hXML;
- bool m_bDestroy;
- };
-}
+ virtual tstring GetAttributeValue(const tstring& rsAttrName)
+ {
+ LPCTSTR pszValue = xmlGetAttrValue(m_hXML, rsAttrName.c_str());
+ return ((NULL != pszValue) ? tstring(pszValue) : tstring());
+ }
+
+ virtual void Write(tostream& o)const
+ {
+ safe_string<TCHAR> ss(xmlToString(m_hXML, NULL));
+ if (ss.m_p)
+ o << (char*)T2Utf(ss.m_p);
+ }
+
+private:
+ HXML m_hXML;
+ bool m_bDestroy;
+};
CXMLEngineMI::CXMLEngineMI()
{
@@ -131,38 +97,23 @@ CXMLEngineMI::~CXMLEngineMI()
IXMLNode::TXMLNodePtr CXMLEngineMI::LoadFile(const tstring& rsFileName)const
{
- // struct mir_safe_string
- // {
- // mir_safe_string(LPTSTR p) : m_p(p){}
- // ~mir_safe_string(){mir_free(m_p);}
- //
- // LPTSTR m_p;
- // };
-
-
IXMLNode::TXMLNodePtr pResult;
FILE* stream;
- if (0 == ::_tfopen_s(&stream, rsFileName.c_str(), _T("r")))
- {
+ if (0 == ::_tfopen_s(&stream, rsFileName.c_str(), _T("r"))) {
struct _stat st;
- if (-1 != ::_fstat(::_fileno(stream), &st))
- {
+ if (-1 != ::_fstat(::_fileno(stream), &st)) {
std::vector<char> aBuffer(st.st_size + 1);
char* pBuffer = &*(aBuffer.begin());
size_t cBytes = ::fread(pBuffer, sizeof(char), st.st_size, stream);
- if (cBytes > 0 && cBytes <= static_cast<size_t>(st.st_size))
- {
+ if (cBytes > 0 && cBytes <= static_cast<size_t>(st.st_size)) {
pBuffer[cBytes] = '\0';
int nLen = (int)cBytes;
mir_safe_string<TCHAR> ss(mir_utf8decodeT(pBuffer));
- if (ss.m_p)
- {
+ if (ss.m_p) {
HXML h = xmlParseString(ss.m_p, &nLen, NULL);
if (h)
- {
pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true));
- }
}
}
}
@@ -172,39 +123,30 @@ IXMLNode::TXMLNodePtr CXMLEngineMI::LoadFile(const tstring& rsFileName)const
return pResult;
}
-namespace
+static IXMLNode::TXMLNodePtr create_node(const tstring& rsName, const tstring& rsText, bool bIsDecl)
{
- IXMLNode::TXMLNodePtr create_node(const tstring& rsName, const tstring& rsText, bool bIsDecl)
- {
- IXMLNode::TXMLNodePtr pResult;
- HXML h = xmlCreateNode(rsName.c_str(), rsText.c_str(), bIsDecl);
- if (h)
- {
- pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true));
- }
+ IXMLNode::TXMLNodePtr pResult;
+ HXML h = xmlCreateNode(rsName.c_str(), rsText.c_str(), bIsDecl);
+ if (h)
+ pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true));
- return pResult;
- }
+ return pResult;
}
bool CXMLEngineMI::SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodePtr& pNode)const
{
CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get());
- if (pXML)
- {
+ if (pXML) {
tofstream file(rsFileName.c_str());
- if (file.good())
- {
+ if (file.good()) {
IXMLNode::TXMLNodePtr pRoot(create_node(_T("xml"), tstring(), true));
- if (pRoot)
- {
+ if (pRoot) {
pRoot->AddAttribute(_T("version"), _T("1.0"));
pRoot->AddAttribute(_T("encoding"), _T("UTF-8"));
file << *pRoot;
}
- if (file.good())
- {
+ if (file.good()) {
file << *pNode;
}
}
@@ -218,4 +160,4 @@ bool CXMLEngineMI::SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodeP
IXMLNode::TXMLNodePtr CXMLEngineMI::CreateNode(const tstring& rsName, const tstring& rsText)const
{
return create_node(rsName, rsText, false);
-} \ No newline at end of file
+}