init repo

This commit is contained in:
nikolay ivanov
2014-07-05 18:22:49 +00:00
commit a8be6b9e72
17348 changed files with 9229832 additions and 0 deletions

View File

@@ -0,0 +1,177 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include "stdafx.h"
#include <math.h>
#include "./../Common.h"
#include "./../../../../../../Common/XmlUtils.h"
#include "Parser.h"
namespace NSGuidesOdp
{
const double dNonDefResult = 27273042316900;
const long NonDefResult = 0xFFFFFF;
const double RadKoef = M_PI/10800000.0;
class CFormulaManager
{
private:
CSimpleMap<CString, long> mapAdjustments;
CSimpleMap<CString, long> mapGuides;
CSimpleArray<CString> strGuides;
private:
CSimpleArray<long>* Adjustments;
CSimpleArray<double>* Guides;
public:
CFormulaManager(CSimpleArray<long>& a, CSimpleArray<double>& g)
{
Adjustments = &a;
Guides = &g;
AddGuide(_T("left"), _T("0"));
AddGuide(_T("top"), _T("0"));
AddGuide(_T("right"), _T("21600"));
AddGuide(_T("bottom"), _T("21600"));
AddGuide(_T("width"), _T("right - left"));
AddGuide(_T("height"), _T("bottom - top"));
}
~CFormulaManager()
{
mapAdjustments.RemoveAll();
mapGuides.RemoveAll();
strGuides.RemoveAll();
Adjustments->RemoveAll();
Guides->RemoveAll();
}
CFormulaManager& operator =(const CFormulaManager& manager)
{
mapAdjustments.RemoveAll();
for(int i = 0; i < manager.mapAdjustments.GetSize(); i++)
mapAdjustments.Add(manager.mapAdjustments.GetKeyAt(i), manager.mapAdjustments.GetValueAt(i));
mapGuides.RemoveAll();
for(int i = 0; i < manager.mapGuides.GetSize(); i++)
mapGuides.Add(manager.mapGuides.GetKeyAt(i), manager.mapGuides.GetValueAt(i));
strGuides.RemoveAll();
for(int i = 0; i < manager.strGuides.GetSize(); i++)
strGuides.Add(manager.strGuides[i]);
Adjustments->RemoveAll();
for(int i = 0; i < manager.Adjustments->GetSize(); i++)
Adjustments->Add((*manager.Adjustments)[i]);
Guides->RemoveAll();
for(int i = 0; i < manager.Guides->GetSize(); i++)
Guides->Add((*manager.Guides)[i]);
return *this;
}
void AddAdjustment(const CString& name, const long value)
{
long num = mapAdjustments.FindKey(name);
if(num >= 0)
{
(*Adjustments)[mapAdjustments.GetValueAt(num)] = value;
return;
}
Adjustments->Add(value);
mapAdjustments.Add(name, Adjustments->GetSize() - 1);
}
void AddGuide(const CString& name, const CString& fmla)
{
long num = mapGuides.FindKey(name);
if(num >= 0)
{
strGuides[mapGuides.GetValueAt(num)] = fmla;
(*Guides)[mapGuides.GetValueAt(num)] = dNonDefResult;
return;
}
strGuides.Add(fmla);
Guides->Add(dNonDefResult);
mapGuides.Add(name, strGuides.GetSize() - 1);
}
double GetValue(CString str)
{
long numGuide = mapGuides.FindKey(str);
long numAdj = mapAdjustments.FindKey(str);
if(numGuide >= 0)
{
double res = (*Guides)[mapGuides.GetValueAt(numGuide)];
if(res < dNonDefResult)
return res;
TParser parser;
parser.Compile(strGuides[mapGuides.GetValueAt(numGuide)], *this);
parser.Evaluate();
parser.Decompile();
(*Guides)[mapGuides.GetValueAt(numGuide)] = parser.GetResult();
return parser.GetResult();
}
if(numAdj >= 0)
{
return (*Adjustments)[mapAdjustments.GetValueAt(numAdj)];
}
return XmlUtils::GetInteger(CString(str));
}
void Clear()
{
for(long i = 0; i < Guides->GetSize(); i++)
(*Guides)[i] = dNonDefResult;
}
void ReCalculateGuides()
{
Clear();
for(long i = 0; i < strGuides.GetSize(); i++)
{
TParser parser;
parser.Compile(strGuides[i], *this);
parser.Evaluate();
parser.Decompile();
(*Guides)[i] = parser.GetResult();
}
}
};
}

View File

@@ -0,0 +1,47 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "stdafx.h"
#include "OdpShape.h"
#include "PresetShapesHeader.h"
COdpShape* COdpShape::CreateByType(OdpShapes::ShapeType type)
{
switch(type)
{
case OdpShapes::sptCRect: return new OdpShapes::CRect();
case OdpShapes::sptCLine: return new OdpShapes::CLine();
}
return NULL;
}

View File

@@ -0,0 +1,349 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "stdafx.h"
#include "Formula.h"
#include <string>
#include "./../BaseShape.h"
#include "./../../../../../../Common/XmlUtils.h"
using namespace NSOfficeDrawing;
namespace OdpShapes
{
enum ShapeType : unsigned short
{
sptMin = 0,
sptNotPrimitive = sptMin,
sptCRect = 1,
sptCLine = 2,
sptMax,
sptNil = 0x0FFF,
sptCustom = 0x1000
};
}
class COdpShape : public CBaseShape
{
public:
OdpShapes::ShapeType m_eType;
NSGuidesOdp::CFormulaManager FManager;
public:
COdpShape() : CBaseShape(), FManager(m_arAdjustments, Guides)
{
m_eType = OdpShapes::sptMin;
}
~COdpShape()
{
}
virtual bool LoadFromXML(const CString& xml)
{
XmlUtils::CXmlNode oNodePict;
if (oNodePict.FromXmlString(xml))
{
return LoadFromXML(oNodePict);
}
}
virtual bool LoadFromXML(XmlUtils::CXmlNode& root)
{
bool res = true;
res &= LoadAdjustValuesList(root.GetAttributeOrValue(_T("draw:modifiers")));
XmlUtils::CXmlNodes gdLst;
if(root.GetNodes(_T("draw:equation"), gdLst))
{
for(long i = 0; i < gdLst.GetCount(); i++)
{
XmlUtils::CXmlNode gd;
gdLst.GetAt(i, gd);
FManager.AddGuide(_T("?") + gd.GetAttribute(_T("draw:name")), gd.GetAttribute(_T("draw:formula")));
}
}
m_strRect = root.GetAttributeOrValue(_T("draw:text-areas"));
res &= LoadTextRect(m_strRect);
m_strPath = root.GetAttributeOrValue(_T("draw:enhanced-path"));
res &= LoadPathList(m_strPath);
CString viewBox = root.GetAttributeOrValue(_T("svg:viewBox"));
if(viewBox != _T(""))
{
CSimpleArray<CString> borders;
NSStringUtils::ParseString(_T(" "), viewBox, &borders);
FManager.AddGuide(_T("left"), borders[0]);
FManager.AddGuide(_T("top"), borders[1]);
FManager.AddGuide(_T("right"), borders[2]);
FManager.AddGuide(_T("bottom"), borders[3]);
FManager.Clear();
for(int i = 0; i < m_oPath.m_arParts.GetSize(); i++)
{
m_oPath.m_arParts[i].height = (long)FManager.GetValue(_T("height"));
m_oPath.m_arParts[i].width = (long)FManager.GetValue(_T("width"));
}
}
return res;
}
virtual bool LoadAdjustValuesList(const CString& xml)
{
if(xml != _T(""))
{
CSimpleArray<CString> adjusts;
NSStringUtils::ParseString(_T(" "), xml, &adjusts);
m_arAdjustments.RemoveAll();
CString buffer;
for(int i = 0; i < adjusts.GetSize(); i++)
{
buffer.Format(_T("%i"), i);
FManager.AddAdjustment(CString(_T("$")) + buffer, XmlUtils::GetInteger(adjusts[i]));
}
return true;
}
return false;
}
virtual bool LoadGuidesList(const CString& xml)
{
return true;
}
virtual bool LoadAdjustHandlesList(const CString& xml)
{
return true;
}
virtual bool LoadConnectorsList(const CString& xml)
{
return true;
}
virtual bool LoadTextRect(const CString& xml)
{
if(xml != _T(""))
{
CSimpleArray<CString> borders;
NSStringUtils::ParseString(_T(" "), xml, &borders);
RECT TextRect;
TextRect.left = (long)FManager.GetValue(borders[0]);
TextRect.top = (long)FManager.GetValue(borders[1]);
TextRect.right = (long)FManager.GetValue(borders[2]);
TextRect.bottom = (long)FManager.GetValue(borders[3]);
if(m_arTextRects.GetSize() > 0)
m_arTextRects[0] = TextRect;
else m_arTextRects.Add(TextRect);
}
return true;
}
virtual bool LoadPathList(const CString& xml)
{
if(xml != _T(""))
{
m_oPath.FromXML(xml, FManager);
for(int i = 0; i < m_oPath.m_arParts.GetSize(); i++)
{
m_oPath.m_arParts[i].height = (long)FManager.GetValue(_T("height"));
m_oPath.m_arParts[i].width = (long)FManager.GetValue(_T("width"));
}
return true;
}
return false;
}
virtual bool SetAdjustment(long index, long value)
{
FManager.Clear();
CString buffer;
buffer.Format(_T("%i"), index);
FManager.AddAdjustment(_T("$") + buffer, value);
return true;
}
virtual CString ToXML(CGeomShapeInfo& GeomInfo, CMetricInfo& MetricInfo, double StartTime, double EndTime, CBrush_& Brush, CPen_& Pen)
{
ReCalculate();
return m_oPath.ToXml(GeomInfo, StartTime, EndTime, Pen, Brush, MetricInfo, NSBaseShape::odp);
}
virtual void ReCalculate()
{
FManager.ReCalculateGuides();
LoadTextRect(m_strRect);
LoadPathList(m_strPath);
}
static COdpShape* CreateByType(OdpShapes::ShapeType type);
virtual const ClassType GetClassType()const
{
return NSBaseShape::odp;
}
virtual bool SetProperties(CBaseShape* Shape)
{
if(Shape == NULL)
return false;
if(Shape->GetClassType() != NSBaseShape::odp)
return false;
FManager = ((COdpShape*)Shape)->FManager;
return CBaseShape::SetProperties(Shape);
}
virtual bool SetToDublicate(CBaseShape* Shape)
{
if(Shape == NULL)
return false;
if(Shape->GetClassType() != NSBaseShape::odp)
return false;
((COdpShape*)Shape)->FManager = FManager;
return CBaseShape::SetToDublicate(Shape);
}
bool SetShapeType(OdpShapes::ShapeType type)
{
COdpShape* l_pShape = CreateByType(type);
if(l_pShape != NULL)
{
m_eType = type;
SetProperties(l_pShape);
delete l_pShape;
return true;
}
m_eType = OdpShapes::sptCustom;
return false;
}
};

View File

@@ -0,0 +1,62 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "../OdpShape.h"
namespace OdpShapes
{
class CLine : public COdpShape
{
public:
CLine()
{
LoadFromXML(
_T("<draw:enhanced-geometry xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" svg:viewBox=\"0 0 21600 21600\" draw:type=\"non-primitive-line\" draw:modifiers=\"0 0 21600 21600 21600 21600\" draw:enhanced-path=\"M ?f0 ?f1 L ?f4 ?f5 Z N\">")
_T("<draw:equation draw:name=\"f0\" draw:formula=\"$0*21600/$4\" />")
_T("<draw:equation draw:name=\"f1\" draw:formula=\"$1*21600/$5\" />")
_T("<draw:equation draw:name=\"f2\" draw:formula=\"$2*21600/$4\" />")
_T("<draw:equation draw:name=\"f3\" draw:formula=\"$3*21600/$5\" />")
_T("<draw:equation draw:name=\"f4\" draw:formula=\"?f0 + ?f2\" />")
_T("<draw:equation draw:name=\"f5\" draw:formula=\"?f1 + ?f3\" />")
_T("</draw:enhanced-geometry>")
);
}
};
}

View File

@@ -0,0 +1,48 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "../OdpShape.h"
namespace OdpShapes
{
class CRect : public COdpShape
{
public:
CRect()
{
LoadFromXML(
_T("<draw:enhanced-geometry xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" svg:viewBox=\"0 0 21600 21600\" draw:type=\"rectangle\" draw:enhanced-path=\"M 0 0 L 21600 0 21600 21600 0 21600 0 0 Z N\" />")
);
}
};
}

View File

@@ -0,0 +1,34 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "OdpShapes/CRect.h"
#include "OdpShapes/CLine.h"

View File

@@ -0,0 +1,573 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include "stdafx.h"
#include <math.h>
#include "parser.h"
#include "Formula.h"
#define OP_PLUS 0
#define OP_MINUS 1
#define OP_MULTIPLY 2
#define OP_DIVIDE 3
#define OP_PERCENT 4
#define OP_POWER 5
#define OP_UMINUS 6
#define OP_COMMA 7
#define OP_SIN 10
#define OP_COS 11
#define OP_TG 12
#define OP_CTG 13
#define OP_ARCSIN 14
#define OP_ARCCOS 15
#define OP_ARCTG 16
#define OP_ARCCTG 17
#define OP_SH 18
#define OP_CH 19
#define OP_TH 20
#define OP_CTH 21
#define OP_EXP 22
#define OP_LG 23
#define OP_LN 24
#define OP_SQRT 25
#define OP_ABS 26
#define OP_MIN 30
#define OP_MAX 31
#define OP_ATAN2 32
#define OP_IF 40
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
TParserNode *TParser::CreateNode(double _value, TParserNode *_left, TParserNode *_right, TParserNode *_third)
{
TParserNode *pNode = new TParserNode(_value, _left, _right, _third);
history.push_back(pNode);
return pNode;
}
void TParser::SendError(int errNum)
{
static CString errs[7] = { _T(""),
_T(""),
_T("Unexpected end of expression"),
_T("End of expression expected"),
_T("'(' or '[' expected"),
_T("')' or ']' expected"),
_T("")
};
CString buffer;
int len = curToken.GetLength();
if(curToken == _T(""))
curToken = _T("EOL");
switch(errNum)
{
case 0:
buffer.Format(_T("Unknown keyword: '%s'"), curToken);
errs[0] = buffer;
break;
case 1:
buffer.Format(_T("Unknown symbol: '%s'"), curToken);
errs[1] = buffer;
break;
case 6:
buffer.Format(_T("Unexpected '%s'"), curToken);
errs[6] = buffer;
break;
}
TError error(errs[errNum], pos-len);
for(unsigned int i=0; i<history.size(); i++)
delete history[i];
history.clear();
root = NULL;
throw error;
return;
}
bool TParser::GetToken()
{
curToken = _T("");
if(pos >= expr.GetLength())
{
curToken = _T("");
typToken = PARSER_END;
return true;
}
while(IsSpace()) pos++;
if(IsDelim())
{
curToken = expr[pos++];
switch(curToken[0])
{
case _T('+'): typToken = PARSER_PLUS; return true;
case _T('-'): typToken = PARSER_MINUS; return true;
case _T('*'): typToken = PARSER_MULTIPLY; return true;
case _T('/'): typToken = PARSER_DIVIDE; return true;
case _T('%'): typToken = PARSER_PERCENT; return true;
case _T('^'): typToken = PARSER_POWER; return true;
case _T('['):
case _T('('): typToken = PARSER_L_BRACKET; return true;
case _T(']'):
case _T(')'): typToken = PARSER_R_BRACKET; return true;
}
}
else if(IsComma())
{
curToken = expr[pos++];
typToken = PARSER_COMMA;
return true;
}
else if(IsLetter())
{
int i=0;
curToken = _T("");
while(IsLetter() || IsDigit()) curToken += expr[pos++];
curToken.MakeLower();
if(curToken == _T("pi")) { typToken = PARSER_PI; return true; }
else if(curToken == _T("e")) { typToken = PARSER_E; return true; }
else if(curToken == _T("sin")) { typToken = PARSER_SIN; return true; }
else if(curToken == _T("cos")) { typToken = PARSER_COS; return true; }
else if(curToken == _T("tg")) { typToken = PARSER_TG; return true; }
else if(curToken == _T("ctg")) { typToken = PARSER_CTG; return true; }
else if(curToken == _T("arcsin")) { typToken = PARSER_ARCSIN; return true; }
else if(curToken == _T("arccos")) { typToken = PARSER_ARCCOS; return true; }
else if(curToken == _T("arctg")) { typToken = PARSER_ARCTG; return true; }
else if(curToken == _T("arcctg")) { typToken = PARSER_ARCCTG; return true; }
else if(curToken == _T("sh")) { typToken = PARSER_SH; return true; }
else if(curToken == _T("ch")) { typToken = PARSER_CH; return true; }
else if(curToken == _T("th")) { typToken = PARSER_TH; return true; }
else if(curToken == _T("cth")) { typToken = PARSER_CTH; return true; }
else if(curToken == _T("exp")) { typToken = PARSER_EXP; return true; }
else if(curToken == _T("lg")) { typToken = PARSER_LG; return true; }
else if(curToken == _T("ln")) { typToken = PARSER_LN; return true; }
else if(curToken == _T("sqrt")) { typToken = PARSER_SQRT; return true; }
else if(curToken == _T("abs")) { typToken = PARSER_ABS; return true; }
else if(curToken == _T("min")) { typToken = PARSER_MIN; return true; }
else if(curToken == _T("max")) { typToken = PARSER_MAX; return true; }
else if(curToken == _T("atan2")) { typToken = PARSER_ATAN2; return true; }
else if(curToken == _T("if")) { typToken = PARSER_IF; return true; }
else if(curToken == _T("left")) { typToken = PARSER_GUIDE; return true; }
else if(curToken == _T("right")) { typToken = PARSER_GUIDE; return true; }
else if(curToken == _T("top")) { typToken = PARSER_GUIDE; return true; }
else if(curToken == _T("bottom")) { typToken = PARSER_GUIDE; return true; }
else if(curToken == _T("width")) { typToken = PARSER_GUIDE; return true; }
else if(curToken == _T("height")) { typToken = PARSER_GUIDE; return true; }
else SendError(0);
}
else if(IsAdjust())
{
int i=0;
curToken = _T("");
while((!IsSpace())&&(!IsDelim())) curToken += expr[pos++];
typToken = PARSER_ADJUST;
return true;
}
else if(IsGuide())
{
int i=0;
curToken = _T("");
while((!IsSpace())&&(!IsDelim())) curToken += expr[pos++];
typToken = PARSER_GUIDE;
return true;
}
else if(IsDigit() || IsPoint())
{
int i=0;
curToken = _T("");
while(IsDigit()) curToken += expr[pos++];
if(IsPoint())
{
curToken += expr[pos++];
while(IsDigit()) curToken += expr[pos++];
}
typToken = PARSER_NUMBER;
return true;
}
else
{
curToken = expr[pos++];
SendError(1);
}
return false;
}
bool TParser::Compile(CString _expr, NSGuidesOdp::CFormulaManager& pFManager)
{
pos = 0;
expr = _expr;
curToken = _T("");
if(root!=NULL)
{
DelTree(root);
root = NULL;
}
history.clear();
CString strTempString(_T("+-*/%^,"));
while(strTempString.Find(expr[0]) >= 0) expr.Delete(0);
GetToken();
if(typToken==PARSER_END) SendError(2);
root = Expr(pFManager);
if(typToken!=PARSER_END) SendError(3);
history.clear();
return true;
}
TParserNode *TParser::Expr(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp = Expr1(pFManager);
while(1)
{
if(typToken==PARSER_PLUS)
{
GetToken();
temp = CreateNode(OP_PLUS, temp, Expr1(pFManager));
}
else if(typToken==PARSER_MINUS)
{
GetToken();
temp = CreateNode(OP_MINUS, temp, Expr1(pFManager));
}
else break;
}
return temp;
}
TParserNode *TParser::Expr1(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp = Expr2(pFManager);
while(1)
{
if(typToken==PARSER_MULTIPLY)
{
GetToken();
temp = CreateNode(OP_MULTIPLY, temp, Expr2(pFManager));
}
else if(typToken==PARSER_DIVIDE)
{
GetToken();
temp = CreateNode(OP_DIVIDE, temp, Expr2(pFManager));
}
else if(typToken==PARSER_PERCENT)
{
GetToken();
temp = CreateNode(OP_PERCENT, temp, Expr2(pFManager));
}
else break;
}
return temp;
}
TParserNode *TParser::Expr2(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp = Expr3(pFManager);
while(1)
{
if(typToken==PARSER_POWER)
{
GetToken();
temp = CreateNode(OP_POWER, temp, Expr2(pFManager));
}
else break;
}
return temp;
}
TParserNode *TParser::Expr3(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp;
if(typToken==PARSER_PLUS)
{
GetToken();
temp = Expr4(pFManager);
}
else if(typToken==PARSER_MINUS)
{
GetToken();
temp = CreateNode(OP_UMINUS, Expr4(pFManager));
}
else
temp = Expr4(pFManager);
return temp;
}
TParserNode *TParser::Expr4(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp;
if(typToken>=PARSER_SIN && typToken<=PARSER_ABS)
{
temp = CreateNode(OP_SIN-PARSER_SIN+typToken);
GetToken();
if(typToken!=PARSER_L_BRACKET) SendError(4);
GetToken();
temp->left = Expr(pFManager);
if(typToken!=PARSER_R_BRACKET) SendError(5);
GetToken();
}
else if((typToken >= PARSER_MIN) && (typToken <= PARSER_ATAN2))
{
temp = CreateNode(OP_MIN-PARSER_MIN+typToken);
GetToken();
if(typToken!=PARSER_L_BRACKET) SendError(4);
GetToken();
temp->left = Expr(pFManager);
if(typToken != PARSER_COMMA) SendError(6);
GetToken();
temp->right = Expr(pFManager);
if(typToken!=PARSER_R_BRACKET) SendError(5);
GetToken();
}
else if(typToken == PARSER_IF)
{
temp = CreateNode(OP_IF);
GetToken();
if(typToken!=PARSER_L_BRACKET) SendError(4);
GetToken();
temp->left = Expr(pFManager);
if(typToken != PARSER_COMMA) SendError(6);
GetToken();
temp->right = Expr(pFManager);
if(typToken != PARSER_COMMA) SendError(6);
GetToken();
temp->third = Expr(pFManager);
if(typToken!=PARSER_R_BRACKET) SendError(5);
GetToken();
}
else
temp = Expr5(pFManager);
return temp;
}
TParserNode *TParser::Expr5(NSGuidesOdp::CFormulaManager& pFManager)
{
TParserNode *temp;
switch(typToken)
{
case PARSER_ADJUST:
temp = CreateNode(pFManager.GetValue(curToken));
GetToken();
break;
case PARSER_GUIDE:
temp = CreateNode(pFManager.GetValue(curToken));
GetToken();
break;
case PARSER_NUMBER:
temp = CreateNode(XmlUtils::GetDouble(curToken));
GetToken();
break;
case PARSER_PI:
temp = CreateNode((double)M_PI);
GetToken();
break;
case PARSER_E:
temp = CreateNode(exp((double)1.0));
GetToken();
break;
case PARSER_L_BRACKET:
GetToken();
temp = Expr(pFManager);
if(typToken!=PARSER_R_BRACKET) SendError(5);
GetToken();
break;
default:
SendError(6);
}
return temp;
}
double TParser::Evaluate(void)
{
result = CalcTree(root);
return result;
}
double TParser::CalcTree(TParserNode *tree)
{
static double temp;
if(tree->left==NULL && tree->right==NULL)
return tree->value;
else
switch((int)tree->value)
{
case OP_PLUS:
return CalcTree(tree->left)+CalcTree(tree->right);
case OP_MINUS:
return CalcTree(tree->left)-CalcTree(tree->right);
case OP_MULTIPLY:
return CalcTree(tree->left)*CalcTree(tree->right);
case OP_DIVIDE:
return CalcTree(tree->left)/CalcTree(tree->right);
case OP_PERCENT:
return (int)CalcTree(tree->left)%(int)CalcTree(tree->right);
case OP_POWER:
return (double)pow(CalcTree(tree->left),CalcTree(tree->right));
case OP_UMINUS:
return -CalcTree(tree->left);
case OP_SIN:
return sin(CalcTree(tree->left));
case OP_COS:
return cos(CalcTree(tree->left));
case OP_TG:
return tan(CalcTree(tree->left));
case OP_CTG:
return 1.0/tan(CalcTree(tree->left));
case OP_ARCSIN:
return asin(CalcTree(tree->left));
case OP_ARCCOS:
return acos(CalcTree(tree->left));
case OP_ARCTG:
return atan(CalcTree(tree->left));
case OP_ARCCTG:
return M_PI/2.0-atan(CalcTree(tree->left));
case OP_SH:
temp = CalcTree(tree->left);
return (exp(temp)-exp(-temp))/2.0;
case OP_CH:
temp = CalcTree(tree->left);
return (exp(temp)+exp(-temp))/2.0;
case OP_TH:
temp = CalcTree(tree->left);
return (exp(temp)-exp(-temp))/(exp(temp)+exp(-temp));
case OP_CTH:
temp = CalcTree(tree->left);
return (exp(temp)+exp(-temp))/(exp(temp)-exp(-temp));
case OP_EXP:
return exp(CalcTree(tree->left));
case OP_LG:
return log10(CalcTree(tree->left));
case OP_LN:
return log(CalcTree(tree->left));
case OP_SQRT:
return sqrt(CalcTree(tree->left));
case OP_ABS:
return abs(CalcTree(tree->left));
case OP_MIN:
return min(CalcTree(tree->left), CalcTree(tree->right));
case OP_MAX:
return max(CalcTree(tree->left), CalcTree(tree->right));
case OP_ATAN2:
return atan2(CalcTree(tree->left), CalcTree(tree->right));
case OP_IF:
return ((CalcTree(tree->left) > 0) ? CalcTree(tree->right) : CalcTree(tree->third));
}
return 0;
}
void TParser::DelTree(TParserNode *tree)
{
if(tree==NULL) return;
DelTree(tree->left);
DelTree(tree->right);
delete tree;
return;
}

View File

@@ -0,0 +1,124 @@
/*
* (c) Copyright Ascensio System SIA 2010-2014
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#if !defined(__SOLVER_H)
#define __SOLVER_H
#include "stdafx.h"
#include <vector>
using namespace std;
namespace NSGuidesOdp
{
class CFormulaManager;
}
struct TParserNode
{
double value;
TParserNode *left;
TParserNode *right;
TParserNode *third;
TParserNode(double _value=0.0, TParserNode *_left=NULL, TParserNode *_right=NULL, TParserNode *_third=NULL)
{ value = _value; left = _left; right = _right; third = _third; }
};
struct TError
{
CString error;
int pos;
TError() {};
TError(CString _error, int _pos) { error=_error; pos=_pos; }
};
class TParser
{
private:
TParserNode *root;
CString expr;
CString curToken;
enum { PARSER_PLUS, PARSER_MINUS, PARSER_MULTIPLY, PARSER_DIVIDE, PARSER_PERCENT, PARSER_POWER, PARSER_COMMA,
PARSER_SIN, PARSER_COS, PARSER_TG, PARSER_CTG, PARSER_ARCSIN, PARSER_ARCCOS, PARSER_ARCTG, PARSER_ARCCTG, PARSER_SH, PARSER_CH, PARSER_TH, PARSER_CTH,
PARSER_EXP, PARSER_LG, PARSER_LN, PARSER_SQRT, PARSER_ABS,
PARSER_MIN, PARSER_MAX, PARSER_ATAN2,
PARSER_IF,
PARSER_GUIDE, PARSER_ADJUST, PARSER_L_BRACKET, PARSER_R_BRACKET, PARSER_E, PARSER_PI, PARSER_NUMBER, PARSER_END } typToken;
int pos;
double result;
vector<TParserNode *> history;
private:
TParserNode *CreateNode(double _value=0.0, TParserNode *_left=NULL, TParserNode *_right=NULL, TParserNode *_third=NULL);
TParserNode *Expr(NSGuidesOdp::CFormulaManager& pFManager);
TParserNode *Expr1(NSGuidesOdp::CFormulaManager& pFManager);
TParserNode *Expr2(NSGuidesOdp::CFormulaManager& pFManager);
TParserNode *Expr3(NSGuidesOdp::CFormulaManager& pFManager);
TParserNode *Expr4(NSGuidesOdp::CFormulaManager& pFManager);
TParserNode *Expr5(NSGuidesOdp::CFormulaManager& pFManager);
bool GetToken();
bool IsSpace(void) {CString string(_T(" ")); return (string.Find(expr[pos]) >= 0);}
bool IsDelim(void) {CString string(_T("+-*/%^()[]")); return (string.Find(expr[pos]) >= 0);}
bool IsAdjust(void) {CString string(_T("$")); return (string.Find(expr[pos]) >= 0);}
bool IsGuide(void) {CString string(_T("?")); return (string.Find(expr[pos]) >= 0);}
bool IsComma(void) {CString string(_T(",")); return (string.Find(expr[pos]) >= 0);}
bool IsLetter(void) { return ((expr[pos]>=_T('a') && expr[pos]<=_T('z')) ||
(expr[pos]>=_T('A') && expr[pos]<=_T('Z'))); }
bool IsDigit(void) { return (expr[pos]>=_T('0') && expr[pos]<=_T('9')); }
bool IsPoint(void) { return (expr[pos]==_T('.')); }
double CalcTree(TParserNode *tree);
void DelTree(TParserNode *tree);
void SendError(int errNum);
public:
TParser() { result = 0.0; root = NULL; }
~TParser() { DelTree(root); root=NULL; }
bool Compile(CString _expr, NSGuidesOdp::CFormulaManager& pFManager);
void Decompile() { DelTree(root); root=NULL; }
double Evaluate(void);
double GetResult(void) { return result; }
};
#endif