Report patch name instead of index in debug
[foam-extend-3.2.git] / src / foam / containers / Lists / List / ListIO.C
blob488669ee10ebea19ae6a6b8c833deb629c7318fd
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "List.H"
27 #include "Istream.H"
28 #include "token.H"
29 #include "SLList.H"
30 #include "contiguous.H"
32 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
34 // Construct from Istream
35 template<class T>
36 Foam::List<T>::List(Istream& is)
38     UList<T>(NULL, 0)
40     operator>>(is, *this);
44 template<class T>
45 Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
47     // Anull list
48     L.setSize(0);
50     is.fatalCheck("operator>>(Istream&, List<T>&)");
52     token firstToken(is);
54     is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
56     if (firstToken.isCompound())
57     {
58         L.transfer
59         (
60             dynamicCast<token::Compound<List<T> > >
61             (
62                 firstToken.transferCompoundToken()
63             )
64         );
65     }
66     else if (firstToken.isLabel())
67     {
68         label s = firstToken.labelToken();
70         // Set list length to that read
71         L.setSize(s);
73         // Read list contents depending on data format
75         if (is.format() == IOstream::ASCII || !contiguous<T>())
76         {
77             // Read beginning of contents
78             char delimiter = is.readBeginList("List");
80             if (s)
81             {
82                 if (delimiter == token::BEGIN_LIST)
83                 {
84                     for (register label i=0; i<s; i++)
85                     {
86                         is >> L[i];
88                         is.fatalCheck
89                         (
90                             "operator>>(Istream&, List<T>&) : reading entry"
91                         );
92                     }
93                 }
94                 else
95                 {
96                     T element;
97                     is >> element;
99                     is.fatalCheck
100                     (
101                         "operator>>(Istream&, List<T>&) : "
102                         "reading the single entry"
103                     );
105                     for (register label i=0; i<s; i++)
106                     {
107                         L[i] = element;
108                     }
109                 }
110             }
112             // Read end of contents
113             is.readEndList("List");
114         }
115         else
116         {
117             if (s)
118             {
119                 is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
121                 is.fatalCheck
122                 (
123                     "operator>>(Istream&, List<T>&) : reading the binary block"
124                 );
125             }
126         }
127     }
128     else if (firstToken.isPunctuation())
129     {
130         if (firstToken.pToken() != token::BEGIN_LIST)
131         {
132             FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
133                 << "incorrect first token, expected '(', found "
134                 << firstToken.info()
135                 << exit(FatalIOError);
136         }
138         // Putback the openning bracket
139         is.putBack(firstToken);
141         // Now read as a singly-linked list
142         SLList<T> sll(is);
144         // Convert the singly-linked list to this list
145         L = sll;
146     }
147     else
148     {
149         FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
150             << "incorrect first token, expected <int> or '(', found "
151             << firstToken.info()
152             << exit(FatalIOError);
153     }
155     return is;
159 template<class T>
160 Foam::List<T> Foam::readList(Istream& is)
162     List<T> L;
163     token firstToken(is);
164     is.putBack(firstToken);
166     if (firstToken.isPunctuation())
167     {
168         if (firstToken.pToken() != token::BEGIN_LIST)
169         {
170             FatalIOErrorIn("readList<T>(Istream&)", is)
171                 << "incorrect first token, expected '(', found "
172                 << firstToken.info()
173                 << exit(FatalIOError);
174         }
176         // read via a singly-linked list
177         L = SLList<T>(is);
178     }
179     else
180     {
181         // create list with a single item
182         L.setSize(1);
184         is >> L[0];
185     }
187     return L;
191 // ************************************************************************* //