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/match.h" 21 #include "absl/strings/string_view.h" 22 23 namespace absl { 24 ABSL_NAMESPACE_BEGIN 25 namespace flags_internal { 26 27 // A portable interface that returns the basename of the filename passed as an 28 // argument. It is similar to basename(3) 29 // <https://linux.die.net/man/3/basename>. 30 // For example: 31 // flags_internal::Basename("a/b/prog/file.cc") 32 // returns "file.cc" 33 // flags_internal::Basename("file.cc") 34 // returns "file.cc" Basename(absl::string_view filename)35inline absl::string_view Basename(absl::string_view filename) { 36 auto last_slash_pos = filename.find_last_of("/\\"); 37 38 return last_slash_pos == absl::string_view::npos 39 ? filename 40 : filename.substr(last_slash_pos + 1); 41 } 42 43 // A portable interface that returns the directory name of the filename 44 // passed as an argument, including the trailing slash. 45 // Returns the empty string if a slash is not found in the input file name. 46 // For example: 47 // flags_internal::Package("a/b/prog/file.cc") 48 // returns "a/b/prog/" 49 // flags_internal::Package("file.cc") 50 // returns "" Package(absl::string_view filename)51inline absl::string_view Package(absl::string_view filename) { 52 auto last_slash_pos = filename.find_last_of("/\\"); 53 54 return last_slash_pos == absl::string_view::npos 55 ? absl::string_view() 56 : filename.substr(0, last_slash_pos + 1); 57 } 58 59 } // namespace flags_internal 60 ABSL_NAMESPACE_END 61 } // namespace absl 62 63 #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ 64