FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "List.H"
00027 #include <OpenFOAM/Istream.H>
00028 #include <OpenFOAM/token.H>
00029 #include <OpenFOAM/SLList.H>
00030 #include <OpenFOAM/contiguous.H>
00031 
00032 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00033 
00034 // Construct from Istream
00035 template<class T>
00036 Foam::List<T>::List(Istream& is)
00037 :
00038     UList<T>(NULL, 0)
00039 {
00040     operator>>(is, *this);
00041 }
00042 
00043 
00044 template<class T>
00045 Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
00046 {
00047     // Anull list
00048     L.setSize(0);
00049 
00050     is.fatalCheck("operator>>(Istream&, List<T>&)");
00051 
00052     token firstToken(is);
00053 
00054     is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
00055 
00056     if (firstToken.isCompound())
00057     {
00058         L.transfer
00059         (
00060             dynamicCast<token::Compound<List<T> > >
00061             (
00062                 firstToken.transferCompoundToken()
00063             )
00064         );
00065     }
00066     else if (firstToken.isLabel())
00067     {
00068         label s = firstToken.labelToken();
00069 
00070         // Set list length to that read
00071         L.setSize(s);
00072 
00073         // Read list contents depending on data format
00074 
00075         if (is.format() == IOstream::ASCII || !contiguous<T>())
00076         {
00077             // Read beginning of contents
00078             char delimiter = is.readBeginList("List");
00079 
00080             if (s)
00081             {
00082                 if (delimiter == token::BEGIN_LIST)
00083                 {
00084                     for (register label i=0; i<s; i++)
00085                     {
00086                         is >> L[i];
00087 
00088                         is.fatalCheck
00089                         (
00090                             "operator>>(Istream&, List<T>&) : reading entry"
00091                         );
00092                     }
00093                 }
00094                 else
00095                 {
00096                     T element;
00097                     is >> element;
00098 
00099                     is.fatalCheck
00100                     (
00101                         "operator>>(Istream&, List<T>&) : "
00102                         "reading the single entry"
00103                     );
00104 
00105                     for (register label i=0; i<s; i++)
00106                     {
00107                         L[i] = element;
00108                     }
00109                 }
00110             }
00111 
00112             // Read end of contents
00113             is.readEndList("List");
00114         }
00115         else
00116         {
00117             if (s)
00118             {
00119                 is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
00120 
00121                 is.fatalCheck
00122                 (
00123                     "operator>>(Istream&, List<T>&) : reading the binary block"
00124                 );
00125             }
00126         }
00127     }
00128     else if (firstToken.isPunctuation())
00129     {
00130         if (firstToken.pToken() != token::BEGIN_LIST)
00131         {
00132             FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
00133                 << "incorrect first token, expected '(', found "
00134                 << firstToken.info()
00135                 << exit(FatalIOError);
00136         }
00137 
00138         // Putback the openning bracket
00139         is.putBack(firstToken);
00140 
00141         // Now read as a singly-linked list
00142         SLList<T> sll(is);
00143 
00144         // Convert the singly-linked list to this list
00145         L = sll;
00146     }
00147     else
00148     {
00149         FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
00150             << "incorrect first token, expected <int> or '(', found "
00151             << firstToken.info()
00152             << exit(FatalIOError);
00153     }
00154 
00155     return is;
00156 }
00157 
00158 
00159 template<class T>
00160 Foam::List<T> Foam::readList(Istream& is)
00161 {
00162     List<T> L;
00163     token firstToken(is);
00164     is.putBack(firstToken);
00165 
00166     if (firstToken.isPunctuation())
00167     {
00168         if (firstToken.pToken() != token::BEGIN_LIST)
00169         {
00170             FatalIOErrorIn("readList<T>(Istream&)", is)
00171                 << "incorrect first token, expected '(', found "
00172                 << firstToken.info()
00173                 << exit(FatalIOError);
00174         }
00175 
00176         // read via a singly-linked list
00177         L = SLList<T>(is);
00178     }
00179     else
00180     {
00181         // create list with a single item
00182         L.setSize(1);
00183 
00184         is >> L[0];
00185     }
00186 
00187     return L;
00188 }
00189 
00190 
00191 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines