User Guide
file_library.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "file_library.hpp"
9 
10 #include <cassert>
11 #include <filesystem>
12 
13 #include "utils/common_utils.hpp"
14 #include "utils/string_utils.hpp"
15 
16 namespace fs = std::filesystem;
17 
18 namespace nmodl {
19 
20 
22  FileLibrary library;
23  library.append_env_var("NMODL_PATH");
24  library.push_cwd();
25  return library;
26 }
27 
28 void FileLibrary::append_env_var(const std::string& env_var) {
29  const auto value = getenv(env_var.c_str());
30  if (value != nullptr) {
31  for (const auto& path: stringutils::split_string(value, utils::envpathsep)) {
32  if (!path.empty()) {
33  paths_.insert(paths_.begin(), path);
34  }
35  }
36  }
37 }
38 
39 void FileLibrary::push_current_directory(const fs::path& path) {
40  paths_.push_back(path);
41 }
42 
44  push_current_directory(fs::current_path());
45 }
46 
48  assert(!paths_.empty());
49  if (!paths_.empty()) {
50  paths_.pop_back();
51  }
52 }
53 
54 std::string FileLibrary::find_file(const fs::path& file) {
55  if (file.is_absolute() && fs::exists(file)) {
56  return "";
57  }
58  for (auto paths_it = paths_.rbegin(); paths_it != paths_.rend(); ++paths_it) {
59  auto file_abs = *paths_it / file;
60  if (fs::exists(file_abs)) {
61  return paths_it->string();
62  }
63  }
64  return "";
65 }
66 
67 } // namespace nmodl
nmodl::FileLibrary::append_env_var
void append_env_var(const std::string &env_var)
Definition: file_library.cpp:28
nmodl::FileLibrary::find_file
std::string find_file(const std::filesystem::path &file)
Search a file.
Definition: file_library.cpp:54
nmodl::FileLibrary::push_current_directory
void push_current_directory(const std::filesystem::path &path)
Definition: file_library.cpp:39
nmodl::FileLibrary::paths_
std::vector< std::filesystem::path > paths_
inclusion path list
Definition: file_library.hpp:68
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
file_library.hpp
Manage search path.
string_utils.hpp
Implement string manipulation functions.
nmodl::FileLibrary::pop_current_directory
void pop_current_directory()
Definition: file_library.cpp:47
nmodl::stringutils::split_string
static std::vector< std::string > split_string(const std::string &text, char delimiter)
Split a text in a list of words, using a given delimiter character.
Definition: string_utils.hpp:116
nmodl::FileLibrary::default_instance
static FileLibrary default_instance()
Initialize the library with the following path:
Definition: file_library.cpp:21
nmodl::FileLibrary
Manage search path.
Definition: file_library.hpp:30
nmodl::FileLibrary::push_cwd
void push_cwd()
push the working directory in the directories stack
Definition: file_library.cpp:43
nmodl::utils::envpathsep
static constexpr char envpathsep
The character conventionally used by the operating system to separate search path components.
Definition: common_utils.hpp:46
common_utils.hpp
Common utility functions for file/dir manipulation.