void ConfigManager::parseFile(QString fname){ tok.init(); tok.seterrorhandler(&tokerrorhandler); tok.settokens(tokens); tok.setcommentlinesequence("#"); // read the entire file! QFile file(fname); if(!file.open(QIODevice::ReadOnly)) throw Exception().set("could not open config file"); QByteArray b = file.readAll(); if(b.isNull() || b.isEmpty()) throw Exception().set("could not read config file"); b.append((char)0); file.close(); const char *data = b.constData(); tok.reset(data); bool done = false; while(!done){ // at the top level we parse frames and // variables int t = tok.getnext(); switch(t){ case T_VAR: parseVars(); break; case T_WINDOW: parseWindow(); break; case T_END: done=true; break; case T_PORT: port = tok.getnextint(); break; case T_SENDPORT: udpSendPort = tok.getnextint(); break; // the UDP client now sets its address from the first packet received // but this will override it case T_SENDADDR: tok.getnextstring(udpSendAddr); UDPClient::getInstance()->setAddress(udpSendAddr); break; case T_VALIDTIME: DataManager::dataValidInterval = tok.getnextfloat(); break; case T_SENDINTERVAL: sendInterval = tok.getnextfloat(); break; case T_UPDATEINTERVAL: graphicalUpdateInterval = tok.getnextfloat()*1000; break; case T_AUDIO: parseAudio(); break; case T_WAYPOINT: parseWaypoint(); break; default: throw UnexpException(&tok,"'var', 'frame', config data or end of file"); } } }
/// Attempt to read a whole HTTP response or request from a data buffer. /// If succesful, fills its own fields with the proper data and removes the response/request /// from the data buffer. /// \param HTTPbuffer The data buffer to read from. /// \return True on success, false otherwise. bool HTTP::Parser::parse(std::string & HTTPbuffer) { size_t f; std::string tmpA, tmpB, tmpC; /// \todo Make this not resize HTTPbuffer in parts, but read all at once and then remove the entire request, like doxygen claims it does? while (!HTTPbuffer.empty()) { if (!seenHeaders) { f = HTTPbuffer.find('\n'); if (f == std::string::npos) return false; tmpA = HTTPbuffer.substr(0, f); if (f + 1 == HTTPbuffer.size()) { HTTPbuffer.clear(); } else { HTTPbuffer.erase(0, f + 1); } while (tmpA.find('\r') != std::string::npos) { tmpA.erase(tmpA.find('\r')); } if (!seenReq) { seenReq = true; f = tmpA.find(' '); if (f != std::string::npos) { if (tmpA.substr(0, 4) == "HTTP") { protocol = tmpA.substr(0, f); tmpA.erase(0, f + 1); f = tmpA.find(' '); if (f != std::string::npos) { url = tmpA.substr(0, f); tmpA.erase(0, f + 1); method = tmpA; if (url.find('?') != std::string::npos) { parseVars(url.substr(url.find('?') + 1)); //parse GET variables url.erase(url.find('?')); } url = urlunescape(url); } else { seenReq = false; } } else { method = tmpA.substr(0, f); tmpA.erase(0, f + 1); f = tmpA.find(' '); if (f != std::string::npos) { url = tmpA.substr(0, f); tmpA.erase(0, f + 1); protocol = tmpA; if (url.find('?') != std::string::npos) { parseVars(url.substr(url.find('?') + 1)); //parse GET variables url.erase(url.find('?')); } url = urlunescape(url); } else { seenReq = false; } } } else { seenReq = false; } } else { if (tmpA.size() == 0) { seenHeaders = true; body.clear(); if (GetHeader("Content-Length") != "") { length = atoi(GetHeader("Content-Length").c_str()); if (body.capacity() < length) { body.reserve(length); } } if (GetHeader("Transfer-Encoding") == "chunked") { getChunks = true; doingChunk = 0; } } else { f = tmpA.find(':'); if (f == std::string::npos) continue; tmpB = tmpA.substr(0, f); tmpC = tmpA.substr(f + 1); SetHeader(tmpB, tmpC); } } } if (seenHeaders) { if (length > 0) { if (headerOnly) { return true; } unsigned int toappend = length - body.length(); if (toappend > 0) { body.append(HTTPbuffer, 0, toappend); HTTPbuffer.erase(0, toappend); } if (length == body.length()) { parseVars(body); //parse POST variables return true; } else { return false; } } else { if (getChunks) { if (headerOnly) { return true; } if (doingChunk) { unsigned int toappend = HTTPbuffer.size(); if (toappend > doingChunk) { toappend = doingChunk; } body.append(HTTPbuffer, 0, toappend); HTTPbuffer.erase(0, toappend); doingChunk -= toappend; } else { f = HTTPbuffer.find('\n'); if (f == std::string::npos) return false; tmpA = HTTPbuffer.substr(0, f); while (tmpA.find('\r') != std::string::npos) { tmpA.erase(tmpA.find('\r')); } unsigned int chunkLen = 0; if (!tmpA.empty()) { for (unsigned int i = 0; i < tmpA.size(); ++i) { chunkLen = (chunkLen << 4) | unhex(tmpA[i]); } if (chunkLen == 0) { getChunks = false; return true; } doingChunk = chunkLen; } if (f + 1 == HTTPbuffer.size()) { HTTPbuffer.clear(); } else { HTTPbuffer.erase(0, f + 1); } } return false; } else { return true; } } } } return false; //empty input } //HTTPReader::parse