-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16097 from lrusak/sysfs-cleanup
SysfsUtils: cleanup and improve interface according to the documentation
- Loading branch information
Showing
15 changed files
with
147 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
xbmc/platform/linux/test platform/linux/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright (C) 2011-2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#include "SysfsPath.h" | ||
|
||
bool CSysfsPath::Exists() | ||
{ | ||
std::ifstream file(m_path); | ||
|
||
if (!file.is_open()) | ||
return false; | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2011-2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "utils/log.h" | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
class CSysfsPath | ||
{ | ||
public: | ||
CSysfsPath() = default; | ||
CSysfsPath(const std::string& path) : m_path(path) {} | ||
~CSysfsPath() = default; | ||
|
||
bool Exists(); | ||
|
||
template<typename T> | ||
T Get() | ||
{ | ||
std::ifstream file(m_path); | ||
|
||
T value; | ||
|
||
file >> value; | ||
|
||
if (file.bad()) | ||
{ | ||
CLog::LogF(LOGERROR, "error reading from '{}'", m_path); | ||
throw std::runtime_error("error reading from " + m_path); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private: | ||
std::string m_path; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
list(APPEND SOURCES TestSysfsPath.cpp) | ||
|
||
core_add_test_library(linux_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2005-2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#include "platform/linux/SysfsPath.h" | ||
|
||
#include <fstream> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
struct TestSysfsPath : public ::testing::Test | ||
{ | ||
~TestSysfsPath() { std::remove("/tmp/kodi-test"); } | ||
}; | ||
|
||
TEST_F(TestSysfsPath, SysfsPathTest) | ||
{ | ||
int temp{1234}; | ||
std::ofstream output{"/tmp/kodi-test"}; | ||
output << temp; | ||
output.close(); | ||
|
||
CSysfsPath path("/tmp/kodi-test"); | ||
ASSERT_TRUE(path.Exists()); | ||
ASSERT_TRUE(path.Get<int>() == 1234); | ||
ASSERT_TRUE(path.Get<float>() == 1234); | ||
ASSERT_TRUE(path.Get<double>() == 1234); | ||
ASSERT_TRUE(path.Get<uint64_t>() == 1234); | ||
ASSERT_TRUE(path.Get<uint16_t>() == 1234); | ||
ASSERT_TRUE(path.Get<unsigned int>() == 1234); | ||
ASSERT_TRUE(path.Get<unsigned long int>() == 1234); | ||
ASSERT_TRUE(path.Get<std::string>() == "1234"); | ||
|
||
CSysfsPath otherPath{"/thispathdoesnotexist"}; | ||
ASSERT_FALSE(otherPath.Exists()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.