From 5d13ad11340a73214642245f4418aec9f197186d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 23 Sep 2018 21:32:37 +0200 Subject: Fix copy/paste of ports --- src/ClashAvoider.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/ClashAvoider.cpp') diff --git a/src/ClashAvoider.cpp b/src/ClashAvoider.cpp index fdbc45b4..dbcc1a7e 100644 --- a/src/ClashAvoider.cpp +++ b/src/ClashAvoider.cpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2015 David Robillard + Copyright 2007-2018 David Robillard Ingen is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free @@ -15,11 +15,14 @@ */ #include +#include #include #include #include #include +#include + #include "ingen/ClashAvoider.hpp" #include "ingen/Store.hpp" #include "ingen/paths.hpp" @@ -133,4 +136,46 @@ ClashAvoider::exists(const Raul::Path& path) const return _store.find(path) != _store.end(); } +static boost::optional +numeric_suffix_start(const std::string& str) +{ + if (!isdigit(str[str.length() - 1])) { + return {}; + } + + size_t i = str.length() - 1; + while (i > 0 && isdigit(str[i - 1])) { + --i; + } + + return i; +} + +std::string +ClashAvoider::adjust_name(const Raul::Path& old_path, + const Raul::Path& new_path, + std::string name) +{ + const auto name_suffix_start = numeric_suffix_start(name); + if (!name_suffix_start) { + return name; // No numeric suffix, just re-use old label + } + + const auto name_suffix = atoi(name.c_str() + *name_suffix_start); + const auto old_suffix_start = numeric_suffix_start(old_path); + const auto new_suffix_start = numeric_suffix_start(new_path); + if (old_suffix_start && new_suffix_start) { + // Add the offset applied to the symbol to the label suffix + const auto old_suffix = atoi(old_path.c_str() + *old_suffix_start); + const auto new_suffix = atoi(new_path.c_str() + *new_suffix_start); + const auto offset = new_suffix - old_suffix; + return (name.substr(0, *name_suffix_start) + + std::to_string(name_suffix + offset)); + } else { + // Add 1 to previous label suffix + return (name.substr(0, *name_suffix_start) + + std::to_string(name_suffix + 1)); + } +} + } // namespace ingen -- cgit v1.2.1