Unverified Commit 5a22f4d7 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

support that LAMMPS_POTENTIALS is a real path variable with multiple entries,...

support that LAMMPS_POTENTIALS is a real path variable with multiple entries, not just a single folder
parent 05ff3520
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <cstdlib>
#include <cerrno>
#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,10 +838,14 @@ 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);
    if (var != nullptr){
      Tokenizer dirs(var,OS_PATH_VAR_SEP);

      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)) {
@@ -842,8 +853,10 @@ std::string utils::get_potential_file_path(const std::string &path) {
        }
      }
    }
  }
  return "";
}
#undef OS_PATH_VAR_SEP

/* ----------------------------------------------------------------------
   read first line of potential file
+9 −0
Original line number Diff line number Diff line
@@ -62,6 +62,15 @@ TEST(Tokenizer, iterate_words)
    ASSERT_EQ(t.count(), 2);
}

TEST(Tokenizer, no_separator_path)
{
    Tokenizer t("one", ":");
    ASSERT_EQ(t.has_next(), true);
    ASSERT_EQ(t.count(), 1);
    ASSERT_THAT(t.next(), Eq("one"));
    ASSERT_EQ(t.has_next(), false);
}

TEST(Tokenizer, unix_paths)
{
    Tokenizer t(":one:two:three:", ":");
+7 −7

File changed.

Contains only whitespace changes.