initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / strings / word / wordIO.C
blob093a282846ef82f2cb6f7b80956c6cb5b0a93347
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Istream constructor and IOstream operators for word.
28 \*---------------------------------------------------------------------------*/
30 #include "word.H"
31 #include "IOstreams.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 Foam::word::word(Istream& is)
37     string()
39     is >> *this;
43 Foam::Istream& Foam::operator>>(Istream& is, word& w)
45     token t(is);
47     if (!t.good())
48     {
49         is.setBad();
50         return is;
51     }
53     if (t.isWord())
54     {
55         w = t.wordToken();
56     }
57     else if (t.isString())
58     {
59         // try a bit harder and convert string to word
60         w = t.stringToken();
61         string::stripInvalid<word>(w);
63         // flag empty strings and bad chars as an error
64         if (w.empty() || w.size() != t.stringToken().size())
65         {
66             is.setBad();
67             FatalIOErrorIn("operator>>(Istream&, word&)", is)
68                 << "wrong token type - expected word found non-word characters "
69                 << t.info()
70                 << exit(FatalIOError);
71             return is;
72         }
73     }
74     else
75     {
76         is.setBad();
77         FatalIOErrorIn("operator>>(Istream&, word&)", is)
78             << "wrong token type - expected word found "
79             << t.info()
80             << exit(FatalIOError);
82         return is;
83     }
85     // Check state of IOstream
86     is.check("Istream& operator>>(Istream&, word&)");
88     return is;
92 Foam::Ostream& Foam::operator<<(Ostream& os, const word& w)
94     os.write(w);
95     os.check("Ostream& operator<<(Ostream&, const word&)");
96     return os;
100 // ************************************************************************* //