blob: 307e3abee8a3d562f01efbfadb21a67d48cee256 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001/**
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 Feist0c8223b2019-05-08 15:33:33 -070017#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
Patrick Venture005d4622019-03-08 13:03:03 -080019#include <filesystem>
Patrick Venture863b9242018-03-08 08:29:23 -080020#include <string>
21
Patrick Venturea0764872020-08-08 07:48:43 -070022namespace pid_control
23{
24
Patrick Venture863b9242018-03-08 08:29:23 -080025/*
Manojkiran Eda7ca88872024-06-17 11:55:48 +053026 * Replace "**" in the provided path string with an appropriate concrete path
Zev Weiss796f06d2023-06-29 20:11:39 -070027 * component (the first directory entry found, on the assumption that there
28 * will only be a single candidate).
Patrick Venture863b9242018-03-08 08:29:23 -080029 */
30
Patrick Venture005d4622019-03-08 13:03:03 -080031namespace fs = std::filesystem;
Patrick Venture863b9242018-03-08 08:29:23 -080032
Patrick Venture863b9242018-03-08 08:29:23 -080033std::string FixupPath(std::string original)
34{
Zev Weiss796f06d2023-06-29 20:11:39 -070035 std::string::size_type n;
Patrick Venture863b9242018-03-08 08:29:23 -080036
37 /* TODO: Consider the merits of using regex for this. */
38 n = original.find("**");
Patrick Venture863b9242018-03-08 08:29:23 -080039
Zev Weiss796f06d2023-06-29 20:11:39 -070040 if (n != std::string::npos)
Patrick Venture863b9242018-03-08 08:29:23 -080041 {
42 /* This path has some missing pieces and we support it. */
43 std::string base = original.substr(0, n);
44 std::string fldr;
45 std::string f = original.substr(n + 2, original.size() - (n + 2));
46
47 /* Equivalent to glob and grab 0th entry. */
48 for (const auto& folder : fs::directory_iterator(base))
49 {
50 fldr = folder.path();
51 break;
52 }
53
54 if (!fldr.length())
55 {
56 return original;
57 }
58
59 return fldr + f;
60 }
Ed Tanousd2768c52025-06-26 11:42:57 -070061
62 /* It'll throw an exception when we use it if it's still bad. */
63 return original;
Patrick Venture863b9242018-03-08 08:29:23 -080064}
Patrick Venturea0764872020-08-08 07:48:43 -070065
66} // namespace pid_control