| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | /** | 
|  | 2 | * Copyright 2017 Google Inc. | 
|  | 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 | *     http://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 |  | 
| James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 17 | #include "util.hpp" | 
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 |  | 
| Patrick Venture | 005d462 | 2019-03-08 13:03:03 -0800 | [diff] [blame] | 19 | #include <filesystem> | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 20 | #include <iostream> | 
|  | 21 | #include <string> | 
|  | 22 |  | 
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 23 | namespace pid_control | 
|  | 24 | { | 
|  | 25 |  | 
| 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 | * Replace "**" in the provided path string with an appopriate concrete path | 
|  | 28 | * component (the first directory entry found, on the assumption that there | 
|  | 29 | * will only be a single candidate). | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 30 | */ | 
|  | 31 |  | 
| Patrick Venture | 005d462 | 2019-03-08 13:03:03 -0800 | [diff] [blame] | 32 | namespace fs = std::filesystem; | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 33 |  | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 34 | std::string FixupPath(std::string original) | 
|  | 35 | { | 
| Zev Weiss | 796f06d | 2023-06-29 20:11:39 -0700 | [diff] [blame] | 36 | std::string::size_type n; | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | /* TODO: Consider the merits of using regex for this. */ | 
|  | 39 | n = original.find("**"); | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 40 |  | 
| Zev Weiss | 796f06d | 2023-06-29 20:11:39 -0700 | [diff] [blame] | 41 | if (n != std::string::npos) | 
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 42 | { | 
|  | 43 | /* This path has some missing pieces and we support it. */ | 
|  | 44 | std::string base = original.substr(0, n); | 
|  | 45 | std::string fldr; | 
|  | 46 | std::string f = original.substr(n + 2, original.size() - (n + 2)); | 
|  | 47 |  | 
|  | 48 | /* Equivalent to glob and grab 0th entry. */ | 
|  | 49 | for (const auto& folder : fs::directory_iterator(base)) | 
|  | 50 | { | 
|  | 51 | fldr = folder.path(); | 
|  | 52 | break; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | if (!fldr.length()) | 
|  | 56 | { | 
|  | 57 | return original; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | return fldr + f; | 
|  | 61 | } | 
|  | 62 | else | 
|  | 63 | { | 
|  | 64 | /* It'll throw an exception when we use it if it's still bad. */ | 
|  | 65 | return original; | 
|  | 66 | } | 
|  | 67 | } | 
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | } // namespace pid_control |