fixed writing out entries in advective bc
[OpenFOAM-1.6-ext.git] / src / OpenFOAM / containers / Lists / FixedList / FixedListIO.C
blob4f6f7747e015ae5389db846580d0776848c12e59
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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 \*---------------------------------------------------------------------------*/
27 #include "FixedList.H"
28 #include "Istream.H"
29 #include "Ostream.H"
30 #include "token.H"
31 #include "contiguous.H"
33 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
35 template<class T, unsigned Size>
36 Foam::FixedList<T, Size>::FixedList(Istream& is)
38     operator>>(is, *this);
42 template<class T, unsigned Size>
43 Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
45     is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
47     if (is.format() == IOstream::ASCII || !contiguous<T>())
48     {
49         token firstToken(is);
51         is.fatalCheck
52         (
53             "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
54         );
56         if (firstToken.isCompound())
57         {
58             L = dynamicCast<token::Compound<List<T> > >
59             (
60                 firstToken.transferCompoundToken()
61             );
62         }
63         else if (firstToken.isLabel())
64         {
65             label s = firstToken.labelToken();
67             // Set list length to that read
68             L.checkSize(s);
69         }
70         else if (!firstToken.isPunctuation())
71         {
72             FatalIOErrorIn("operator>>(Istream&, FixedList<T, Size>&)", is)
73                 << "incorrect first token, expected <label> "
74                    "or '(' or '{', found "
75                 << firstToken.info()
76                 << exit(FatalIOError);
77         }
78         else
79         {
80             // Putback the opening bracket
81             is.putBack(firstToken);
82         }
84         // Read beginning of contents
85         char delimiter = is.readBeginList("FixedList");
87         if (delimiter == token::BEGIN_LIST)
88         {
89             for (register unsigned i=0; i<Size; i++)
90             {
91                 is >> L[i];
93                 is.fatalCheck
94                 (
95                     "operator>>(Istream&, FixedList<T, Size>&) : "
96                     "reading entry"
97                 );
98             }
99         }
100         else
101         {
102             T element;
103             is >> element;
105             is.fatalCheck
106             (
107                 "operator>>(Istream&, FixedList<T, Size>&) : "
108                 "reading the single entry"
109             );
111             for (register unsigned i=0; i<Size; i++)
112             {
113                 L[i] = element;
114             }
115         }
117         // Read end of contents
118         is.readEndList("FixedList");
119     }
120     else
121     {
122         is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
124         is.fatalCheck
125         (
126             "operator>>(Istream&, FixedList<T, Size>&) : "
127             "reading the binary block"
128         );
129     }
131     return is;
135 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
137 template<class T, unsigned Size>
138 void Foam::FixedList<T, Size>::writeEntry(Ostream& os) const
140     if
141     (
142         size()
143      && token::compound::isCompound
144         (
145             "List<" + word(pTraits<T>::typeName) + '>'
146         )
147     )
148     {
149         os  << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
150     }
152     os << *this;
156 template<class T, unsigned Size>
157 void Foam::FixedList<T, Size>::writeEntry
159     const word& keyword,
160     Ostream& os
161 ) const
163     os.writeKeyword(keyword);
164     writeEntry(os);
165     os << token::END_STATEMENT << endl;
169 template<class T, unsigned Size>
170 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
172     // Write list contents depending on data format
173     if (os.format() == IOstream::ASCII || !contiguous<T>())
174     {
175         bool uniform = false;
177         if (Size > 1 && contiguous<T>())
178         {
179             uniform = true;
181             forAll(L, i)
182             {
183                 if (L[i] != L[0])
184                 {
185                     uniform = false;
186                     break;
187                 }
188             }
189         }
191         if (uniform)
192         {
193             // Write size (so it is valid dictionary entry) and start delimiter
194             os << L.size() << token::BEGIN_BLOCK;
196             // Write contents
197             os << L[0];
199             // Write end delimiter
200             os << token::END_BLOCK;
201         }
202         else if (Size < 11 && contiguous<T>())
203         {
204             // Write start delimiter
205             os << token::BEGIN_LIST;
207             // Write contents
208             forAll(L, i)
209             {
210                 if (i > 0) os << token::SPACE;
211                 os << L[i];
212             }
214             // Write end delimiter
215             os << token::END_LIST;
216         }
217         else
218         {
219             // Write start delimiter
220             os << nl << token::BEGIN_LIST;
222             // Write contents
223             forAll(L, i)
224             {
225                 os << nl << L[i];
226             }
228             // Write end delimiter
229             os << nl << token::END_LIST << nl;
230         }
231     }
232     else
233     {
234         os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
235     }
237     // Check state of IOstream
238     os.check("Ostream& operator<<(Ostream&, const FixedList&)");
240     return os;
244 // ************************************************************************* //