dictionaryCompat.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) 2017-2022 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 "dictionary.H"
29 #include "Pstream.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
34 (
35  const word& keyword,
36  std::initializer_list<std::pair<const char*,int>> compat,
37  enum keyType::option matchOpt
38 ) const
39 {
40  const_searcher finder(csearch(keyword, matchOpt));
41 
42  if (finder.good())
43  {
44  return finder;
45  }
46 
47  for (const std::pair<const char*,int>& alt : compat)
48  {
49  finder = csearch(word::validate(alt.first), matchOpt);
50 
51  if (finder.good())
52  {
53  if (error::warnAboutAge(alt.second) && error::master())
54  {
55  std::cerr
56  << "--> FOAM IOWarning :" << nl
57  << " Found [v" << alt.second << "] '"
58  << alt.first << "' entry instead of '"
59  << keyword.c_str() << "' in dictionary \""
60  << relativeName() << '"' << nl
61  << std::endl;
62 
63  error::warnAboutAge("keyword", alt.second);
64  }
65 
66  break;
67  }
68  }
69 
70  return finder;
71 }
72 
73 
75 (
76  const word& keyword,
77  std::initializer_list<std::pair<const char*,int>> compat,
78  enum keyType::option matchOpt
79 ) const
80 {
81  return csearchCompat(keyword, compat, matchOpt).ptr();
82 }
83 
84 
86 (
87  const word& keyword,
88  std::initializer_list<std::pair<const char*,int>> compat,
89  enum keyType::option matchOpt
90 ) const
91 {
92  return static_cast<bool>(findCompat(keyword, compat, matchOpt));
93 }
94 
95 
97 (
98  const word& keyword,
99  std::initializer_list<std::pair<const char*,int>> compat,
100  enum keyType::option matchOpt
101 ) const
102 {
103  const entry* eptr = findCompat(keyword, compat, matchOpt);
104 
105  if (!eptr)
106  {
108  << "Entry '" << keyword << "' not found in dictionary "
109  << relativeName()
110  << exit(FatalIOError);
111  }
112 
113  return *eptr;
114 }
115 
116 
118 (
119  const word& keyword,
120  std::initializer_list<std::pair<const char*,int>> compat,
121  enum keyType::option matchOpt
122 ) const
123 {
124  return lookupEntryCompat(keyword, compat, matchOpt).stream();
125 }
126 
127 
128 // ************************************************************************* //
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:39
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:67
static bool master(const label communicator=-1)
Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised...
Definition: error.C:53
Generic const/non-const dictionary entry searcher.
Definition: dictionary.H:149
bool good() const noexcept
True if entry was found.
Definition: dictionary.H:235
A class for handling words, derived from Foam::string.
Definition: word.H:63
const_searcher csearchCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword and any compatibility names.
ITstream & lookupCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt=keyType::REGEX) const
Find and return an entry data stream, using any compatibility names if needed.
const entry * findCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt) const
Find and return an entry pointer if present, or return a nullptr, using any compatibility names if ne...
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:179
bool foundCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword and any compatibility names.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
const_searcher csearch(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword.
const entry & lookupEntryCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt) const
Find and return an entry if present, otherwise FatalIOError, using any compatibility names if needed...
An input stream of tokens.
Definition: ITstream.H:52
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...