FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2011 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 "csvTableReader.H"
27 #include <OpenFOAM/IFstream.H>
28 #include <OpenFOAM/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 {
41  if (componentColumns_.size() != pTraits<Type>::nComponents)
42  {
43  FatalErrorIn("csvTableReader<Type>::csvTableReader(const dictionary&)")
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<>
64  scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
65  {
66  if (componentColumns_[0] >= splitted.size())
67  {
69  (
70  "csvTableReader<scalar>::readValue(const List<string>&)"
71  ) << "No column " << componentColumns_[0] << " in "
72  << splitted << endl
73  << exit(FatalError);
74  }
75 
76  return readScalar(IStringStream(splitted[componentColumns_[0]])());
77  }
78 
79 
80  template<class Type>
81  Type csvTableReader<Type>::readValue(const List<string>& splitted)
82  {
83  Type result;
84 
85  for(label i = 0;i < pTraits<Type>::nComponents; i++)
86  {
87  if (componentColumns_[i] >= splitted.size())
88  {
90  (
91  "csvTableReader<Type>::readValue(const List<string>&)"
92  ) << "No column " << componentColumns_[i] << " in "
93  << splitted << endl
94  << exit(FatalError);
95  }
96 
97  result[i] = readScalar
98  (
99  IStringStream(splitted[componentColumns_[i]])()
100  );
101  }
102 
103  return result;
104  }
105 }
106 
107 
108 template<class Type>
110 (
111  const fileName& fName,
113 )
114 {
115  IFstream in(fName);
116 
118 
119  // Skip header
120  if (headerLine_)
121  {
122  string line;
123  in.getLine(line);
124  }
125 
126  while (in.good())
127  {
128  string line;
129  in.getLine(line);
130 
131  DynamicList<string> splitted;
132 
133  std::size_t pos = 0;
134  while (pos != std::string::npos)
135  {
136  std::size_t nPos = line.find(separator_, pos);
137 
138  if (nPos == std::string::npos)
139  {
140  splitted.append(line.substr(pos));
141  pos=nPos;
142  }
143  else
144  {
145  splitted.append(line.substr(pos, nPos-pos));
146  pos=nPos+1;
147  }
148  }
149 
150  if (splitted.size() <= 1)
151  {
152  break;
153  }
154 
155  scalar time = readScalar(IStringStream(splitted[timeColumn_])());
156  Type value = readValue(splitted);
157 
158  values.append(Tuple2<scalar,Type>(time, value));
159  }
160 
161  data.transfer(values);
162 }
163 
164 
165 template<class Type>
167 {
169 
170  os.writeKeyword("hasHeaderLine")
171  << headerLine_ << token::END_STATEMENT << nl;
172  os.writeKeyword("timeColumn")
173  << timeColumn_ << token::END_STATEMENT << nl;
174  os.writeKeyword("valueColumns")
175  << componentColumns_ << token::END_STATEMENT << nl;
176  os.writeKeyword("separator")
177  << string(separator_) << token::END_STATEMENT << nl;
178 }
179 
180 
181 // ************************ vim: set sw=4 sts=4 et: ************************ //