OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

ListIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "List.H"
00028 #include "Istream.H"
00029 #include "token.H"
00030 #include "SLList.H"
00031 #include "contiguous.H"
00032 
00033 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00034 
00035 // Construct from Istream
00036 template<class T>
00037 Foam::List<T>::List(Istream& is)
00038 :
00039     UList<T>(NULL, 0)
00040 {
00041     operator>>(is, *this);
00042 }
00043 
00044 
00045 template<class T>
00046 Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
00047 {
00048     // Anull list
00049     L.setSize(0);
00050 
00051     is.fatalCheck("operator>>(Istream&, List<T>&)");
00052 
00053     token firstToken(is);
00054 
00055     is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
00056 
00057     if (firstToken.isCompound())
00058     {
00059         L.transfer
00060         (
00061             dynamicCast<token::Compound<List<T> > >
00062             (
00063                 firstToken.transferCompoundToken()
00064             )
00065         );
00066     }
00067     else if (firstToken.isLabel())
00068     {
00069         label s = firstToken.labelToken();
00070 
00071         // Set list length to that read
00072         L.setSize(s);
00073 
00074         // Read list contents depending on data format
00075 
00076         if (is.format() == IOstream::ASCII || !contiguous<T>())
00077         {
00078             // Read beginning of contents
00079             char delimiter = is.readBeginList("List");
00080 
00081             if (s)
00082             {
00083                 if (delimiter == token::BEGIN_LIST)
00084                 {
00085                     for (register label i=0; i<s; i++)
00086                     {
00087                         is >> L[i];
00088 
00089                         is.fatalCheck
00090                         (
00091                             "operator>>(Istream&, List<T>&) : reading entry"
00092                         );
00093                     }
00094                 }
00095                 else
00096                 {
00097                     T element;
00098                     is >> element;
00099 
00100                     is.fatalCheck
00101                     (
00102                         "operator>>(Istream&, List<T>&) : "
00103                         "reading the single entry"
00104                     );
00105 
00106                     for (register label i=0; i<s; i++)
00107                     {
00108                         L[i] = element;
00109                     }
00110                 }
00111             }
00112 
00113             // Read end of contents
00114             is.readEndList("List");
00115         }
00116         else
00117         {
00118             if (s)
00119             {
00120                 is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
00121 
00122                 is.fatalCheck
00123                 (
00124                     "operator>>(Istream&, List<T>&) : reading the binary block"
00125                 );
00126             }
00127         }
00128     }
00129     else if (firstToken.isPunctuation())
00130     {
00131         if (firstToken.pToken() != token::BEGIN_LIST)
00132         {
00133             FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
00134                 << "incorrect first token, expected '(', found "
00135                 << firstToken.info()
00136                 << exit(FatalIOError);
00137         }
00138 
00139         // Putback the openning bracket
00140         is.putBack(firstToken);
00141 
00142         // Now read as a singly-linked list
00143         SLList<T> sll(is);
00144 
00145         // Convert the singly-linked list to this list
00146         L = sll;
00147     }
00148     else
00149     {
00150         FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
00151             << "incorrect first token, expected <int> or '(', found "
00152             << firstToken.info()
00153             << exit(FatalIOError);
00154     }
00155 
00156     return is;
00157 }
00158 
00159 
00160 template<class T>
00161 Foam::List<T> Foam::readList(Istream& is)
00162 {
00163     List<T> L;
00164     token firstToken(is);
00165     is.putBack(firstToken);
00166 
00167     if (firstToken.isPunctuation())
00168     {
00169         if (firstToken.pToken() != token::BEGIN_LIST)
00170         {
00171             FatalIOErrorIn("readList<T>(Istream&)", is)
00172                 << "incorrect first token, expected '(', found "
00173                 << firstToken.info()
00174                 << exit(FatalIOError);
00175         }
00176 
00177         // read via a singly-linked list
00178         L = SLList<T>(is);
00179     }
00180     else
00181     {
00182         // create list with a single item
00183         L.setSize(1);
00184 
00185         is >> L[0];
00186     }
00187 
00188     return L;
00189 }
00190 
00191 
00192 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd