From 480a3c6ea2e960dedbefe025acfb06dd43066514 Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 2 Oct 2008 17:28:33 +0100 Subject: [PATCH] wildcards in dictionaries --- .../primitives/strings/keyType/keyType.H | 142 ++++++++++++++++++ .../primitives/strings/keyType/keyTypeI.H | 132 ++++++++++++++++ .../primitives/strings/keyType/keyTypeIO.C | 88 +++++++++++ 3 files changed, 362 insertions(+) create mode 100644 src/OpenFOAM/primitives/strings/keyType/keyType.H create mode 100644 src/OpenFOAM/primitives/strings/keyType/keyTypeI.H create mode 100644 src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H new file mode 100644 index 0000000000..4d4c358d5d --- /dev/null +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H @@ -0,0 +1,142 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::keyType + +Description + A class for handling keywords in dictionaries. + + A keyType is the keyword of a dictionary. It differs from word in that + it accepts wildcards. + +SourceFiles + keyType.C + keyTypeIO.C + +\*---------------------------------------------------------------------------*/ + +#ifndef keyType_H +#define keyType_H + +#include "word.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of classes +class Istream; +class Ostream; + + +/*---------------------------------------------------------------------------*\ + Class keyType Declaration +\*---------------------------------------------------------------------------*/ + +class keyType +: + public word +{ + // Private member data + + bool isWildCard_; + + // Private Member Functions + + //- Disallow assignments where we cannot determine string/word type + void operator=(const std::string&); + +public: + + + // Constructors + + //- Construct null + inline keyType(); + + //- Construct as copy + inline keyType(const keyType& s); + + //- Construct as copy of word + inline keyType(const word& s); + + //- Construct as copy of string. Expect it to be regular expression. + inline keyType(const string& s); + + //- Construct as copy of character array + inline keyType(const char* s); + + //- Construct as copy of std::string + inline keyType(const std::string& s, const bool isWildCard); + + //- Construct from Istream + keyType(Istream& is); + + + // Member functions + + //- Is this character valid for a keyType + inline static bool valid(char c); + + //- Is the type a wildcard? + inline bool isWildCard() const; + + + // Member operators + + // Assignment + + inline void operator=(const keyType& s); + + //- Assign from regular expression. + inline void operator=(const string& s); + + inline void operator=(const word& s); + + inline void operator=(const char*); + + + // IOstream operators + + friend Istream& operator>>(Istream& is, keyType& w); + + friend Ostream& operator<<(Ostream& os, const keyType& w); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "keyTypeI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H new file mode 100644 index 0000000000..f3785ebbff --- /dev/null +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -0,0 +1,132 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +//- Construct null +inline Foam::keyType::keyType() +: + word(), + isWildCard_(false) +{} + + +//- Construct as copy +inline Foam::keyType::keyType(const keyType& s) +: + word(s, false), + isWildCard_(s.isWildCard()) +{} + + +//- Construct as copy of word +inline Foam::keyType::keyType(const word& s) +: + word(s, false), + isWildCard_(false) +{} + + +//- Construct as copy of string. Expect it to be regular expression +inline Foam::keyType::keyType(const string& s) +: + word(s, false), + isWildCard_(true) +{} + + +//- Construct as copy of character array +inline Foam::keyType::keyType(const char* s) +: + word(s, false), + isWildCard_(false) +{} + + +//- Construct as copy of std::string +inline Foam::keyType::keyType +( + const std::string& s, + const bool isWildCard +) +: + word(s, false), + isWildCard_(isWildCard) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline bool Foam::keyType::valid(char c) +{ + return c != '"'; +} + + +bool Foam::keyType::isWildCard() const +{ + return isWildCard_; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline void Foam::keyType::operator=(const keyType& s) +{ + // Bypass checking + string::operator=(s); + isWildCard_ = s.isWildCard(); +} + + +inline void Foam::keyType::operator=(const word& s) +{ + word::operator=(s); + isWildCard_ = false; +} + + +inline void Foam::keyType::operator=(const string& s) +{ + // Bypass checking + string::operator=(s); + isWildCard_ = true; +} + + +inline void Foam::keyType::operator=(const char* s) +{ + // Bypass checking + string::operator=(s); + isWildCard_ = false; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C b/src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C new file mode 100644 index 0000000000..11232282f9 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C @@ -0,0 +1,88 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Description + Istream constructor and IOstream operators for word. + +\*---------------------------------------------------------------------------*/ + +#include "keyType.H" +#include "IOstreams.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::keyType::keyType(Istream& is) +: + word() +{ + is >> *this; +} + + +Foam::Istream& Foam::operator>>(Istream& is, keyType& w) +{ + token t(is); + + if (!t.good()) + { + is.setBad(); + return is; + } + + if (t.isWord()) + { + w = t.wordToken(); + } + else if (t.isString()) + { + // Assign from string. Sets regular expression. + w = t.stringToken(); + } + else + { + is.setBad(); + FatalIOErrorIn("operator>>(Istream&, keyType&)", is) + << "wrong token type - expected word or string found " + << t.info() + << exit(FatalIOError); + + return is; + } + + // Check state of IOstream + is.check("Istream& operator>>(Istream&, keyType&)"); + + return is; +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& w) +{ + os.write(w); + os.check("Ostream& operator<<(Ostream&, const keyType&)"); + return os; +} + + +// ************************************************************************* //