Use sfread and sfgets in reader_native.cpp

This commit is contained in:
Richard Berger
2021-12-17 16:58:42 -05:00
parent 2d4f030f11
commit a3a6077115
2 changed files with 10 additions and 18 deletions

View File

@ -63,10 +63,10 @@ int ReaderNative::read_time(bigint &ntimestep)
magic_string = ""; magic_string = "";
unit_style = ""; unit_style = "";
fread(&ntimestep, sizeof(bigint), 1, fp); auto ret = fread(&ntimestep, sizeof(bigint), 1, fp);
// detect end-of-file // detect end-of-file
if (feof(fp)) return 1; if (ret != 1 || feof(fp)) return 1;
// detect newer format // detect newer format
if (ntimestep < 0) { if (ntimestep < 0) {
@ -485,12 +485,8 @@ void ReaderNative::read_atoms(int n, int nfield, double **fields)
} }
} }
} else { } else {
int i,m; for (int i = 0; i < n; i++) {
char *eof; utils::sfgets(FLERR, line, MAXLINE, fp, nullptr, error);
for (i = 0; i < n; i++) {
eof = fgets(line,MAXLINE,fp);
if (eof == nullptr) error->one(FLERR,"Unexpected end of dump file");
// tokenize the line // tokenize the line
std::vector<std::string> words = Tokenizer(line).as_vector(); std::vector<std::string> words = Tokenizer(line).as_vector();
@ -499,7 +495,7 @@ void ReaderNative::read_atoms(int n, int nfield, double **fields)
// convert selected fields to floats // convert selected fields to floats
for (m = 0; m < nfield; m++) for (int m = 0; m < nfield; m++)
fields[i][m] = atof(words[fieldindex[m]].c_str()); fields[i][m] = atof(words[fieldindex[m]].c_str());
} }
} }
@ -527,18 +523,14 @@ int ReaderNative::find_label(const std::string &label, const std::map<std::strin
void ReaderNative::read_lines(int n) void ReaderNative::read_lines(int n)
{ {
char *eof = nullptr; for (int i = 0; i < n; i++) {
if (n <= 0) return; utils::sfgets(FLERR, line, MAXLINE, fp, nullptr, error);
for (int i = 0; i < n; i++) eof = fgets(line,MAXLINE,fp); }
if (eof == nullptr) error->one(FLERR,"Unexpected end of dump file");
} }
void ReaderNative::read_buf(void * ptr, size_t size, size_t count) void ReaderNative::read_buf(void * ptr, size_t size, size_t count)
{ {
fread(ptr, size, count, fp); utils::sfread(FLERR, ptr, size, count, fp, nullptr, error);
// detect end-of-file
if (feof(fp)) error->one(FLERR,"Unexpected end of dump file");
} }
std::string ReaderNative::read_binary_str(size_t size) std::string ReaderNative::read_binary_str(size_t size)

View File

@ -52,7 +52,7 @@ class ReaderNative : public Reader {
int nwords; // # of per-atom columns in dump file int nwords; // # of per-atom columns in dump file
int size_one; // number of double for one atom int size_one; // number of double for one atom
int maxbuf; // maximum buffer size size_t maxbuf; // maximum buffer size
int nchunk; // number of chunks in the binary file int nchunk; // number of chunks in the binary file
int ichunk; // index of current reading chunk int ichunk; // index of current reading chunk
int natom_chunk; // number of atoms in the current chunks int natom_chunk; // number of atoms in the current chunks