Refactor reader_native.cpp
- Use std::string instead of error-prone char buffers - Limit reading files to known magic strings DUMPATOM and DUMPCUSTOM
This commit is contained in:
@ -38,8 +38,6 @@ ReaderNative::ReaderNative(LAMMPS *lmp) : Reader(lmp)
|
|||||||
fieldindex = nullptr;
|
fieldindex = nullptr;
|
||||||
maxbuf = 0;
|
maxbuf = 0;
|
||||||
databuf = nullptr;
|
databuf = nullptr;
|
||||||
magic_string = nullptr;
|
|
||||||
unit_style = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
@ -47,8 +45,6 @@ ReaderNative::ReaderNative(LAMMPS *lmp) : Reader(lmp)
|
|||||||
ReaderNative::~ReaderNative()
|
ReaderNative::~ReaderNative()
|
||||||
{
|
{
|
||||||
delete[] line;
|
delete[] line;
|
||||||
delete[] magic_string;
|
|
||||||
delete[] unit_style;
|
|
||||||
memory->destroy(fieldindex);
|
memory->destroy(fieldindex);
|
||||||
memory->destroy(databuf);
|
memory->destroy(databuf);
|
||||||
}
|
}
|
||||||
@ -64,10 +60,8 @@ int ReaderNative::read_time(bigint &ntimestep)
|
|||||||
if (binary) {
|
if (binary) {
|
||||||
int endian = 0x0001;
|
int endian = 0x0001;
|
||||||
revision = 0x0001;
|
revision = 0x0001;
|
||||||
delete[] magic_string;
|
magic_string = "";
|
||||||
delete[] unit_style;
|
unit_style = "";
|
||||||
magic_string = nullptr;
|
|
||||||
unit_style = nullptr;
|
|
||||||
|
|
||||||
fread(&ntimestep, sizeof(bigint), 1, fp);
|
fread(&ntimestep, sizeof(bigint), 1, fp);
|
||||||
|
|
||||||
@ -79,9 +73,7 @@ int ReaderNative::read_time(bigint &ntimestep)
|
|||||||
// first bigint encodes negative format name length
|
// first bigint encodes negative format name length
|
||||||
bigint magic_string_len = -ntimestep;
|
bigint magic_string_len = -ntimestep;
|
||||||
|
|
||||||
magic_string = new char[magic_string_len + 1];
|
magic_string = read_binary_str(magic_string_len);
|
||||||
read_buf(magic_string, sizeof(char), magic_string_len);
|
|
||||||
magic_string[magic_string_len] = '\0';
|
|
||||||
|
|
||||||
// read endian flag
|
// read endian flag
|
||||||
read_buf(&endian, sizeof(int), 1);
|
read_buf(&endian, sizeof(int), 1);
|
||||||
@ -168,7 +160,7 @@ void ReaderNative::skip()
|
|||||||
|
|
||||||
void ReaderNative::skip_reading_magic_str()
|
void ReaderNative::skip_reading_magic_str()
|
||||||
{
|
{
|
||||||
if (magic_string && revision > 0x0001) {
|
if (is_known_magic_str() && revision > 0x0001) {
|
||||||
int len;
|
int len;
|
||||||
read_buf(&len, sizeof(int), 1);
|
read_buf(&len, sizeof(int), 1);
|
||||||
|
|
||||||
@ -212,7 +204,7 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
|
|||||||
{
|
{
|
||||||
bigint natoms = 0;
|
bigint natoms = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
char *labelline = nullptr;
|
std::string labelline;
|
||||||
|
|
||||||
if (binary) {
|
if (binary) {
|
||||||
read_buf(&natoms, sizeof(bigint), 1);
|
read_buf(&natoms, sizeof(bigint), 1);
|
||||||
@ -241,16 +233,15 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
|
|||||||
return natoms;
|
return natoms;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (magic_string && revision > 0x0001) {
|
|
||||||
|
if (is_known_magic_str() && revision > 0x0001) {
|
||||||
// newer format includes units string, columns string
|
// newer format includes units string, columns string
|
||||||
// and time
|
// and time
|
||||||
read_buf(&len, sizeof(int), 1);
|
read_buf(&len, sizeof(int), 1);
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
// has units
|
// has units
|
||||||
unit_style = new char[len + 1];
|
unit_style = read_binary_str(len);
|
||||||
read_buf(unit_style, sizeof(char), len);
|
|
||||||
unit_style[len] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char flag = 0;
|
char flag = 0;
|
||||||
@ -262,9 +253,9 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
|
|||||||
}
|
}
|
||||||
|
|
||||||
read_buf(&len, sizeof(int), 1);
|
read_buf(&len, sizeof(int), 1);
|
||||||
labelline = new char[len + 1];
|
labelline = read_binary_str(len);
|
||||||
read_buf(labelline, sizeof(char), len);
|
} else {
|
||||||
labelline[len] = '\0';
|
error->one(FLERR, "Unsupported old binary dump format");
|
||||||
}
|
}
|
||||||
|
|
||||||
read_buf(&nchunk, sizeof(int), 1);
|
read_buf(&nchunk, sizeof(int), 1);
|
||||||
@ -310,14 +301,15 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
|
|||||||
labelline = &line[strlen("ITEM: ATOMS ")];
|
labelline = &line[strlen("ITEM: ATOMS ")];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, int> labels;
|
|
||||||
Tokenizer tokens(labelline);
|
Tokenizer tokens(labelline);
|
||||||
|
std::map<std::string, int> labels;
|
||||||
nwords = 0;
|
nwords = 0;
|
||||||
|
|
||||||
while (tokens.has_next()) {
|
while (tokens.has_next()) {
|
||||||
labels[tokens.next()] = nwords++;
|
labels[tokens.next()] = nwords++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (nwords == 0) {
|
if (nwords == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -549,6 +541,13 @@ void ReaderNative::read_buf(void * ptr, size_t size, size_t count)
|
|||||||
if (feof(fp)) error->one(FLERR,"Unexpected end of dump file");
|
if (feof(fp)) error->one(FLERR,"Unexpected end of dump file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string ReaderNative::read_binary_str(size_t size)
|
||||||
|
{
|
||||||
|
std::string str(size, '\0');
|
||||||
|
read_buf(&str[0], sizeof(char), size);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
void ReaderNative::read_double_chunk(size_t count)
|
void ReaderNative::read_double_chunk(size_t count)
|
||||||
{
|
{
|
||||||
if (count < 0) return;
|
if (count < 0) return;
|
||||||
@ -566,3 +565,8 @@ void ReaderNative::skip_buf(size_t size)
|
|||||||
pos += size;
|
pos += size;
|
||||||
platform::fseek(fp,pos);
|
platform::fseek(fp,pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ReaderNative::is_known_magic_str() const
|
||||||
|
{
|
||||||
|
return magic_string == "DUMPATOM" || magic_string == "DUMPCUSTOM";
|
||||||
|
}
|
||||||
|
|||||||
@ -24,6 +24,7 @@ ReaderStyle(native,ReaderNative);
|
|||||||
|
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
namespace LAMMPS_NS {
|
namespace LAMMPS_NS {
|
||||||
@ -42,8 +43,8 @@ class ReaderNative : public Reader {
|
|||||||
private:
|
private:
|
||||||
int revision;
|
int revision;
|
||||||
|
|
||||||
char *magic_string;
|
std::string magic_string;
|
||||||
char *unit_style;
|
std::string unit_style;
|
||||||
int *fieldindex;
|
int *fieldindex;
|
||||||
|
|
||||||
char *line; // line read from dump file
|
char *line; // line read from dump file
|
||||||
@ -64,6 +65,8 @@ class ReaderNative : public Reader {
|
|||||||
void read_double_chunk(size_t);
|
void read_double_chunk(size_t);
|
||||||
void skip_buf(size_t);
|
void skip_buf(size_t);
|
||||||
void skip_reading_magic_str();
|
void skip_reading_magic_str();
|
||||||
|
bool is_known_magic_str() const;
|
||||||
|
std::string read_binary_str(size_t);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace LAMMPS_NS
|
} // namespace LAMMPS_NS
|
||||||
|
|||||||
Reference in New Issue
Block a user