-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.cpp
103 lines (87 loc) · 1.62 KB
/
fs.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "common.h"
#include <stdio.h>
bool fs::exists(string path)
{
std::ifstream fin;
fin.open(path);
if (fin.is_open()) {
fin.close();
return true;
}
return false;
}
/* --------- FILE CLASS ---------*/
fs::file *fs::open(std::string path, int flags)
{
file *_file = new file();
_file->getStream()->open(path, (std::ios_base::openmode)flags);
return _file;
}
void fs::file::close()
{
this->_stream.close();
delete this;
}
string fs::file::read()
{
string data;
string line;
while (std::getline(this->_stream, line)) {
data += line;
data += "\n";
}
if(data.size())
data.resize(data.size()-1);
return data;
}
void fs::file::readBinary(char *data, size_t size)
{
this->_stream.read(data, size);
}
fstream * fs::file::getStream()
{
return &this->_stream;
}
void fs::file::write(string data)
{
this->_stream << data;
}
void fs::file::writeBinary(const char *data, size_t size)
{
this->_stream.write(data, size);
}
/* --------- /FILE CLASS/ ---------*/
void fs::writeData(string path, string data)
{
if (fs::file *file = fs::open(path, FS_WRITE)) {
file->write(data);
file->close();
}
}
void fs::safeWriteData(string path, string data)
{
fs::writeData(path + ".safe", data);
remove(path.c_str());
fs::rename(path + ".safe", path);
}
string fs::readData(string path)
{
string data;
fs::file *in = fs::open(path);
data = in->read();
in->close();
return data;
}
void fs::rename(string oldname, string newname)
{
std::rename(oldname.c_str(), newname.c_str());
}
/* CLASS EXCEPTION */
fs::exception::exception(string what)
{
this->what_str = what;
}
string fs::exception::what()
{
return this->what_str;
}