Improved pars of the OFD format structure

This commit is contained in:
Green
2025-03-19 15:47:49 +03:00
parent 84ecffa75e
commit 7252d29579
22 changed files with 337 additions and 152 deletions

View File

@ -28,8 +28,10 @@ HEADERS += \
src/Document.h \
src/Page.h \
src/PublicRes.h \
src/Types/Color.h \
src/Utils/Types.h \
src/Utils/Utils.h
src/Utils/Utils.h \
src/Utils/XmlReader.h
SOURCES += \
OFDFile.cpp \
@ -41,7 +43,6 @@ SOURCES += \
src/Document.cpp \
src/Page.cpp \
src/PublicRes.cpp \
src/Utils/Types.cpp
HEADERS += $$CORE_ROOT_DIR/OOXML/Base/Unit.h
SOURCES += $$CORE_ROOT_DIR/OOXML/Base/Unit.cpp
src/Types/Color.cpp \
src/Utils/Types.cpp \
src/Utils/XmlReader.cpp

View File

@ -26,7 +26,7 @@ CDocInfo::CDocInfo()
: m_eDocUsage(EDocUsege::Normal)
{}
bool CDocInfo::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CDocInfo::Read(CXmlReader& oLiteReader)
{
if (L"ofd:DocInfo" != oLiteReader.GetName())
return false;
@ -61,7 +61,7 @@ CDocBody::CDocBody()
}
CDocBody* CDocBody::Read(XmlUtils::CXmlLiteReader& oLiteReader, IFolder* pFolder)
CDocBody* CDocBody::Read(CXmlReader& oLiteReader, IFolder* pFolder)
{
if (L"ofd:DocBody" != oLiteReader.GetName())
return nullptr;
@ -114,7 +114,7 @@ bool CBase::Read(IFolder* pFolder)
if (nullptr == pFolder || !pFolder->existsXml(L"OFD.xml"))
return false;
XmlUtils::CXmlLiteReader oLiteReader;
CXmlReader oLiteReader;
if (!oLiteReader.FromFile(pFolder->getFullFilePath(L"OFD.xml")) || !oLiteReader.ReadNextNode() || L"ofd:OFD" != oLiteReader.GetName())
return false;

View File

@ -36,7 +36,7 @@ class CDocInfo
std::vector<std::wstring> m_arCustomData;
public:
CDocInfo();
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(CXmlReader& oLiteReader);
};
class CDocBody
@ -48,7 +48,7 @@ class CDocBody
std::wstring m_wsSignature;
public:
CDocBody();
static CDocBody* Read(XmlUtils::CXmlLiteReader& oLiteReader, IFolder* pFolder);
static CDocBody* Read(CXmlReader& oLiteReader, IFolder* pFolder);
};
@ -61,12 +61,12 @@ public:
bool Read(IFolder* pFolder);
std::wstring GetDocId() const;
std::wstring GetCreationDate() const;
std::wstring GetCreator() const;
std::wstring GetCreatorVersion() const;
// std::wstring GetDocId() const;
// std::wstring GetCreationDate() const;
// std::wstring GetCreator() const;
// std::wstring GetCreatorVersion() const;
std::wstring GetpathToDocRoot() const;
// std::wstring GetPathToDocRoot() const;
};
}

View File

@ -3,9 +3,7 @@
namespace OFD
{
CContent::CContent()
{
}
{}
CContent::~CContent()
{
@ -13,7 +11,7 @@ CContent::~CContent()
delete pLayer;
}
bool CContent::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CContent::Read(CXmlReader& oLiteReader)
{
if (L"ofd:Content" != oLiteReader.GetName())
return false;

View File

@ -12,7 +12,7 @@ public:
CContent();
~CContent();
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(CXmlReader& oLiteReader);
};
}

View File

@ -3,10 +3,10 @@
namespace OFD
{
CGraphicUnit::CGraphicUnit(XmlUtils::CXmlLiteReader& oLiteReader)
: m_bVisible(true), m_dLineWidth(0.353), m_eCap(ECap::Butt),
m_eJoin(EJoin::Miter), m_dMiterLimit(4.234), m_dDashOffset(0.),
m_uchAlpha(255)
CGraphicUnit::CGraphicUnit(CXmlReader& oLiteReader)
: m_bVisible(true), m_dLineWidth(0.353), m_eCap(ECap::Butt),
m_eJoin(EJoin::Miter), m_dMiterLimit(4.234), m_dDashOffset(0.),
m_uchAlpha(255)
{
if (0 == oLiteReader.GetAttributesCount() || !oLiteReader.MoveToFirstAttribute())
return;
@ -18,17 +18,17 @@ CGraphicUnit::CGraphicUnit(XmlUtils::CXmlLiteReader& oLiteReader)
wsAttributeName = oLiteReader.GetName();
if (L"Boundary" == wsAttributeName)
m_oBoundary.Read(oLiteReader.GetText());
m_oBoundary.Read(oLiteReader.GetTextA());
else if (L"Name" == wsAttributeName)
m_wsName = oLiteReader.GetText();
else if (L"Visible" == wsAttributeName)
m_bVisible = XmlUtils::GetBoolean(oLiteReader.GetText());
m_bVisible = oLiteReader.GetBoolean(true);
else if (L"CTM" == wsAttributeName)
m_oCTM.Read(oLiteReader.GetText());
m_oCTM.Read(oLiteReader.GetTextA());
else if (L"DrawParam" == wsAttributeName)
m_unDrawParam = XmlUtils::GetUInteger(oLiteReader.GetText());
m_unDrawParam = oLiteReader.GetUInteger(true);
else if (L"LineWidth" == wsAttributeName)
m_dLineWidth = XmlUtils::GetDouble(oLiteReader.GetText());
m_dLineWidth = oLiteReader.GetDouble(true);
else if (L"Cap" == wsAttributeName)
{
const std::wstring wsValue{oLiteReader.GetText()};
@ -52,11 +52,11 @@ CGraphicUnit::CGraphicUnit(XmlUtils::CXmlLiteReader& oLiteReader)
m_eJoin = EJoin::Bevel;
}
else if (L"MiterLimit" == wsAttributeName)
m_dMiterLimit = XmlUtils::GetDouble(oLiteReader.GetText());
m_dMiterLimit = oLiteReader.GetDouble(true);
else if (L"DashOffset" == wsAttributeName)
m_dDashOffset = XmlUtils::GetDouble(oLiteReader.GetText());
m_dDashOffset = oLiteReader.GetDouble(true);
else if (L"Alpha" == wsAttributeName)
m_uchAlpha = XmlUtils::GetUInteger(oLiteReader.GetText());
m_uchAlpha = oLiteReader.GetUInteger(true);
} while (oLiteReader.MoveToNextAttribute());
oLiteReader.MoveToElement();

View File

@ -1,7 +1,7 @@
#ifndef GRAPHICUNIT_H
#define GRAPHICUNIT_H
#include "../../../DesktopEditor/xml/include/xmlutils.h"
#include "../Utils/XmlReader.h"
#include "../Utils/Types.h"
#include <vector>
@ -35,7 +35,7 @@ class CGraphicUnit
std::vector<double> m_arDashPattern;
unsigned char m_uchAlpha;
public:
CGraphicUnit(XmlUtils::CXmlLiteReader& oLiteReader);
CGraphicUnit(CXmlReader& oLiteReader);
};
}

View File

@ -1,7 +1,7 @@
#ifndef IPAGEBLOCK_H
#define IPAGEBLOCK_H
#include "../../../DesktopEditor/xml/include/xmlutils.h"
#include "../Utils/XmlReader.h"
namespace OFD
{
@ -11,7 +11,7 @@ class IPageBlock
public:
IPageBlock(){};
virtual ~IPageBlock(){};
virtual bool Read(XmlUtils::CXmlLiteReader& oLiteReader) = 0;
virtual bool Read(CXmlReader& oLiteReader) = 0;
};
}

View File

@ -6,7 +6,7 @@
namespace OFD
{
CLayer::CLayer(XmlUtils::CXmlLiteReader& oLiteReader)
CLayer::CLayer(CXmlReader& oLiteReader)
: m_eType(EType::Body)
{
Read(oLiteReader);
@ -18,7 +18,7 @@ CLayer::~CLayer()
delete pPageBlock;
}
bool CLayer::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CLayer::Read(CXmlReader& oLiteReader)
{
if (L"ofd:Layer" != oLiteReader.GetName())
return false;
@ -28,7 +28,7 @@ bool CLayer::Read(XmlUtils::CXmlLiteReader& oLiteReader)
do
{
if (L"ID" == oLiteReader.GetName())
m_unID = XmlUtils::GetUInteger(oLiteReader.GetText());
m_unID = oLiteReader.GetUInteger(true);
} while (oLiteReader.MoveToNextAttribute());
oLiteReader.MoveToElement();

View File

@ -17,10 +17,10 @@ class CLayer
unsigned int m_unID;
std::vector<IPageBlock*> m_arPageBlocks;
public:
CLayer(XmlUtils::CXmlLiteReader& oLiteReader);
CLayer(CXmlReader& oLiteReader);
~CLayer();
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(CXmlReader& oLiteReader);
};
}

View File

@ -1,12 +1,10 @@
#include "TextObject.h"
#include "../Utils/Utils.h"
namespace OFD
{
CTextCode::CTextCode(XmlUtils::CXmlLiteReader& oLiteReader)
CTextCode::CTextCode(CXmlReader& oLiteReader)
{
if (NO_VALID_NODE(oLiteReader, L"ofd:TextCode"))
if (L"ofd:TextCode" != oLiteReader.GetName() || oLiteReader.IsEmptyElement() || !oLiteReader.IsValid())
return;
if (0 != oLiteReader.GetAttributesCount() && oLiteReader.MoveToFirstAttribute())
@ -18,13 +16,13 @@ CTextCode::CTextCode(XmlUtils::CXmlLiteReader& oLiteReader)
wsAttributeName = oLiteReader.GetName();
if (L"X" == wsAttributeName)
m_dX = XmlUtils::GetDouble(oLiteReader.GetText());
m_dX = oLiteReader.GetDouble(true);
else if (L"Y" == wsAttributeName)
m_dY = XmlUtils::GetDouble(oLiteReader.GetText());
m_dY = oLiteReader.GetDouble(true);
else if (L"DeltaX" == wsAttributeName)
m_arDeltaX = GetDoubleValues(oLiteReader.GetText());
m_arDeltaX = oLiteReader.GetArrayDoubles(true);
else if (L"DeltaY" == wsAttributeName)
m_arDeltaY = GetDoubleValues(oLiteReader.GetText());
m_arDeltaY = oLiteReader.GetArrayDoubles(true);
} while (oLiteReader.MoveToNextAttribute());
}
@ -33,12 +31,12 @@ CTextCode::CTextCode(XmlUtils::CXmlLiteReader& oLiteReader)
m_wsText = oLiteReader.GetText2();
}
CTextObject::CTextObject(XmlUtils::CXmlLiteReader& oLiteReader)
CTextObject::CTextObject(CXmlReader& oLiteReader)
: CGraphicUnit(oLiteReader),
m_bStroke(false), m_bFill(false), m_dHScale(1.),
m_unReadDirection(0), m_unCharDirection(0), m_unWeight(400),
m_bItalic(false),
m_pFillColor(nullptr), m_pStrokeColor(nullptr)
m_bStroke(false), m_bFill(false), m_dHScale(1.),
m_unReadDirection(0), m_unCharDirection(0), m_unWeight(400),
m_bItalic(false),
m_pFillColor(nullptr), m_pStrokeColor(nullptr)
{
CTextObject::Read(oLiteReader);
}
@ -55,9 +53,9 @@ CTextObject::~CTextObject()
delete pTextCode;
}
bool CTextObject::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CTextObject::Read(CXmlReader& oLiteReader)
{
if (NO_VALID_NODE(oLiteReader, L"ofd:TextObject"))
if (L"ofd:TextObject" != oLiteReader.GetName() || oLiteReader.IsEmptyElement() || !oLiteReader.IsValid())
return false;
if (0 != oLiteReader.GetAttributesCount() && oLiteReader.MoveToFirstAttribute())
@ -69,25 +67,23 @@ bool CTextObject::Read(XmlUtils::CXmlLiteReader& oLiteReader)
wsAttributeName = oLiteReader.GetName();
if (L"Font" == wsAttributeName)
{
m_unFont = XmlUtils::GetUInteger(oLiteReader.GetText());
}
m_unFont = oLiteReader.GetUInteger(true);
else if (L"Size" == wsAttributeName)
m_dSize = XmlUtils::GetDouble(oLiteReader.GetText());
m_dSize = oLiteReader.GetDouble(true);
else if (L"Stroke" == wsAttributeName)
m_bStroke = XmlUtils::GetBoolean(oLiteReader.GetText());
m_bStroke = oLiteReader.GetBoolean(true);
else if (L"Fill" == wsAttributeName)
m_bFill = XmlUtils::GetBoolean(oLiteReader.GetText());
m_bFill = oLiteReader.GetBoolean(true);
else if (L"HScale" == wsAttributeName)
m_dHScale = XmlUtils::GetDouble(oLiteReader.GetText());
m_dHScale = oLiteReader.GetDouble(true);
else if (L"ReadDirection" == wsAttributeName)
m_unReadDirection = XmlUtils::GetUInteger(oLiteReader.GetText());
m_unReadDirection = oLiteReader.GetUInteger(true);
else if (L"CharDirection" == wsAttributeName)
m_unCharDirection = XmlUtils::GetUInteger(oLiteReader.GetText());
m_unCharDirection =oLiteReader.GetUInteger(true);
else if (L"Weight" == wsAttributeName)
m_unWeight = XmlUtils::GetUInteger(oLiteReader.GetText());
m_unWeight = oLiteReader.GetUInteger(true);
else if (L"Italic" == wsAttributeName)
m_bItalic = XmlUtils::GetBoolean(oLiteReader.GetText());
m_bItalic = oLiteReader.GetBoolean(true);
} while (oLiteReader.MoveToNextAttribute());
}
@ -105,14 +101,14 @@ bool CTextObject::Read(XmlUtils::CXmlLiteReader& oLiteReader)
if (nullptr != m_pFillColor)
delete m_pFillColor;
m_pFillColor = new TColor(oLiteReader);
m_pFillColor = new CColor(oLiteReader);
}
else if (L"ofd:StrokeColor" == wsNodeName && m_bStroke)
{
if (nullptr != m_pStrokeColor)
delete m_pStrokeColor;
m_pStrokeColor = new TColor(oLiteReader);
m_pStrokeColor = new CColor(oLiteReader);
}
else if (L"ofd:TextCode" == wsNodeName)
m_arTextCodes.push_back(new CTextCode(oLiteReader));

View File

@ -4,6 +4,8 @@
#include "IPageBlock.h"
#include "GraphicUnit.h"
#include "../Types/Color.h"
namespace OFD
{
class CTextCode
@ -16,7 +18,7 @@ class CTextCode
std::wstring m_wsText;
public:
CTextCode(XmlUtils::CXmlLiteReader& oLiteReader);
CTextCode(CXmlReader& oLiteReader);
};
class CTextObject : public IPageBlock, public CGraphicUnit
@ -31,15 +33,15 @@ class CTextObject : public IPageBlock, public CGraphicUnit
unsigned int m_unWeight;
bool m_bItalic;
TColor* m_pFillColor;
TColor* m_pStrokeColor;
CColor* m_pFillColor;
CColor* m_pStrokeColor;
std::vector<CTextCode*> m_arTextCodes;
public:
CTextObject(XmlUtils::CXmlLiteReader& oLiteReader);
CTextObject(CXmlReader& oLiteReader);
~CTextObject();
virtual bool Read(XmlUtils::CXmlLiteReader& oLiteReader) override;
virtual bool Read(CXmlReader& oLiteReader) override;
};
}

View File

@ -1,13 +1,11 @@
#include "Document.h"
#include "../../OOXML/Base/Unit.h"
namespace OFD
{
CPageArea::CPageArea()
{}
bool CPageArea::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CPageArea::Read(CXmlReader& oLiteReader)
{
if (L"ofd:PageArea" != oLiteReader.GetName())
return false;
@ -20,13 +18,13 @@ bool CPageArea::Read(XmlUtils::CXmlLiteReader& oLiteReader)
wsNodeName = oLiteReader.GetName();
if (L"ofd:PhysicalBox" == wsNodeName)
m_oPhysicalBox.Read(oLiteReader.GetText2());
m_oPhysicalBox.Read(oLiteReader.GetText2A());
else if (L"ofd:ApplicationBox" == wsNodeName)
m_oApplicationBox.Read(oLiteReader.GetText2());
m_oApplicationBox.Read(oLiteReader.GetText2A());
else if (L"ofd:ContentBox" == wsNodeName)
m_oContentBox.Read(oLiteReader.GetText2());
m_oContentBox.Read(oLiteReader.GetText2A());
else if (L"ofd:BleedBox" == wsNodeName)
m_oBleedBox.Read(oLiteReader.GetText2());
m_oBleedBox.Read(oLiteReader.GetText2A());
}
return true;
@ -36,7 +34,7 @@ CCommonData::CCommonData()
: m_unMaxUnitID(0)
{}
bool CCommonData::Read(XmlUtils::CXmlLiteReader& oLiteReader)
bool CCommonData::Read(CXmlReader& oLiteReader)
{
if (L"ofd:CommonData" != oLiteReader.GetName())
return false;
@ -55,7 +53,7 @@ bool CCommonData::Read(XmlUtils::CXmlLiteReader& oLiteReader)
// m_oPublicRes.Read();
}
else if (L"ofd:MaxUnitID" == wsNodeName)
m_unMaxUnitID = XmlUtils::GetUInteger(oLiteReader.GetText2());
m_unMaxUnitID = oLiteReader.GetUInteger();
// else if (L"ofd:DocumentRes" == wsNodeName)
// else if (L"ofd:TemplatePage" == wsNodeName)
// else if (L"ofd:DefaultCS" == wsNodeName)
@ -64,6 +62,30 @@ bool CCommonData::Read(XmlUtils::CXmlLiteReader& oLiteReader)
return true;
}
CPermission::CPermission()
: m_bEdit(true), m_bAnnot(true), m_bExport(true),
m_bSignature(true), m_bWatermark(true), m_bPrintScreen(true)
{}
bool CPermission::Read(CXmlReader& oLiteReader)
{
if (L"ofd:Permission" != oLiteReader.GetName())
return false;
const int nDepth = oLiteReader.GetDepth();
std::wstring wsNodeName;
while (oLiteReader.ReadNextSiblingNode(nDepth))
{
wsNodeName = oLiteReader.GetName();
if (L"ofd:Edit" == wsNodeName)
m_bEdit = oLiteReader.GetBoolean();
}
return true;
}
CDocument::CDocument()
{}
@ -83,7 +105,7 @@ bool CDocument::Read(const std::wstring& wsFilePath)
if (wsFilePath.empty())
return false;
XmlUtils::CXmlLiteReader oLiteReader;
CXmlReader oLiteReader;
if (!oLiteReader.FromFile(wsFilePath) || !oLiteReader.ReadNextNode() || L"ofd:Document" != oLiteReader.GetName())
return false;
@ -111,7 +133,7 @@ bool CDocument::Read(const std::wstring& wsFilePath)
do
{
if (L"ID" == oLiteReader.GetName())
nID = XmlUtils::GetUInteger(oLiteReader.GetText());
nID = oLiteReader.GetInteger(true);
else if (L"BaseLoc" == oLiteReader.GetName())
wsBaseLoc = oLiteReader.GetText();
}while (oLiteReader.MoveToNextAttribute());

View File

@ -17,7 +17,7 @@ class CPageArea
public:
CPageArea();
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(CXmlReader& oLiteReader);
};
class CCommonData
@ -28,7 +28,21 @@ class CCommonData
public:
CCommonData();
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(CXmlReader& oLiteReader);
};
class CPermission
{
bool m_bEdit;
bool m_bAnnot;
bool m_bExport;
bool m_bSignature;
bool m_bWatermark;
bool m_bPrintScreen;
public:
CPermission();
bool Read(CXmlReader& oLiteReader);
};
class CDocument

View File

@ -1,6 +1,5 @@
#include "Page.h"
#include <iostream>
#include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Path.h"
@ -22,7 +21,7 @@ CPage* CPage::Read(const std::wstring& wsFilePath)
wsNormalizedPath = NSSystemPath::Combine(wsNormalizedPath, L"Content.xml");
XmlUtils::CXmlLiteReader oLiteReader;
CXmlReader oLiteReader;
if (!oLiteReader.FromFile(wsNormalizedPath) || !oLiteReader.ReadNextNode() || L"ofd:Page" != oLiteReader.GetName())
return nullptr;

View File

@ -0,0 +1,15 @@
#include "Color.h"
namespace OFD
{
CColor::CColor(CXmlReader& oXmlReader)
{
Read(oXmlReader);
}
bool CColor::Read(CXmlReader& oXmlReader)
{
return false;
}
}

32
OFDFile/src/Types/Color.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef COLOR_H
#define COLOR_H
#include "../Utils/XmlReader.h"
namespace OFD
{
class CColor
{
struct TColorChannels
{
unsigned char m_chRed;
BYTE m_chGreen;
BYTE m_chBlue;
} m_oValue;
int m_nIndex;
unsigned int m_unColorSpace;
BYTE m_chAlpha;
// Pattern
// AxialShd
// RadialShd
// GouraudShd
// LaGouraudhd
public:
CColor(CXmlReader& oXmlReader);
bool Read(CXmlReader& oXmlReader);
};
}
#endif // COLOR_H

View File

@ -1,8 +1,6 @@
#include "Types.h"
#include <vector>
#include "../../../DesktopEditor/common/StringExt.h"
#include "../../../OOXML/Base/Unit.h"
#include "Utils.h"
namespace OFD
{
@ -15,35 +13,19 @@ bool TBox::Empty() const
return m_dWidth < OFD_EPSILON || m_dHeight < OFD_EPSILON;
}
bool TBox::Read(const std::wstring& wsValue)
bool TBox::Read(const std::string& sValue)
{
const std::vector<std::wstring> arValues{NSStringExt::Split(wsValue, L' ')};
const std::vector<std::string> arValues{Split(sValue, ' ')};
if (4 > arValues.size())
return false;
m_dX = XmlUtils::GetDouble(arValues[0]);
m_dY = XmlUtils::GetDouble(arValues[1]);
m_dWidth = XmlUtils::GetDouble(arValues[2]);
m_dHeight = XmlUtils::GetDouble(arValues[3]);
if (!StringToDouble(arValues[0], m_dX) ||
!StringToDouble(arValues[1], m_dY) ||
!StringToDouble(arValues[2], m_dWidth) ||
!StringToDouble(arValues[3], m_dHeight))
return false;
return true;
}
TColor::TColor()
{
}
TColor::TColor(XmlUtils::CXmlLiteReader& oLiteReader)
{
Read(oLiteReader);
}
bool TColor::Read(XmlUtils::CXmlLiteReader& oLiteReader)
{
return false;
}
}

View File

@ -2,13 +2,10 @@
#define TYPES_H
#include <string>
#include "../../../DesktopEditor/xml/include/xmlutils.h"
namespace OFD
{
#define OFD_EPSILON 0.0001
struct TBox
{
double m_dX;
@ -19,15 +16,7 @@ struct TBox
TBox();
bool Empty() const;
bool Read(const std::wstring& wsValue);
};
struct TColor
{
TColor();
TColor(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(XmlUtils::CXmlLiteReader& oLiteReader);
bool Read(const std::string& wsValue);
};
}

View File

@ -1,49 +1,96 @@
#ifndef UTILS_H
#define UTILS_H
#include <vector>
#include <algorithm>
#include <string>
#include "../../../OOXML/Base/Unit.h"
#include "../../../DesktopEditor/common/StringExt.h"
#include <vector>
#include <sstream>
#include <cstdlib>
bool GetBoolean(const std::wstring& wsValue)
namespace OFD
{
return false;
inline std::vector<std::string> Split(const std::string& sValue, char chDelim)
{
if (sValue.empty())
return std::vector<std::string>();
std::vector<std::string> arValues;
std::stringstream oStringStream(sValue);
std::string sItem;
while (std::getline(oStringStream, sItem, chDelim))
arValues.push_back(sItem);
return arValues;
}
int GetInt(const std::wstring& wsValue)
inline bool StringToBoolean(const std::string& sValue, bool& bValue)
{
return 0;
//TODO:: скорректировать метод, если возможно чтение не стандартных значений с плавающей запятой
if (sValue.empty())
return false;
size_t unStart = sValue.find_first_not_of(" \t\n\r\f\v");
if (std::string::npos == unStart)
return false;
size_t unEnd = sValue.find_last_not_of(" \t\n\r\f\v");
std::string sTrimmed = sValue.substr(unStart, unEnd - unStart + 1);
std::transform(sTrimmed.begin(), sTrimmed.end(), sTrimmed.begin(),
[](unsigned char c){ return std::tolower(c); });
return "true" == sTrimmed;
}
unsigned int GetUInt(const std::wstring& wsValue)
inline bool StringToInteger(const std::string& sValue, int& nValue)
{
return 0;
//TODO:: скорректировать метод, если возможно чтение не стандартных значений с плавающей запятой
if (sValue.empty())
return false;
char* pEnd = nullptr;
nValue = std::strtol(sValue.c_str(), &pEnd, 10);
if (pEnd == sValue.c_str() || '\0' != *pEnd)
return false;
return true;
}
double GetDouble(const std::wstring& wsValue)
inline bool StringToUInteger(const std::string& sValue, unsigned int& unValue)
{
return 0.;
//TODO:: скорректировать метод, если возможно чтение не стандартных значений с плавающей запятой
if (sValue.empty())
return false;
char* pEnd = nullptr;
unValue = std::strtoul(sValue.c_str(), &pEnd, 10);
if (pEnd == sValue.c_str() || '\0' != *pEnd)
return false;
return true;
}
std::vector<double> GetDoubleValues(const std::wstring& wsValue)
inline bool StringToDouble(const std::string& sValue, double& dValue)
{
const std::vector<std::wstring> arValues{NSStringExt::Split(wsValue, L' ')};
//TODO:: скорректировать метод, если возможно чтение не стандартных значений с плавающей запятой
if (sValue.empty())
return false;
if(arValues.empty())
return std::vector<double>();
char* pEnd = nullptr;
std::vector<double> arDoubleValues(arValues.size());
dValue = std::strtod(sValue.c_str(), &pEnd);
for (unsigned int unIndex = 0; unIndex < arValues.size(); ++unIndex)
arDoubleValues[unIndex] = XmlUtils::GetDouble(arValues[unIndex]);
if (pEnd == sValue.c_str() || '\0' != *pEnd)
return false;
return arDoubleValues;
return true;
}
}
#define NO_VALID_NODE(lite_reader, node_name) node_name != oLiteReader.GetName() || oLiteReader.IsEmptyElement() || !oLiteReader.IsValid()
#endif // UTILS_H

View File

@ -0,0 +1,63 @@
#include "XmlReader.h"
#include "Utils.h"
namespace OFD
{
CXmlReader::CXmlReader()
{}
std::wstring CXmlReader::GetTextValue(bool bIsAttribute)
{
return (bIsAttribute) ? XmlUtils::CXmlLiteReader::GetText() : XmlUtils::CXmlLiteReader::GetText2();
}
std::string CXmlReader::GetTextValueA(bool bIsAttribute)
{
return (bIsAttribute) ? XmlUtils::CXmlLiteReader::GetTextA() : XmlUtils::CXmlLiteReader::GetText2A();
}
bool CXmlReader::GetBoolean(bool bIsAttribute)
{
bool bValue = false;
StringToBoolean(GetTextValueA(bIsAttribute), bValue);
return bValue;
}
int CXmlReader::GetInteger(bool bIsAttribute)
{
int nValue = 0;
StringToInteger(GetTextValueA(bIsAttribute), nValue);
return nValue;
}
unsigned int CXmlReader::GetUInteger(bool bIsAttribute)
{
unsigned int unValue = 0;
StringToUInteger(GetTextValueA(bIsAttribute), unValue);
return 0;
}
double CXmlReader::GetDouble(bool bIsAttribute)
{
double dValue = 0.;
StringToDouble(GetTextValueA(bIsAttribute), dValue);
return dValue;
}
std::vector<double> CXmlReader::GetArrayDoubles(bool bIsAttribute)
{
const std::vector<std::string> arValues{Split(GetTextValueA(bIsAttribute), ' ')};
if(arValues.empty())
return std::vector<double>();
std::vector<double> arDoubleValues(arValues.size());
for (unsigned int unIndex = 0; unIndex < arValues.size(); ++unIndex)
if (!StringToDouble(arValues[unIndex], arDoubleValues[unIndex]))
return std::vector<double>();
return arDoubleValues;
}
}

View File

@ -0,0 +1,25 @@
#ifndef XMLREADER_H
#define XMLREADER_H
#include <vector>
#include <string>
#include "../../../DesktopEditor/xml/include/xmlutils.h"
namespace OFD
{
class CXmlReader : public XmlUtils::CXmlLiteReader
{
std::wstring GetTextValue(bool bIsAttribute = false);
std::string GetTextValueA(bool bIsAttribute = false);
public:
CXmlReader();
bool GetBoolean(bool bIsAttribute = false);
int GetInteger(bool bIsAttribute = false);
unsigned int GetUInteger(bool bIsAttribute = false);
double GetDouble(bool bIsAttribute = false);
std::vector<double> GetArrayDoubles(bool bIsAttribute = false);
};
}
#endif // XMLREADER_H