add custom constructor for TextFileReader that uses an already opened file descriptor

This commit is contained in:
Axel Kohlmeyer
2021-04-17 15:41:45 -04:00
parent 4fa5840f13
commit ac60cfb0c3
2 changed files with 15 additions and 2 deletions

View File

@ -41,7 +41,7 @@ using namespace LAMMPS_NS;
* \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype)
: filename(filename), filetype(filetype), ignore_comments(true)
: filetype(filetype), ignore_comments(true)
{
fp = fopen(filename.c_str(), "r");
@ -51,6 +51,18 @@ TextFileReader::TextFileReader(const std::string &filename, const std::string &f
}
}
/**
* \overload
*
* \param fp File descriptor of the already opened file
* \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(FILE *fp, const std::string &filetype)
: filetype(filetype), fp(fp), ignore_comments(true)
{
if (fp == nullptr) throw FileReaderException("Invalid file descriptor");
}
/** Closes the file */
TextFileReader::~TextFileReader() {

View File

@ -25,7 +25,6 @@
namespace LAMMPS_NS
{
class TextFileReader {
std::string filename;
std::string filetype;
static constexpr int MAXLINE = 1024;
char line[MAXLINE];
@ -35,6 +34,8 @@ namespace LAMMPS_NS
bool ignore_comments; //!< Controls whether comments are ignored
TextFileReader(const std::string &filename, const std::string &filetype);
TextFileReader(FILE *fp, const std::string &filetype);
~TextFileReader();
void skip_line();