1 /* Copyright 2020 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_PROFILER_UTILS_FILE_SYSTEM_UTILS_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_FILE_SYSTEM_UTILS_H_
18 
19 #include <initializer_list>
20 #include <string>
21 
22 #include "absl/strings/match.h"
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/strings/strip.h"
26 #include "tensorflow/core/platform/platform.h"
27 
28 #ifdef PLATFORM_WINDOWS
29 const absl::string_view kPathSep = "\\";
30 #else
31 const absl::string_view kPathSep = "/";
32 #endif
33 
34 namespace tensorflow {
35 namespace profiler {
36 
ProfilerJoinPathImpl(std::initializer_list<absl::string_view> paths)37 inline std::string ProfilerJoinPathImpl(
38     std::initializer_list<absl::string_view> paths) {
39   std::string result;
40   for (absl::string_view path : paths) {
41     if (path.empty()) continue;
42 
43     if (result.empty()) {
44       result = std::string(path);
45       continue;
46     }
47 
48     path = absl::StripPrefix(path, kPathSep);
49     if (absl::EndsWith(result, kPathSep)) {
50       absl::StrAppend(&result, path);
51     } else {
52       absl::StrAppend(&result, kPathSep, path);
53     }
54   }
55 
56   return result;
57 }
58 
59 // A local duplication of ::tensorflow::io::JoinPath that supports windows.
60 // TODO(b/150699701): revert to use ::tensorflow::io::JoinPath when fixed.
61 template <typename... T>
ProfilerJoinPath(const T &...args)62 std::string ProfilerJoinPath(const T&... args) {
63   return ProfilerJoinPathImpl({args...});
64 }
65 
66 }  // namespace profiler
67 }  // namespace tensorflow
68 
69 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_FILE_SYSTEM_UTILS_H_
70