00001 // 00002 // Copyright 2019 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 00016 #ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ 00017 #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ 00018 00019 #include "absl/strings/match.h" 00020 #include "absl/strings/string_view.h" 00021 00022 namespace absl { 00023 namespace flags_internal { 00024 00025 // A portable interface that returns the basename of the filename passed as an 00026 // argument. It is similar to basename(3) 00027 // <https://linux.die.net/man/3/basename>. 00028 // For example: 00029 // flags_internal::Basename("a/b/prog/file.cc") 00030 // returns "file.cc" 00031 // flags_internal::Basename("file.cc") 00032 // returns "file.cc" 00033 inline absl::string_view Basename(absl::string_view filename) { 00034 auto last_slash_pos = filename.find_last_of("/\\"); 00035 00036 return last_slash_pos == absl::string_view::npos 00037 ? filename 00038 : filename.substr(last_slash_pos + 1); 00039 } 00040 00041 // A portable interface that returns the directory name of the filename 00042 // passed as an argument, including the trailing slash. 00043 // Returns the empty string if a slash is not found in the input file name. 00044 // For example: 00045 // flags_internal::Package("a/b/prog/file.cc") 00046 // returns "a/b/prog/" 00047 // flags_internal::Package("file.cc") 00048 // returns "" 00049 inline absl::string_view Package(absl::string_view filename) { 00050 auto last_slash_pos = filename.find_last_of("/\\"); 00051 00052 return last_slash_pos == absl::string_view::npos 00053 ? absl::string_view() 00054 : filename.substr(0, last_slash_pos + 1); 00055 } 00056 00057 } // namespace flags_internal 00058 } // namespace absl 00059 00060 #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_