ファイル関係関数を消した
諏訪子 <suwako@076.moe>、2026-07-05 04:12
コミット 74f040d88c983759e92634996ac2769ebaa4ffba
・親 1a069d7
・ファイルを見る
7個のファイル、+101 -103
変更
CHANGELOG.md
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | # 1.0.1 (2026年07月05日) |
| 2 | 2 | * `serialize()`関数を終わらせる |
| 3 | +* ファイル関係関数を消した | |
| 3 | 4 | |
| 4 | 5 | # 1.0.0 (2026年06月30日) |
| 5 | 6 | * 最初リリース |
| @@ -175,7 +175,6 @@ | ||
| 175 | 175 | |
| 176 | 176 | public: |
| 177 | 177 | string serialize(const SerializeOptions &opts = {}) const; |
| 178 | - bool serialize_to_file(const string &path, const SerializeOptions &opts = {}) const; | |
| 179 | 178 | |
| 180 | 179 | private: |
| 181 | 180 | static string make_indent(size_t depth, size_t width); |
| @@ -193,7 +192,6 @@ | ||
| 193 | 192 | Parser(std::string_view text) : m_Text(text), m_Pos(0) {} |
| 194 | 193 | |
| 195 | 194 | ParseResult parse(); |
| 196 | - static ParseResult parse_from_file(const string &path); | |
| 197 | 195 | |
| 198 | 196 | private: |
| 199 | 197 | 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 @@ | ||
| 22 | 22 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | 23 | */ |
| 24 | 24 | #include <suwa/json.hh> |
| 25 | -#include <util/file.hh> | |
| 26 | 25 | #include <charconv> |
| 27 | 26 | #include <utility> |
| 28 | 27 | #include <cassert> |
| @@ -235,12 +234,6 @@ | ||
| 235 | 234 | return res; |
| 236 | 235 | } |
| 237 | 236 | |
| 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 | - | |
| 244 | 237 | string Value::serialize(const SerializeOptions &opts) const { |
| 245 | 238 | return serialize_impl(opts, 0); |
| 246 | 239 | } |
| @@ -315,10 +308,6 @@ | ||
| 315 | 308 | } |
| 316 | 309 | |
| 317 | 310 | 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); | |
| 322 | 311 | } |
| 323 | 312 | |
| 324 | 313 | string Value::make_indent(size_t depth, size_t width) { |
| @@ -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
| @@ -1,8 +1,18 @@ | ||
| 1 | 1 | #include <cassert> |
| 2 | 2 | #include <iostream> |
| 3 | 3 | #include <suwa/json.hh> |
| 4 | -#include <util/file.hh> | |
| 4 | +#include "file.hh" | |
| 5 | 5 | |
| 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 | + | |
| 6 | 16 | int main(void) { |
| 7 | 17 | { |
| 8 | 18 | const string src = R"({ |
| @@ -60,7 +70,7 @@ | ||
| 60 | 70 | } |
| 61 | 71 | |
| 62 | 72 | { |
| 63 | - auto res = json::Parser::parse_from_file("webfinger.json"); | |
| 73 | + auto res = parse_from_file("webfinger.json"); | |
| 64 | 74 | assert(res.error == json::Error::None); |
| 65 | 75 | assert(res.value); |
| 66 | 76 | assert(res.value->is_object()); |
| @@ -87,20 +97,20 @@ | ||
| 87 | 97 | assert(subject); |
| 88 | 98 | assert(subject->as_string() == "acct:suwako@sns.076.moe"); |
| 89 | 99 | |
| 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 }); | |
| 92 | 102 | |
| 93 | - auto res2 = json::Parser::parse_from_file("webfinger2.json"); | |
| 103 | + auto res2 = parse_from_file("webfinger2.json"); | |
| 94 | 104 | assert(res2.error == json::Error::None); |
| 95 | 105 | |
| 96 | - auto res3 = json::Parser::parse_from_file("webfinger3.json"); | |
| 106 | + auto res3 = parse_from_file("webfinger3.json"); | |
| 97 | 107 | assert(res3.error == json::Error::None); |
| 98 | 108 | |
| 99 | 109 | std::cout << "WebFinger OK" << std::endl; |
| 100 | 110 | } |
| 101 | 111 | |
| 102 | 112 | { |
| 103 | - auto res = json::Parser::parse_from_file("gltf.json"); | |
| 113 | + auto res = parse_from_file("gltf.json"); | |
| 104 | 114 | assert(res.error == json::Error::None); |
| 105 | 115 | assert(res.value); |
| 106 | 116 | assert(res.value->is_object()); |
| @@ -200,20 +210,20 @@ | ||
| 200 | 210 | assert(obj.get("version")->as_string() == "2.0"); |
| 201 | 211 | } |
| 202 | 212 | |
| 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}); | |
| 205 | 215 | |
| 206 | - auto res2 = json::Parser::parse_from_file("gltf2.json"); | |
| 216 | + auto res2 = parse_from_file("gltf2.json"); | |
| 207 | 217 | assert(res2.error == json::Error::None); |
| 208 | 218 | |
| 209 | - auto res3 = json::Parser::parse_from_file("gltf3.json"); | |
| 219 | + auto res3 = parse_from_file("gltf3.json"); | |
| 210 | 220 | assert(res3.error == json::Error::None); |
| 211 | 221 | |
| 212 | 222 | std::cout << "glTF OK" << std::endl; |
| 213 | 223 | } |
| 214 | 224 | |
| 215 | 225 | { |
| 216 | - auto res = json::Parser::parse_from_file("activitypub.json"); | |
| 226 | + auto res = parse_from_file("activitypub.json"); | |
| 217 | 227 | assert(res.error == json::Error::None); |
| 218 | 228 | assert(res.value); |
| 219 | 229 | assert(res.value->is_object()); |
| @@ -255,13 +265,13 @@ | ||
| 255 | 265 | assert(pubkey.get("owner")->as_string() == "https://example.social/users/sam"); |
| 256 | 266 | assert(pubkey.get("publicKeyPem")->as_string() == "-----BEGIN PUBLIC KEY-----\n...\n----END PUBLIC KEY----"); |
| 257 | 267 | |
| 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 }); | |
| 260 | 270 | |
| 261 | - auto res2 = json::Parser::parse_from_file("activitypub2.json"); | |
| 271 | + auto res2 = parse_from_file("activitypub2.json"); | |
| 262 | 272 | assert(res2.error == json::Error::None); |
| 263 | 273 | |
| 264 | - auto res3 = json::Parser::parse_from_file("activitypub3.json"); | |
| 274 | + auto res3 = parse_from_file("activitypub3.json"); | |
| 265 | 275 | assert(res3.error == json::Error::None); |
| 266 | 276 | |
| 267 | 277 | std::cout << "ActivityPub OK" << std::endl; |