Move common potentials opening code to utils

This commit is contained in:
Richard Berger
2020-06-04 17:18:28 -04:00
parent 7ab65c05da
commit 4a6d3bc0e0
6 changed files with 85 additions and 151 deletions

View File

@ -17,6 +17,7 @@
#include "lammps.h"
#include "error.h"
#include "tokenizer.h"
#include "text_file_reader.h"
#include "fmt/format.h"
#if defined(__linux__)
@ -450,6 +451,59 @@ bool utils::file_is_readable(const std::string & path) {
return false;
}
/* ----------------------------------------------------------------------
try to find potential file as specified by name
search current directory and the LAMMPS_POTENTIALS directory if
specified
------------------------------------------------------------------------- */
std::string utils::get_potential_file_path(const std::string& path) {
std::string filepath = path;
std::string filename = utils::path_basename(path);
if(utils::file_is_readable(filepath)) {
return filepath;
} else {
// try the environment variable directory
const char *path = getenv("LAMMPS_POTENTIALS");
if (path != nullptr){
std::string pot = utils::path_basename(filepath);
filepath = utils::path_join(path, pot);
if (utils::file_is_readable(filepath)) {
return filepath;
}
}
}
return "";
}
/* ----------------------------------------------------------------------
read first line of potential file
if has DATE field, print following word
------------------------------------------------------------------------- */
std::string utils::get_potential_date(const std::string & path, const std::string & potential_name) {
TextFileReader reader(path, potential_name);
reader.ignore_comments = false;
char * line = nullptr;
while (line = reader.next_line()) {
ValueTokenizer values(line);
while (values.has_next()) {
std::string word = values.next_string();
if (word == "DATE:") {
if (values.has_next()) {
std::string date = values.next_string();
return date;
}
}
}
}
return "";
}
/* ------------------------------------------------------------------ */
extern "C" {