Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1
9
10/*
11Copyright (c) 2026 MrXie1109
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19
20The above copyright notice and this permission notice shall be included in all
21copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29SOFTWARE.
30*/
31
32#pragma once
33#include <iostream>
34#include <fstream>
35#include <string>
36#include <vector>
37#include <iterator>
38#include <type_traits>
39#include "csexc.h"
40#include "strpp.h"
41
42namespace console
43{
50 class Path
51 {
52 std::string path;
53
54 public:
56 using Bytes = std::vector<unsigned char>;
57
63 Path(const std::string &str) : path(str)
64 {
65#ifdef _WIN32
66 for (char &ch : path)
67 {
68 if (ch == '/')
69 ch = '\\';
70 }
71#endif
72 }
73
81 friend Path operator/(const Path &p1, const Path &p2)
82 {
83 return p1.path + '/' + p2.path;
84 }
85
91 std::string read_text()
92 {
93 std::ifstream fin(path);
94 if (!fin.is_open())
95 throw file_error("Cannot Open File \"" + path + '"');
96 std::string text{
97 std::istreambuf_iterator<char>(fin),
98 std::istreambuf_iterator<char>()};
99 if (!fin.good())
100 throw file_error("The Stream of \"" + path + "\" is Not Good");
101 return text;
102 }
103
110 {
111 std::ifstream fin(path, std::ios::binary);
112 if (!fin.is_open())
113 throw file_error("Cannot Open File \"" + path + '"');
114 Bytes bytes{
115 std::istreambuf_iterator<char>(fin),
116 std::istreambuf_iterator<char>()};
117 if (!fin.good())
118 throw file_error("The Stream of \"" + path + "\" is Not Good");
119 return bytes;
120 }
121
127 std::vector<std::string> read_lines()
128 {
129 return split(read_text(), "\n");
130 }
131
139 template <class T>
141 {
142 static_assert(std::is_trivially_copyable<T>::value,
143 "This Type is Not POD Type!");
144 std::ifstream fin(path, std::ios::binary);
145 if (!fin.is_open())
146 throw file_error("Cannot Open File \"" + path + '"');
147 T data;
148 fin.read((char *)(&data), sizeof(data));
149 if (!fin.good())
150 throw file_error("The Stream of \"" + path + "\" is Not Good");
151 return data;
152 }
153
161 template <class T>
163 {
164 std::ifstream fin(path, std::ios::binary);
165 if (!fin.is_open())
166 throw file_error("Cannot Open File \"" + path + '"');
167 T data;
168 fin.read((char *)(&data), sizeof(data));
169 if (!fin.good())
170 throw file_error("The Stream of \"" + path + "\" is Not Good");
171 return data;
172 }
173
179 void write_text(const std::string &text)
180 {
181 std::ofstream fout(path);
182 if (!fout.is_open())
183 throw file_error("Cannot Open File \"" + path + '"');
184 fout << text;
185 if (!fout.good())
186 throw file_error("The Stream of \"" + path + "\" is Not Good");
187 }
188
194 void write_binary(const Bytes &bts)
195 {
196 std::ofstream fout(path, std::ios::binary);
197 if (!fout.is_open())
198 throw file_error("Cannot Open File \"" + path + '"');
199 fout.write((const char *)(bts.data()), bts.size());
200 if (!fout.good())
201 throw file_error("The Stream of \"" + path + "\" is Not Good");
202 }
203
210 void write_lines(const std::vector<std::string> &lines)
211 {
212 std::ofstream fout(path, std::ios::binary);
213 if (lines.empty())
214 return;
215 if (!fout.is_open())
216 throw file_error("Cannot Open File \"" + path + '"');
217 auto it = lines.begin();
218 fout << *it;
219 while (++it != lines.end())
220 fout << '\n'
221 << *it;
222 if (!fout.good())
223 throw file_error("The Stream of \"" + path + "\" is Not Good");
224 }
225
233 template <class T>
234 void write_POD(const T &data)
235 {
236 static_assert(std::is_trivially_copyable<T>::value,
237 "This Type is Not POD Type!");
238 std::ofstream fout(path, std::ios::binary);
239 if (!fout.is_open())
240 throw file_error("Cannot Open File \"" + path + '"');
241 fout.write((const char *)(&data), sizeof(data));
242 if (!fout.good())
243 throw file_error("The Stream of \"" + path + "\" is Not Good");
244 }
245
253 template <class T>
254 void unsafe_write_POD(const T &data)
255 {
256 std::ofstream fout(path, std::ios::binary);
257 if (!fout.is_open())
258 throw file_error("Cannot Open File \"" + path + '"');
259 fout.write((const char *)(&data), sizeof(data));
260 if (!fout.good())
261 throw file_error("The Stream of \"" + path + "\" is Not Good");
262 }
263
268 bool exists()
269 {
270 return std::ifstream{path}.is_open();
271 }
272
277 void touch()
278 {
279 std::ofstream{path};
280 }
281
286 void ensure()
287 {
288 std::ofstream{path, std::ios::app};
289 }
290 };
291}
T unsafe_read_POD()
从二进制文件读取一个 POD 类型对象(不安全版本)。
Definition file.h:162
std::vector< std::string > read_lines()
按行读取文本文件,返回每行字符串的 vector。
Definition file.h:127
void write_text(const std::string &text)
以文本模式写入字符串到文件(覆盖模式)。
Definition file.h:179
void write_lines(const std::vector< std::string > &lines)
将多行字符串写入文件,每行之间用换行符分隔。
Definition file.h:210
Path(const std::string &str)
从字符串构造 Path 对象。
Definition file.h:63
void touch()
创建空文件(若已存在则更新访问和修改时间)。
Definition file.h:277
void ensure()
确保文件存在,若不存在则创建空文件。
Definition file.h:286
void write_POD(const T &data)
写入一个 POD 类型对象到二进制文件(类型安全版本)。
Definition file.h:234
bool exists()
检查文件是否存在。
Definition file.h:268
std::vector< unsigned char > Bytes
字节类型别名,表示二进制数据的容器(unsigned char 的 vector)。
Definition file.h:56
T read_POD()
从二进制文件读取一个 POD 类型对象(类型安全版本)。
Definition file.h:140
std::string read_text()
以文本模式读取文件全部内容。
Definition file.h:91
void unsafe_write_POD(const T &data)
写入一个对象到二进制文件(不安全版本)。
Definition file.h:254
Bytes read_binary()
以二进制模式读取文件全部内容。
Definition file.h:109
friend Path operator/(const Path &p1, const Path &p2)
路径拼接运算符。
Definition file.h:81
void write_binary(const Bytes &bts)
以二进制模式写入字节数据到文件(覆盖模式)。
Definition file.h:194
表示文件操作错误,如打开失败、读取失败等。
Definition csexc.h:89
定义 console 库使用的自定义异常类层次结构。
std::vector< std::string > split(std::string text, const std::string &sep=" ")
以分隔符分割字符串(类似 Python 的 split,默认按空格分割)。
Definition strpp.h:237
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:72
提供字符串处理工具函数和格式化类。