forked from leonmaxx/asus_fanmode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.hpp
86 lines (67 loc) · 1.43 KB
/
file.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* file.hpp
*
* Created on: Nov 12, 2019
* Author: Leonid Maksymchuk
*/
#ifndef FILE_HPP_
#define FILE_HPP_
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string>
class File {
public:
File();
File(const std::string& rsFileName, int nMode = O_RDONLY);
~File();
bool isValid() const;
bool open(const std::string& rsFileName, int nMode = O_RDONLY);
ssize_t read(void* pvPtr, size_t nSize);
ssize_t write(const void* pvPtr, size_t nSize);
size_t size() const;
int handle() const;
void close();
private:
int m_nFd;
};
inline File::File() {
m_nFd = -1;
}
inline File::File(const std::string& rsFileName, int nMode) {
open(rsFileName, nMode);
}
inline File::~File() {
close();
}
inline bool File::isValid() const {
return m_nFd >= 0;
}
inline bool File::open(const std::string& rsFileName, int nMode) {
m_nFd = ::open(rsFileName.c_str(), nMode);
return isValid();
}
inline ssize_t File::read(void* pvPtr, size_t nSize) {
return ::read(m_nFd, pvPtr, nSize);
}
inline ssize_t File::write(const void* pvPtr, size_t nSize) {
return ::write(m_nFd, pvPtr, nSize);
}
inline size_t File::size() const {
struct stat stStat;
if (isValid() && !fstat(m_nFd, &stStat)) {
return stStat.st_size;
}
return 0;
}
inline int File::handle() const {
return m_nFd;
}
inline void File::close() {
if (isValid()) {
::close(m_nFd);
m_nFd = -1;
}
}
#endif