48 is.format(IOstreamOption::ASCII);
52 if (cols.size() != pTraits<Type>::nComponents)
55 <<
name <<
" with " << cols
56 <<
" does not have the expected length "
57 << pTraits<Type>::nComponents <<
nl
58 <<
exit(FatalIOError);
68Type Foam::Function1Types::CSV<Type>::readValue
70 const UList<string>& strings
75 if constexpr (std::is_integral_v<Type>)
82 for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; ++cmpt)
85 readScalar(strings[componentColumns_[cmpt]]);
94void Foam::Function1Types::CSV<Type>::read()
96 fileName expandedFile(fName_);
97 autoPtr<ISstream> isPtr(
fileHandler().NewIFstream(expandedFile.expand()));
98 ISstream& is = isPtr();
103 <<
"Cannot open CSV file for reading."
104 <<
exit(FatalIOError);
107 const label maxEntry =
108 max(refColumn_, componentColumns_[
findMax(componentColumns_)]);
114 for (label i = 0; i < nHeaderLine_; ++i)
120 DynamicList<Tuple2<scalar, Type>>
values;
121 DynamicList<string> strings(maxEntry+1);
135 (
pos != std::string::npos) && (
n <= maxEntry);
139 if (mergeSeparators_)
144 const auto nPos = line.find(separator_, pos);
146 if ((nPos != std::string::npos) && (nPos - pos == 0))
157 const auto nPos = line.find(separator_, pos);
159 if (nPos == std::string::npos)
161 strings.push_back(line.substr(pos));
166 strings.push_back(line.substr(pos, nPos - pos));
171 if (strings.size() <= 1)
176 if (strings.size() <= maxEntry)
179 <<
"Not enough columns near line " << lineNo
180 <<
". Require " << (maxEntry+1) <<
" but found "
185 scalar
x = readScalar(strings[refColumn_]);
186 Type value = readValue(strings);
200 const word& entryName,
207 nHeaderLine_(
dict.get<label>(
"nHeaderLine")),
208 refColumn_(
dict.get<label>(
"refColumn")),
209 componentColumns_(getComponentColumns(
"componentColumns",
dict)),
210 separator_(
dict.getOrDefault<
string>(
"separator",
",")[0]),
211 mergeSeparators_(
dict.get<bool>(
"mergeSeparators")),
212 fName_(fName.empty() ?
dict.get<
fileName>(
"file") : fName)
224 nHeaderLine_(csv.nHeaderLine_),
225 refColumn_(csv.refColumn_),
226 componentColumns_(csv.componentColumns_),
227 separator_(csv.separator_),
228 mergeSeparators_(csv.mergeSeparators_),
249 os.writeEntry(
"nHeaderLine", nHeaderLine_);
250 os.writeEntry(
"refColumn", refColumn_);
254 os.writeEntry(
"componentColumns", componentColumns_);
257 os.writeEntry(
"separator",
string(separator_));
258 os.writeEntry(
"mergeSeparators", mergeSeparators_);
259 os.writeEntry(
"file", fName_);
Various functions to operate on Lists.
CSV(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr, const fileName &fName=fileName::null)
Construct from entry name, dictionary and optional registry.
virtual void writeData(Ostream &os) const
Write in dictionary format.
virtual const fileName & fName() const
Return const access to the file name.
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Base class for table with bounds handling, interpolation and integration.
void initialise()
Check the table for size and consistency.
TableBase(const word &name, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from dictionary - note table is not populated.
virtual void writeEntries(Ostream &os) const
Write keywords only in dictionary format.
List< Tuple2< scalar, Type > > table_
Table data.
virtual void writeData(Ostream &os) const
Write in dictionary format.
@ ASCII
"ascii" (normal default)
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
A class for handling file names.
Registry of regIOobjects.
A class for handling character strings derived from std::string.
A class for handling words, derived from Foam::string.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
OBJstream os(runTime.globalPath()/outputName)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
dimensionedScalar pos(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
List< label > labelList
A List of labels.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components).
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
errorManipArg< error, int > exit(error &err, const int errNo=1)
constexpr char nl
The newline '\n' character (0x0a).