blob: dba33eb7f1edc32f2861bc9fa267d04e47a75423 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Venture863b9242018-03-08 08:29:23 -08003
James Feist0c8223b2019-05-08 15:33:33 -07004#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005
Patrick Venture005d4622019-03-08 13:03:03 -08006#include <filesystem>
Patrick Venture863b9242018-03-08 08:29:23 -08007#include <string>
8
Patrick Venturea0764872020-08-08 07:48:43 -07009namespace pid_control
10{
11
Patrick Venture863b9242018-03-08 08:29:23 -080012/*
Manojkiran Eda7ca88872024-06-17 11:55:48 +053013 * Replace "**" in the provided path string with an appropriate concrete path
Zev Weiss796f06d2023-06-29 20:11:39 -070014 * component (the first directory entry found, on the assumption that there
15 * will only be a single candidate).
Patrick Venture863b9242018-03-08 08:29:23 -080016 */
17
Patrick Venture005d4622019-03-08 13:03:03 -080018namespace fs = std::filesystem;
Patrick Venture863b9242018-03-08 08:29:23 -080019
Patrick Venture863b9242018-03-08 08:29:23 -080020std::string FixupPath(std::string original)
21{
Zev Weiss796f06d2023-06-29 20:11:39 -070022 std::string::size_type n;
Patrick Venture863b9242018-03-08 08:29:23 -080023
24 /* TODO: Consider the merits of using regex for this. */
25 n = original.find("**");
Patrick Venture863b9242018-03-08 08:29:23 -080026
Zev Weiss796f06d2023-06-29 20:11:39 -070027 if (n != std::string::npos)
Patrick Venture863b9242018-03-08 08:29:23 -080028 {
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 Tanousd2768c52025-06-26 11:42:57 -070048
49 /* It'll throw an exception when we use it if it's still bad. */
50 return original;
Patrick Venture863b9242018-03-08 08:29:23 -080051}
Patrick Venturea0764872020-08-08 07:48:43 -070052
53} // namespace pid_control