make buffer size for text file reader adjustable

This commit is contained in:
Axel Kohlmeyer
2022-04-09 17:26:03 -04:00
parent b20c98753f
commit eb9af11d64
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 */
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");
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 */
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");
}
@ -80,6 +82,20 @@ TextFileReader::TextFileReader(FILE *fp, std::string filetype) :
TextFileReader::~TextFileReader()
{
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 */
@ -93,7 +109,7 @@ void TextFileReader::rewind()
void TextFileReader::skip_line()
{
char *ptr = fgets(line, MAXLINE, fp);
char *ptr = fgets(line, bufsize, fp);
if (ptr == nullptr) {
// EOF
throw EOFException(fmt::format("Missing line in {} file!", filetype));
@ -120,7 +136,7 @@ char *TextFileReader::next_line(int nparams)
int n = 0;
int nwords = 0;
char *ptr = fgets(line, MAXLINE, fp);
char *ptr = fgets(line, bufsize, fp);
if (ptr == nullptr) {
// EOF
@ -134,7 +150,7 @@ char *TextFileReader::next_line(int nparams)
if (nwords > 0) n = strlen(line);
while (nwords == 0 || nwords < nparams) {
ptr = fgets(&line[n], MAXLINE - n, fp);
ptr = fgets(&line[n], bufsize - n, fp);
if (ptr == nullptr) {
// EOF

View File

@ -26,8 +26,8 @@ namespace LAMMPS_NS {
class TextFileReader {
std::string filetype;
bool closefp;
static constexpr int MAXLINE = 1024;
char line[MAXLINE];
int bufsize;
char *line;
FILE *fp;
public:
@ -35,9 +35,13 @@ class TextFileReader {
TextFileReader(const std::string &filename, const 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 skip_line();
char *next_line(int nparams = 0);