1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 08:55:37 +00:00

XML parameter file reader based on tinyxml2

This commit is contained in:
Antonin Portelli 2014-03-17 15:01:58 +00:00
parent a14e5eb20e
commit 11fb009d11
6 changed files with 4416 additions and 2 deletions

View File

@ -8,7 +8,7 @@ AC_CONFIG_SRCDIR([lib/Global.cpp])
AC_CONFIG_SRCDIR([utils/sample_read.cpp])
AC_CONFIG_SRCDIR([examples/exMathInterpreter.cpp])
AC_CONFIG_MACRO_DIR([.buildutils/m4])
AM_INIT_AUTOMAKE([-Wall -Werror])
AM_INIT_AUTOMAKE([-Wall -Werror subdir-objects])
AM_SILENT_RULES([yes])
AC_CONFIG_HEADERS([config.h])

View File

@ -18,7 +18,7 @@ AM_LFLAGS = -olex.yy.c
AM_YFLAGS = -d
include eigen_files.mk
nobase_dist_pkginclude_HEADERS = $(eigen_files)
nobase_dist_pkginclude_HEADERS = $(eigen_files) XML/tinyxml2.hpp
BUILT_SOURCES = AsciiParser.hpp MathParser.hpp
@ -47,8 +47,10 @@ libLatAnalyze_la_SOURCES =\
Model.cpp \
Plot.cpp \
RandGen.cpp \
XmlReader.cpp \
XYSampleData.cpp \
XYStatData.cpp \
XML/tinyxml2.cpp \
../config.h
libLatAnalyze_ladir = $(pkgincludedir)
libLatAnalyze_la_HEADERS =\
@ -57,6 +59,7 @@ libLatAnalyze_la_HEADERS =\
CompiledFunction.hpp \
CompiledModel.hpp \
Dataset.hpp \
Exceptions.hpp \
FitInterface.hpp \
Function.hpp \
Global.hpp \
@ -72,6 +75,7 @@ libLatAnalyze_la_HEADERS =\
Plot.hpp \
RandGen.hpp \
StatArray.hpp \
XmlReader.hpp \
XYSampleData.hpp \
XYStatData.hpp
if HAVE_MINUIT

2194
lib/XML/tinyxml2.cpp Normal file

File diff suppressed because it is too large Load Diff

2076
lib/XML/tinyxml2.hpp Normal file

File diff suppressed because it is too large Load Diff

56
lib/XmlReader.cpp Normal file
View File

@ -0,0 +1,56 @@
/*
* XmlReader.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2014 Antonin Portelli
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#include <LatAnalyze/XmlReader.hpp>
#include <LatAnalyze/includes.hpp>
using namespace std;
using namespace Latan;
/******************************************************************************
* XmlReader implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
XmlReader::XmlReader(const string &fileName)
{
open(fileName);
}
// IO //////////////////////////////////////////////////////////////////////////
void XmlReader::open(const std::string &fileName)
{
name_ = fileName;
doc_.LoadFile(name_.c_str());
if (doc_.Error())
{
string errMsg1, errMsg2;
if (doc_.GetErrorStr1())
{
errMsg1 = doc_.GetErrorStr1();
}
if (doc_.GetErrorStr2())
{
errMsg2 = doc_.GetErrorStr2();
}
LATAN_ERROR(Io, "tinyxml2 code " + strFrom(doc_.ErrorID()) + ": " +
errMsg1 + " - " + errMsg2);
}
root_ = doc_.RootElement();
}

84
lib/XmlReader.hpp Normal file
View File

@ -0,0 +1,84 @@
/*
* XmlReader.hpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2014 Antonin Portelli
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Latan_XmlReader_hpp_
#define Latan_XmlReader_hpp_
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/XML/tinyxml2.hpp>
#include <iostream>
BEGIN_NAMESPACE
/******************************************************************************
* XML parameter file reader *
******************************************************************************/
class XmlReader
{
public:
// constructor
XmlReader(void) = default;
explicit XmlReader(const std::string &fileName);
// destructor
virtual ~XmlReader(void) = default;
// IO
template <typename T, typename... Strs>
T getFirstValue(const std::string &nodeName, Strs... nodeNames);
void open(const std::string &fileName);
private:
std::string name_;
tinyxml2::XMLDocument doc_;
tinyxml2::XMLElement *root_{nullptr};
};
/******************************************************************************
* XmlReader template implementation *
******************************************************************************/
template <typename T, typename... Strs>
T XmlReader::getFirstValue(const std::string &nodeName, Strs... nodeNames)
{
static_assert(static_or<std::is_assignable<std::string, Strs>::value...>::value,
"getFirstValue arguments are not compatible with std::string");
const unsigned int nName = sizeof...(nodeNames) + 1;
const std::string name[] = {nodeName, nodeNames...};
tinyxml2::XMLElement *node = root_;
if (!root_)
{
LATAN_ERROR(Io, "no XML file opened");
}
//std::cout << "root: " << name[i] << " ";
for (unsigned int i = 0; i < nName; ++i)
{
//std::cout << i << ": " << name[i] << " ";
node = node->FirstChildElement(name[i].c_str());
// std::cout <<
if (!node)
{
LATAN_ERROR(Parsing, "XML node " + name[i] + " not found");
}
}
return strTo<T>(node->GetText());
}
END_NAMESPACE
#endif // Latan_XmlReader_hpp_