diff options
author | George Hazan <ghazan@miranda.im> | 2018-09-04 22:37:40 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-09-04 22:37:40 +0300 |
commit | cfa148c35f375537a8be67317b55f7d92e2ec3d5 (patch) | |
tree | 68d94a48b3c0d96a07c597b744ae752d8dfa7fa4 /plugins/SendScreenshotPlus/src | |
parent | 101b8eb8a62c8cfcba71faaa38fb378c402b44a7 (diff) |
SendSS: self-made JSON parser removed
Diffstat (limited to 'plugins/SendScreenshotPlus/src')
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSend.cpp | 147 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSend.h | 3 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp | 30 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/stdafx.h | 1 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/version.h | 2 |
5 files changed, 17 insertions, 166 deletions
diff --git a/plugins/SendScreenshotPlus/src/CSend.cpp b/plugins/SendScreenshotPlus/src/CSend.cpp index 98a22dfe63..180afe817e 100644 --- a/plugins/SendScreenshotPlus/src/CSend.cpp +++ b/plugins/SendScreenshotPlus/src/CSend.cpp @@ -452,153 +452,6 @@ const char* CSend::GetHTMLContent(char* str, const char* startTag, const char* e return begin; } -int JSON_ParseData_(const char** jsondata, size_t jsonlen, const char** rawdata) -{ - const char* c = *jsondata; - const char* jsonend = *jsondata + jsonlen; - int len = 0; - *rawdata = nullptr; - if (c == jsonend) - return 0; - if (*c == '{') { // scope (object) - *rawdata = c; - do { - if (*c == '{') ++len; - else if (*c == '}') --len; - if (++c == jsonend) - return 0; - } while (len > 0); - len = c - *rawdata; - if (*c == ',') ++c; - } - else if (*c == '"' || *c == '\'') { // string - char needle = *c; - if (++c == jsonend) - return 0; - *rawdata = c; - do { - if (c == jsonend || (*c == '\\' && ++c == jsonend)) - return 0; - } while (*c++ != needle); - len = c - *rawdata - 1; - if (*c == ',') ++c; - } - else { // other - for (*rawdata = c; c < jsonend && *c++ != ',';); - len = c - *rawdata; - if (c[-1] == ',') --len; - } - *jsondata = c; - return len; -} - -int JSON_Get_(const char* json, size_t jsonlen, const char* variable, const char** value) -{ - char needle[32]; - const char* needlechild; - char var[32]; - char* tmp; - const char* c; - const char* jsonend = json + jsonlen; - // get needle - if (!jsonlen || *json != '{') - return 0; - for (tmp = needle, c = *variable == '[' ? variable + 1 : variable; *c != '[' && *c != ']'; ++c) { - if (c == jsonend) - return 0; - if (tmp < needle + sizeof(needle) - 1) *tmp++ = *c; - } - *tmp = '\0'; - // get child needle (if any) - if (*c == ']') ++c; - if (c == jsonend) - return 0; - needlechild = c; - // parse JSON - for (c = json + 1; c < jsonend && (*c == '"' || *c == '\'');) { - for (++c, tmp = var; c < jsonend && (*c != '"' && *c != '\''); ++c) - if (tmp < var + sizeof(var) - 1) *tmp++ = *c; - *tmp = '\0'; - if (c + 2 >= jsonend || *++c != ':') break; - /// read data - ++c; - if (!mir_strcmp(var, needle)) { - int datalen = JSON_ParseData_(&c, jsonend - c, value); - if (!datalen) - return 0; - if (*needlechild && **value == '{') { // we need a child value, parse child object - return JSON_Get_(*value, datalen, needlechild, value); - } - return datalen; - } - else { - JSON_ParseData_(&c, jsonend - c, value); - } - } - *value = nullptr; - return 0; -} - -int CSend::GetJSONString(const char* json, size_t jsonlen, const char* variable, char* value, size_t valuesize) -{ - if (!jsonlen || !valuesize) - return 0; - const char* rawvalue; - int rawlen = JSON_Get_(json, jsonlen, variable, &rawvalue); - if (rawlen) { - size_t out = 0; - --valuesize; - /// copy & parse escape sequences - for (int in = 0; in < rawlen && out < valuesize; ++in, ++out) { - if (rawvalue[in] == '\\') { - if (++in == rawlen) - break; - switch (rawvalue[in]) { - case 's': value[out] = ' '; break; - case 't': value[out] = '\t'; break; - case 'n': value[out] = '\n'; break; - case 'r': value[out] = '\r'; break; - default: value[out] = rawvalue[in]; - } - continue; - } - value[out] = rawvalue[in]; - } - value[out] = '\0'; - return 1; - } - *value = '\0'; - return 0; -} - -int CSend::GetJSONInteger(const char* json, size_t jsonlen, const char* variable, int defvalue) -{ - const char* rawvalue; - int rawlen = JSON_Get_(json, jsonlen, variable, &rawvalue); - if (rawlen) { - defvalue = 0; - for (int offset = 0; offset < rawlen; ++offset) { - if (rawvalue[offset]<'0' || rawvalue[offset]>'9') break; - defvalue *= 10; - defvalue += rawvalue[offset] - '0'; - } - } - return defvalue; -} - -bool CSend::GetJSONBool(const char* json, size_t jsonlen, const char* variable) -{ - const char* rawvalue; - int rawlen = JSON_Get_(json, jsonlen, variable, &rawvalue); - if (rawlen) { - if (rawlen == 4 && !memcmp(rawvalue, "true", 4)) - return true; - if (*rawvalue > '0' && *rawvalue <= '9') - return true; - } - return false; -} - static void HTTPFormAppendData(NETLIBHTTPREQUEST* nlhr, size_t* dataMax, char** dataPos, const char* data, size_t len) { nlhr->dataLength = (*dataPos - nlhr->pData); diff --git a/plugins/SendScreenshotPlus/src/CSend.h b/plugins/SendScreenshotPlus/src/CSend.h index 00b347de8e..707a457b29 100644 --- a/plugins/SendScreenshotPlus/src/CSend.h +++ b/plugins/SendScreenshotPlus/src/CSend.h @@ -130,9 +130,6 @@ protected: }; static const char* GetHTMLContent(char* str, const char* startTag, const char* endTag); // changes "str", can be successfully used only once - static int GetJSONString(const char* json, size_t jsonlen, const char* variable, char* value, size_t valuesize); - static int GetJSONInteger(const char* json, size_t jsonlen, const char* variable,int defvalue); - static bool GetJSONBool(const char* json, size_t jsonlen, const char* variable); void HTTPFormDestroy(NETLIBHTTPREQUEST* nlhr); // use to free data inside "nlhr" created by HTTPFormCreate int HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum); // returns "0" on success, Exit() will be called on failure (stop processing) }; diff --git a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp index 21730d165f..10be0bfc49 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp @@ -57,27 +57,27 @@ int CSendHost_Imgur::Send() void CSendHost_Imgur::SendThread(void* obj) { - CSendHost_Imgur* self = (CSendHost_Imgur*)obj; + CSendHost_Imgur *self = (CSendHost_Imgur*)obj; // send DATA and wait for m_nlreply - NETLIBHTTPREQUEST* reply = Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr); + NETLIBHTTPREQUEST *reply = Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr); self->HTTPFormDestroy(&self->m_nlhr); if (reply) { if (reply->dataLength) { - char buf[128]; - - if (GetJSONBool(reply->pData, reply->dataLength, "success")) { - GetJSONString(reply->pData, reply->dataLength, "data[link]", buf, sizeof(buf)); - - self->m_URL = buf; - int idx = self->m_URL.ReverseFind('.'); - if (idx != -1) { - self->m_URLthumb = self->m_URL; - self->m_URLthumb.Insert(idx, 'm'); + JSONROOT root(reply->pData); + if (root) { + if ((*root)["success"].as_bool()) { + self->m_URL = (*root)["data"]["link"].as_mstring(); + int idx = self->m_URL.ReverseFind('.'); + if (idx != -1) { + self->m_URLthumb = self->m_URL; + self->m_URLthumb.Insert(idx, 'm'); + } + Netlib_FreeHttpRequest(reply); + self->svcSendMsgExit(self->m_URL); return; } - Netlib_FreeHttpRequest(reply); - self->svcSendMsgExit(self->m_URL); return; + else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, (*root)["status"].as_int(), 0); } - else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, GetJSONInteger(reply->pData, reply->dataLength, "status", 0)); + else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode); } else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode); diff --git a/plugins/SendScreenshotPlus/src/stdafx.h b/plugins/SendScreenshotPlus/src/stdafx.h index 3fec3a8af1..47268c0b43 100644 --- a/plugins/SendScreenshotPlus/src/stdafx.h +++ b/plugins/SendScreenshotPlus/src/stdafx.h @@ -64,6 +64,7 @@ using namespace std; #include <m_netlib.h> #include <m_protosvc.h> #include <m_skin.h> +#include <m_json.h> #include <m_popup.h> #include <m_icolib.h> #include <m_string.h> diff --git a/plugins/SendScreenshotPlus/src/version.h b/plugins/SendScreenshotPlus/src/version.h index cbcdd47655..50c828b061 100644 --- a/plugins/SendScreenshotPlus/src/version.h +++ b/plugins/SendScreenshotPlus/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 9 #define __RELEASE_NUM 0 -#define __BUILD_NUM 1 +#define __BUILD_NUM 2 #include <stdver.h> |