Loading...
Searching...
No Matches
fileRegEx.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \ / O peration |
5 \ / A nd | www.openfoam.com
6 \/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2024-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "fileRegEx.H"
30#include "IFstream.H"
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36namespace substitutionModels
37{
39 addToRunTimeSelectionTable(substitutionModel, fileRegEx, dictionary);
40}
41}
42
43// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44
45Foam::substitutionModels::fileRegEx::fileRegEx
46(
47 const dictionary& dict,
48 const Time& time
49)
50:
52 path_(dict.get<fileName>("path")),
53 entries_(),
54 sectionSeparator_
55 (
56 dict.getOrDefault<string>
57 (
58 "sectionSeparator",
59 "Time ="
60 )
61 ),
62 matchSeparator_(dict.getOrDefault<string>("matchSeparator", " ")),
63 lastMatch_(dict.getOrDefault<bool>("lastMatch", true))
64{
65 // Populate entries
66 const dictionary& entriesDict = dict.subDict("entries");
67 for (const auto& e : entriesDict)
68 {
69 entries_.insert(cleanKey(e.keyword()), string(e.stream()));
70 }
71}
72
73
74// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76bool Foam::substitutionModels::fileRegEx::valid(const word& keyName) const
77{
78 return entries_.found(keyName);
79}
80
81
83(
84 const word& key,
85 string& buffer
86) const
87{
88 if (!valid(key)) return false;
89
90 fileName path(path_);
91 replaceBuiltin(path);
92 IFstream is(path);
93
94 if (!is.good())
95 {
97 << "Unable to find file at " << path_
98 << ". Deactivating." << endl;
99
100 return false;
101 }
102
103 Info<< "Scanning for sections beginning with "
104 << sectionSeparator_ << endl;
105
106 // For log files containing multiple time steps
107 // - put strings for last time step into a string list
108 DynamicList<string> lines(96);
109 string line;
110 bool started = sectionSeparator_.empty() ? true : false;
111 while (is.good())
112 {
113 is.getLine(line);
114 if (line.starts_with(sectionSeparator_))
115 {
116 started = true;
117 lines.clear();
118 }
119 if (started)
120 {
121 lines.append(line);
122 }
123 }
124
125 Info<< "Cached " << lines.size() << " lines" << endl;
126
127 OCharStream oss;
128 const regExp re(entries_[key].c_str());
129
130 for (const string& data : lines)
131 {
133 if (re.match(data, match))
134 {
135 oss.rewind();
136
137 for (size_t i = 1; i < match.size(); ++i)
138 {
139 if (i > 1) oss << matchSeparator_;
140 oss << match[i].str().c_str();
141 }
142
143 if (!lastMatch_) break;
144 }
145 }
146
147 if (oss.count())
148 {
149 buffer.replaceAll(keyify(key), oss.str());
150 return true;
151 }
152
153 return false;
154}
155
156
158{
159 return entries_.sortedToc();
160}
161
162
163// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
void append(const T &val)
Copy append an element to the end of this list.
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition ISstreamI.H:69
An OSstream with internal List storage.
auto str() const
For OStringStream compatibility, return the buffer as string copy.
std::streamsize count() const
The number of bytes outputted.
virtual void rewind()
Rewind the stream, clearing any old errors.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling file names.
Definition fileName.H:75
A line primitive.
Definition line.H:180
std::smatch results_type
Type for matches.
Definition regExpCxx.H:124
A class for handling character strings derived from std::string.
Definition string.H:76
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Replace all occurrences of sub-string s1 with s2, beginning at pos in the string.
Definition string.C:117
Base class for substitution models.
static word cleanKey(const string &str)
Clean the key text.
static string keyify(const word &w)
Return a key representation from a word.
static bool replaceBuiltin(const word &key, string &str)
Replace key in string.
The fileRegEx substitution model.
Definition fileRegEx.H:139
virtual bool apply(const word &key, string &buffer) const
Apply substitutions to this string buffer.
Definition fileRegEx.C:76
virtual bool valid(const word &keyName) const
Return true of model applies to this keyName.
Definition fileRegEx.C:69
virtual wordList keys() const
Return a word list of the keys.
Definition fileRegEx.C:150
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionedScalar re
Classical electron radius: default SI units: [m].
bool match(const UList< wordRe > &selectors, const std::string &text)
True if text matches one of the selector expressions.
Definition stringOps.H:79
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
messageStream Info
Information stream (stdout output on master, null elsewhere).
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition regExpFwd.H:37
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
dictionary dict
volScalarField & e