diff options
-rw-r--r-- | include/m_json.h | 6 | ||||
-rw-r--r-- | libs/libjson/src/libJSON.cpp | 40 | ||||
-rw-r--r-- | libs/libjson/src/libjson.def | 2 | ||||
-rw-r--r-- | libs/libjson/src/libjson64.def | 2 | ||||
-rw-r--r-- | libs/libjson/src/stdafx.h | 1 | ||||
-rw-r--r-- | libs/win32/libjson.lib | bin | 69346 -> 69946 bytes | |||
-rw-r--r-- | libs/win64/libjson.lib | bin | 70070 -> 70690 bytes | |||
-rw-r--r-- | protocols/Discord/src/guilds.cpp | 18 | ||||
-rw-r--r-- | protocols/JabberG/src/jabber_caps.cpp | 25 |
9 files changed, 57 insertions, 37 deletions
diff --git a/include/m_json.h b/include/m_json.h index f258b0d9e6..62f3126ad1 100644 --- a/include/m_json.h +++ b/include/m_json.h @@ -166,6 +166,12 @@ LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const WCHAR_PARAM ¶m); LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const NULL_PARAM ¶m);
LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const JSON_PARAM ¶m);
+// loads a JSON contents of a file
+LIBJSON_DLL(bool) file2json(const wchar_t *pwszFilename, JSONNode &root);
+
+// saves a JSON variable into a flie
+LIBJSON_DLL(bool) json2file(const JSONNode &root, const wchar_t *pwszFilename);
+
#endif // __cplusplus
#ifndef LIBJSON_EXPORTS
diff --git a/libs/libjson/src/libJSON.cpp b/libs/libjson/src/libJSON.cpp index 7cd2255d4a..3dd7544afa 100644 --- a/libs/libjson/src/libJSON.cpp +++ b/libs/libjson/src/libJSON.cpp @@ -458,3 +458,43 @@ LIBJSON_DLL(int) json_equal(JSONNode *node, JSONNode *node2) { JSON_ASSERT_SAFE(node2, JSON_TEXT("null node2 to json_equal"), return false;);
return *node == *node2;
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+LIBJSON_DLL(bool) file2json(const wchar_t *pwszFilename, JSONNode &root)
+{
+ if (!pwszFilename)
+ return false;
+
+ int fileId = _wopen(pwszFilename, _O_BINARY | _O_RDONLY);
+ if (fileId == -1)
+ return false;
+
+ size_t dwFileLength = _filelength(fileId), dwReadLen;
+ ptrA szBuf((char *)mir_alloc(dwFileLength + 1));
+ dwReadLen = _read(fileId, szBuf, (unsigned)dwFileLength);
+ _close(fileId);
+ if (dwFileLength != dwReadLen)
+ return false;
+
+ szBuf[dwFileLength] = 0;
+
+ root = JSONNode::parse(szBuf);
+ if (!root)
+ return false;
+
+ return true;
+}
+
+LIBJSON_DLL(bool) json2file(const JSONNode &root, const wchar_t *pwszFilename)
+{
+ auto szBody = root.write_formatted();
+
+ int fileId = _wopen(pwszFilename, _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);
+ if (fileId == -1)
+ return false;
+
+ int cbLen = _write(fileId, szBody.c_str(), (unsigned)szBody.length());
+ _close(fileId);
+ return cbLen == (int)szBody.length();
+}
diff --git a/libs/libjson/src/libjson.def b/libs/libjson/src/libjson.def index debf2a9ff5..5cbfc3394a 100644 --- a/libs/libjson/src/libjson.def +++ b/libs/libjson/src/libjson.def @@ -211,3 +211,5 @@ json_write_formatted @191 ??9JSONNode@@QBE_N_K@Z @208 NONAME
??6@YGAAVJSONNode@@AAV0@ABUSINT64_PARAM@@@Z @209 NONAME
??0JSONNode@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PB_W@Z @210 NONAME
+?file2json@@YG_NPB_WAAVJSONNode@@@Z @211 NONAME
+?json2file@@YG_NABVJSONNode@@PB_W@Z @212 NONAME
diff --git a/libs/libjson/src/libjson64.def b/libs/libjson/src/libjson64.def index c46a354d53..5ed328af4b 100644 --- a/libs/libjson/src/libjson64.def +++ b/libs/libjson/src/libjson64.def @@ -211,3 +211,5 @@ json_write_formatted @191 ??9JSONNode@@QEBA_N_K@Z @208 NONAME
??6@YAAEAVJSONNode@@AEAV0@AEBUSINT64_PARAM@@@Z @209 NONAME
??0JSONNode@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEB_W@Z @210 NONAME
+?file2json@@YA_NPEB_WAEAVJSONNode@@@Z @211 NONAME
+?json2file@@YA_NAEBVJSONNode@@PEB_W@Z @212 NONAME
diff --git a/libs/libjson/src/stdafx.h b/libs/libjson/src/stdafx.h index e9d52cfe52..5e22df85fd 100644 --- a/libs/libjson/src/stdafx.h +++ b/libs/libjson/src/stdafx.h @@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <stddef.h>
#include <process.h>
#include <io.h>
+#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <locale.h>
diff --git a/libs/win32/libjson.lib b/libs/win32/libjson.lib Binary files differindex d65592560c..fb96379485 100644 --- a/libs/win32/libjson.lib +++ b/libs/win32/libjson.lib diff --git a/libs/win64/libjson.lib b/libs/win64/libjson.lib Binary files differindex 8e026eca78..3d82a4ed37 100644 --- a/libs/win64/libjson.lib +++ b/libs/win64/libjson.lib 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);
}
diff --git a/protocols/JabberG/src/jabber_caps.cpp b/protocols/JabberG/src/jabber_caps.cpp index 3f25928b12..7c665053b6 100644 --- a/protocols/JabberG/src/jabber_caps.cpp +++ b/protocols/JabberG/src/jabber_caps.cpp @@ -573,21 +573,8 @@ static const char *str2buf(const std::string &str) void CJabberClientCapsManager::Load()
{
- int fileId = _wopen(VARSW(L"%miranda_userdata%\\jabberCaps.json"), _O_BINARY | _O_RDONLY);
- if (fileId == -1)
- return;
-
- size_t dwFileLength = _filelength(fileId), dwReadLen;
- ptrA szBuf((char *)mir_alloc(dwFileLength + 1));
- dwReadLen = _read(fileId, szBuf, (unsigned)dwFileLength);
- _close(fileId);
- if (dwFileLength != dwReadLen)
- return;
-
- szBuf[dwFileLength] = 0;
-
- JSONNode root = JSONNode::parse(szBuf);
- if (!root)
+ JSONNode root;
+ if (!file2json(VARSW(L"%miranda_userdata%\\jabberCaps.json"), root))
return;
for (auto &node : root) {
@@ -646,11 +633,5 @@ void CJabberClientCapsManager::Save() root << node;
}
- std::string szBody = root.write_formatted();
-
- int fileId = _wopen(VARSW(L"%miranda_userdata%\\jabberCaps.json"), _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);
- if (fileId != -1) {
- _write(fileId, szBody.c_str(), (unsigned)szBody.length());
- _close(fileId);
- }
+ json2file(root, VARSW(L"%miranda_userdata%\\jabberCaps.json"));
}
|