-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkstd_textfile.hpp
205 lines (178 loc) · 4.81 KB
/
dkstd_textfile.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// author: Khiêm Đoàn Hoà (KhiemDH)
// github: https://github.com/khiemdoan/dkstd
// created: 2016-07-06
// modified: 2019-08-16
#pragma once
#include "fstream"
#include "vector"
#include "dkstd_string.hpp"
namespace dkstd
{
// textfile is used to read and write file by utf-8
// KhiemDH - 2016-07-06
class textfile
{
public:
textfile() {};
textfile(std::string sFilePath);
textfile(std::wstring sFilePath);
textfile(std::string sFilePath, std::ios::openmode mode);
textfile(std::wstring sFilePath, std::ios::openmode mode);
~textfile();
void open(std::string sFilePath);
void open(std::wstring sFilePath);
void open(std::string sFilePath, std::ios::openmode mode);
void open(std::wstring sFilePath, std::ios::openmode mode);
void close();
template<typename ...Args>
bool write_line(std::string sContent);
template<typename ...Args>
bool write_line(std::wstring sContent);
std::vector<std::wstring> get_all_lines();
void clear();
private:
std::fstream m_file;
std::ios::openmode m_mode = std::fstream::in | std::fstream::out | std::fstream::app;
#ifdef _WIN32
std::wstring m_sFilePath;
#else
std::string m_sFilePath;
#endif
};
}
// constructor with file path
// KhiemDH - 2016-07-06
inline dkstd::textfile::textfile(std::string sFilePath)
{
this->open(sFilePath);
}
// constructor with file path
// KhiemDH - 2016-07-06
inline dkstd::textfile::textfile(std::wstring sFilePath)
{
this->open(sFilePath);
}
// constructor with file path and open mode
// KhiemDH - 2016-09-24
inline dkstd::textfile::textfile(std::string sFilePath, std::ios::openmode mode)
{
this->open(sFilePath, mode);
}
// constructor with file path and open mode
// KhiemDH - 2016-09-24
inline dkstd::textfile::textfile(std::wstring sFilePath, std::ios::openmode mode)
{
this->open(sFilePath, mode);
}
// destructor
// KhiemDH - 2016-07-06
inline dkstd::textfile::~textfile()
{
this->close();
}
// open file
// KhiemDH - 2016-09-24
inline void dkstd::textfile::open(std::string sFilePath)
{
this->open(sFilePath, std::fstream::in | std::fstream::out | std::fstream::app);
}
// open file
// KhiemDH - 2016-09-24
inline void dkstd::textfile::open(std::wstring sFilePath)
{
this->open(sFilePath, std::fstream::in | std::fstream::out | std::fstream::app);
}
// open file with custom mode
// KhiemDH - 2016-09-24
inline void dkstd::textfile::open(std::string sFilePath, std::ios::openmode mode)
{
#ifdef _WIN32
this->open(dkstd::s2ws(sFilePath), mode);
#else
this->close();
m_file.open(sFilePath, mode);
m_sFilePath = sFilePath;
m_mode = mode;
#endif
}
// open file with custom mode
// KhiemDH - 2016-09-24
inline void dkstd::textfile::open(std::wstring sFilePath, std::ios::openmode mode)
{
#ifdef _WIN32
this->close();
m_file.open(sFilePath, mode);
m_sFilePath = sFilePath;
m_mode = mode;
#else
this->open(dkstd::ws2s(sFilePath), mode);
#endif
}
// close file
// KhiemDH - 2016-08-15
inline void dkstd::textfile::close()
{
if (m_file.is_open())
{
m_file.close();
}
}
// write a line to end file
// input: UTF-8 string
// KhiemDH - 2019-08-16
template<typename ...Args>
inline bool dkstd::textfile::write_line(std::string sContent)
{
if (!m_file.good())
return false;
m_file.seekp(0, std::fstream::end);
if (m_file.tellg() != std::streampos(0))
{
sContent = "\n" + sContent;
}
m_file << sContent;
m_file.flush();
m_file.clear(); // clear the error flag
return true;
}
// write a line to end file
// input: Unicode string
// KhiemDH - 2017-10-16
template<typename ...Args>
inline bool dkstd::textfile::write_line(std::wstring sContent)
{
return this->write_line(dkstd::ws2s(sContent));
}
// read file to std::vector
// KhiemDH - 2019-08-16
inline std::vector<std::wstring> dkstd::textfile::get_all_lines()
{
if (!m_file.good())
return std::vector<std::wstring>();
std::vector<std::wstring> vectorContents;
m_file.seekg(0, std::fstream::end);
if (m_file.tellg() > std::streampos(0))
{
m_file.seekg(0, std::fstream::beg);
while (!m_file.eof())
{
std::string sLine;
std::getline(m_file, sLine);
if (sLine.length() > 0 && sLine.back() == '\r')
{
sLine.pop_back();
}
vectorContents.emplace_back(dkstd::s2ws(sLine));
}
}
m_file.clear();
return vectorContents;
}
// clear text in file
inline void dkstd::textfile::clear()
{
this->close();
m_file.open(m_sFilePath, std::fstream::out | std::fstream::trunc);
this->close();
this->open(m_sFilePath, m_mode);
}