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

csvTableReader.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-2011 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 "csvTableReader.H"
00027 #include <OpenFOAM/IFstream.H>
00028 #include <OpenFOAM/DynamicList.H>
00029 
00030 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 Foam::csvTableReader<Type>::csvTableReader(const dictionary& dict)
00034 :
00035     tableReader<Type>(dict),
00036     headerLine_(readBool(dict.lookup("hasHeaderLine"))),
00037     timeColumn_(readLabel(dict.lookup("timeColumn"))),
00038     componentColumns_(dict.lookup("valueColumns")),
00039     separator_(dict.lookupOrDefault<string>("separator", string(","))[0])
00040 {
00041     if (componentColumns_.size() != pTraits<Type>::nComponents)
00042     {
00043         FatalErrorIn("csvTableReader<Type>::csvTableReader(const dictionary&)")
00044             << componentColumns_ << " does not have the expected length "
00045             << pTraits<Type>::nComponents << endl
00046             << exit(FatalError);
00047     }
00048 }
00049 
00050 
00051 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00052 
00053 template<class Type>
00054 Foam::csvTableReader<Type>::~csvTableReader()
00055 {}
00056 
00057 
00058 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00059 
00060 namespace Foam
00061 {
00062     // doesn't recognize specialization otherwise
00063     template<>
00064     scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
00065     {
00066         if (componentColumns_[0] >= splitted.size())
00067         {
00068             FatalErrorIn
00069             (
00070                 "csvTableReader<scalar>::readValue(const List<string>&)"
00071             )   << "No column " << componentColumns_[0] << " in "
00072                 << splitted << endl
00073                 << exit(FatalError);
00074         }
00075 
00076         return readScalar(IStringStream(splitted[componentColumns_[0]])());
00077     }
00078 
00079 
00080     template<class Type>
00081     Type csvTableReader<Type>::readValue(const List<string>& splitted)
00082     {
00083         Type result;
00084 
00085         for(label i = 0;i < pTraits<Type>::nComponents; i++)
00086         {
00087             if (componentColumns_[i] >= splitted.size())
00088             {
00089                 FatalErrorIn
00090                 (
00091                     "csvTableReader<Type>::readValue(const List<string>&)"
00092                 )   << "No column " << componentColumns_[i] << " in "
00093                     << splitted << endl
00094                     << exit(FatalError);
00095             }
00096 
00097             result[i] = readScalar
00098             (
00099                 IStringStream(splitted[componentColumns_[i]])()
00100             );
00101         }
00102 
00103         return result;
00104     }
00105 }
00106 
00107 
00108 template<class Type>
00109 void Foam::csvTableReader<Type>::operator()
00110 (
00111     const fileName& fName,
00112     List<Tuple2<scalar, Type> >& data
00113 )
00114 {
00115     IFstream in(fName);
00116 
00117     DynamicList<Tuple2<scalar, Type> > values;
00118 
00119     // Skip header
00120     if (headerLine_)
00121     {
00122         string line;
00123         in.getLine(line);
00124     }
00125 
00126     while (in.good())
00127     {
00128         string line;
00129         in.getLine(line);
00130 
00131         DynamicList<string> splitted;
00132 
00133         std::size_t pos = 0;
00134         while (pos != std::string::npos)
00135         {
00136             std::size_t nPos = line.find(separator_, pos);
00137 
00138             if (nPos == std::string::npos)
00139             {
00140                 splitted.append(line.substr(pos));
00141                 pos=nPos;
00142             }
00143             else
00144             {
00145                 splitted.append(line.substr(pos, nPos-pos));
00146                 pos=nPos+1;
00147             }
00148         }
00149 
00150         if (splitted.size() <= 1)
00151         {
00152             break;
00153         }
00154 
00155         scalar time = readScalar(IStringStream(splitted[timeColumn_])());
00156         Type value = readValue(splitted);
00157 
00158         values.append(Tuple2<scalar,Type>(time, value));
00159     }
00160 
00161     data.transfer(values);
00162 }
00163 
00164 
00165 template<class Type>
00166 void Foam::csvTableReader<Type>::write(Ostream& os) const
00167 {
00168     tableReader<Type>::write(os);
00169 
00170     os.writeKeyword("hasHeaderLine")
00171         << headerLine_ << token::END_STATEMENT << nl;
00172     os.writeKeyword("timeColumn")
00173         << timeColumn_ << token::END_STATEMENT << nl;
00174     os.writeKeyword("valueColumns")
00175         << componentColumns_ << token::END_STATEMENT << nl;
00176     os.writeKeyword("separator")
00177         << string(separator_) << token::END_STATEMENT << nl;
00178 }
00179 
00180 
00181 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines