initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / List / ListIO.C
blob2ba1d4f00e33205fd5fc3f891143cf79a0bdcc32
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 \*---------------------------------------------------------------------------*/
27 #include "List.H"
28 #include "Istream.H"
29 #include "token.H"
30 #include "SLList.H"
31 #include "contiguous.H"
33 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
35 // Construct from Istream
36 template<class T>
37 Foam::List<T>::List(Istream& is)
39     UList<T>(NULL, 0)
41     operator>>(is, *this);
45 template<class T>
46 Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
48     // Anull list
49     L.setSize(0);
51     is.fatalCheck("operator>>(Istream&, List<T>&)");
53     token firstToken(is);
55     is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
57     if (firstToken.isCompound())
58     {
59         L.transfer
60         (
61             dynamicCast<token::Compound<List<T> > >
62             (
63                 firstToken.transferCompoundToken()
64             )
65         );
66     }
67     else if (firstToken.isLabel())
68     {
69         label s = firstToken.labelToken();
71         // Set list length to that read
72         L.setSize(s);
74         // Read list contents depending on data format
76         if (is.format() == IOstream::ASCII || !contiguous<T>())
77         {
78             // Read beginning of contents
79             char delimiter = is.readBeginList("List");
81             if (s)
82             {
83                 if (delimiter == token::BEGIN_LIST)
84                 {
85                     for (register label i=0; i<s; i++)
86                     {
87                         is >> L[i];
89                         is.fatalCheck
90                         (
91                             "operator>>(Istream&, List<T>&) : reading entry"
92                         );
93                     }
94                 }
95                 else
96                 {
97                     T element;
98                     is >> element;
100                     is.fatalCheck
101                     (
102                         "operator>>(Istream&, List<T>&) : "
103                         "reading the single entry"
104                     );
106                     for (register label i=0; i<s; i++)
107                     {
108                         L[i] = element;
109                     }
110                 }
111             }
113             // Read end of contents
114             is.readEndList("List");
115         }
116         else
117         {
118             if (s)
119             {
120                 is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
122                 is.fatalCheck
123                 (
124                     "operator>>(Istream&, List<T>&) : reading the binary block"
125                 );
126             }
127         }
128     }
129     else if (firstToken.isPunctuation())
130     {
131         if (firstToken.pToken() != token::BEGIN_LIST)
132         {
133             FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
134                 << "incorrect first token, expected '(', found "
135                 << firstToken.info()
136                 << exit(FatalIOError);
137         }
139         // Putback the openning bracket
140         is.putBack(firstToken);
142         // Now read as a singly-linked list
143         SLList<T> sll(is);
145         // Convert the singly-linked list to this list
146         L = sll;
147     }
148     else
149     {
150         FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
151             << "incorrect first token, expected <int> or '(', found "
152             << firstToken.info()
153             << exit(FatalIOError);
154     }
156     return is;
160 template<class T>
161 Foam::List<T> Foam::readList(Istream& is)
163     List<T> L;
164     token firstToken(is);
165     is.putBack(firstToken);
167     if (firstToken.isPunctuation())
168     {
169         if (firstToken.pToken() != token::BEGIN_LIST)
170         {
171             FatalIOErrorIn("readList<T>(Istream&)", is)
172                 << "incorrect first token, expected '(', found "
173                 << firstToken.info()
174                 << exit(FatalIOError);
175         }
177         // read via a singly-linked list
178         L = SLList<T>(is);
179     }
180     else
181     {
182         // create list with a single item
183         L.setSize(1);
185         is >> L[0];
186     }
188     return L;
192 // ************************************************************************* //