From b06ba74d18f124e9fc4a88f6603c880290a0a282 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 29 Aug 2020 22:09:07 -0400 Subject: [PATCH] support that LAMMPS_POTENTIALS is a real path variable with multiple entries, not just a single folder --- src/tokenizer.h | 14 +++++++------- src/utils.cpp | 25 +++++++++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/tokenizer.h b/src/tokenizer.h index cc77c8ee0d..30bb71276f 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -33,7 +33,7 @@ class Tokenizer { size_t start; size_t ntokens; public: - Tokenizer(const std::string & str, const std::string & separators = TOKENIZER_DEFAULT_SEPARATORS); + Tokenizer(const std::string &str, const std::string &separators = TOKENIZER_DEFAULT_SEPARATORS); Tokenizer(Tokenizer &&); Tokenizer(const Tokenizer &); Tokenizer& operator=(const Tokenizer&) = default; @@ -42,7 +42,7 @@ public: void reset(); void skip(int n); bool has_next() const; - bool contains(const std::string & str) const; + bool contains(const std::string &str) const; std::string next(); size_t count(); @@ -52,7 +52,7 @@ public: class TokenizerException : public std::exception { std::string message; public: - TokenizerException(const std::string & msg, const std::string & token); + TokenizerException(const std::string &msg, const std::string &token); ~TokenizerException() throw() { } @@ -64,20 +64,20 @@ public: class InvalidIntegerException : public TokenizerException { public: - InvalidIntegerException(const std::string & token) : TokenizerException("Not a valid integer number", token) { + InvalidIntegerException(const std::string &token) : TokenizerException("Not a valid integer number", token) { } }; class InvalidFloatException : public TokenizerException { public: - InvalidFloatException(const std::string & token) : TokenizerException("Not a valid floating-point number", token) { + InvalidFloatException(const std::string &token) : TokenizerException("Not a valid floating-point number", token) { } }; class ValueTokenizer { Tokenizer tokens; public: - ValueTokenizer(const std::string & str, const std::string & separators = TOKENIZER_DEFAULT_SEPARATORS); + ValueTokenizer(const std::string &str, const std::string &separators = TOKENIZER_DEFAULT_SEPARATORS); ValueTokenizer(const ValueTokenizer &); ValueTokenizer(ValueTokenizer &&); ValueTokenizer& operator=(const ValueTokenizer&) = default; @@ -90,7 +90,7 @@ public: double next_double(); bool has_next() const; - bool contains(const std::string & value) const; + bool contains(const std::string &value) const; void skip(int ntokens); size_t count(); diff --git a/src/utils.cpp b/src/utils.cpp index b6a6df9311..eecc88a529 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -16,6 +16,7 @@ #include #include #include "lammps.h" +#include "comm.h" #include "compute.h" #include "error.h" #include "fix.h" @@ -23,6 +24,7 @@ #include "modify.h" #include "tokenizer.h" #include "text_file_reader.h" +#include "update.h" #include "fmt/format.h" #if defined(__linux__) @@ -822,6 +824,11 @@ bool utils::file_is_readable(const std::string &path) { search current directory and the LAMMPS_POTENTIALS directory if specified ------------------------------------------------------------------------- */ +#if defined(_WIN32) +#define OS_PATH_VAR_SEP ";" +#else +#define OS_PATH_VAR_SEP ":" +#endif std::string utils::get_potential_file_path(const std::string &path) { std::string filepath = path; @@ -831,19 +838,25 @@ std::string utils::get_potential_file_path(const std::string &path) { return filepath; } else { // try the environment variable directory - const char *path = getenv("LAMMPS_POTENTIALS"); + const char *var = getenv("LAMMPS_POTENTIALS"); - if (path != nullptr){ - std::string pot = utils::path_basename(filepath); - filepath = utils::path_join(path, pot); + if (var != nullptr){ + Tokenizer dirs(var,OS_PATH_VAR_SEP); - if (utils::file_is_readable(filepath)) { - return filepath; + while (dirs.has_next()) { + auto pot = utils::path_basename(filepath); + auto path = dirs.next(); + filepath = utils::path_join(path, pot); + + if (utils::file_is_readable(filepath)) { + return filepath; + } } } } return ""; } +#undef OS_PATH_VAR_SEP /* ---------------------------------------------------------------------- read first line of potential file