• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
17 #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
18 
19 #include "absl/base/config.h"
20 #include "absl/strings/string_view.h"
21 
22 namespace absl {
23 ABSL_NAMESPACE_BEGIN
24 namespace flags_internal {
25 
26 // A portable interface that returns the basename of the filename passed as an
27 // argument. It is similar to basename(3)
28 // <https://linux.die.net/man/3/basename>.
29 // For example:
30 //     flags_internal::Basename("a/b/prog/file.cc")
31 // returns "file.cc"
32 //     flags_internal::Basename("file.cc")
33 // returns "file.cc"
Basename(absl::string_view filename)34 inline absl::string_view Basename(absl::string_view filename) {
35   auto last_slash_pos = filename.find_last_of("/\\");
36 
37   return last_slash_pos == absl::string_view::npos
38              ? filename
39              : filename.substr(last_slash_pos + 1);
40 }
41 
42 // A portable interface that returns the directory name of the filename
43 // passed as an argument, including the trailing slash.
44 // Returns the empty string if a slash is not found in the input file name.
45 // For example:
46 //      flags_internal::Package("a/b/prog/file.cc")
47 // returns "a/b/prog/"
48 //      flags_internal::Package("file.cc")
49 // returns ""
Package(absl::string_view filename)50 inline absl::string_view Package(absl::string_view filename) {
51   auto last_slash_pos = filename.find_last_of("/\\");
52 
53   return last_slash_pos == absl::string_view::npos
54              ? absl::string_view()
55              : filename.substr(0, last_slash_pos + 1);
56 }
57 
58 }  // namespace flags_internal
59 ABSL_NAMESPACE_END
60 }  // namespace absl
61 
62 #endif  // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
63