NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
procedurelibrary.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2017 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "procedurelibrary.hpp"
20#include "../ui/error.hpp"
21#include "../utils/tools.hpp"
22
23
30{
31 for (auto iter = mLibraryEntries.begin(); iter != mLibraryEntries.end(); ++iter)
32 delete (iter->second);
33}
34
35
45{
46 if (fileExists(sProcedureFileName))
47 {
48 return new ProcedureElement(getFileContents(sProcedureFileName), sProcedureFileName);
49 }
50
51 return nullptr;
52}
53
54
63StyledTextFile ProcedureLibrary::getFileContents(const std::string& sProcedureFileName)
64{
65 StyledTextFile procFile(sProcedureFileName);
66
67 if (!procFile.getLinesCount())
68 throw SyntaxError(SyntaxError::FILE_NOT_EXIST, sProcedureFileName, SyntaxError::invalid_position, sProcedureFileName);
69
70 return procFile;
71}
72
73
84ProcedureElement* ProcedureLibrary::getProcedureContents(const std::string& sProcedureFileName)
85{
86 if (mLibraryEntries.find(sProcedureFileName) == mLibraryEntries.end())
87 {
88 ProcedureElement* element = constructProcedureElement(sProcedureFileName);
89
90 if (element)
91 mLibraryEntries[sProcedureFileName] = element;
92 else
93 throw SyntaxError(SyntaxError::FILE_NOT_EXIST, sProcedureFileName, SyntaxError::invalid_position, sProcedureFileName);
94 }
95
96 return mLibraryEntries[sProcedureFileName];
97}
98
99
108{
109 for (auto iter = mLibraryEntries.begin(); iter != mLibraryEntries.end(); )
110 {
111 delete (iter->second);
112
113 try
114 {
115 ProcedureElement* element = constructProcedureElement(iter->first);
116
117 if (element)
118 {
119 iter->second = element;
120 iter++;
121 }
122 else
123 iter = mLibraryEntries.erase(iter);
124 }
125 catch (...)
126 {
127 iter = mLibraryEntries.erase(iter);
128 }
129 }
130}
131
This class contains the pre-parsed contents of a single procedure file.
std::map< std::string, ProcedureElement * > mLibraryEntries
StyledTextFile getFileContents(const std::string &sProcedureFileName)
Reads the contents of the passed file and returns it as a std::vector.
void updateLibrary()
Perform an update, e.g. if a procedure was deleted.
ProcedureElement * getProcedureContents(const std::string &sProcedureFileName)
Returns the ProcedureElement pointer to the desired procedure file. It also creates the element,...
ProcedureElement * constructProcedureElement(const std::string &sProcedureFileName)
Constructs a new ProcedureElement, if the file exists. Otherwise returns a nullptr.
~ProcedureLibrary()
Destructor avoiding memory leaks. Releases the memory allocated for each ProcedureElement.
This class represents a text file in memory (e.g. a code file). This class will try to lex the loaded...
int getLinesCount() const
Returns the number of lines in the current loaded file.
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ FILE_NOT_EXIST
Definition: error.hpp:105
static size_t invalid_position
Definition: error.hpp:235
bool fileExists(const string &)
This function checks, whether the file with the passed file name exists.
Definition: tools.cpp:2500