/* function read(buffer: ByteArray, offset: Number = 0, count: Number = -1): Number Returns a count of bytes read. Non-blocking if a callback is defined. Otherwise, blocks. Offset: -1 then read to the buffer write position, >= 0 then read to that offset count: -1 then read as much as the buffer will hold. If buffer is growable, read all content. If not growable, read the buffer size. If count >= 0, then read that number of bytes. */ static EjsNumber *http_read(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { EjsByteArray *buffer; HttpConn *conn; MprOff contentLength; ssize offset, count; conn = hp->conn; buffer = (EjsByteArray*) argv[0]; offset = (argc >= 2) ? ejsGetInt(ejs, argv[1]) : 0; count = (argc >= 3) ? ejsGetInt(ejs, argv[2]): -1; if (!waitForResponseHeaders(hp)) { return 0; } contentLength = httpGetContentLength(conn); if (conn->state >= HTTP_STATE_PARSED && contentLength == hp->readCount) { /* End of input */ return ESV(null); } if (offset < 0) { offset = buffer->writePosition; } else if (offset < buffer->size) { ejsSetByteArrayPositions(ejs, buffer, offset, offset); } else { ejsThrowOutOfBoundsError(ejs, "Bad read offset value"); return 0; } if (count < 0 && !buffer->resizable) { count = buffer->size - offset; } if ((count = readHttpData(ejs, hp, count)) < 0) { assert(ejs->exception); return 0; } else if (count == 0 && conn->state > HTTP_STATE_CONTENT) { return ESV(null); } hp->readCount += count; if (ejsCopyToByteArray(ejs, buffer, offset, (char*) mprGetBufStart(hp->responseContent), count) != count) { ejsThrowMemoryError(ejs); } ejsSetByteArrayPositions(ejs, buffer, -1, buffer->writePosition + count); mprAdjustBufStart(hp->responseContent, count); mprResetBufIfEmpty(hp->responseContent); return ejsCreateNumber(ejs, (MprNumber) count); }
/* function readString(count: Number = -1): String Read count bytes (default all) of content as a string. This always starts at the first character of content. */ static EjsString *http_readString(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { EjsString *result; ssize count; count = (argc == 1) ? ejsGetInt(ejs, argv[0]) : -1; if (!waitForState(hp, HTTP_STATE_CONTENT, -1, 1)) { return 0; } if ((count = readHttpData(ejs, hp, count)) < 0) { assert(ejs->exception); return 0; } else if (count == 0 && hp->conn->state > HTTP_STATE_CONTENT) { return ESV(null); } // UNICODE ENCODING result = ejsCreateStringFromMulti(ejs, mprGetBufStart(hp->responseContent), count); mprAdjustBufStart(hp->responseContent, count); mprResetBufIfEmpty(hp->responseContent); return result; }
#include "jsonreader.h" #include <QJsonDocument> #include <QJsonObject> #include <QJsonValue> #include <QJsonArray> #include <QDebug> JsonReader::JsonReader(ProgressBar *progressBar) { QObject::connect(this,SIGNAL(progressBarSetup(QString,int,int)),progressBar,SLOT(setInfo(QString,int,int))); QObject::connect(this,SIGNAL(progressBarUpdate(QString,int)),progressBar,SLOT(setValue(QString,int))); QObject::connect(this,SIGNAL(progressFinished()),progressBar,SLOT(progressFinished())); emit progressBarSetup(QString("Checking for updates"),0,0); QObject::connect(&m_ApiReader,SIGNAL(sigJsonData(QString,ApiReader::DataType)),this,SLOT(readHttpData(QString,ApiReader::DataType))); QObject::connect(&m_ApiReader,SIGNAL(networkError(QString,NetworkHandler::ErrorCode)),this,SLOT(errorSlot(QString,NetworkHandler::ErrorCode))); m_ApiReader.startCheckUpdateNecessary(); m_IterationStep = eIndexData; } void JsonReader::triggerLeagueRead() { emit progressBarSetup(QString("Downloading leagues ..."),0,0); m_ApiReader.startLeageJsonDownload(); } void JsonReader::triggerFixtureRead(QString leagueCaption,int matchday) { emit progressBarSetup(QString("Downloading Fixtures ..."),0,0); m_ApiReader.startFixtureJsonDownload(m_LeagueNameMap.key(leagueCaption),matchday); }