CsvTableReader.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 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 Class
25  Foam::TableReaders::Csv
26 
27 Description
28  Reads an interpolation table from a file in CSV-format. Entries govern the
29  layout of the CSV file. The index of the first (x) column of the table is
30  given by the refColumn entry, and is always scalar. The indices of the
31  components of the second (y) column are given by the componentColumns
32  entry.
33 
34 Usage
35  \verbatim
36  nHeaderLine 4; // number of header lines
37  refColumn 0; // reference column index
38  componentColumns (1 2 3); // component column indices
39  separator ","; // optional (defaults to ",")
40  mergeSeparators no; // merge multiple separators
41  \endverbatim
42 
43 SourceFiles
44  CsvTableReader.C
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef CsvTableReader_H
49 #define CsvTableReader_H
50 
51 #include "TableFileReader.H"
52 #include "labelList.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 namespace TableReaders
59 {
60 
61 /*---------------------------------------------------------------------------*\
62  Class Csv Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class Type>
66 class Csv
67 :
68  public TableFileReader<Type>
69 {
70  // Private Data
71 
72  //- Number of header lines
73  const label nHeaderLine_;
74 
75  //- Reference column
76  const label refColumn_;
77 
78  //- Labels of the components
79  const labelList componentColumns_;
80 
81  //- Separator character
82  const char separator_;
83 
84  //- Merge separators flag; e.g. ',,,' becomes ','
85  bool mergeSeparators_;
86 
87 
88  // Private Member functions
89 
90  //- Read the next value from the split string
91  Type readValue(const List<string>&) const;
92 
93  //- Read a 1D table
94  virtual void read(ISstream&, List<Tuple2<scalar, Type>>&) const;
95 
96 
97 public:
98 
99  //- Runtime type information
100  TypeName("csv");
101 
102 
103  // Constructors
104 
105  //- Construct from dictionary
106  Csv
107  (
108  const word& name,
109  const dictionary &dict,
110  List<Tuple2<scalar, Type>>& table
111  );
112 
113  //- Construct and return a copy
114  virtual autoPtr<TableReader<Type>> clone() const
115  {
116  return autoPtr<TableReader<Type>>(new Csv<Type>(*this));
117  }
118 
119 
120  //- Destructor
121  virtual ~Csv();
122 
123 
124  // Member Functions
125 
126  //- Write the CSV parameters
127  virtual void write
128  (
129  Ostream& os,
130  const List<Tuple2<scalar, Type>>& table
131  ) const;
132 };
133 
134 
135 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
136 
137 } // End namespace TableReaders
138 } // End namespace Foam
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 #ifdef NoRepository
143  #include "CsvTableReader.C"
144 #endif
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 #endif
149 
150 // ************************************************************************* //
TypeName("csv")
Runtime type information.
dictionary dict
virtual autoPtr< TableReader< Type > > clone() const
Construct and return a copy.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:65
virtual void write(Ostream &os, const List< Tuple2< scalar, Type >> &table) const
Write the CSV parameters.
Base class to read table data for tables.
Reads an interpolation table from a file in CSV-format. Entries govern the layout of the CSV file...
virtual ~Csv()
Destructor.
A class for handling words, derived from string.
Definition: word.H:59
Csv(const word &name, const dictionary &dict, List< Tuple2< scalar, Type >> &table)
Construct from dictionary.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Generic input stream.
Definition: ISstream.H:52
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Namespace for OpenFOAM.