make buffer size for text file reader adjustable

This commit is contained in:
Axel Kohlmeyer
2022-04-09 17:26:03 -04:00
parent fcd3e76767
commit 4e81adaf8c
2 changed files with 29 additions and 9 deletions

View File

@ -42,8 +42,9 @@ using namespace LAMMPS_NS;
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) : TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) :
filetype(filetype), closefp(true), ignore_comments(true) filetype(filetype), closefp(true), line(nullptr), ignore_comments(true)
{ {
set_bufsize(1024);
fp = fopen(filename.c_str(), "r"); fp = fopen(filename.c_str(), "r");
if (fp == nullptr) { if (fp == nullptr) {
@ -70,8 +71,9 @@ This function is useful in combination with :cpp:func:`utils::open_potential`.
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(FILE *fp, std::string filetype) : TextFileReader::TextFileReader(FILE *fp, std::string filetype) :
filetype(std::move(filetype)), closefp(false), fp(fp), ignore_comments(true) filetype(std::move(filetype)), closefp(false), line(nullptr), fp(fp), ignore_comments(true)
{ {
set_bufsize(1024);
if (fp == nullptr) throw FileReaderException("Invalid file descriptor"); if (fp == nullptr) throw FileReaderException("Invalid file descriptor");
} }
@ -80,6 +82,20 @@ TextFileReader::TextFileReader(FILE *fp, std::string filetype) :
TextFileReader::~TextFileReader() TextFileReader::~TextFileReader()
{ {
if (closefp) fclose(fp); if (closefp) fclose(fp);
delete[] line;
}
/** adjust line buffer size */
void TextFileReader::set_bufsize(int newsize)
{
if (newsize < 100) {
throw FileReaderException(
fmt::format("line buffer size {} for {} file too small, must be > 100", newsize, filetype));
}
delete[] line;
bufsize = newsize;
line = new char[bufsize];
} }
/** Reset file to the beginning */ /** Reset file to the beginning */
@ -93,7 +109,7 @@ void TextFileReader::rewind()
void TextFileReader::skip_line() void TextFileReader::skip_line()
{ {
char *ptr = fgets(line, MAXLINE, fp); char *ptr = fgets(line, bufsize, fp);
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
throw EOFException(fmt::format("Missing line in {} file!", filetype)); throw EOFException(fmt::format("Missing line in {} file!", filetype));
@ -120,7 +136,7 @@ char *TextFileReader::next_line(int nparams)
int n = 0; int n = 0;
int nwords = 0; int nwords = 0;
char *ptr = fgets(line, MAXLINE, fp); char *ptr = fgets(line, bufsize, fp);
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
@ -134,7 +150,7 @@ char *TextFileReader::next_line(int nparams)
if (nwords > 0) n = strlen(line); if (nwords > 0) n = strlen(line);
while (nwords == 0 || nwords < nparams) { while (nwords == 0 || nwords < nparams) {
ptr = fgets(&line[n], MAXLINE - n, fp); ptr = fgets(&line[n], bufsize - n, fp);
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF

View File

@ -26,8 +26,8 @@ namespace LAMMPS_NS {
class TextFileReader { class TextFileReader {
std::string filetype; std::string filetype;
bool closefp; bool closefp;
static constexpr int MAXLINE = 1024; int bufsize;
char line[MAXLINE]; char *line;
FILE *fp; FILE *fp;
public: public:
@ -35,9 +35,13 @@ class TextFileReader {
TextFileReader(const std::string &filename, const std::string &filetype); TextFileReader(const std::string &filename, const std::string &filetype);
TextFileReader(FILE *fp, std::string filetype); TextFileReader(FILE *fp, std::string filetype);
TextFileReader() = delete;
TextFileReader(const TextFileReader &) = delete;
TextFileReader(const TextFileReader &&) = delete;
TextFileReader &operator=(const TextFileReader &) = delete;
virtual ~TextFileReader();
~TextFileReader(); void set_bufsize(int);
void rewind(); void rewind();
void skip_line(); void skip_line();
char *next_line(int nparams = 0); char *next_line(int nparams = 0);