QString GpxFileParser::parseGpxFile(QIODevice &file) { QXmlStreamReader xml(&file); parseRoot(xml, m_cacheList); /* Error handling. */ if(xml.hasError()) return QObject::tr("Parse error at line %1, column %2:\n%3") .arg(xml.lineNumber()).arg(xml.columnNumber()).arg(xml.errorString()); xml.clear(); qDebug() << __FUNCTION__ << "Parser finished"; return NULL; }
api_return ShareRootApi::handleUpdateRoot(ApiRequest& aRequest) { const auto& reqJson = aRequest.getRequestBody(); auto path = JsonUtil::getField<string>("path", reqJson, false); auto info = ShareManager::getInstance()->getRootInfo(path); if (!info) { aRequest.setResponseErrorStr("Path not found"); return websocketpp::http::status_code::not_found; } parseRoot(info, reqJson, false); ShareManager::getInstance()->updateRootDirectory(info); return websocketpp::http::status_code::ok; }
api_return ShareRootApi::handleAddRoot(ApiRequest& aRequest) { const auto& reqJson = aRequest.getRequestBody(); auto path = Util::validatePath(JsonUtil::getField<string>("path", reqJson, false), true); // Validate the path try { ShareManager::getInstance()->validateRootPath(path); } catch (ShareException& e) { JsonUtil::throwError("path", JsonUtil::ERROR_INVALID, e.what()); } auto info = std::make_shared<ShareDirectoryInfo>(path); parseRoot(info, reqJson, true); ShareManager::getInstance()->addRootDirectory(info); return websocketpp::http::status_code::ok; }
static int devicePulse( tr_upnp_device_t * dev, tr_fd_t * fdlimit, int port ) { const char * body; int len, code; uint8_t laststate; switch( dev->state ) { case UPNPDEV_STATE_READY: if( 0 < port ) { tr_dbg( "upnp device %s: want mapping, state ready -> get", dev->host ); dev->mappedport = port; dev->state = UPNPDEV_STATE_GET; break; } return 1; case UPNPDEV_STATE_MAPPED: if( port != dev->mappedport ) { tr_dbg( "upnp device %s: change mapping, " "state mapped -> delete", dev->host ); dev->state = UPNPDEV_STATE_DEL; break; } if( tr_date() > dev->lastcheck + MAPPING_CHECK_INTERVAL ) { tr_dbg( "upnp device %s: check mapping, " "state mapped -> get", dev->host ); dev->state = UPNPDEV_STATE_GET; } return 1; case UPNPDEV_STATE_ERROR: return 0; } code = devicePulseHttp( dev, fdlimit, &body, &len ); if( 0 > code ) { return 1; } if( LOOP_DETECT_THRESHOLD <= dev->looping ) { tr_dbg( "upnp device %s: loop detected, state %hhu -> error", dev->host, dev->state ); dev->state = UPNPDEV_STATE_ERROR; dev->looping = 0; killHttp( fdlimit, &dev->http ); return 1; } laststate = dev->state; dev->state = UPNPDEV_STATE_ERROR; switch( laststate ) { case UPNPDEV_STATE_ROOT: if( !TR_HTTP_STATUS_OK( code ) ) { tr_dbg( "upnp device %s: fetch root failed with http code %i", dev->host, code ); } else if( parseRoot( body, len, &dev->soap, &dev->scpd ) ) { tr_dbg( "upnp device %s: parse root failed", dev->host ); } else { tr_dbg( "upnp device %s: found scpd \"%s\" and soap \"%s\"", dev->root, dev->scpd, dev->soap ); tr_dbg( "upnp device %s: parsed root, state root -> scpd", dev->host ); dev->state = UPNPDEV_STATE_SCPD; } break; case UPNPDEV_STATE_SCPD: if( !TR_HTTP_STATUS_OK( code ) ) { tr_dbg( "upnp device %s: fetch scpd failed with http code %i", dev->host, code ); } else if( parseScpd( body, len, &dev->getcmd, &dev->addcmd, &dev->delcmd ) ) { tr_dbg( "upnp device %s: parse scpd failed", dev->host ); } else { tr_dbg( "upnp device %s: parsed scpd, state scpd -> ready", dev->host ); dev->state = UPNPDEV_STATE_READY; dev->looping = 0; } break; case UPNPDEV_STATE_ADD: dev->looping++; if( IGD_ADD_CONFLICT == code ) { tr_dbg( "upnp device %s: add conflict, state add -> delete", dev->host ); dev->state = UPNPDEV_STATE_DEL; } else if( TR_HTTP_STATUS_OK( code ) || IGD_GENERIC_ERROR == code || IGD_GENERIC_FAILED == code ) { tr_dbg( "upnp device %s: add attempt, state add -> get", dev->host ); dev->state = UPNPDEV_STATE_GET; } else { tr_dbg( "upnp device %s: add failed with http code %i", dev->host, code ); } break; case UPNPDEV_STATE_GET: dev->looping++; if( TR_HTTP_STATUS_OK( code ) ) { switch( parseMapping( dev, body, len ) ) { case -1: break; case 0: tr_dbg( "upnp device %s: invalid mapping, " "state get -> delete", dev->host ); dev->state = UPNPDEV_STATE_DEL; break; case 1: tr_dbg( "upnp device %s: good mapping, " "state get -> mapped", dev->host ); dev->state = UPNPDEV_STATE_MAPPED; dev->looping = 0; dev->lastcheck = tr_date(); tr_inf( "upnp successful for port %i", dev->mappedport ); break; default: assert( 0 ); break; } } else if( IGD_NO_MAPPING_EXISTS == code || IGD_GENERIC_ERROR == code || IGD_GENERIC_FAILED == code ) { tr_dbg( "upnp device %s: no mapping, state get -> add", dev->host ); dev->state = UPNPDEV_STATE_ADD; } else { tr_dbg( "upnp device %s: get failed with http code %i", dev->host, code ); } break; case UPNPDEV_STATE_DEL: dev->looping++; if( TR_HTTP_STATUS_OK( code ) || IGD_NO_MAPPING_EXISTS == code || IGD_GENERIC_ERROR == code || IGD_GENERIC_FAILED == code ) { tr_dbg( "upnp device %s: deleted, state delete -> ready", dev->host ); dev->state = UPNPDEV_STATE_READY; dev->looping = 0; } else { tr_dbg( "upnp device %s: del failed with http code %i", dev->host, code ); } break; default: assert( 0 ); break; } dev->lastrequest = tr_date(); killHttp( fdlimit, &dev->http ); if( UPNPDEV_STATE_ERROR == dev->state ) { tr_dbg( "upnp device %s: error, state %hhu -> error", dev->host, laststate ); return 0; } return 1; }
bool Lvk::Clue::ScriptParser::parse(const QString &filename, Clue::Script &script, Clue::ScriptFormat format) { qDebug() << "ScriptParser: parsing filename" << filename; m_errMsg.clear(); script.clear(); script.filename = QFileInfo(filename).fileName(); QFile f(filename); if (!f.exists()) { m_errMsg = QObject::tr("File not found: '%1'").arg(filename); m_error = Clue::FileNotFoundError; return false; } QIODevice::OpenMode flags = (format == XmlObfuscated) ? QFile::ReadOnly : QFile::ReadOnly | QFile::Text; if (!f.open(flags)) { m_errMsg = QObject::tr("Cannot read file: '%1'").arg(filename); m_error = Clue::ReadError; return false; } QByteArray data = f.readAll(); if (format == XmlObfuscated) { if (!deobfuscate(data)) { m_errMsg = QObject::tr("Cannot deobfuscate: '%1'").arg(filename); m_error = Clue::InvalidFormatError; return false; } } QString xml = QString::fromUtf8(data); QDomDocument doc; QString err; int line = 0; int col = 0; bool parsingOk = false; removeComments(xml); if (doc.setContent(xml, &err, &line, &col)) { QDomElement root = doc.documentElement(); if (requireTagName("script", root)) { parsingOk = parseRoot(root, script); } } else { m_errMsg = QObject::tr("Ill-formed XML in line %1:%2").arg(line).arg(col); // More details: //m_errMsg = QObject::tr("Ill-formed XML in line %1:%2 %3").arg(line).arg(col).arg(xml); } if (!parsingOk) { m_errMsg = QObject::tr("Invalid format in file '%1'.\n%2").arg(filename, m_errMsg); m_error = Clue::InvalidFormatError; return false; } return true; }
KoFilter::ConversionStatus KWord13Import::convert( const QByteArray& from, const QByteArray& to ) { if ( to != "application/vnd.oasis.opendocument.text" || from != "application/x-kword" ) { return KoFilter::NotImplemented; } // We need KimageIO's help in OOWriterWorker::convertUnknownImage KWord13Document kwordDocument; const QString fileName( m_chain->inputFile() ); if ( fileName.isEmpty() ) { kError(30520) << "No input file name!"; return KoFilter::StupidError; } KoStore* store = KoStore::createStore( fileName, KoStore::Read ); if ( store && store->hasFile( "maindoc.xml" ) ) { kDebug(30520) <<"Maindoc.xml found in KoStore!"; // We do not really care about errors while reading/parsing documentinfo store->open( "documentinfo.xml" ); KoStoreDevice ioInfo( store ); ioInfo.open( QIODevice::ReadOnly ); kDebug (30520) <<"Processing document info..."; if ( ! parseInfo ( &ioInfo, kwordDocument ) ) { kWarning(30520) << "Parsing documentinfo.xml has failed. Ignoring!"; } ioInfo.close(); store->close(); // ### TODO: error return values if ( ! store->open( "maindoc.xml" ) ) { kError(30520) << "Opening root has failed"; delete store; return KoFilter::StupidError; } KoStoreDevice ioMain( store ); ioMain.open( QIODevice::ReadOnly ); kDebug (30520) <<"Processing root..."; if ( ! parseRoot ( &ioMain, kwordDocument ) ) { kWarning(30520) << "Parsing maindoc.xml has failed! Aborting!"; delete store; return KoFilter::StupidError; } ioMain.close(); store->close(); if ( store->open( "preview.png" ) ) { kDebug(30520) <<"Preview found!"; KoStoreDevice ioPreview( store ); ioPreview.open( QIODevice::ReadOnly ); const QByteArray image ( ioPreview.readAll() ); if ( image.isNull() ) { kWarning(30520) << "Loading of preview failed! Ignoring!"; } else { kwordDocument.m_previewFile = new KTemporaryFile(); kwordDocument.m_previewFile->setSuffix(".png"); kwordDocument.m_previewFile->open(); // ### TODO check KTemporaryFile // ### TODO: check if file is correctly written kwordDocument.m_previewFile->write( image ); kwordDocument.m_previewFile->close(); } ioPreview.close(); store->close(); } else { kDebug(30520) <<"No preview found!"; } } else { kWarning(30520) << "Opening store has failed. Trying raw XML file!"; // Be sure to undefine store delete store; store = 0; QFile file( fileName ); file.open( QIODevice::ReadOnly ); if ( ! parseRoot( &file, kwordDocument ) ) { kError(30520) << "Could not process document! Aborting!"; file.close(); return KoFilter::StupidError; } file.close(); } if ( store && ! postParse( store, kwordDocument ) ) { kError(30520) << "Error during post-parsing! Aborting!"; return KoFilter::StupidError; } // We have finished with the input store/file, so close the store (already done for a raw XML file) kDebug(30520) <<"Deleting input store..."; delete store; store = 0; kDebug(30520) <<"Input store deleted!"; KWord13OasisGenerator generator; kDebug(30520) << __FILE__ <<":" << __LINE__; if ( ! generator.prepare( kwordDocument ) ) { kError(30520) << "Could not prepare the OASIS document! Aborting!"; return KoFilter::StupidError; } const QString filenameOut ( m_chain->outputFile() ); if ( filenameOut.isEmpty() ) { kError(30520) << "Empty file name for saving as OASIS! Aborting!"; return KoFilter::StupidError; } if ( ! generator.generate( filenameOut, kwordDocument ) ) { kError(30520) << "Could not save as OASIS! Aborting!"; return KoFilter::StupidError; } kDebug(30520) <<"Filter has finished!"; return KoFilter::OK; }