FreeFOAM The Cross-Platform CFD Toolkit
ListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "List.H"
27 #include <OpenFOAM/Istream.H>
28 #include <OpenFOAM/token.H>
29 #include <OpenFOAM/SLList.H>
30 #include <OpenFOAM/contiguous.H>
31 
32 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
33 
34 // Construct from Istream
35 template<class T>
37 :
38  UList<T>(NULL, 0)
39 {
40  operator>>(is, *this);
41 }
42 
43 
44 template<class T>
46 {
47  // Anull list
48  L.setSize(0);
49 
50  is.fatalCheck("operator>>(Istream&, List<T>&)");
51 
52  token firstToken(is);
53 
54  is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
55 
56  if (firstToken.isCompound())
57  {
58  L.transfer
59  (
61  (
62  firstToken.transferCompoundToken()
63  )
64  );
65  }
66  else if (firstToken.isLabel())
67  {
68  label s = firstToken.labelToken();
69 
70  // Set list length to that read
71  L.setSize(s);
72 
73  // Read list contents depending on data format
74 
75  if (is.format() == IOstream::ASCII || !contiguous<T>())
76  {
77  // Read beginning of contents
78  char delimiter = is.readBeginList("List");
79 
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];
87 
88  is.fatalCheck
89  (
90  "operator>>(Istream&, List<T>&) : reading entry"
91  );
92  }
93  }
94  else
95  {
96  T element;
97  is >> element;
98 
99  is.fatalCheck
100  (
101  "operator>>(Istream&, List<T>&) : "
102  "reading the single entry"
103  );
104 
105  for (register label i=0; i<s; i++)
106  {
107  L[i] = element;
108  }
109  }
110  }
111 
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));
120 
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  }
137 
138  // Putback the openning bracket
139  is.putBack(firstToken);
140 
141  // Now read as a singly-linked list
142  SLList<T> sll(is);
143 
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  }
154 
155  return is;
156 }
157 
158 
159 template<class T>
161 {
162  List<T> L;
163  token firstToken(is);
164  is.putBack(firstToken);
165 
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  }
175 
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);
183 
184  is >> L[0];
185  }
186 
187  return L;
188 }
189 
190 
191 // ************************ vim: set sw=4 sts=4 et: ************************ //