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 | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 23 | /* |
| 24 | * There are two basic paths I want to support: |
| 25 | * 1. /sys/class/hwmon/hwmon0/pwm1 |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | * 2. /sys/devices/platform/ahb/1e786000.pwm-tacho-controller/hwmon/<asterisk |
| 27 | * asterisk>/pwm1 |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | * |
| 29 | * In this latter case, I want to fill in that gap. Assuming because it's this |
| 30 | * path that it'll only have one directory there. |
| 31 | */ |
| 32 | |
| 33 | static constexpr auto platform = "/sys/devices/platform/"; |
Patrick Venture | 005d462 | 2019-03-08 13:03:03 -0800 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 35 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 36 | std::string FixupPath(std::string original) |
| 37 | { |
| 38 | std::string::size_type n, x; |
| 39 | |
| 40 | /* TODO: Consider the merits of using regex for this. */ |
| 41 | n = original.find("**"); |
| 42 | x = original.find(platform); |
| 43 | |
| 44 | if ((n != std::string::npos) && (x != std::string::npos)) |
| 45 | { |
| 46 | /* This path has some missing pieces and we support it. */ |
| 47 | std::string base = original.substr(0, n); |
| 48 | std::string fldr; |
| 49 | std::string f = original.substr(n + 2, original.size() - (n + 2)); |
| 50 | |
| 51 | /* Equivalent to glob and grab 0th entry. */ |
| 52 | for (const auto& folder : fs::directory_iterator(base)) |
| 53 | { |
| 54 | fldr = folder.path(); |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | if (!fldr.length()) |
| 59 | { |
| 60 | return original; |
| 61 | } |
| 62 | |
| 63 | return fldr + f; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | /* It'll throw an exception when we use it if it's still bad. */ |
| 68 | return original; |
| 69 | } |
| 70 | } |