NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
tools.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2014 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
20#ifndef TOOLS_HPP
21#define TOOLS_HPP
22
23#include <string>
24#include <cstdlib>
25#include <vector>
26#include <random>
27
28#include "../structures.hpp"
29#include "../ui/error.hpp"
30#include "../settings.hpp"
31#include "stringtools.hpp"
32
33extern const std::string sVersion;
34
35long long int intCast(double number);
36long long int intCast(const std::complex<double>& number);
37bool isInt(const std::complex<double>& number);
38
39std::mt19937& getRandGenInstance();
40int findParameter(const std::string& sCmd, const std::string& sParam, const char cFollowing = ' ');
41bool getStringArgument(const std::string& sCmd, std::string& sArgument);
42std::string extractStringToken(const std::string& sCmd, size_t nPos);
43unsigned int getMatchingParenthesis(const StringView&);
44bool isMultiValue(const std::string& sExpr, bool bIgnoreClosingParenthesis = false);
45std::string replaceToTeX(const std::string& sString, bool replaceForTeXFile = false);
46Match findCommand(StringView sCmd, const std::string& sCommand = "");
47std::string extractCommandString(const std::string& sCmd, const Match& _mMatch);
48void openExternally(const std::string&);
49void moveFile(const std::string&, const std::string&);
50void copyFile(const std::string&, const std::string&);
51void writeTeXMain(const std::string&);
52
54{
60};
61
62std::string getArgAtPos(const std::string& sCmd, unsigned int nPos, int extraction = ARGEXTRACT_STRIPPED);
63bool isInQuotes(StringView sExpr, unsigned int nPos, bool bIgnoreVarParser = false);
64bool isToStringArg(const std::string& sExpr, unsigned int nPos);
65bool isDelimiter(char cChar);
66bool addLegends(std::string&);
67bool checkDelimiter(const std::string& sToken, bool stringdelim = false);
68std::vector<std::string> splitIntoLines(std::string sOutput, size_t lineWidth, bool bAllowDashBreaks = true, int nFirstIndent = 4, int nIndent = 4);
69std::string outputString(std::vector<std::string> stringInLines, int nFirstIndent, int nIndent);
70std::string LineBreak(std::string sOutput, const Settings& _option, bool bAllowDashBreaks = true, int nFirstIndent = 4, int nIndent = 4);
71double Linearize(double x_0, double y_0, double x_1, double y_1);
72void make_hline(int nLength = -1);
73void make_progressBar(int nStep, int nFirstStep = 1, int nFinalStep = 100, const std::string& sType = "std");
74bool containsStrings(const std::string& sLine);
75bool fileExists(const std::string& sFilename);
76void reduceLogFilesize(const std::string& sFileName);
77std::string replaceToVectorname(const std::string& sExpression);
78void eraseToken(std::string& sExpr, const std::string& sToken, bool bTokenHasValue = false);
79std::string getFileInfo(const std::string& sFilename);
80std::string decodeNameSpace(std::string sCommandLine, const std::string& sThisNameSpace);
81
82// This inline function checks, if the passed value is finite and
83// not a NAN value
84inline bool isValidValue(double d)
85{
86 return !std::isnan(d) && !std::isinf(d);
87}
88
97bool validateParenthesisNumber(const std::string& sCmd);
98
99void addArgumentQuotes(std::string& sToAdd, const std::string& sParam);
100void addArgumentQuotes(std::string& sToAdd, size_t pos);
101
109double intPower(double dNumber, int nExponent);
110std::complex<double> intPower(const std::complex<double>& dNumber, int nExponent);
111
112std::string getNextArgument(std::string& sArgList, bool bCut = true);
113std::string getNextIndex(std::string& sArgList, bool bCut = true);
114std::string getNextSemiColonSeparatedToken(std::string& sArgList, bool bCut = true);
118EndlessVector<std::string> getAllArguments(std::string sArgList);
119EndlessVector<std::string> getAllIndices(std::string sArgList);
121
122
130bool isToCmd(const std::string& sCmd, unsigned int nPos);
131
132unsigned int countEscapeSymbols(const std::string& sLine);
133std::vector<std::string> getFileList(const std::string& sDirectory, const Settings& _option, int nFlags = 0);
134std::vector<std::string> getFolderList(const std::string& sDirectory, const Settings& _option, int nFlags = 0);
135
137{
138 template <typename Word>
139 inline std::ostream& write_word( std::ostream& outs, Word value, unsigned size = sizeof( Word ) )
140 {
141 for (; size; --size, value >>= 8)
142 outs.put( static_cast <char> (value & 0xFF) );
143 return outs;
144 }
145
146 template <typename Word>
147 inline std::istream& read_word( std::istream& ins, Word& value, unsigned size = sizeof( Word ) )
148 {
149 for (unsigned n = 0, value = 0; n < size; ++n)
150 value |= ins.get() << (8 * n);
151 return ins;
152 }
153}
154
156{
157 template <typename Word>
158 inline std::ostream& write_word( std::ostream& outs, Word value, unsigned size = sizeof( Word ) )
159 {
160 while (size)
161 outs.put( static_cast <char> ( (value >> (8 * --size)) & 0xFF ));
162 return outs;
163 }
164
165 template <typename Word>
166 inline std::istream& read_word( std::istream& ins, Word& value, unsigned size = sizeof( Word ) )
167 {
168 for (value = 0; size; --size)
169 value = (value << 8) | ins.get();
170 return ins;
171 }
172}
173
174void evalRecursiveExpressions(std::string& sExpr);
175
176size_t qSortDouble(double* dArray, size_t nlength);
177void replaceStringMethod(std::string& sLine, size_t nPos, size_t nLength, const std::string& sReplacement);
178std::string shortenFileName(const std::string& sFullFileName);
179std::string incrementVersion(std::string _sVer);
180
181
182#endif
183
This class extends the std::vector for endlessness.
Definition: structures.hpp:838
This class manages the setting values of the internal (kernel) settings of this application.
Definition: settings.hpp:663
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
std::istream & read_word(std::istream &ins, Word &value, unsigned size=sizeof(Word))
Definition: tools.hpp:166
std::ostream & write_word(std::ostream &outs, Word value, unsigned size=sizeof(Word))
Definition: tools.hpp:158
std::ostream & write_word(std::ostream &outs, Word value, unsigned size=sizeof(Word))
Definition: tools.hpp:139
std::istream & read_word(std::istream &ins, Word &value, unsigned size=sizeof(Word))
Definition: tools.hpp:147
bool isnan(const value_type &v)
Definition: muParserDef.h:379
bool isinf(const value_type &v)
Definition: muParserDef.h:374
Structure for the findCommand function.
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
Definition: tools.cpp:414
std::string getNextArgument(std::string &sArgList, bool bCut=true)
Definition: tools.cpp:2294
EndlessVector< std::string > getAllIndices(std::string sArgList)
Splits up the complete index list and returns them as an EndlessVector.
Definition: tools.cpp:2384
void writeTeXMain(const std::string &)
Definition: tools.cpp:1449
int findParameter(const std::string &sCmd, const std::string &sParam, const char cFollowing=' ')
This function searches the passed parameter in the passed command string. If something is found,...
Definition: tools.cpp:113
Match findCommand(StringView sCmd, const std::string &sCommand="")
This function is very important for the command handler.
Definition: tools.cpp:1275
std::string extractCommandString(const std::string &sCmd, const Match &_mMatch)
Extracts the whole command string from a command line (which might contain more than one).
Definition: tools.cpp:1325
bool isDelimiter(char cChar)
This function determines, if the passed character is a delimiter character.
Definition: tools.cpp:1852
std::string shortenFileName(const std::string &sFullFileName)
This function will return a shortened version of the data file name, where each "/Path/" string part ...
Definition: tools.cpp:3953
bool isToCmd(const std::string &sCmd, unsigned int nPos)
Checks, whether the "to_cmd()" function was used.
Definition: tools.cpp:3675
void openExternally(const std::string &)
Opens a file with the program, which is associated by default within the Windows shell.
Definition: tools.cpp:1375
void make_progressBar(int nStep, int nFirstStep=1, int nFinalStep=100, const std::string &sType="std")
Wrapper for the static member function of the kernel.
Definition: tools.cpp:2425
bool isMultiValue(const std::string &sExpr, bool bIgnoreClosingParenthesis=false)
Determines, if a string contains multiple comma-separated expressions.
Definition: tools.cpp:484
std::string getFileInfo(const std::string &sFilename)
This function opens a NumeRe-Datafile file, reads its header and converts it to a human-readable stri...
Definition: tools.cpp:3446
bool fileExists(const std::string &sFilename)
This function checks, whether the file with the passed file name exists.
Definition: tools.cpp:2500
std::string getArgAtPos(const std::string &sCmd, unsigned int nPos, int extraction=ARGEXTRACT_STRIPPED)
Extracts a options value at the selected position and applies automatic parsing, if necessary.
Definition: tools.cpp:1598
bool isToStringArg(const std::string &sExpr, unsigned int nPos)
This function determines, whether the current position is part of an argument of the three value to s...
Definition: tools.cpp:1766
void copyFile(const std::string &, const std::string &)
Copy the file's contents to another file.
Definition: tools.cpp:1431
void reduceLogFilesize(const std::string &sFileName)
This function is used to reduce the log file size from >= 100.000 to 20.000 lines to reduce the neede...
Definition: tools.cpp:2998
StringView getNextViewedIndex(StringView &sView)
Definition: tools.cpp:2332
std::vector< std::string > getFolderList(const std::string &sDirectory, const Settings &_option, int nFlags=0)
This function returns a list of directories (including their paths, if nFlags & 1).
Definition: tools.cpp:2925
bool containsStrings(const std::string &sLine)
This function checks, whether the passed expression contains strings or valtostring parser.
Definition: tools.cpp:2470
std::vector< std::string > splitIntoLines(std::string sOutput, size_t lineWidth, bool bAllowDashBreaks=true, int nFirstIndent=4, int nIndent=4)
This function splits a string into multiple lines if the line is longer than the desired line width.
Definition: tools.cpp:2002
std::string replaceToVectorname(const std::string &sExpression)
This function replaces a data access expression (i.e. the contents of the object argument parentheses...
Definition: tools.cpp:3108
void eraseToken(std::string &sExpr, const std::string &sToken, bool bTokenHasValue=false)
This function erases option tokens including their possible value from a parameter string.
Definition: tools.cpp:2527
double intPower(double dNumber, int nExponent)
Calculates the power of a number using an integer as exponent.
Definition: tools.cpp:3607
std::string replaceToTeX(const std::string &sString, bool replaceForTeXFile=false)
Replace the special tokens with their TeX counterparts, so that they can be handled by a LaTeX interp...
Definition: tools.cpp:753
void replaceStringMethod(std::string &sLine, size_t nPos, size_t nLength, const std::string &sReplacement)
This function searches the indicated string variable occurence for possible string methods and replac...
Definition: tools.cpp:3875
bool getStringArgument(const std::string &sCmd, std::string &sArgument)
This function searches for the first string value in the passed expression and writes that into sArgu...
Definition: tools.cpp:250
void make_hline(int nLength=-1)
This function prints a horizontal line to the terminal using either minus or equal signs.
Definition: kernel.cpp:3720
double Linearize(double x_0, double y_0, double x_1, double y_1)
Determines the root of the line connecting the two passed points.
Definition: tools.cpp:2222
ArgExtraction
Definition: tools.hpp:54
@ ARGEXTRACT_ASINT
Definition: tools.hpp:58
@ ARGEXTRACT_STRIPPED
Definition: tools.hpp:56
@ ARGEXTRACT_PARSED
Definition: tools.hpp:57
@ ARGEXTRACT_ASSTRING
Definition: tools.hpp:59
@ ARGEXTRACT_NONE
Definition: tools.hpp:55
StringView getNextViewedArgument(StringView &sView)
Definition: tools.cpp:2327
unsigned int countEscapeSymbols(const std::string &sLine)
This function counts the number of escaped dollar signs.
Definition: tools.cpp:3708
bool checkDelimiter(const std::string &sToken, bool stringdelim=false)
Checks, whether the first and the last character in the passed string is a delimiter character.
Definition: tools.cpp:1982
bool isInt(const std::complex< double > &number)
Determines, whether the content of a complex value is actually a regular integer.
Definition: tools.cpp:1838
std::string outputString(std::vector< std::string > stringInLines, int nFirstIndent, int nIndent)
This function outputs a string by adding the required prefixes and joining the various strings togeth...
Definition: tools.cpp:2164
std::vector< std::string > getFileList(const std::string &sDirectory, const Settings &_option, int nFlags=0)
This function returns a list of files (including their paths, if nFlags & 1).
Definition: tools.cpp:2853
EndlessVector< std::string > getAllSemiColonSeparatedTokens(std::string sArgList)
Splits up the complete index list and returns them as an EndlessVector.
Definition: tools.cpp:2403
std::string getNextSemiColonSeparatedToken(std::string &sArgList, bool bCut=true)
Definition: tools.cpp:2316
std::mt19937 & getRandGenInstance()
Returns a reference to the central random number generator instance, which is globally accessible by ...
Definition: tools.cpp:91
bool isValidValue(double d)
Definition: tools.hpp:84
std::string LineBreak(std::string sOutput, const Settings &_option, bool bAllowDashBreaks=true, int nFirstIndent=4, int nIndent=4)
This function takes a string, splits it into multiple lines if it is too long and returns the result.
Definition: tools.cpp:2205
size_t qSortDouble(double *dArray, size_t nlength)
This is a wrapper for the standard qsort algorithm. It returns the number of valid elements and sorts...
Definition: tools.cpp:3762
std::string incrementVersion(std::string _sVer)
Increments a MAJOR.MINOR.BUILD version string by one build count.
Definition: tools.cpp:3991
bool addLegends(std::string &)
This function adds the missing legend strings to the expressions in the passed string.
Definition: tools.cpp:1875
void moveFile(const std::string &, const std::string &)
Move a file from one to another location. Can be used for renaming as well.
Definition: tools.cpp:1412
std::string getNextIndex(std::string &sArgList, bool bCut=true)
Definition: tools.cpp:2305
std::string decodeNameSpace(std::string sCommandLine, const std::string &sThisNameSpace)
Definition: tools.cpp:3476
long long int intCast(double number)
Casts doubles to integers and avoids rounding errors.
Definition: tools.cpp:1805
const std::string sVersion
EndlessVector< StringView > getAllArguments(StringView sArgList)
Splits up the complete argument list and returns them as an EndlessVector.
Definition: tools.cpp:2346
void evalRecursiveExpressions(std::string &sExpr)
This function converts the recursive assignments like VAR += VAL into VAR = VAR + (VAL)
Definition: tools.cpp:3346
bool validateParenthesisNumber(const std::string &sCmd)
Checks, whether the number of parentheses is an even number.
Definition: tools.cpp:3512
void addArgumentQuotes(std::string &sToAdd, const std::string &sParam)
This function adds quotation marks around the value of the specified parameter.
Definition: tools.cpp:3560
bool isInQuotes(StringView sExpr, unsigned int nPos, bool bIgnoreVarParser=false)
Checks, whether the position in the passed expression is part of a string literal.
Definition: tools.cpp:1672
std::string extractStringToken(const std::string &sCmd, size_t nPos)
This function extracts a string token at the selected position. If the position is start of an option...
Definition: tools.cpp:310