csvTableReader.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) 2011-2015 OpenFOAM Foundation
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 "csvTableReader.H"
27 #include "IFstream.H"
28 #include "DynamicList.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 :
35  tableReader<Type>(dict),
36  headerLine_(readBool(dict.lookup("hasHeaderLine"))),
37  timeColumn_(readLabel(dict.lookup("timeColumn"))),
38  componentColumns_(dict.lookup("valueColumns")),
39  separator_(dict.lookupOrDefault<string>("separator", string(","))[0])
40 {
42  {
44  << componentColumns_ << " does not have the expected length "
46  << exit(FatalError);
47  }
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
52 
53 template<class Type>
55 {}
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62  // doesn't recognize specialization otherwise
63  template<>
65  {
66  if (componentColumns_[0] >= splitted.size())
67  {
69  << "No column " << componentColumns_[0] << " in "
70  << splitted << endl
71  << exit(FatalError);
72  }
73 
74  return readScalar(IStringStream(splitted[componentColumns_[0]])());
75  }
76 
77 
78  template<class Type>
80  {
81  Type result;
82 
83  for(label i = 0;i < pTraits<Type>::nComponents; i++)
84  {
85  if (componentColumns_[i] >= splitted.size())
86  {
88  << "No column " << componentColumns_[i] << " in "
89  << splitted << endl
90  << exit(FatalError);
91  }
92 
93  result[i] = readScalar
94  (
95  IStringStream(splitted[componentColumns_[i]])()
96  );
97  }
98 
99  return result;
100  }
101 }
102 
103 
104 template<class Type>
106 (
107  const fileName& fName,
109 )
110 {
111  IFstream in(fName);
112 
114 
115  // Skip header
116  if (headerLine_)
117  {
118  string line;
119  in.getLine(line);
120  }
121 
122  while (in.good())
123  {
124  string line;
125  in.getLine(line);
126 
127  DynamicList<string> splitted;
128 
129  std::size_t pos = 0;
130  while (pos != std::string::npos)
131  {
132  std::size_t nPos = line.find(separator_, pos);
133 
134  if (nPos == std::string::npos)
135  {
136  splitted.append(line.substr(pos));
137  pos=nPos;
138  }
139  else
140  {
141  splitted.append(line.substr(pos, nPos-pos));
142  pos=nPos+1;
143  }
144  }
145 
146  if (splitted.size() <= 1)
147  {
148  break;
149  }
150 
151  scalar time = readScalar(IStringStream(splitted[timeColumn_])());
152  Type value = readValue(splitted);
153 
154  values.append(Tuple2<scalar,Type>(time, value));
155  }
156 
157  data.transfer(values);
158 }
159 
160 
161 template<class Type>
163 (
164  const fileName& fName,
166 )
167 {
169 }
170 
171 
172 template<class Type>
174 {
176 
177  os.writeKeyword("hasHeaderLine")
178  << headerLine_ << token::END_STATEMENT << nl;
179  os.writeKeyword("timeColumn")
180  << timeColumn_ << token::END_STATEMENT << nl;
181 
182  // Force writing labelList in ascii
183  os.writeKeyword("valueColumns");
184  if (os.format() == IOstream::BINARY)
185  {
187  os << componentColumns_;
189  }
190  os << token::END_STATEMENT << nl;
191 
192  os.writeKeyword("separator")
193  << string(separator_) << token::END_STATEMENT << nl;
194 }
195 
196 
197 // ************************************************************************* //
csvTableReader.H
Foam::IOstream::format
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
Foam::csvTableReader::readValue
Type readValue(const List< string > &)
Read the next value from the splitted string.
Definition: csvTableReader.C:79
Foam::token::END_STATEMENT
@ END_STATEMENT
Definition: token.H:99
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::tableReader
Base class to read table data for the interpolationTable.
Definition: tableReader.H:57
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::csvTableReader::csvTableReader
csvTableReader(const dictionary &dict)
Construct from dictionary.
Definition: csvTableReader.C:33
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::csvTableReader::write
virtual void write(Ostream &os) const
Write the remaining parameters.
Definition: csvTableReader.C:173
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:365
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::csvTableReader::~csvTableReader
virtual ~csvTableReader()
Destructor.
Definition: csvTableReader.C:54
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::ISstream::getLine
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
Foam::csvTableReader::componentColumns_
const labelList componentColumns_
Labels of the components.
Definition: csvTableReader.H:63
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::csvTableReader
Reads an interpolation table from a file - CSV-format.
Definition: csvTableReader.H:50
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::IOstream::BINARY
@ BINARY
Definition: IOstream.H:89
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::readScalar
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
Foam::List< string >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::Ostream::writeKeyword
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
Foam::dictionary::transfer
void transfer(dictionary &)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:1059
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
Foam::line
A line primitive.
Definition: line.H:56
Foam::tableReader::write
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:77
DynamicList.H
Foam::readBool
bool readBool(Istream &)
Definition: boolIO.C:60
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::Tuple2
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:47
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:190