summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Klug <john.klug@multitech.com>2023-09-14 10:15:14 -0500
committerJohn Klug <john.klug@multitech.com>2023-09-14 10:15:14 -0500
commit117a407c31ab61ff3c01498eddc60ee87d5ea54b (patch)
tree615ec703995a97a304cda85d07a814cd339774e3
parent24df995dd3860fea4b5a77fe492de50550164fa2 (diff)
downloadmts-io-sysfs-117a407c31ab61ff3c01498eddc60ee87d5ea54b.tar.gz
mts-io-sysfs-117a407c31ab61ff3c01498eddc60ee87d5ea54b.tar.bz2
mts-io-sysfs-117a407c31ab61ff3c01498eddc60ee87d5ea54b.zip
Use stream.is_open to test if file open works and use strerror to print errno string
-rw-r--r--src/Device/Device.cpp44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/Device/Device.cpp b/src/Device/Device.cpp
index 5f0e281..1a2aca5 100644
--- a/src/Device/Device.cpp
+++ b/src/Device/Device.cpp
@@ -500,11 +500,10 @@ void Device::show(std::string name) {
if (code == 0) {
printf("%s", fileData.c_str());
exitHandler(0);
- } else {
- printError("cat: can't open %s%s: No such file or directory",
- SYSFS_PLATFORM, name.c_str());
- exitHandler(99);
}
+ printError("cat: can't open %s%s: %s",
+ SYSFS_PLATFORM, name.c_str(),strerror(errno));
+ exitHandler(99);
}
void Device::showTrigger(std::string name) {
@@ -514,11 +513,10 @@ void Device::showTrigger(std::string name) {
if (code == 0) {
printf("%s", fileData.c_str());
exitHandler(0);
- } else {
- printError("Can't not open %s%s/trigger': No such file or directory",
- LEDS_GPIO_DIR, name.c_str());
- exitHandler(99);
}
+ printError("Can't open %s%s/trigger: %s",
+ LEDS_GPIO_DIR, name.c_str(),strerror(errno));
+ exitHandler(99);
}
void Device::store(std::string name, std::string value) {
@@ -528,15 +526,19 @@ void Device::store(std::string name, std::string value) {
}
printDebug("Setting %s to %s", name.c_str(), value.c_str());
std::ofstream fileToWrite(SYSFS_PLATFORM + name);
- if (!fileToWrite.bad()) {
+ if (fileToWrite.is_open()) {
fileToWrite << value;
+ if (fileToWrite.bad()) {
+ printError("Can't write %s%s: %s", SYSFS_PLATFORM,
+ name.c_str(),strerror(errno));
+ exitHandler(98);
+ }
fileToWrite.close();
exitHandler(0);
- } else {
- printError("Can't open %s%s: No such file or directory", SYSFS_PLATFORM,
- name.c_str());
- exitHandler(99);
}
+ printError("Can't open %s%s: %s", SYSFS_PLATFORM,
+ name.c_str(),strerror(errno));
+ exitHandler(99);
}
void Device::storeTrigger(std::string name, std::string value) {
@@ -546,15 +548,19 @@ void Device::storeTrigger(std::string name, std::string value) {
}
printDebug("Setting %s to %s", name.c_str(), value.c_str());
std::ofstream fileToWrite(LEDS_GPIO_DIR + name + "/trigger");
- if (!fileToWrite.bad()) {
+ if (fileToWrite.is_open()) {
fileToWrite << value;
+ if(fileToWrite.bad()) {
+ printError("Can't write %s%s/trigger: %s",
+ LEDS_GPIO_DIR, name.c_str(), strerror(errno));
+ exitHandler(98);
+ }
fileToWrite.close();
exitHandler(0);
- } else {
- printError("Can't not open %s%s/trigger': No such file or directory",
- LEDS_GPIO_DIR, name.c_str());
- exitHandler(99);
}
+ printError("Can't open %s%s/trigger: %s",
+ LEDS_GPIO_DIR, name.c_str(), strerror(errno));
+ exitHandler(99);
}
void Device::writeJson() {
@@ -563,7 +569,7 @@ void Device::writeJson() {
deviceInfo.Accept(writer);
std::ofstream os(DEVICE_INFO_FILE);
if (!os) {
- printError("Can't write to %s", DEVICE_INFO_FILE);
+ printError("Can't write to %s: %s", DEVICE_INFO_FILE,strerror(errno));
exitHandler(99);
} else {
os << buffer.GetString();