diff --git a/src/potential_file_reader.cpp b/src/potential_file_reader.cpp index c1fa157ef9..bb723d0e82 100644 --- a/src/potential_file_reader.cpp +++ b/src/potential_file_reader.cpp @@ -1,4 +1,3 @@ -// clang-format off /* -*- c++ -*- ---------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -45,26 +44,20 @@ using namespace LAMMPS_NS; * \param auto_convert Bitmask of supported unit conversions */ -PotentialFileReader::PotentialFileReader(LAMMPS *lmp, - const std::string &filename, +PotentialFileReader::PotentialFileReader(LAMMPS *lmp, const std::string &filename, const std::string &potential_name, - const std::string &name_suffix, - const int auto_convert) : - Pointers(lmp), - reader(nullptr), - filename(filename), - filetype(potential_name + name_suffix), - unit_convert(auto_convert) + const std::string &name_suffix, const int auto_convert) : + Pointers(lmp), + reader(nullptr), filename(filename), filetype(potential_name + name_suffix), + unit_convert(auto_convert) { - if (comm->me != 0) { - error->one(FLERR, "FileReader should only be called by proc 0!"); - } + if (comm->me != 0) { error->one(FLERR, "FileReader should only be called by proc 0!"); } try { reader = open_potential(filename); if (!reader) { - error->one(FLERR, "cannot open {} potential file {}: {}", - potential_name, filename, utils::getsyserror()); + error->one(FLERR, "cannot open {} potential file {}: {}", potential_name, filename, + utils::getsyserror()); } } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -77,30 +70,32 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp, * \param potential_name Name of potential style for error messages * \param auto_convert Bitmask of supported unit conversions */ -PotentialFileReader::PotentialFileReader(LAMMPS *lmp, - const std::string &filename, +PotentialFileReader::PotentialFileReader(LAMMPS *lmp, const std::string &filename, const std::string &potential_name, const int auto_convert) : - PotentialFileReader(lmp, filename, potential_name, " potential", auto_convert) + PotentialFileReader(lmp, filename, potential_name, " potential", auto_convert) { } /** Closes the file */ -PotentialFileReader::~PotentialFileReader() { +PotentialFileReader::~PotentialFileReader() +{ delete reader; } /** Set comment (= text after '#') handling preference for the file to be read * * \param value Comment text is ignored if true, or not if false */ -void PotentialFileReader::ignore_comments(bool value) { +void PotentialFileReader::ignore_comments(bool value) +{ reader->ignore_comments = value; } /** Read a line but ignore its content */ -void PotentialFileReader::skip_line() { +void PotentialFileReader::skip_line() +{ try { reader->skip_line(); } catch (FileReaderException &e) { @@ -119,7 +114,8 @@ void PotentialFileReader::skip_line() { * \param nparams Number of words that must be read. Default: 0 * \return String with the concatenated text */ -char *PotentialFileReader::next_line(int nparams) { +char *PotentialFileReader::next_line(int nparams) +{ try { return reader->next_line(nparams); } catch (FileReaderException &e) { @@ -137,7 +133,8 @@ char *PotentialFileReader::next_line(int nparams) { * \param list Pointer to array with suitable storage for *n* doubles * \param n Number of doubles to be read */ -void PotentialFileReader::next_dvector(double * list, int n) { +void PotentialFileReader::next_dvector(double *list, int n) +{ try { return reader->next_dvector(list, n); } catch (FileReaderException &e) { @@ -155,7 +152,8 @@ void PotentialFileReader::next_dvector(double * list, int n) { * \param separators String with list of separators. * \return ValueTokenizer object for read in text */ -ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string &separators) { +ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string &separators) +{ try { return reader->next_values(nparams, separators); } catch (FileReaderException &e) { @@ -168,9 +166,10 @@ ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string & * * \return Value of first word in line as double */ -double PotentialFileReader::next_double() { +double PotentialFileReader::next_double() +{ try { - char * line = reader->next_line(1); + char *line = reader->next_line(1); return ValueTokenizer(line).next_double(); } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -182,9 +181,10 @@ double PotentialFileReader::next_double() { * * \return Value of first word in line as int */ -int PotentialFileReader::next_int() { +int PotentialFileReader::next_int() +{ try { - char * line = reader->next_line(1); + char *line = reader->next_line(1); return ValueTokenizer(line).next_int(); } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -196,9 +196,10 @@ int PotentialFileReader::next_int() { * * \return Value of first word in line as tagint */ -tagint PotentialFileReader::next_tagint() { +tagint PotentialFileReader::next_tagint() +{ try { - char * line = reader->next_line(1); + char *line = reader->next_line(1); return ValueTokenizer(line).next_tagint(); } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -210,9 +211,10 @@ tagint PotentialFileReader::next_tagint() { * * \return Value of first word in line as bigint */ -bigint PotentialFileReader::next_bigint() { +bigint PotentialFileReader::next_bigint() +{ try { - char * line = reader->next_line(1); + char *line = reader->next_line(1); return ValueTokenizer(line).next_bigint(); } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -224,9 +226,10 @@ bigint PotentialFileReader::next_bigint() { * * \return First word of read in line */ -std::string PotentialFileReader::next_string() { +std::string PotentialFileReader::next_string() +{ try { - char * line = reader->next_line(1); + char *line = reader->next_line(1); return ValueTokenizer(line).next_string(); } catch (FileReaderException &e) { error->one(FLERR, e.what()); @@ -246,17 +249,17 @@ std::string PotentialFileReader::next_string() { * \param path Path of the potential file to open * \return Pointer to TextFileReader object created */ -TextFileReader *PotentialFileReader::open_potential(const std::string &path) { +TextFileReader *PotentialFileReader::open_potential(const std::string &path) +{ std::string filepath = utils::get_potential_file_path(path); if (!filepath.empty()) { std::string unit_style = lmp->update->unit_style; - std::string date = utils::get_potential_date(filepath, filetype); - std::string units = utils::get_potential_units(filepath, filetype); + std::string date = utils::get_potential_date(filepath, filetype); + std::string units = utils::get_potential_units(filepath, filetype); if (!date.empty()) - utils::logmesg(lmp,"Reading {} file {} with DATE: {}\n", - filetype, filename, date); + utils::logmesg(lmp, "Reading {} file {} with DATE: {}\n", filetype, filename, date); if (units.empty()) { unit_convert = utils::NOCONVERT; @@ -266,18 +269,18 @@ TextFileReader *PotentialFileReader::open_potential(const std::string &path) { } else { if ((units == "metal") && (unit_style == "real") && (unit_convert & utils::METAL2REAL)) { unit_convert = utils::METAL2REAL; - } else if ((units == "real") && (unit_style == "metal") && (unit_convert & utils::REAL2METAL)) { + } else if ((units == "real") && (unit_style == "metal") && + (unit_convert & utils::REAL2METAL)) { unit_convert = utils::REAL2METAL; } else { - lmp->error->one(FLERR, "{} file {} requires {} units " - "but {} units are in use", filetype, - filename, units, unit_style); + lmp->error->one(FLERR, "{} file {} requires {} units but {} units are in use", filetype, + filename, units, unit_style); } } } if (unit_convert != utils::NOCONVERT) - lmp->error->warning(FLERR, "Converting {} in {} units to {} units", - filetype, units, unit_style); + lmp->error->warning(FLERR, "Converting {} in {} units to {} units", filetype, units, + unit_style); return new TextFileReader(filepath, filetype); } return nullptr; diff --git a/src/text_file_reader.cpp b/src/text_file_reader.cpp index d4847c67ee..926dedcf80 100644 --- a/src/text_file_reader.cpp +++ b/src/text_file_reader.cpp @@ -1,4 +1,3 @@ -// clang-format off /* -*- c++ -*- ---------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -41,14 +40,14 @@ using namespace LAMMPS_NS; * \param filename Name of file to be read * \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) +TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) : + filetype(filetype), closefp(true), ignore_comments(true) { fp = fopen(filename.c_str(), "r"); if (fp == nullptr) { - throw FileReaderException(fmt::format("cannot open {} file {}: {}", - filetype, filename, utils::getsyserror())); + throw FileReaderException( + fmt::format("cannot open {} file {}: {}", filetype, filename, utils::getsyserror())); } } @@ -69,21 +68,23 @@ This function is useful in combination with :cpp:func:`utils::open_potential`. * \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), closefp(false), fp(fp), ignore_comments(true) +TextFileReader::TextFileReader(FILE *fp, const std::string &filetype) : + filetype(filetype), closefp(false), fp(fp), ignore_comments(true) { if (fp == nullptr) throw FileReaderException("Invalid file descriptor"); } /** Closes the file */ -TextFileReader::~TextFileReader() { +TextFileReader::~TextFileReader() +{ if (closefp) fclose(fp); } /** Read the next line and ignore it */ -void TextFileReader::skip_line() { +void TextFileReader::skip_line() +{ char *ptr = fgets(line, MAXLINE, fp); if (ptr == nullptr) { // EOF @@ -105,7 +106,8 @@ void TextFileReader::skip_line() { * \param nparams Number of words that must be read. Default: 0 * \return String with the concatenated text */ -char *TextFileReader::next_line(int nparams) { +char *TextFileReader::next_line(int nparams) +{ // concatenate lines until have nparams words int n = 0; int nwords = 0; @@ -129,7 +131,8 @@ char *TextFileReader::next_line(int nparams) { if (ptr == nullptr) { // EOF if (nwords > 0 && nwords < nparams) { - throw EOFException(fmt::format("Incorrect format in {} file! {}/{} parameters", filetype, nwords, nparams)); + throw EOFException(fmt::format("Incorrect format in {} file! {}/{} parameters", filetype, + nwords, nparams)); } return nullptr; } @@ -140,9 +143,7 @@ char *TextFileReader::next_line(int nparams) { nwords += utils::count_words(&line[n]); // skip line if blank - if (nwords > 0) { - n = strlen(line); - } + if (nwords > 0) { n = strlen(line); } } return line; @@ -157,7 +158,8 @@ char *TextFileReader::next_line(int nparams) { * \param list Pointer to array with suitable storage for *n* doubles * \param n Number of doubles to be read */ -void TextFileReader::next_dvector(double * list, int n) { +void TextFileReader::next_dvector(double *list, int n) +{ int i = 0; while (i < n) { char *ptr = next_line(); @@ -165,14 +167,13 @@ void TextFileReader::next_dvector(double * list, int n) { if (ptr == nullptr) { // EOF if (i < n) { - throw FileReaderException(fmt::format("Incorrect format in {} file! {}/{} values", filetype, i, n)); + throw FileReaderException( + fmt::format("Incorrect format in {} file! {}/{} values", filetype, i, n)); } } ValueTokenizer values(line); - while (values.has_next()) { - list[i++] = values.next_double(); - } + while (values.has_next()) { list[i++] = values.next_double(); } } } @@ -186,9 +187,9 @@ void TextFileReader::next_dvector(double * list, int n) { * \param separators String with list of separators. * \return ValueTokenizer object for read in text */ -ValueTokenizer TextFileReader::next_values(int nparams, const std::string &separators) { +ValueTokenizer TextFileReader::next_values(int nparams, const std::string &separators) +{ char *ptr = next_line(nparams); - if (ptr == nullptr) - throw EOFException(fmt::format("Missing line in {} file!", filetype)); + if (ptr == nullptr) throw EOFException(fmt::format("Missing line in {} file!", filetype)); return ValueTokenizer(line, separators); }