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 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace substitutionModels
37 {
38  defineTypeNameAndDebug(fileRegEx, 0);
40 }
41 }
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
45 Foam::substitutionModels::fileRegEx::fileRegEx
46 (
47  const dictionary& dict,
48  const Time& time
49 )
50 :
51  substitutionModel(dict, time),
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 * * * * * * * * * * * * * //
75 
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  OStringStream oss;
128  regExp re(entries_[key].c_str());
129 
130  for (const string& data : lines)
131  {
133  if (re.match(data, match))
134  {
135  oss.reset();
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 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
dictionary dict
A line primitive.
Definition: line.H:52
A class for handling file names.
Definition: fileName.H:72
SubStrings< std::string > results_type
Type for matches - similar to std::smatch.
Definition: regExpPosix.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual bool apply(const word &key, string &buffer) const
Apply substitutions to this string buffer.
Definition: fileRegEx.C:76
Base class for substitution models.
const dimensionedScalar re
Classical electron radius: default SI units: [m].
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
regExpPosix regExp
Selection of preferred regular expression implementation.
Definition: regExpFwd.H:36
virtual bool valid(const word &keyName) const
Return true of model applies to this keyName.
Definition: fileRegEx.C:69
bool match(const UList< wordRe > &selectors, const std::string &text)
True if text matches one of the selector expressions.
Definition: stringOps.H:77
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
The fileRegEx substitution model.
Definition: fileRegEx.H:136
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
A class for handling words, derived from Foam::string.
Definition: word.H:63
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Input from file stream as an ISstream, normally using std::ifstream for the actual input...
Definition: IFstream.H:51
addToRunTimeSelectionTable(substitutionModel, dictionaryValue, dictionary)
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
virtual wordList keys() const
Return a word list of the keys.
Definition: fileRegEx.C:150
#define WarningInFunction
Report a warning using Foam::Warning.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
defineTypeNameAndDebug(dictionaryValue, 0)
messageStream Info
Information stream (stdout output on master, null elsewhere)
static word cleanKey(const string &str)
Clean the key text.
A class for handling character strings derived from std::string.
Definition: string.H:72
Namespace for OpenFOAM.