| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 3 | |
| James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 4 | #include "util.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | |
| Patrick Venture | 005d462 | 2019-03-08 13:03:03 -0800 | [diff] [blame] | 6 | #include <filesystem> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 9 | namespace pid_control |
| 10 | { |
| 11 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 12 | /* |
| Manojkiran Eda | 7ca8887 | 2024-06-17 11:55:48 +0530 | [diff] [blame] | 13 | * Replace "**" in the provided path string with an appropriate concrete path |
| Zev Weiss | 796f06d | 2023-06-29 20:11:39 -0700 | [diff] [blame] | 14 | * component (the first directory entry found, on the assumption that there |
| 15 | * will only be a single candidate). |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
| Patrick Venture | 005d462 | 2019-03-08 13:03:03 -0800 | [diff] [blame] | 18 | namespace fs = std::filesystem; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 20 | std::string FixupPath(std::string original) |
| 21 | { |
| Zev Weiss | 796f06d | 2023-06-29 20:11:39 -0700 | [diff] [blame] | 22 | std::string::size_type n; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 23 | |
| 24 | /* TODO: Consider the merits of using regex for this. */ |
| 25 | n = original.find("**"); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 26 | |
| Zev Weiss | 796f06d | 2023-06-29 20:11:39 -0700 | [diff] [blame] | 27 | if (n != std::string::npos) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | { |
| 29 | /* This path has some missing pieces and we support it. */ |
| 30 | std::string base = original.substr(0, n); |
| 31 | std::string fldr; |
| 32 | std::string f = original.substr(n + 2, original.size() - (n + 2)); |
| 33 | |
| 34 | /* Equivalent to glob and grab 0th entry. */ |
| 35 | for (const auto& folder : fs::directory_iterator(base)) |
| 36 | { |
| 37 | fldr = folder.path(); |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | if (!fldr.length()) |
| 42 | { |
| 43 | return original; |
| 44 | } |
| 45 | |
| 46 | return fldr + f; |
| 47 | } |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 48 | |
| 49 | /* It'll throw an exception when we use it if it's still bad. */ |
| 50 | return original; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 51 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 52 | |
| 53 | } // namespace pid_control |