mirror of
https://github.com/chararomandroid/android_device_samsung_a20
synced 2026-08-24 04:45:32 -04:00
universal7885: hidl: Introduce simple fileio lib
This commit is contained in:
parent
2e0da038da
commit
840e252b72
3 changed files with 61 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
||||||
|
cc_library_shared {
|
||||||
|
name: "libfileio",
|
||||||
|
srcs: ["FileIO.cpp"],
|
||||||
|
include_dirs: ["include"],
|
||||||
|
export_include_dirs: ["include"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace FileIO {
|
||||||
|
int readline(const char *path);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void writeline(const char *path, const T data);
|
||||||
|
} // namespace FileIO
|
||||||
Loading…
Reference in a new issue