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 17 #include <experimental/filesystem> 18 #include <iostream> 19 #include <string> 20 21 22 #include "sysfs/util.hpp" 23 24 /* 25 * There are two basic paths I want to support: 26 * 1. /sys/class/hwmon/hwmon0/pwm1 27 * 2. /sys/devices/platform/ahb/1e786000.pwm-tacho-controller/hwmon/<asterisk asterisk>/pwm1 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/"; 34 namespace fs = std::experimental::filesystem; 35 36 37 std::string FixupPath(std::string original) 38 { 39 std::string::size_type n, x; 40 41 /* TODO: Consider the merits of using regex for this. */ 42 n = original.find("**"); 43 x = original.find(platform); 44 45 if ((n != std::string::npos) && (x != std::string::npos)) 46 { 47 /* This path has some missing pieces and we support it. */ 48 std::string base = original.substr(0, n); 49 std::string fldr; 50 std::string f = original.substr(n + 2, original.size() - (n + 2)); 51 52 /* Equivalent to glob and grab 0th entry. */ 53 for (const auto& folder : fs::directory_iterator(base)) 54 { 55 fldr = folder.path(); 56 break; 57 } 58 59 if (!fldr.length()) 60 { 61 return original; 62 } 63 64 return fldr + f; 65 } 66 else 67 { 68 /* It'll throw an exception when we use it if it's still bad. */ 69 return original; 70 } 71 } 72