• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_PLATFORM_PATH_H_
17 #define TENSORFLOW_CORE_PLATFORM_PATH_H_
18 
19 #include "tensorflow/core/platform/stringpiece.h"
20 #include "tensorflow/core/platform/types.h"
21 
22 namespace tensorflow {
23 namespace io {
24 namespace internal {
25 std::string JoinPathImpl(std::initializer_list<tensorflow::StringPiece> paths);
26 }
27 
28 // Utility routines for processing filenames
29 
30 #ifndef SWIG  // variadic templates
31 // Join multiple paths together, without introducing unnecessary path
32 // separators.
33 // For example:
34 //
35 //  Arguments                  | JoinPath
36 //  ---------------------------+----------
37 //  '/foo', 'bar'              | /foo/bar
38 //  '/foo/', 'bar'             | /foo/bar
39 //  '/foo', '/bar'             | /foo/bar
40 //
41 // Usage:
42 // string path = io::JoinPath("/mydir", filename);
43 // string path = io::JoinPath(FLAGS_test_srcdir, filename);
44 // string path = io::JoinPath("/full", "path", "to", "filename");
45 template <typename... T>
JoinPath(const T &...args)46 std::string JoinPath(const T&... args) {
47   return internal::JoinPathImpl({args...});
48 }
49 #endif /* SWIG */
50 
51 // Return true if path is absolute.
52 bool IsAbsolutePath(tensorflow::StringPiece path);
53 
54 // Returns the part of the path before the final "/".  If there is a single
55 // leading "/" in the path, the result will be the leading "/".  If there is
56 // no "/" in the path, the result is the empty prefix of the input.
57 tensorflow::StringPiece Dirname(tensorflow::StringPiece path);
58 
59 // Returns the part of the path after the final "/".  If there is no
60 // "/" in the path, the result is the same as the input.
61 tensorflow::StringPiece Basename(tensorflow::StringPiece path);
62 
63 // Returns the part of the basename of path after the final ".".  If
64 // there is no "." in the basename, the result is empty.
65 tensorflow::StringPiece Extension(tensorflow::StringPiece path);
66 
67 // Returns the largest common subpath of `paths`.
68 //
69 // For example, for "/alpha/beta/gamma" and "/alpha/beta/ga" returns
70 // "/alpha/beta/". For "/alpha/beta/gamma" and "/alpha/beta/gamma" returns
71 // "/alpha/beta/".
72 //
73 // Does not perform any path normalization.
74 std::string CommonPathPrefix(absl::Span<std::string const> paths);
75 
76 // Collapse duplicate "/"s, resolve ".." and "." path elements, remove
77 // trailing "/".
78 //
79 // NOTE: This respects relative vs. absolute paths, but does not
80 // invoke any system calls (getcwd(2)) in order to resolve relative
81 // paths with respect to the actual working directory.  That is, this is purely
82 // string manipulation, completely independent of process state.
83 std::string CleanPath(tensorflow::StringPiece path);
84 
85 // Populates the scheme, host, and path from a URI. scheme, host, and path are
86 // guaranteed by this function to point into the contents of uri, even if
87 // empty.
88 //
89 // Corner cases:
90 // - If the URI is invalid, scheme and host are set to empty strings and the
91 //   passed string is assumed to be a path
92 // - If the URI omits the path (e.g. file://host), then the path is left empty.
93 void ParseURI(tensorflow::StringPiece uri, tensorflow::StringPiece* scheme,
94               tensorflow::StringPiece* host, tensorflow::StringPiece* path);
95 
96 // Creates a URI from a scheme, host, and path. If the scheme is empty, we just
97 // return the path.
98 std::string CreateURI(tensorflow::StringPiece scheme,
99                       tensorflow::StringPiece host,
100                       tensorflow::StringPiece path);
101 
102 // Creates a temporary file name with an extension.
103 std::string GetTempFilename(const std::string& extension);
104 
105 // Reads the TEST_UNDECLARED_OUTPUTS_DIR environment variable, and if set
106 // assigns `dir` to the value. `dir` is not modified if the environment variable
107 // is unset. Returns true if the environment variable is set, otherwise false.
108 // Passing `dir` as nullptr, will just probe for the environment variable.
109 //
110 // Note: This function obviates the need to deal with Bazel's odd path decisions
111 // on Windows, and should be preferred over a simple `getenv`.
112 bool GetTestUndeclaredOutputsDir(std::string* dir);
113 
114 }  // namespace io
115 }  // namespace tensorflow
116 
117 #endif  // TENSORFLOW_CORE_PLATFORM_PATH_H_
118