From 3753faf14731f13e094f576dd601c565ea2131d0 Mon Sep 17 00:00:00 2001 From: andy <andy> Date: Mon, 28 Jan 2013 16:46:59 +0000 Subject: [PATCH] ENH: Updated CSV DataEntry --- .../primitives/functions/DataEntry/CSV/CSV.C | 95 +++++++++++++++---- .../primitives/functions/DataEntry/CSV/CSV.H | 29 ++++-- .../functions/DataEntry/CSV/CSVIO.C | 9 +- 3 files changed, 107 insertions(+), 26 deletions(-) diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C index 4719be11452..f896fa2fdb5 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -87,37 +87,84 @@ void Foam::CSV<Type>::read() DynamicList<Tuple2<scalar, Type> > values; // skip header - if (headerLine_) + for (label i = 0; i < nHeaderLine_; i++) { string line; is.getLine(line); } + label nEntries = max(componentColumns_); + // read data while (is.good()) { string line; is.getLine(line); - DynamicList<string> splitted; + label n = 0; std::size_t pos = 0; - while (pos != std::string::npos) + DynamicList<string> splitted; + + if (mergeSeparators_) { - std::size_t nPos = line.find(separator_, pos); + std::size_t nPos = 0; - if (nPos == std::string::npos) + while ((pos != std::string::npos) && (n <= nEntries)) { - splitted.append(line.substr(pos)); - pos = nPos; + bool found = false; + while (!found) + { + nPos = line.find(separator_, pos); + + if ((nPos != std::string::npos) && (nPos - pos == 0)) + { + pos = nPos + 1; + } + else + { + found = true; + } + } + + nPos = line.find(separator_, pos); + + if (nPos == std::string::npos) + { + splitted.append(line.substr(pos)); + pos = nPos; + n++; + } + else + { + splitted.append(line.substr(pos, nPos - pos)); + pos = nPos + 1; + n++; + } } - else + } + else + { + while ((pos != std::string::npos) && (n <= nEntries)) { - splitted.append(line.substr(pos, nPos - pos)); - pos = nPos + 1; + std::size_t nPos = line.find(separator_, pos); + + if (nPos == std::string::npos) + { + splitted.append(line.substr(pos)); + pos = nPos; + n++; + } + else + { + splitted.append(line.substr(pos, nPos - pos)); + pos = nPos + 1; + n++; + } } } + if (splitted.size() <= 1) { break; @@ -136,15 +183,21 @@ void Foam::CSV<Type>::read() // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class Type> -Foam::CSV<Type>::CSV(const word& entryName, const dictionary& dict) +Foam::CSV<Type>::CSV +( + const word& entryName, + const dictionary& dict, + const word& ext +) : DataEntry<Type>(entryName), - TableBase<Type>(entryName, dict.subDict(type() + "Coeffs")), - coeffs_(dict.subDict(type() + "Coeffs")), - headerLine_(readBool(coeffs_.lookup("hasHeaderLine"))), + TableBase<Type>(entryName, dict.subDict(type() + ext)), + coeffs_(dict.subDict(type() + ext)), + nHeaderLine_(readLabel(coeffs_.lookup("nHeaderLine"))), refColumn_(readLabel(coeffs_.lookup("refColumn"))), componentColumns_(coeffs_.lookup("componentColumns")), separator_(coeffs_.lookupOrDefault<string>("separator", string(","))[0]), + mergeSeparators_(readBool(coeffs_.lookup("mergeSeparators"))), fName_(coeffs_.lookup("fileName")) { if (componentColumns_.size() != pTraits<Type>::nComponents) @@ -166,10 +219,11 @@ Foam::CSV<Type>::CSV(const CSV<Type>& tbl) : DataEntry<Type>(tbl), TableBase<Type>(tbl), - headerLine_(tbl.headerLine_), + nHeaderLine_(tbl.nHeaderLine_), refColumn_(tbl.refColumn_), componentColumns_(tbl.componentColumns_), separator_(tbl.separator_), + mergeSeparators_(tbl.mergeSeparators_), fName_(tbl.fName_) {} @@ -181,6 +235,15 @@ Foam::CSV<Type>::~CSV() {} +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class Type> +const Foam::fileName& Foam::CSV<Type>::fName() const +{ + return fName_; +} + + // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * // #include "CSVIO.C" diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H index bcc3aee956b..e5d0d23055b 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,10 +32,11 @@ Description <entryName> csvFile; csvFileCoeffs { - hasHeaderLine true; + nHeaderLines 4; refColumn 0; // reference column index componentColumns (1 2 3); // component column indices separator ","; // optional (defaults to ",") + mergeSeparators no; // merge multiple separators fileName "fileXYZ"; // name of csv data file outOfBounds clamp; // optional out-of-bounds handling interpolationScheme linear; // optional interpolation scheme @@ -86,8 +87,8 @@ class CSV //- Coefficients dictionary (for convenience on reading) dictionary coeffs_; - //- Does the file have a header line? - bool headerLine_; + //- Number header lines + label nHeaderLine_; //- Column of the time label refColumn_; @@ -98,7 +99,10 @@ class CSV //- Separator character char separator_; - //- File name for csv table (optional) + //- Merge separators flag, e.g. ',,,' becomes ',' + bool mergeSeparators_; + + //- File name for csv table fileName fName_; @@ -122,8 +126,13 @@ public: // Constructors - //- Construct from entry name and Istream - CSV(const word& entryName, const dictionary& dict); + //- Construct from entry name and dictionary + CSV + ( + const word& entryName, + const dictionary& dict, + const word& ext = "Ceoffs" + ); //- Copy constructor CSV(const CSV<Type>& tbl); @@ -150,6 +159,12 @@ public: } + // Access + + //- Return const access to the file name + virtual const fileName& fName() const; + + // Evaluation //- Return Table value diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C index b5bfad8b3af..683b2edf1e1 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -37,10 +37,11 @@ Foam::Ostream& Foam::operator<< if (os.format() == IOstream::ASCII) { os << static_cast<const DataEntry<Type>& >(tbl) - << token::SPACE << tbl.headerLine_ + << token::SPACE << tbl.nHeaderLine_ << token::SPACE << tbl.timeColumn_ << token::SPACE << tbl.componentColumns_ << token::SPACE << tbl.separator_ + << token::SPACE << tbl.mergeSeparators_ << token::SPACE << tbl.fileName_; } else @@ -70,13 +71,15 @@ void Foam::CSV<Type>::writeData(Ostream& os) const // the values themselves TableBase<Type>::writeEntries(os); - os.writeKeyword("hasHeaderLine") << headerLine_ << token::END_STATEMENT + os.writeKeyword("nHeaderLine") << nHeaderLine_ << token::END_STATEMENT << nl; os.writeKeyword("refColumn") << refColumn_ << token::END_STATEMENT << nl; os.writeKeyword("componentColumns") << componentColumns_ << token::END_STATEMENT << nl; os.writeKeyword("separator") << string(separator_) << token::END_STATEMENT << nl; + os.writeKeyword("mergeSeparators") << string(mergeSeparators_) + << token::END_STATEMENT << nl; os.writeKeyword("fileName") << fName_ << token::END_STATEMENT << nl; os << decrIndent << indent << token::END_BLOCK << endl; } -- GitLab