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_TSL_PLATFORM_PATH_H_ |
17 | #define TENSORFLOW_TSL_PLATFORM_PATH_H_ |
18 | |
19 | #include <string> |
20 | |
21 | #include "tensorflow/tsl/platform/stringpiece.h" |
22 | #include "tensorflow/tsl/platform/types.h" |
23 | |
24 | namespace tsl { |
25 | namespace io { |
26 | namespace internal { |
27 | std::string JoinPathImpl(std::initializer_list<tsl::StringPiece> paths); |
28 | } |
29 | |
30 | // Utility routines for processing filenames |
31 | |
32 | #ifndef SWIG // variadic templates |
33 | // Join multiple paths together, without introducing unnecessary path |
34 | // separators. |
35 | // For example: |
36 | // |
37 | // Arguments | JoinPath |
38 | // ---------------------------+---------- |
39 | // '/foo', 'bar' | /foo/bar |
40 | // '/foo/', 'bar' | /foo/bar |
41 | // '/foo', '/bar' | /foo/bar |
42 | // |
43 | // Usage: |
44 | // string path = io::JoinPath("/mydir", filename); |
45 | // string path = io::JoinPath(FLAGS_test_srcdir, filename); |
46 | // string path = io::JoinPath("/full", "path", "to", "filename"); |
47 | template <typename... T> |
48 | std::string JoinPath(const T&... args) { |
49 | return internal::JoinPathImpl({args...}); |
50 | } |
51 | #endif /* SWIG */ |
52 | |
53 | // Return true if path is absolute. |
54 | bool IsAbsolutePath(tsl::StringPiece path); |
55 | |
56 | // Returns the part of the path before the final "/". If there is a single |
57 | // leading "/" in the path, the result will be the leading "/". If there is |
58 | // no "/" in the path, the result is the empty prefix of the input. |
59 | tsl::StringPiece Dirname(tsl::StringPiece path); |
60 | |
61 | // Returns the part of the path after the final "/". If there is no |
62 | // "/" in the path, the result is the same as the input. |
63 | tsl::StringPiece Basename(tsl::StringPiece path); |
64 | |
65 | // Returns the part of the basename of path after the final ".". If |
66 | // there is no "." in the basename, the result is empty. |
67 | tsl::StringPiece Extension(tsl::StringPiece path); |
68 | |
69 | // Returns the largest common subpath of `paths`. |
70 | // |
71 | // For example, for "/alpha/beta/gamma" and "/alpha/beta/ga" returns |
72 | // "/alpha/beta/". For "/alpha/beta/gamma" and "/alpha/beta/gamma" returns |
73 | // "/alpha/beta/". |
74 | // |
75 | // Does not perform any path normalization. |
76 | std::string CommonPathPrefix(absl::Span<std::string const> paths); |
77 | |
78 | // Collapse duplicate "/"s, resolve ".." and "." path elements, remove |
79 | // trailing "/". |
80 | // |
81 | // NOTE: This respects relative vs. absolute paths, but does not |
82 | // invoke any system calls (getcwd(2)) in order to resolve relative |
83 | // paths with respect to the actual working directory. That is, this is purely |
84 | // string manipulation, completely independent of process state. |
85 | std::string CleanPath(tsl::StringPiece path); |
86 | |
87 | // Populates the scheme, host, and path from a URI. scheme, host, and path are |
88 | // guaranteed by this function to point into the contents of uri, even if |
89 | // empty. |
90 | // |
91 | // Corner cases: |
92 | // - If the URI is invalid, scheme and host are set to empty strings and the |
93 | // passed string is assumed to be a path |
94 | // - If the URI omits the path (e.g. file://host), then the path is left empty. |
95 | void ParseURI(tsl::StringPiece uri, tsl::StringPiece* scheme, |
96 | tsl::StringPiece* host, tsl::StringPiece* path); |
97 | |
98 | // Creates a URI from a scheme, host, and path. If the scheme is empty, we just |
99 | // return the path. |
100 | std::string CreateURI(tsl::StringPiece scheme, tsl::StringPiece host, |
101 | tsl::StringPiece path); |
102 | |
103 | // Creates a temporary file name with an extension. |
104 | std::string GetTempFilename(const std::string& extension); |
105 | |
106 | // Reads the TEST_UNDECLARED_OUTPUTS_DIR environment variable, and if set |
107 | // assigns `dir` to the value. `dir` is not modified if the environment variable |
108 | // is unset. Returns true if the environment variable is set, otherwise false. |
109 | // Passing `dir` as nullptr, will just probe for the environment variable. |
110 | // |
111 | // Note: This function obviates the need to deal with Bazel's odd path decisions |
112 | // on Windows, and should be preferred over a simple `getenv`. |
113 | bool GetTestUndeclaredOutputsDir(std::string* dir); |
114 | |
115 | } // namespace io |
116 | } // namespace tsl |
117 | |
118 | #endif // TENSORFLOW_TSL_PLATFORM_PATH_H_ |
119 | |