universal7885: hidl: Introduce simple fileio lib

This commit is contained in:
roynatech2544 2022-10-03 18:15:32 +09:00
commit 840e252b72
3 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,7 @@
cc_library_shared {
name: "libfileio",
srcs: ["FileIO.cpp"],
include_dirs: ["include"],
export_include_dirs: ["include"],
}

View file

@ -0,0 +1,48 @@
#define LOG_TAG "libFileIO"
#include <log/log.h>
#include <fstream>
#include <memory>
#include <string>
namespace FileIO {
constexpr const int EXIT_ERR = -1;
int readline(const char *path) {
std::ifstream file;
std::string value;
file.open(path);
ALOGD("%s: Opening %s", __func__, path);
if (file.is_open()) {
getline(file, value);
file.close();
} else {
ALOGE("%s: Failed to open %s", __func__, path);
return EXIT_ERR;
}
try {
return stoi(value);
} catch (std::invalid_argument const &ex) {
ALOGE("%s: stoi(): invalid argument: for %s", __func__, value);
} catch (std::out_of_range const &ex) {
ALOGE("%s: stoi(): out of range: for %s", __func__, value);
}
return EXIT_ERR;
}
template <typename T>
void writeline(const char *path, const T data) {
std::ofstream file;
file.open(path);
ALOGD("%s: Opening %s", __func__, path);
if (file.is_open()) {
file << data;
file.close();
return;
}
ALOGE("%s: Failed to open %s", __func__, path);
}
} // namespace FileIO

View file

@ -0,0 +1,6 @@
namespace FileIO {
int readline(const char *path);
template <typename T>
void writeline(const char *path, const T data);
} // namespace FileIO