ファイル関係関数を消した

諏訪子 <suwako@076.moe>、2026-07-05 04:12
コミット 74f040d88c983759e92634996ac2769ebaa4ffba ・親 1a069d7 ・ファイルを見る

7個のファイル、+101 -103

変更 CHANGELOG.md
@@ -1,5 +1,6 @@
11 # 1.0.1 (2026年07月05日)
22 * `serialize()`関数を終わらせる
3+* ファイル関係関数を消した
34
45 # 1.0.0 (2026年06月30日)
56 * 最初リリース
@@ -175,7 +175,6 @@
175175
176176 public:
177177 string serialize(const SerializeOptions &opts = {}) const;
178- bool serialize_to_file(const string &path, const SerializeOptions &opts = {}) const;
179178
180179 private:
181180 static string make_indent(size_t depth, size_t width);
@@ -193,7 +192,6 @@
193192 Parser(std::string_view text) : m_Text(text), m_Pos(0) {}
194193
195194 ParseResult parse();
196- static ParseResult parse_from_file(const string &path);
197195
198196 private:
199197 std::string_view m_Text;
削除 src/file.cc
@@ -1,74 +0,0 @@
1-/*
2-# 076 Free License
3-
4-Copyright (c) テクニカル諏訪子
5-
6-Permission is hereby granted to any person obtaining a copy of the software
7-C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8-sublicense, and/or sell copies of the Software, subject to the following conditions:
9-
10- 1. **Origin Attribution**:
11- - You must not misrepresent the origin of the Software; you must not claim
12- you created the original Software.
13- 2. **Notice Preservation**:
14- - This license and the above copyright notice must remain intact in all copies
15- of the source code.
16-
17-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19-PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21-CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22-OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-*/
24-#include <util/file.hh>
25-
26-#include <iostream>
27-#include <fstream>
28-
29-vector<u8> read_file(const string &filename, bool binary) {
30- std::ios::openmode flags = std::ios::in
31- | std::ios::ate
32- | std::ios::binary;
33-
34- std::ifstream file(filename, flags);
35- if (!file) {
36- throw std::runtime_error("ファイルを開くに失敗: " + filename);
37- }
38-
39- auto size = file.tellg();
40- file.clear();
41- file.seekg(0, std::ios::beg);
42-
43- vector<u8> data(static_cast<size_t>(size));
44- if (!file.read(reinterpret_cast<char *>(data.data()), size)) {
45- throw std::runtime_error("ファイルの読み込みに失敗。");
46- }
47-
48- file.close();
49-
50- return data;
51-}
52-
53-bool write_file(const string &filename, vector<string> contents, bool binary) {
54- std::ios::openmode flags;
55- if (binary) {
56- flags = std::ios::binary | std::ios::ate;
57- } else {
58- flags = std::ios::out | std::ios::trunc;
59- }
60-
61- std::ofstream file(filename, flags);
62- if (!file) {
63- throw std::runtime_error("ファイルを開くに失敗: " + filename);
64- }
65-
66- for (const auto &c : contents) {
67- file << c;
68- file << "\n";
69- }
70-
71- file.close();
72-
73- return true;
74-}
変更 src/json.cc
@@ -22,7 +22,6 @@
2222 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323 */
2424 #include <suwa/json.hh>
25-#include <util/file.hh>
2625 #include <charconv>
2726 #include <utility>
2827 #include <cassert>
@@ -235,12 +234,6 @@
235234 return res;
236235 }
237236
238- ParseResult Parser::parse_from_file(const string &path) {
239- vector<u8> data = read_file(path, false);
240- string content(data.begin(), data.end());
241- return Parser(content).parse();
242- }
243-
244237 string Value::serialize(const SerializeOptions &opts) const {
245238 return serialize_impl(opts, 0);
246239 }
@@ -315,10 +308,6 @@
315308 }
316309
317310 return "null";
318- }
319-
320- bool Value::serialize_to_file(const string &path, const SerializeOptions &opts) const {
321- return write_file(path, { serialize(opts) }, false);
322311 }
323312
324313 string Value::make_indent(size_t depth, size_t width) {
追加 tests/file.cc
@@ -0,0 +1,74 @@
1+/*
2+# 076 Free License
3+
4+Copyright (c) テクニカル諏訪子
5+
6+Permission is hereby granted to any person obtaining a copy of the software
7+C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8+sublicense, and/or sell copies of the Software, subject to the following conditions:
9+
10+ 1. **Origin Attribution**:
11+ - You must not misrepresent the origin of the Software; you must not claim
12+ you created the original Software.
13+ 2. **Notice Preservation**:
14+ - This license and the above copyright notice must remain intact in all copies
15+ of the source code.
16+
17+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22+OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+*/
24+#include "file.hh"
25+
26+#include <iostream>
27+#include <fstream>
28+
29+vector<u8> read_file(const string &filename, bool binary) {
30+ std::ios::openmode flags = std::ios::in
31+ | std::ios::ate
32+ | std::ios::binary;
33+
34+ std::ifstream file(filename, flags);
35+ if (!file) {
36+ throw std::runtime_error("ファイルを開くに失敗: " + filename);
37+ }
38+
39+ auto size = file.tellg();
40+ file.clear();
41+ file.seekg(0, std::ios::beg);
42+
43+ vector<u8> data(static_cast<size_t>(size));
44+ if (!file.read(reinterpret_cast<char *>(data.data()), size)) {
45+ throw std::runtime_error("ファイルの読み込みに失敗。");
46+ }
47+
48+ file.close();
49+
50+ return data;
51+}
52+
53+bool write_file(const string &filename, vector<string> contents, bool binary) {
54+ std::ios::openmode flags;
55+ if (binary) {
56+ flags = std::ios::binary | std::ios::ate;
57+ } else {
58+ flags = std::ios::out | std::ios::trunc;
59+ }
60+
61+ std::ofstream file(filename, flags);
62+ if (!file) {
63+ throw std::runtime_error("ファイルを開くに失敗: " + filename);
64+ }
65+
66+ for (const auto &c : contents) {
67+ file << c;
68+ file << "\n";
69+ }
70+
71+ file.close();
72+
73+ return true;
74+}
名前の変更 include/util/file.hh → tests/file.hh
変更 tests/main.cc
@@ -1,8 +1,18 @@
11 #include <cassert>
22 #include <iostream>
33 #include <suwa/json.hh>
4-#include <util/file.hh>
4+#include "file.hh"
55
6+json::ParseResult parse_from_file(const string &path) {
7+ vector<u8> data = read_file(path, false);
8+ string content(data.begin(), data.end());
9+ return json::Parser(content).parse();
10+}
11+
12+bool serialize_to_file(const string &path, const json::Value &value, const json::SerializeOptions &opts) {
13+ return write_file(path, { value.serialize(opts) }, false);
14+}
15+
616 int main(void) {
717 {
818 const string src = R"({
@@ -60,7 +70,7 @@
6070 }
6171
6272 {
63- auto res = json::Parser::parse_from_file("webfinger.json");
73+ auto res = parse_from_file("webfinger.json");
6474 assert(res.error == json::Error::None);
6575 assert(res.value);
6676 assert(res.value->is_object());
@@ -87,20 +97,20 @@
8797 assert(subject);
8898 assert(subject->as_string() == "acct:suwako@sns.076.moe");
8999
90- auto out = res.value->serialize_to_file("webfinger2.json");
91- auto out2 = res.value->serialize_to_file("webfinger3.json", { .pretty = true, .indent = 2 });
100+ auto out = serialize_to_file("webfinger2.json", &res.value, {});
101+ auto out2 = serialize_to_file("webfinger3.json", &res.value, { .pretty = true, .indent = 2 });
92102
93- auto res2 = json::Parser::parse_from_file("webfinger2.json");
103+ auto res2 = parse_from_file("webfinger2.json");
94104 assert(res2.error == json::Error::None);
95105
96- auto res3 = json::Parser::parse_from_file("webfinger3.json");
106+ auto res3 = parse_from_file("webfinger3.json");
97107 assert(res3.error == json::Error::None);
98108
99109 std::cout << "WebFinger OK" << std::endl;
100110 }
101111
102112 {
103- auto res = json::Parser::parse_from_file("gltf.json");
113+ auto res = parse_from_file("gltf.json");
104114 assert(res.error == json::Error::None);
105115 assert(res.value);
106116 assert(res.value->is_object());
@@ -200,20 +210,20 @@
200210 assert(obj.get("version")->as_string() == "2.0");
201211 }
202212
203- auto out = res.value->serialize_to_file("gltf2.json");
204- auto out2 = res.value->serialize_to_file("gltf3.json", {.pretty = true, .indent = 2});
213+ auto out = serialize_to_file("gltf2.json", &res.value, {});
214+ auto out2 = serialize_to_file("gltf3.json", &res.value, {.pretty = true, .indent = 2});
205215
206- auto res2 = json::Parser::parse_from_file("gltf2.json");
216+ auto res2 = parse_from_file("gltf2.json");
207217 assert(res2.error == json::Error::None);
208218
209- auto res3 = json::Parser::parse_from_file("gltf3.json");
219+ auto res3 = parse_from_file("gltf3.json");
210220 assert(res3.error == json::Error::None);
211221
212222 std::cout << "glTF OK" << std::endl;
213223 }
214224
215225 {
216- auto res = json::Parser::parse_from_file("activitypub.json");
226+ auto res = parse_from_file("activitypub.json");
217227 assert(res.error == json::Error::None);
218228 assert(res.value);
219229 assert(res.value->is_object());
@@ -255,13 +265,13 @@
255265 assert(pubkey.get("owner")->as_string() == "https://example.social/users/sam");
256266 assert(pubkey.get("publicKeyPem")->as_string() == "-----BEGIN PUBLIC KEY-----\n...\n----END PUBLIC KEY----");
257267
258- auto out = res.value->serialize_to_file("activitypub2.json");
259- auto out2 = res.value->serialize_to_file("activitypub3.json", { .pretty = true, .indent = 2 });
268+ auto out = serialize_to_file("activitypub2.json", &res.value, {});
269+ auto out2 = serialize_to_file("activitypub3.json", &res.value, { .pretty = true, .indent = 2 });
260270
261- auto res2 = json::Parser::parse_from_file("activitypub2.json");
271+ auto res2 = parse_from_file("activitypub2.json");
262272 assert(res2.error == json::Error::None);
263273
264- auto res3 = json::Parser::parse_from_file("activitypub3.json");
274+ auto res3 = parse_from_file("activitypub3.json");
265275 assert(res3.error == json::Error::None);
266276
267277 std::cout << "ActivityPub OK" << std::endl;