Skip to content
Snippets Groups Projects
Commit 1edb1e41 authored by Christoph Lehmann's avatar Christoph Lehmann
Browse files

Removed <fstream> includes from headers

parent a324b818
No related branches found
No related tags found
No related merge requests found
Showing
with 260 additions and 150 deletions
......@@ -13,6 +13,7 @@
#include "AsciiRasterInterface.h"
#include <fstream>
#include <tuple>
#include "BaseLib/FileTools.h"
......
......@@ -13,7 +13,6 @@
#pragma once
#include <fstream>
#include <optional>
#include <string>
#include <vector>
......
......@@ -14,7 +14,6 @@
#include "CsvInterface.h"
#include <algorithm>
#include <fstream>
#include <numeric>
#include "GeoLib/Point.h"
......
......@@ -13,6 +13,7 @@
#include <QtXml/QDomDocument>
#include <boost/algorithm/string/trim.hpp>
#include <cctype>
#include <fstream>
#include <memory>
#include "BaseLib/FileTools.h"
......
......@@ -10,6 +10,7 @@
#include <boost/algorithm/string/trim.hpp>
#include <cctype>
#include <fstream>
#include <memory>
#include "Applications/FileIO/FEFLOW/FEFLOWGeoInterface.h"
......
......@@ -9,7 +9,8 @@
#pragma once
#include <fstream>
#include <iosfwd>
#include <string>
namespace FileIO
{
......
......@@ -9,7 +9,7 @@
#include "GocadAsciiReader.h"
#include <iosfwd>
#include <fstream>
#include "Applications/FileIO/GocadIO/CoordinateSystem.h"
#include "BaseLib/FileTools.h"
......
......@@ -14,6 +14,7 @@
#include "OGSIOVer4.h"
#include <fstream>
#include <iomanip>
#include <limits>
#include <sstream>
......
......@@ -18,6 +18,7 @@
#include <mpi.h>
#endif
#include <fstream>
#include <memory>
#include "BaseLib/FileTools.h"
......
......@@ -11,6 +11,7 @@
#include <tclap/CmdLine.h>
#include <fstream>
#include <memory>
#include <string>
......
......@@ -17,6 +17,7 @@
#endif
#include <algorithm>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
......
......@@ -16,6 +16,7 @@
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <fstream>
#include <typeindex>
#include <unordered_map>
......@@ -259,4 +260,69 @@ void removeFiles(std::vector<std::string> const& files)
removeFile(file);
}
}
template <typename T>
T readBinaryValue(std::istream& in)
{
T v;
in.read(reinterpret_cast<char*>(&v), sizeof(T));
return v;
}
// explicit template instantiation
template float readBinaryValue<float>(std::istream&);
template double readBinaryValue<double>(std::istream&);
template <typename T>
std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
{
std::ifstream in(filename.c_str());
if (!in)
{
ERR("readBinaryArray(): Error while reading from file '{:s}'.",
filename);
ERR("Could not open file '{:s}' for input.", filename);
in.close();
return std::vector<T>();
}
std::vector<T> result;
result.reserve(n);
for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
{
result.push_back(BaseLib::readBinaryValue<T>(in));
}
if (result.size() == n)
{
return result;
}
ERR("readBinaryArray(): Error while reading from file '{:s}'.", filename);
ERR("Read different number of values. Expected {:d}, got {:d}.",
n,
result.size());
if (!in.eof())
{
ERR("EOF reached.\n");
}
return std::vector<T>();
}
// explicit template instantiation
template std::vector<float> readBinaryArray<float>(std::string const&,
std::size_t const);
template <typename T>
void writeValueBinary(std::ostream& out, T const& val)
{
out.write(reinterpret_cast<const char*>(&val), sizeof(T));
}
// explicit template instantiation
template void writeValueBinary<std::size_t>(std::ostream&, std::size_t const&);
} // end namespace BaseLib
......@@ -14,12 +14,10 @@
#pragma once
#include <fstream>
#include <iosfwd>
#include <string>
#include <vector>
#include "Logging.h"
namespace BaseLib
{
/**
......@@ -60,10 +58,7 @@ std::string constructFormattedFileName(std::string const& format_specification,
* \param val value
*/
template <typename T>
void writeValueBinary(std::ostream& out, T const& val)
{
out.write(reinterpret_cast<const char*>(&val), sizeof(T));
}
void writeValueBinary(std::ostream& out, T const& val);
template <typename T>
T swapEndianness(T const& v)
......@@ -86,51 +81,11 @@ T swapEndianness(T const& v)
double swapEndianness(double const& v);
template <typename T>
T readBinaryValue(std::istream& in)
{
T v;
in.read(reinterpret_cast<char*>(&v), sizeof(T));
return v;
}
T readBinaryValue(std::istream& in);
template <typename T>
std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
{
std::ifstream in(filename.c_str());
if (!in)
{
ERR("readBinaryArray(): Error while reading from file '{:s}'.",
filename);
ERR("Could not open file '{:s}' for input.", filename);
in.close();
return std::vector<T>();
}
std::vector<T> result;
result.reserve(n);
for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
{
result.push_back(BaseLib::readBinaryValue<T>(in));
}
if (result.size() == n)
{
return result;
}
ERR("readBinaryArray(): Error while reading from file '{:s}'.", filename);
ERR("Read different number of values. Expected {:d}, got {:d}.",
n,
result.size());
if (!in.eof())
{
ERR("EOF reached.\n");
}
return std::vector<T>();
}
std::vector<T> readBinaryArray(std::string const& filename,
std::size_t const n);
/**
* Extracts basename from given pathname with extension.
......
/**
* \file
* \copyright
* Copyright (c) 2012-2022, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "Histogram.h"
#include <fstream>
#include "BaseLib/Logging.h"
namespace BaseLib
{
template <typename T>
int Histogram<T>::write(std::string const& file_name,
std::string const& data_set_name,
std::string const& param_name) const
{
if (file_name.empty())
{
ERR("No file name specified.");
return 1;
}
std::ofstream out(file_name);
if (!out)
{
ERR("Error writing histogram: Could not open file.");
return 1;
}
out << "# Histogram for parameter " << param_name << " of data set "
<< data_set_name << "\n";
std::size_t const n_bins = this->getNumberOfBins();
std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
double const min(this->getMinimum());
double const bin_width(this->getBinWidth());
for (std::size_t k(0); k < n_bins; k++)
{
out << min + k * bin_width << " " << bin_cnts[k] << "\n";
}
out.close();
return 0;
}
template <typename T>
void Histogram<T>::prettyPrint(std::ostream& os,
const unsigned int line_width) const
{
const std::size_t count_max =
*std::max_element(histogram_.begin(), histogram_.end());
for (unsigned int bin = 0; bin < nr_bins_; ++bin)
{
os << "[" << min_ + bin * bin_width_ << ", "
<< min_ + (bin + 1) * bin_width_ << ")\t";
os << histogram_[bin] << "\t";
const int n_stars = static_cast<int>(
std::ceil(line_width * ((double)histogram_[bin] / count_max)));
for (int star = 0; star < n_stars; star++)
{
os << "*";
}
os << "\n";
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const Histogram<T>& h)
{
os << h.getNumberOfBins() << " " << h.getMinimum() << " " << h.getMaximum()
<< " ";
std::copy(h.getBinCounts().begin(), h.getBinCounts().end(),
std::ostream_iterator<std::size_t>(os, " "));
return os << std::endl;
}
template class Histogram<double>;
template std::ostream& operator<<(std::ostream& os, const Histogram<double>& h);
} // namespace BaseLib
......@@ -14,14 +14,11 @@
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iosfwd>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
#include "Logging.h"
namespace BaseLib
{
/** Basic Histogram implementation.
......@@ -115,56 +112,11 @@ public:
const T& getMaximum() const { return max_; }
const T& getBinWidth() const { return bin_width_; }
void prettyPrint(std::ostream& os, const unsigned int line_width = 16) const
{
const std::size_t count_max =
*std::max_element(histogram_.begin(), histogram_.end());
for (unsigned int bin = 0; bin < nr_bins_; ++bin)
{
os << "[" << min_ + bin * bin_width_ << ", "
<< min_ + (bin + 1) * bin_width_ << ")\t";
os << histogram_[bin] << "\t";
const int n_stars =
std::ceil(line_width * ((double)histogram_[bin] / count_max));
for (int star = 0; star < n_stars; star++)
{
os << "*";
}
os << "\n";
}
}
void prettyPrint(std::ostream& os,
const unsigned int line_width = 16) const;
int write(std::string const& file_name, std::string const& data_set_name,
std::string const& param_name) const
{
if (file_name.empty())
{
ERR("No file name specified.");
return 1;
}
std::ofstream out(file_name);
if (!out)
{
ERR("Error writing histogram: Could not open file.");
return 1;
}
out << "# Histogram for parameter " << param_name << " of data set "
<< data_set_name << "\n";
std::size_t const n_bins = this->getNumberOfBins();
std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
double const min(this->getMinimum());
double const bin_width(this->getBinWidth());
for (std::size_t k(0); k < n_bins; k++)
{
out << min + k * bin_width << " " << bin_cnts[k] << "\n";
}
out.close();
return 0;
}
std::string const& param_name) const;
protected:
/** Initialize class members after constructor call.
......@@ -200,12 +152,9 @@ private:
* number of bins, minimum, maximum, bin0 count, ..., binN-1 count.
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const Histogram<T>& h)
{
os << h.getNumberOfBins() << " " << h.getMinimum() << " " << h.getMaximum()
<< " ";
std::copy(h.getBinCounts().begin(), h.getBinCounts().end(),
std::ostream_iterator<T>(os, " "));
return os << std::endl;
}
std::ostream& operator<<(std::ostream& os, const Histogram<T>& h);
extern template class Histogram<double>;
extern template std::ostream& operator<<(std::ostream& os,
const Histogram<double>& h);
} // namespace BaseLib
/**
* \file
* \copyright
* Copyright (c) 2012-2022, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "EigenMatrix.h"
#include <fstream>
namespace MathLib
{
/// printout this matrix for debugging
void EigenMatrix::write(const std::string& filename) const
{
std::ofstream of(filename);
if (of)
{
write(of);
}
}
/// printout this matrix for debugging
void EigenMatrix::write(std::ostream& os) const
{
for (int k = 0; k < mat_.outerSize(); ++k)
{
for (RawMatrixType::InnerIterator it(mat_, k); it; ++it)
{
os << it.row() << " " << it.col() << ": " << it.value() << "\n";
}
}
os << std::endl;
}
} // namespace MathLib
......@@ -11,10 +11,9 @@
#pragma once
#include <Eigen/Sparse>
#include <fstream>
#include <iosfwd>
#include <string>
#include "EigenVector.h"
#include "MathLib/LinAlg/RowColumnIndices.h"
#include "MathLib/LinAlg/SetMatrixSparsity.h"
......@@ -137,27 +136,10 @@ public:
static constexpr bool isAssembled() { return true; }
/// printout this matrix for debugging
void write(const std::string& filename) const
{
std::ofstream of(filename);
if (of)
{
write(of);
}
}
void write(const std::string& filename) const;
/// printout this matrix for debugging
void write(std::ostream& os) const
{
for (int k = 0; k < mat_.outerSize(); ++k)
{
for (RawMatrixType::InnerIterator it(mat_, k); it; ++it)
{
os << it.row() << " " << it.col() << ": " << it.value() << "\n";
}
}
os << std::endl;
}
void write(std::ostream& os) const;
RawMatrixType& getRawMatrix() { return mat_; }
const RawMatrixType& getRawMatrix() const { return mat_; }
......
/**
* \file
* \copyright
* Copyright (c) 2012-2022, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "EigenVector.h"
#include "EigenMapTools.h"
#ifndef NDEBUG
#include <fstream>
#endif
namespace MathLib
{
void EigenVector::copyValues(std::vector<double>& u) const
{
u.resize(size());
toVector(u) = vec_;
}
#ifndef NDEBUG
void EigenVector::write(const std::string& filename) const
{
std::ofstream os(filename);
os << vec_;
}
#endif
} // namespace MathLib
......@@ -11,16 +11,13 @@
#pragma once
#include <vector>
#ifndef NDEBUG
#include <fstream>
#include <string>
#endif
#include <Eigen/Core>
#include <Eigen/Sparse>
#include "EigenMapTools.h"
namespace MathLib
{
/// Global vector based on Eigen vector
......@@ -103,19 +100,11 @@ public:
/// Copy local entries to a vector.
/// \param u a vector for the values of local entries. It will be resized to
/// hold the current vector data.
void copyValues(std::vector<double>& u) const
{
u.resize(size());
toVector(u) = vec_;
}
void copyValues(std::vector<double>& u) const;
#ifndef NDEBUG
/// printout this equation for debugging
void write(const std::string& filename) const
{
std::ofstream os(filename);
os << vec_;
}
/// write this vector to a file for debugging
void write(const std::string& filename) const;
#endif
/// return a raw Eigen vector object
......
......@@ -18,6 +18,7 @@
#include "MeshIO.h"
#include <fstream>
#include <iomanip>
#include <memory>
#include <sstream>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment