diff --git a/src/option_parser.cpp b/src/option_parser.cpp index 0b9bb938..eb71ba14 100644 --- a/src/option_parser.cpp +++ b/src/option_parser.cpp @@ -133,7 +133,9 @@ set_fsname(struct fuse_args &args, { std::string fsname; - fsname = "-ofsname=" + str::join(config.srcmounts,':'); + fsname = + "-ofsname=" + + str::remove_common_prefix_and_join(config.srcmounts,':'); fuse_opt_insert_arg(&args,1,fsname.c_str()); } diff --git a/src/str.cpp b/src/str.cpp index 98329faa..69458d52 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -46,15 +46,69 @@ namespace str string join(const vector &vec, + const size_t substridx, const char sep) { if(vec.empty()) return string(); - string rv = vec[0]; + string rv = vec[0].substr(substridx); for(size_t i = 1; i < vec.size(); i++) - rv += sep + vec[i]; + rv += sep + vec[i].substr(substridx); return rv; } + + string + join(const vector &vec, + const char sep) + { + return str::join(vec,0,sep); + } + + size_t + longest_common_prefix_index(const vector &vec) + { + if(vec.empty()) + return string::npos; + + for(size_t n = 0; n < vec[0].size(); n++) + { + char chr = vec[0][n]; + for(size_t i = 1; i < vec.size(); i++) + { + if(n >= vec[i].size()) + return n; + if(vec[i][n] != chr) + return n; + } + } + + return string::npos; + } + + string + longest_common_prefix(const vector &vec) + { + size_t idx; + + idx = longest_common_prefix_index(vec); + if(idx != string::npos) + return vec[0].substr(0,idx); + + return string(); + } + + string + remove_common_prefix_and_join(const vector &vec, + const char sep) + { + size_t idx; + + idx = str::longest_common_prefix_index(vec); + if(idx == string::npos) + idx = 0; + + return str::join(vec,idx,sep); + } } diff --git a/src/str.hpp b/src/str.hpp index 36887af1..e768bdc7 100644 --- a/src/str.hpp +++ b/src/str.hpp @@ -34,5 +34,20 @@ namespace str std::string join(const std::vector &vec, + const size_t substridx, const char sep); + + std::string + join(const std::vector &vec, + const char sep); + + size_t + longest_common_prefix_index(const std::vector &vec); + + std::string + longest_common_prefix(const std::vector &vec); + + std::string + remove_common_prefix_and_join(const std::vector &vec, + const char sep); }