From 78034bcd615e739ad51a1b4bc570b6f1927932fc Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 17 Jan 2024 18:05:48 +0300 Subject: json2file + file2json = helpers to load json from file and write it back --- protocols/Discord/src/guilds.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index fdaab2cefe..b149059972 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -456,17 +456,10 @@ void CDiscordGuild::ProcessRole(const JSONNode &role) void CDiscordGuild::LoadFromFile() { - int fileNo = _wopen(GetCacheFile(), O_TEXT | O_RDONLY); - if (fileNo == -1) + JSONNode cached; + if (!file2json(GetCacheFile(), cached)) return; - int fSize = ::filelength(fileNo); - ptrA json((char*)mir_alloc(fSize + 1)); - read(fileNo, json, fSize); - json[fSize] = 0; - close(fileNo); - - JSONNode cached = JSONNode::parse(json); for (auto &it : cached) { SnowFlake userId = getId(it["id"]); auto *pUser = FindUser(userId); @@ -491,10 +484,5 @@ void CDiscordGuild ::SaveToFile() CMStringW wszFileName(GetCacheFile()); CreatePathToFileW(wszFileName); - int fileNo = _wopen(wszFileName, O_CREAT | O_TRUNC | O_TEXT | O_WRONLY); - if (fileNo != -1) { - std::string json = members.write_formatted(); - write(fileNo, json.c_str(), (int)json.size()); - close(fileNo); - } + json2file(members, wszFileName); } -- cgit v1.2.3