CSV.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Function1Types::CSV
29 
30 Description
31  Templated CSV function.
32 
33  Reference column is always a scalar, e.g. time.
34 
35  Usage:
36  \verbatim
37  <entryName> csvFile;
38  <entryName>Coeffs
39  {
40  nHeaderLine 4; // number of header lines
41  refColumn 0; // reference column index
42  componentColumns (1 2 3); // component column indices
43  separator ","; // optional (defaults to ",")
44  mergeSeparators no; // merge multiple separators
45  file "fileXYZ"; // name of csv data file
46  outOfBounds clamp; // optional out-of-bounds handling
47  interpolationScheme linear; // optional interpolation scheme
48  }
49  \endverbatim
50 
51 SourceFiles
52  CSV.C
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef CSV_H
57 #define CSV_H
58 
59 #include "Function1.H"
60 #include "TableBase.H"
61 #include "Tuple2.H"
62 #include "labelList.H"
63 #include "ISstream.H"
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 namespace Foam
68 {
69 namespace Function1Types
70 {
71 
72 /*---------------------------------------------------------------------------*\
73  Class CSV Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 template<class Type>
77 class CSV
78 :
79  public TableBase<Type>
80 {
81  // Private data
82 
83  //- Number header lines
84  label nHeaderLine_;
85 
86  //- Column of the time
87  label refColumn_;
88 
89  //- Labels of the components
90  labelList componentColumns_;
91 
92  //- Separator character
93  char separator_;
94 
95  //- Merge separators flag, e.g. ',,,' becomes ','
96  bool mergeSeparators_;
97 
98  //- File name for csv table
99  fileName fName_;
100 
101 
102  // Private Member Functions
103 
104  //- Read csv data table
105  void read();
106 
107  //- Read the next value from the splitted string
108  Type readValue(const List<string>& splitted) const;
109 
110  //- No copy assignment
111  void operator=(const CSV<Type>&) = delete;
112 
113 
114 public:
115 
116  //- Runtime type information
117  TypeName("csvFile");
118 
119 
120  // Constructors
121 
122  //- Construct from entry name and dictionary
123  CSV
124  (
125  const word& entryName,
126  const dictionary& dict,
127  const fileName& fName = fileName::null
128  );
129 
130  //- Copy constructor
131  explicit CSV(const CSV<Type>& csv);
132 
133  //- Construct and return a clone
134  virtual tmp<Function1<Type>> clone() const
135  {
136  return tmp<Function1<Type>>(new CSV<Type>(*this));
137  }
138 
139 
140  //- Destructor
141  virtual ~CSV() = default;
142 
143 
144  // Member Functions
145 
146  //- Return const access to the file name
147  virtual const fileName& fName() const;
148 
149  //- Write in dictionary format
150  virtual void writeData(Ostream& os) const;
151 };
152 
153 
154 // Template specialisations
155 template<>
156 label CSV<label>::readValue(const List<string>& splitted) const;
157 
158 template<>
159 Foam::scalar CSV<scalar>::readValue(const List<string>& splitted) const;
160 
161 
162 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
163 
164 } // End namespace Function1Types
165 } // End namespace Foam
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 #ifdef NoRepository
170  #include "CSV.C"
171 #endif
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 #endif
176 
177 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Tuple2.H
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Function1::entryName
const word & entryName
Definition: Function1.H:96
Function1.H
ISstream.H
Foam::Function1Types::CSV
Templated CSV function.
Definition: CSV.H:76
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:98
Foam::Function1Types::CSV::CSV
CSV(const word &entryName, const dictionary &dict, const fileName &fName=fileName::null)
Construct from entry name and dictionary.
Definition: CSV.C:209
Foam::Function1Types::CSV::~CSV
virtual ~CSV()=default
Destructor.
TableBase.H
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:62
labelList.H
Foam::Function1Types::CSV::TypeName
TypeName("csvFile")
Runtime type information.
Foam::Function1Types::CSV::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CSV.C:267
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
CSV.C
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:97
Foam::Function1Types::CSV::clone
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: CSV.H:133
Foam::List< label >
Foam::Function1Types::TableBase
Base class for table with bounds handling, interpolation and integration.
Definition: TableBase.H:58
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1Types::CSV::fName
virtual const fileName & fName() const
Return const access to the file name.
Definition: CSV.C:260