LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
file_utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
28 
29 #ifndef LBANN_UTILS_FILE_HPP_INCLUDED
30 #define LBANN_UTILS_FILE_HPP_INCLUDED
31 
32 #include <fstream>
33 #include <iostream>
34 #include <iterator>
35 #include <string>
36 #include <utility>
37 #include <vector>
38 
39 namespace lbann {
40 
42 {
43  static const std::string characters;
44  static std::string preferred() { return std::string(1, characters[0]); }
45  static bool check(const char ch)
46  {
47  return (characters.find(ch) != std::string::npos);
48  }
49  bool operator()(const char ch) const
50  {
51  return (characters.find(ch) != std::string::npos);
52  }
53 };
54 
57 std::vector<int> get_tokens(std::string str, const std::vector<char> delims);
59 std::vector<std::string> get_tokens(const std::string str,
60  const std::string delims = " :;\t\r\n");
61 
64 bool parse_path(const std::string& path,
65  std::string& dir,
66  std::string& basename);
67 std::string get_ext_name(const std::string file_name);
68 std::string get_basename_without_ext(const std::string file_name);
69 std::string add_delimiter(const std::string dir);
70 std::string modify_file_name(const std::string file_name,
71  const std::string tag,
72  const std::string new_ext = "");
73 
75 bool check_if_file_exists(const std::string& filename);
77 bool check_if_dir_exists(const std::string& dirname);
79 bool create_dir(const std::string output_dir);
80 
81 bool load_file(const std::string filename,
82  std::vector<char>& buf,
83  bool append = false);
84 
85 inline void __swapEndianInt(unsigned int& ui)
86 {
87  ui = ((ui >> 24) | ((ui << 8) & 0x00FF0000) | ((ui >> 8) & 0x0000FF00) |
88  (ui << 24));
89 }
90 
91 // The generic approach
92 template <typename T>
93 std::basic_string<T> pad(const std::basic_string<T>& s,
94  typename std::basic_string<T>::size_type n,
95  T c)
96 {
97  if (n > s.length()) {
98  std::string t = s;
99  t.insert(t.begin(), n - t.length(), c);
100  return t;
101  }
102  else {
103  return s;
104  }
105 }
106 
107 namespace file {
108 #if !defined(DOXYGEN_SHOULD_SKIP_THIS)
109 namespace details {
110 
111 template <typename BaseTypeT, typename... PathRepresentationType>
112 std::string join_path_impl(BaseTypeT const& base,
113  PathRepresentationType&&... rest);
114 
115 // Base cases
116 inline std::string const& join_path_impl(std::string const& x) { return x; }
117 inline std::string join_path_impl(char const* const x)
118 {
119  return std::string(x);
120 }
121 
122 // Recursive cases
123 template <typename... PathRepresentationType>
124 std::string join_path_impl(std::string const& base,
125  PathRepresentationType&&... rest)
126 {
127  return base + "/" +
128  join_path_impl(std::forward<PathRepresentationType>(rest)...);
129 }
130 
131 template <typename BaseType, typename... PathRepresentationType>
132 std::string join_path_impl(BaseType const& base,
133  PathRepresentationType&&... rest)
134 {
135  return std::string(base) + "/" +
136  join_path_impl(std::forward<PathRepresentationType>(rest)...);
137 }
138 
139 } // namespace details
140 #endif // !defined(DOXYGEN_SHOULD_SKIP_THIS)
141 
146 template <typename... PathNameType>
147 std::string join_path(PathNameType&&... paths)
148 {
149  return details::join_path_impl(std::forward<PathNameType>(paths)...);
150 }
151 
157 std::string extract_parent_directory(const std::string& path);
158 
163 std::string extract_base_name(const std::string& path);
164 
166 bool file_exists(const std::string& path);
167 
169 bool directory_exists(const std::string& path);
170 
177 void make_directory(const std::string& path);
178 
179 void remove_multiple_slashes(std::string& str);
180 
181 } // namespace file
182 
183 } // namespace lbann
184 
185 #endif // LBANN_UTILS_FILE_HPP_INCLUDED
bool check_if_file_exists(const std::string &filename)
std::vector< int > get_tokens(std::string str, const std::vector< char > delims)
std::basic_string< T > pad(const std::basic_string< T > &s, typename std::basic_string< T >::size_type n, T c)
Definition: file_utils.hpp:93
std::string modify_file_name(const std::string file_name, const std::string tag, const std::string new_ext="")
static std::string preferred()
Definition: file_utils.hpp:44
bool check_if_dir_exists(const std::string &dirname)
std::string get_ext_name(const std::string file_name)
static const std::string characters
Definition: file_utils.hpp:43
bool load_file(const std::string filename, std::vector< char > &buf, bool append=false)
void make_directory(const std::string &path)
Create directory if needed.
std::string join_path(PathNameType &&... paths)
Concatenate all paths, injecting path separators between each argument.
Definition: file_utils.hpp:147
std::string extract_parent_directory(const std::string &path)
Wrapper around dirname.
std::string extract_base_name(const std::string &path)
Wrapper around basename.
bool directory_exists(const std::string &path)
Check if directory exists.
bool create_dir(const std::string output_dir)
std::string add_delimiter(const std::string dir)
static bool check(const char ch)
Definition: file_utils.hpp:45
bool operator()(const char ch) const
Definition: file_utils.hpp:49
bool file_exists(const std::string &path)
Check if file exists.
void remove_multiple_slashes(std::string &str)
void __swapEndianInt(unsigned int &ui)
Definition: file_utils.hpp:85
std::string get_basename_without_ext(const std::string file_name)
bool parse_path(const std::string &path, std::string &dir, std::string &basename)