CSV.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 "CSV.H"
27 #include "DynamicList.H"
28 #include "IFstream.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  template<>
36  {
37  if (componentColumns_[0] >= splitted.size())
38  {
40  << "No column " << componentColumns_[0] << " in "
41  << splitted << endl
42  << exit(FatalError);
43  }
44 
45  return readLabel(IStringStream(splitted[componentColumns_[0]])());
46  }
47 
48  template<>
49  scalar CSV<scalar>::readValue(const List<string>& splitted)
50  {
51  if (componentColumns_[0] >= splitted.size())
52  {
54  << "No column " << componentColumns_[0] << " in "
55  << splitted << endl
56  << exit(FatalError);
57  }
58 
59  return readScalar(IStringStream(splitted[componentColumns_[0]])());
60  }
61 
62 
63  template<class Type>
64  Type CSV<Type>::readValue(const List<string>& splitted)
65  {
66  Type result;
67 
68  for (label i = 0; i < pTraits<Type>::nComponents; i++)
69  {
70  if (componentColumns_[i] >= splitted.size())
71  {
73  << "No column " << componentColumns_[i] << " in "
74  << splitted << endl
75  << exit(FatalError);
76  }
77 
78  result[i] =
79  readScalar(IStringStream(splitted[componentColumns_[i]])());
80  }
81 
82  return result;
83  }
84 }
85 
86 
87 template<class Type>
89 {
90  fileName expandedFile(fName_);
91  IFstream is(expandedFile.expand());
92 
93  if (!is.good())
94  {
96  << "Cannot open CSV file for reading."
97  << exit(FatalIOError);
98  }
99 
101 
102  // skip header
103  for (label i = 0; i < nHeaderLine_; i++)
104  {
105  string line;
106  is.getLine(line);
107  }
108 
109  label nEntries = max(componentColumns_);
110 
111  // read data
112  while (is.good())
113  {
114  string line;
115  is.getLine(line);
116 
117 
118  label n = 0;
119  std::size_t pos = 0;
120  DynamicList<string> splitted;
121 
122  if (mergeSeparators_)
123  {
124  std::size_t nPos = 0;
125 
126  while ((pos != std::string::npos) && (n <= nEntries))
127  {
128  bool found = false;
129  while (!found)
130  {
131  nPos = line.find(separator_, pos);
132 
133  if ((nPos != std::string::npos) && (nPos - pos == 0))
134  {
135  pos = nPos + 1;
136  }
137  else
138  {
139  found = true;
140  }
141  }
142 
143  nPos = line.find(separator_, pos);
144 
145  if (nPos == std::string::npos)
146  {
147  splitted.append(line.substr(pos));
148  pos = nPos;
149  n++;
150  }
151  else
152  {
153  splitted.append(line.substr(pos, nPos - pos));
154  pos = nPos + 1;
155  n++;
156  }
157  }
158  }
159  else
160  {
161  while ((pos != std::string::npos) && (n <= nEntries))
162  {
163  std::size_t nPos = line.find(separator_, pos);
164 
165  if (nPos == std::string::npos)
166  {
167  splitted.append(line.substr(pos));
168  pos = nPos;
169  n++;
170  }
171  else
172  {
173  splitted.append(line.substr(pos, nPos - pos));
174  pos = nPos + 1;
175  n++;
176  }
177  }
178  }
179 
180 
181  if (splitted.size() <= 1)
182  {
183  break;
184  }
185 
186  scalar x = readScalar(IStringStream(splitted[refColumn_])());
187  Type value = readValue(splitted);
188 
189  values.append(Tuple2<scalar,Type>(x, value));
190  }
191 
192  this->table_.transfer(values);
193 }
194 
195 
196 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
197 
198 template<class Type>
200 (
201  const word& entryName,
202  const dictionary& dict,
203  const word& ext
204 )
205 :
206  TableBase<Type>(entryName, dict.subDict(entryName + ext)),
207  coeffs_(dict.subDict(entryName + ext)),
208  nHeaderLine_(readLabel(coeffs_.lookup("nHeaderLine"))),
209  refColumn_(readLabel(coeffs_.lookup("refColumn"))),
210  componentColumns_(coeffs_.lookup("componentColumns")),
211  separator_(coeffs_.lookupOrDefault<string>("separator", string(","))[0]),
212  mergeSeparators_(readBool(coeffs_.lookup("mergeSeparators"))),
213  fName_(coeffs_.lookup("fileName"))
214 {
216  {
218  << componentColumns_ << " does not have the expected length of "
220  << exit(FatalError);
221  }
222 
223  read();
224 
226 }
227 
228 
229 template<class Type>
231 :
232  TableBase<Type>(tbl),
233  nHeaderLine_(tbl.nHeaderLine_),
234  refColumn_(tbl.refColumn_),
235  componentColumns_(tbl.componentColumns_),
236  separator_(tbl.separator_),
237  mergeSeparators_(tbl.mergeSeparators_),
238  fName_(tbl.fName_)
239 {}
240 
241 
242 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
243 
244 template<class Type>
246 {}
247 
248 
249 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
250 
251 template<class Type>
253 {
254  return fName_;
255 }
256 
257 
258 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
259 
260 #include "CSVIO.C"
261 
262 
263 // ************************************************************************* //
Foam::CSV::~CSV
virtual ~CSV()
Destructor.
Definition: CSV.C:245
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
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::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::CSV::CSV
CSV(const word &entryName, const dictionary &dict, const word &ext="Coeffs")
Construct from entry name and dictionary.
Foam::CSV
Templated CSV container data entry. Reference column is always a scalar, e.g. time.
Definition: CSV.H:65
n
label n
Definition: TABSMDCalcMethod2.H:31
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::ISstream::getLine
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
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::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
readScalar
#define readScalar
Definition: doubleScalar.C:38
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::CSV::fName
virtual const fileName & fName() const
Return const access to the file name.
Definition: CSV.C:252
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::CSV::read
void read()
Read csv data table.
Definition: CSV.C:88
CSV.H
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::CSV::componentColumns_
labelList componentColumns_
Labels of the components.
Definition: CSV.H:95
Foam::List< string >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::string::expand
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurences of environment variables.
Definition: string.C:98
Foam::TableBase::check
void check() const
Check the table for size and consistency.
Definition: TableBase.C:186
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
Foam::line
A line primitive.
Definition: line.H:56
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
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.
CSVIO.C
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::CSV::readValue
Type readValue(const List< string > &)
Read the next value from the splitted string.
Foam::TableBase
Base class for table with bounds handling, interpolation and integration.
Definition: TableBase.H:47
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:190