void XmlElementReader::read(MapCoordVector& coords) { namespace literal = XmlStreamLiteral; coords.clear(); const auto num_coords = attribute<unsigned int>(literal::count); coords.reserve(std::min(num_coords, 500000u)); try { for( xml.readNext(); xml.tokenType() != QXmlStreamReader::EndElement; xml.readNext() ) { const QXmlStreamReader::TokenType token = xml.tokenType(); if (xml.error() || token == QXmlStreamReader::EndDocument) { throw FileFormatException(ImportExport::tr("Could not parse the coordinates.")); } else if (token == QXmlStreamReader::Characters && !xml.isWhitespace()) { QStringRef text = xml.text(); QString data = QString::fromRawData(text.constData(), text.length()); QTextStream stream(&data, QIODevice::ReadOnly); stream.setIntegerBase(10); while (!stream.atEnd()) { coords.emplace_back(); stream >> coords.back(); } if (stream.status() == QTextStream::ReadCorruptData) { throw FileFormatException(ImportExport::tr("Could not parse the coordinates.")); } } else if (token == QXmlStreamReader::StartElement) { if (xml.name() == literal::coord) { coords.emplace_back(MapCoord::load(xml)); } else { xml.skipCurrentElement(); } } // otherwise: ignore element }
FeatureFunction *SelectedWordSlowLMFactory::createNgramModel( const Parameters ¶ms ) { std::string file = params.get<std::string>("lm-file"); lm::ngram::ModelType mtype; std::string smtype = params.get<std::string>("model-type", ""); Logger logger("SelectedWordSlowLM"); if(!lm::ngram::RecognizeBinary(file.c_str(), mtype)) { if(smtype.empty() || smtype == "hash-probing") mtype = lm::ngram::HASH_PROBING; else if(smtype == "trie-sorted") mtype = lm::ngram::TRIE_SORTED; else { LOG(logger, error, "Unsupported LM type " << smtype << " for file " << file); } } switch(mtype) { case lm::ngram::HASH_PROBING: if(!smtype.empty() && smtype != "hash-probing") LOG(logger, error, "Incorrect LM type in configuration for file " << file); return new SelectedWordSlowLM<lm::ngram::ProbingModel>(file,params); case lm::ngram::TRIE_SORTED: if(!smtype.empty() && smtype != "trie-sorted") LOG(logger, error, "Incorrect LM type in configuration for file " << file); return new SelectedWordSlowLM<lm::ngram::TrieModel>(file,params); default: LOG(logger, error, "Unsupported LM type for file " << file); BOOST_THROW_EXCEPTION(FileFormatException()); } }
Exporter *FileFormat::createExporter(QIODevice* stream, Map *map, MapView *view) const { Q_UNUSED(stream); Q_UNUSED(map); Q_UNUSED(view); throw FileFormatException(QString("Format (%1) does not support export").arg(description())); }
void ConfigDatabase::checkAndCreateCube(PServer server, PDatabase db, const string &name, IdentifiersType &dims, bool &dbChanged) { PCube cube = lookupCubeByName(name, false); if (!cube) { cube = PCube(new Cube(db, name, &dims, Cube::NORMAL)); cube->setDeletable(false); cube->setRenamable(false); db->addCube(server, cube, false, true, NULL, NULL, NULL, false); dbChanged = true; } else { bool corrupted = cube->getType() != NORMALTYPE; if (!corrupted) { const IdentifiersType *cubeDims = cube->getDimensions(); corrupted = dims != *cubeDims; } if (corrupted) { throw FileFormatException("cube '" + name + "' corrupted", 0); } if (cube->isDeletable() || cube->isRenamable()) { if (!cube->isCheckedOut()) { PCubeList dbs = db->getCubeList(true); db->setCubeList(dbs); cube = db->lookupCube(cube->getId(), true); dbs->set(cube); } cube->setDeletable(false); cube->setRenamable(false); dbChanged = true; } } }
void NnIO::loadWeights(const std::string& filename, std::vector<arma::mat>& weights) { std::ifstream file(filename); if (file.is_open()) { feedISSOneLine(file); unsigned int numMatrices = 0; m_iss >> numMatrices; if (!numMatrices || !m_iss.eof()) throw FileFormatException(filename, NNFileType::WEIGHTS); unsigned int matrixIndex = 0; while (matrixIndex++ < numMatrices) { feedISSOneLine(file); unsigned int numRows = 0, numCols = 0; m_iss >> numRows >> numCols; if ((!numRows && !numCols) || !m_iss.eof()) throw FileFormatException(filename, NNFileType::WEIGHTS); arma::mat matrix(numRows, numCols); unsigned int rowIndex = 0; while (rowIndex < numRows) { feedISSOneLine(file); std::vector<double> row {std::istream_iterator<double>{m_iss}, std::istream_iterator<double>{}}; if (row.size() != numCols) throw FileFormatException(filename, NNFileType::WEIGHTS); matrix.row(rowIndex++) = arma::conv_to<arma::rowvec>::from(row); } weights.push_back(matrix); } if (!file.eof()) throw FileFormatException(filename, NNFileType::UNIFIED); }
void FeatureDataIterator::readNext() { m_next.clear(); try { StringPiece marker = m_in->ReadDelimited(); if (marker != StringPiece(FEATURES_TXT_BEGIN)) { throw FileFormatException(m_in->FileName(), marker.as_string()); } size_t sentenceId = m_in->ReadULong(); size_t count = m_in->ReadULong(); size_t length = m_in->ReadULong(); m_in->ReadLine(); //discard rest of line for (size_t i = 0; i < count; ++i) { StringPiece line = m_in->ReadLine(); m_next.push_back(FeatureDataItem()); for (TokenIter<AnyCharacter, true> token(line, AnyCharacter(" \t")); token; ++token) { TokenIter<AnyCharacter,false> value(*token,AnyCharacter(":")); if (!value) throw FileFormatException(m_in->FileName(), line.as_string()); StringPiece first = *value; ++value; if (!value) { //regular feature float floatValue = ParseFloat(first); m_next.back().dense.push_back(floatValue); } else { //sparse feature StringPiece second = *value; float floatValue = ParseFloat(second); m_next.back().sparse.set(first.as_string(),floatValue); } } if (length != m_next.back().dense.size()) { throw FileFormatException(m_in->FileName(), line.as_string()); } } StringPiece line = m_in->ReadLine(); if (line != StringPiece(FEATURES_TXT_END)) { throw FileFormatException(m_in->FileName(), line.as_string()); } } catch (EndOfFileException &e) { m_in.reset(); } }
/// <summary>Reads the language tag and parses the ID</summary> /// <param name="element">The language element</param> /// <returns>Language ID</returns> /// <exception cref="Logic::FileFormatException">Missing language element</exception> /// <exception cref="Logic::InvalidValueException">Invalid language ID</exception> /// <exception cref="Logic::ComException">COM Error</exception> GameLanguage LanguageFileReader::ReadLanguageTag(XmlNodePtr& element) { // Ensure present: "Missing '%s' element" if (element == nullptr) throw FileFormatException(HERE, VString(ERR_XML_MISSING_ELEMENT, L"language")); // Verify tag ReadElement(element, L"language"); // Convert language ID return LanguageFilenameReader::ParseLanguageID(ReadAttribute(element, L"id")); }
PDimension ConfigDatabase::checkAndCreateDimension(PServer server, PDatabase db, const string &name, const ElementItem *elemBegin, const ElementItem *elemEnd, const ElementItem *attrBegin, const ElementItem *attrEnd, bool &dbChanged) { string attrName; if (attrBegin) { attrName = AttributesDimension::PREFIX_ATTRIBUTE_DIMENSION + name + AttributesDimension::SUFFIX_ATTRIBUTE_DIMENSION; } PDimension dim = lookupDimensionByName(name, false); if (!dim) { dim = PDimension(new NormalDimension(name)); dim->setDeletable(false); dim->setRenamable(false); dim->setChangable(true); for (const ElementItem *elem = elemBegin; elem != elemEnd; elem++) { checkAndCreateElement(server, db, dim, elem, dbChanged); } db->addDimension(server, dim, true, true, NULL, NULL, NULL, NULL, false); if (attrBegin) { PDimension attrDim = findDimensionByName(attrName, PUser(), true); for (const ElementItem *elem = attrBegin; elem != attrEnd; elem++) { checkAndCreateElement(server, db, attrDim, elem, dbChanged); } } dbChanged = true; } else { if (dim->getType() != NORMALTYPE) { throw FileFormatException("dimension '" + name + "' corrupted", 0); } for (const ElementItem *elem = elemBegin; elem != elemEnd; elem++) { dim = checkAndCreateElement(server, db, dim, elem, dbChanged); } if (attrBegin) { PDimension attrDim = findDimensionByName(attrName, PUser(), false); for (const ElementItem *elem = attrBegin; elem != attrEnd; elem++) { attrDim = checkAndCreateElement(server, db, attrDim, elem, dbChanged); } } if (dim->isDeletable() || dim->isRenamable()) { if (!dim->isCheckedOut()) { PDimensionList dbs = db->getDimensionList(true); db->setDimensionList(dbs); dim = db->lookupDimension(dim->getId(), true); dbs->set(dim); } dim->setDeletable(false); dim->setRenamable(false); dbChanged = true; } } return dim; }
/// <summary>Called for each occurrence of parameterized macros</summary> /// <param name="match">The match.</param> /// <param name="depth">Debugging output depth</param> /// <returns>Replacement text</returns> /// <exception cref="Logic::FileFormatException">Macro contains wrong number of parameters</exception> /// <exception cref="std::regex_error">RegEx error</exception> wstring DescriptionParser::onMatchMacro(const wsmatch& match, int depth) const { const DescriptionMacro* macro; wstring name = match[1].str(), arguments = match[2].str(); // Lookup macro if (DescriptionLib.Macros.TryFind(name, macro)) { // SpecialCase: If macro has one parameter then format manually (this handles HEADING, which can have commas in it's argument) if (macro->ParamCount == 1) StringCchPrintf(FormatBuffer.get(), BUFFER_LENGTH, macro->Text.c_str(), arguments.c_str()); else { // MACRO: Extract arguments and format them into the replacement text vector<wstring> arg = { L"", L"", L"", L"", L"", L"" }; UINT i = 0; // Separate parameters into array for (wsregex_iterator m(arguments.begin(), arguments.end(), MatchParameters), end; m != end && i <= 5; ++m) arg[i++] = (*m)[0].str(); // Verify argument count if (macro->ParamCount != i) throw FileFormatException(HERE, VString(L"The macro '%s' requires %d parameters : '%s'", macro->Name.c_str(), macro->ParamCount, match[0].str().c_str())); // Format macro with up to six parameters... StringCchPrintf(FormatBuffer.get(), BUFFER_LENGTH, macro->Text.c_str(), arg[0].c_str(), arg[1].c_str(), arg[2].c_str(), arg[3].c_str(), arg[4].c_str(), arg[5].c_str()); } #ifdef PRINT_CONSOLE Console << Cons::Cyan << Indent(depth) << "Matched Macro: " << Cons::Yellow << match[0].str() << Cons::White << " with " << Cons::Yellow << FormatBuffer.get() << ENDL; #endif // Recursively parse return Parse(FormatBuffer.get(), depth+1); } #ifdef PRINT_CONSOLE Console << Cons::Red << Indent(depth) << "Ignored Macro: " << Cons::White << match[0].str() << ENDL; #endif // Failed: Return verbatim return match[0].str(); }
FileReadStateInitialiser::FileReadStateInitialiser( const Parameters ¶ms ) : logger_("StateInitialiser") { // get file name from params std::string filename = params.get<std::string>("file"); // open the archive std::ifstream ifs(filename.c_str()); if (!ifs.good()) { LOG(logger_, error, "problem reading file "<<filename); BOOST_THROW_EXCEPTION(FileFormatException()); } boost::archive::text_iarchive ia(ifs); // restore the schedule from the archive ia >> segmentations_; }
PassPoint PassPoint::load(QXmlStreamReader& xml) { Q_ASSERT(xml.name() == QLatin1String("passpoint")); XmlElementReader passpoint{xml}; PassPoint p; p.error = passpoint.attribute<double>(QLatin1String("error")); while (xml.readNextStartElement()) { QStringRef name = xml.name(); while (xml.readNextStartElement()) { if (xml.name() == QLatin1String("coord")) { try { if (name == QLatin1String("source")) p.src_coords = MapCoordF(MapCoord::load(xml)); else if (name == QLatin1String("destination")) p.dest_coords = MapCoordF(MapCoord::load(xml)); else if (name == QLatin1String("calculated")) p.calculated_coords = MapCoordF(MapCoord::load(xml)); else xml.skipCurrentElement(); // unsupported } catch (std::range_error& e) { throw FileFormatException(MapCoord::tr(e.what())); } } else xml.skipCurrentElement(); // unsupported } } return p; }
void OgrFileImport::import(bool load_symbols_only) { auto file = qobject_cast<QFile*>(stream); if (!file) { throw FileFormatException("Internal error"); /// \todo Review design and/or message } auto filename = file->fileName(); // GDAL 2.0: ... = GDALOpenEx(template_path.toLatin1(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr); auto data_source = ogr::unique_datasource(OGROpen(filename.toLatin1(), 0, nullptr)); if (data_source == nullptr) { throw FileFormatException(Importer::tr("Could not read '%1'") .arg(filename)); } empty_geometries = 0; no_transformation = 0; failed_transformation = 0; unsupported_geometry_type = 0; too_few_coordinates = 0; importStyles(data_source.get()); if (!load_symbols_only) { auto num_layers = OGR_DS_GetLayerCount(data_source.get()); for (int i = 0; i < num_layers; ++i) { auto layer = OGR_DS_GetLayer(data_source.get(), i); if (!layer) { addWarning(tr("Unable to load layer %1.").arg(i)); continue; } auto part = map->getCurrentPart(); if (option(QLatin1String("Separate layers")).toBool()) { if (num_layers > 0) { if (part->getNumObjects() == 0) { part->setName(OGR_L_GetName(layer)); } else { part = new MapPart(OGR_L_GetName(layer), map); auto index = map->getNumParts(); map->addPart(part, index); map->setCurrentPartIndex(index); } } } importLayer(part, layer); } } if (empty_geometries) { addWarning(tr("Unable to load %n objects, reason: %1", nullptr, empty_geometries) .arg(tr("Empty geometry."))); } if (no_transformation) { addWarning(tr("Unable to load %n objects, reason: %1", nullptr, no_transformation) .arg(tr("Can't determine the coordinate transformation: %1").arg(CPLGetLastErrorMsg()))); } if (failed_transformation) { addWarning(tr("Unable to load %n objects, reason: %1", nullptr, failed_transformation) .arg(tr("Failed to transform the coordinates."))); } if (unsupported_geometry_type) { addWarning(tr("Unable to load %n objects, reason: %1", nullptr, unsupported_geometry_type) .arg(tr("Unknown or unsupported geometry type."))); } if (too_few_coordinates) { addWarning(tr("Unable to load %n objects, reason: %1", nullptr, too_few_coordinates) .arg(tr("Not enough coordinates."))); } }
Result TestCaseDrv::exec(const string& fileName) throw (Exception) { const string fileHeadSignature = "[testcase]"; /// debe estar en minuscula int lineNumber = 0; string textLine; Result result(Result::ok); try { ifstream ifs(fileName.c_str()); if (!ifs) throw InvalidTestException("no se encuentra el archivo"); ObjectTable symbolTable; getline(ifs, textLine); tolowerstr(trim(textLine)); if (textLine != fileHeadSignature) throw FileFormatException(fileName, "encabezado incorrecto (no es [testcase])"); lineNumber++; while (!ifs.eof()) { getline(ifs, textLine); trim(textLine); lineNumber++; if (!textLine.empty() and textLine[0] != '#') { #ifndef NDEBUG cout << " DEBUG " << " cmd: " << textLine << endl; //! DEBUG #endif cmd::exec(textLine, symbolTable, result); } } } // TODO: hay que factorizar el código que sigue. está despolijro. catch (ErrorTestException& e) { return Result(Result::error, "caso de test: " + fileName + '\n' + "linea " + toString<int>(lineNumber) + ": " + textLine + '\n' + "problema: " + e.message()); } catch (InvalidTestException& e) { // TODO tengo la sospecha de que este código es inalcanzable. revisarlo. // debiera venirse acá cuando hay parámetros de más, de menos, errores // de tipo y cosas similares. en todo caso, puede que haya que corregir // cmd.cpp en lugar de quitar esta sección. return Result(Result::invalid, "caso de test: " + fileName + '\n' + "linea " + toString<int>(lineNumber) + ": " + textLine + '\n' + "problema: " + e.message()); // FIXME: ojo. invalidTest también es arrojada por fileNotFound y se ve // feíto que el error diga "linea 0: problema: bla, bla". no me gusta el // "linea 0". :-( } catch (std::ios_base::failure& e) { return Result(Result::invalid, "caso de test: " + fileName + '\n' + "linea " + toString<int>(lineNumber) + ": " + textLine + '\n' + "problema: E/S incorrecta - " + e.what() + '\n' + "esta es una excepcion tal vez causada por el operador >> o el <<, por la falta de un parametro, " + "por haberse encontrado un tipo de datos no esperado, o vaya uno a saber por que. " + "quiero creer que escribiste 'true' y 'false' BIEN y en minusculas."); } catch (BadTypeException& e) { return Result(Result::invalid, "caso de test: " + fileName + '\n' + "linea " + toString<int>(lineNumber) + ": " + textLine + '\n' + "problema: " + e.message()); } catch (FileFormatException& e) { return Result(Result::invalid, "caso de test: " + e.fileName() + '\n' + "problema: formato invalido - " + e.message()); } catch (UsageException& e) { throw UsageException("caso de test: " + fileName + '\n' + "linea " + toString<int>(lineNumber) + ": " + textLine + '\n' + "problema: " + e.message()); // sube como Exception // TODO para uniformar, ver si conviene que esta excepción también // suba como Result::invalid o algo así. si se encuentra una buena razón // para que no suceda, habría que documentarla. } return result; }
void NativeFileImport::import(bool load_symbols_only) { addWarning(Importer::tr("This file uses an obsolete format. " "Support for this format is to be removed from this program soon. " "To be able to open the file in the future, save it again.")); MapCoord::boundsOffset().reset(true); char buffer[4]; stream->read(buffer, 4); // read the magic int version; stream->read((char*)&version, sizeof(int)); if (version < 0) { addWarning(Importer::tr("Invalid file format version.")); } else if (version < NativeFileFormat::least_supported_file_format_version) { throw FileFormatException(Importer::tr("Unsupported old file format version. Please use an older program version to load and update the file.")); } else if (version > NativeFileFormat::current_file_format_version) { throw FileFormatException(Importer::tr("Unsupported new file format version. Some map features will not be loaded or saved by this version of the program. Consider updating.")); } if (version <= 16) { Georeferencing georef; stream->read((char*)&georef.scale_denominator, sizeof(int)); if (version >= 15) loadString(stream, map->map_notes); bool gps_projection_params_set; // obsolete stream->read((char*)&gps_projection_params_set, sizeof(bool)); GPSProjectionParameters gps_projection_parameters; // obsolete stream->read((char*)&gps_projection_parameters, sizeof(GPSProjectionParameters)); if (gps_projection_params_set) { LatLon ref_point = LatLon::fromRadiant(gps_projection_parameters.center_latitude, gps_projection_parameters.center_longitude); georef.setGeographicRefPoint(ref_point); } *map->georeferencing = georef; } else if (version >= 17) { loadString(stream, map->map_notes); Georeferencing georef; stream->read((char*)&georef.scale_denominator, sizeof(int)); double value; if (version >= 18) { stream->read((char*)&value, sizeof(double)); georef.declination = Georeferencing::roundDeclination(value); } stream->read((char*)&value, sizeof(double)); georef.grivation = Georeferencing::roundDeclination(value); georef.grivation_error = value - georef.grivation; double x,y; stream->read((char*)&x, sizeof(double)); stream->read((char*)&y, sizeof(double)); georef.map_ref_point = MapCoord(x,y); stream->read((char*)&x, sizeof(double)); stream->read((char*)&y, sizeof(double)); georef.projected_ref_point = QPointF(x,y); loadString(stream, georef.projected_crs_id); loadString(stream, georef.projected_crs_spec); stream->read((char*)&y, sizeof(double)); stream->read((char*)&x, sizeof(double)); georef.geographic_ref_point = LatLon::fromRadiant(y, x); QString geographic_crs_id, geographic_crs_spec; loadString(stream, geographic_crs_id); // reserved for geographic crs id loadString(stream, geographic_crs_spec); // reserved for full geographic crs specification if (geographic_crs_spec != Georeferencing::geographic_crs_spec) { addWarning( Importer::tr("The geographic coordinate reference system of the map was \"%1\". This CRS is not supported. Using \"%2\"."). arg(geographic_crs_spec). arg(Georeferencing::geographic_crs_spec) ); } if (version <= 17) georef.initDeclination(); // Correctly set georeferencing state georef.setProjectedCRS(georef.projected_crs_id, georef.projected_crs_spec); *map->georeferencing = georef; } if (version >= 24) map->setGrid(MapGrid().load(stream, version)); map->renderable_options = Symbol::RenderNormal; if (version >= 25) { bool area_hatching_enabled, baseline_view_enabled; stream->read((char*)&area_hatching_enabled, sizeof(bool)); stream->read((char*)&baseline_view_enabled, sizeof(bool)); if (area_hatching_enabled) map->renderable_options |= Symbol::RenderAreasHatched; if (baseline_view_enabled) map->renderable_options |= Symbol::RenderBaselines; } if (version >= 6) { bool print_params_set; stream->read((char*)&print_params_set, sizeof(bool)); if (print_params_set) { MapPrinterConfig printer_config(*map); stream->read((char*)&printer_config.page_format.orientation, sizeof(int)); stream->read((char*)&printer_config.page_format.paper_size, sizeof(int)); float resolution; stream->read((char*)&resolution, sizeof(float)); printer_config.options.resolution = qRound(resolution); stream->read((char*)&printer_config.options.show_templates, sizeof(bool)); if (version >= 24) stream->read((char*)&printer_config.options.show_grid, sizeof(bool)); else printer_config.options.show_grid = false; stream->read((char*)&printer_config.center_print_area, sizeof(bool)); float print_area_left, print_area_top, print_area_width, print_area_height; stream->read((char*)&print_area_left, sizeof(float)); stream->read((char*)&print_area_top, sizeof(float)); stream->read((char*)&print_area_width, sizeof(float)); stream->read((char*)&print_area_height, sizeof(float)); printer_config.print_area = QRectF(print_area_left, print_area_top, print_area_width, print_area_height); if (version >= 26) { bool print_different_scale_enabled; stream->read((char*)&print_different_scale_enabled, sizeof(bool)); stream->read((char*)&printer_config.options.scale, sizeof(int)); if (!print_different_scale_enabled) printer_config.options.scale = map->getScaleDenominator(); } map->setPrinterConfig(printer_config); } } if (version >= 16) { stream->read((char*)&map->image_template_use_meters_per_pixel, sizeof(bool)); stream->read((char*)&map->image_template_meters_per_pixel, sizeof(double)); stream->read((char*)&map->image_template_dpi, sizeof(double)); stream->read((char*)&map->image_template_scale, sizeof(double)); } // Load colors int num_colors; stream->read((char*)&num_colors, sizeof(int)); map->color_set->colors.resize(num_colors); for (int i = 0; i < num_colors; ++i) { int priority; stream->read((char*)&priority, sizeof(int)); MapColor* color = new MapColor(priority); MapColorCmyk cmyk; stream->read((char*)&cmyk.c, sizeof(float)); stream->read((char*)&cmyk.m, sizeof(float)); stream->read((char*)&cmyk.y, sizeof(float)); stream->read((char*)&cmyk.k, sizeof(float)); color->setCmyk(cmyk); float opacity; stream->read((char*)&opacity, sizeof(float)); color->setOpacity(opacity); QString name; loadString(stream, name); color->setName(name); map->color_set->colors[i] = color; } // Load symbols int num_symbols; stream->read((char*)&num_symbols, sizeof(int)); map->symbols.resize(num_symbols); for (int i = 0; i < num_symbols; ++i) { QScopedValueRollback<MapCoord::BoundsOffset> offset { MapCoord::boundsOffset() }; MapCoord::boundsOffset().reset(false); int symbol_type; stream->read((char*)&symbol_type, sizeof(int)); Symbol* symbol = Symbol::getSymbolForType(static_cast<Symbol::Type>(symbol_type)); if (!symbol) { throw FileFormatException(Importer::tr("Error while loading a symbol with type %2.").arg(symbol_type)); } if (!symbol->load(stream, version, map)) { throw FileFormatException(Importer::tr("Error while loading a symbol.")); } map->symbols[i] = symbol; } if (!load_symbols_only) { // Load templates stream->read((char*)&map->first_front_template, sizeof(int)); int num_templates; stream->read((char*)&num_templates, sizeof(int)); map->templates.resize(num_templates); for (int i = 0; i < num_templates; ++i) { QString path; loadString(stream, path); auto temp = Template::templateForFile(path, map); if (!temp) temp.reset(new TemplateImage(path, map)); // fallback if (version >= 27) { loadString(stream, path); temp->setTemplateRelativePath(path); } temp->loadTemplateConfiguration(stream, version); map->templates[i] = temp.release(); } if (version >= 28) { int num_closed_templates; stream->read((char*)&num_closed_templates, sizeof(int)); map->closed_templates.resize(num_closed_templates); for (int i = 0; i < num_closed_templates; ++i) { QString path; loadString(stream, path); auto temp = Template::templateForFile(path, map); if (!temp) temp.reset(new TemplateImage(path, map)); // fallback loadString(stream, path); temp->setTemplateRelativePath(path); temp->loadTemplateConfiguration(stream, version); map->closed_templates[i] = temp.release(); } } // Restore widgets and views if (view) { view->load(stream, version); } else { MapView tmp{ map }; tmp.load(stream, version); } // Load undo steps if (version >= 7) { if (!map->undoManager().load(stream, version)) { throw FileFormatException(Importer::tr("Error while loading undo steps.")); } } // Load parts stream->read((char*)&map->current_part_index, sizeof(int)); int num_parts; if (stream->read((char*)&num_parts, sizeof(int)) < (int)sizeof(int)) { throw FileFormatException(Importer::tr("Error while reading map part count.")); } delete map->parts[0]; map->parts.resize(num_parts); for (int i = 0; i < num_parts; ++i) { MapPart* part = new MapPart({}, map); if (!part->load(stream, version, map)) { throw FileFormatException(Importer::tr("Error while loading map part %2.").arg(i+1)); } map->parts[i] = part; } } emit map->currentMapPartIndexChanged(map->current_part_index); emit map->currentMapPartChanged(map->getPart(map->current_part_index)); }
PhraseSegmentation NistXmlStateInitialiser::initSegmentation( boost::shared_ptr<const PhrasePairCollection> phraseTranslations, const std::vector<Word> &sentence, int documentNumber, int sentenceNumber ) const { if(sentence.empty()) return PhraseSegmentation(); std::vector<AnchoredPhrasePair> ppvec; phraseTranslations->copyPhrasePairs(std::back_inserter(ppvec)); CompareAnchoredPhrasePairs ppComparator; std::sort(ppvec.begin(), ppvec.end(), ppComparator); PhraseSegmentation seg; PhraseData tgtpd; for(PlainTextDocument::const_word_iterator it = documents_[documentNumber].sentence_begin(sentenceNumber); it != documents_[documentNumber].sentence_end(sentenceNumber); ++it ) { if((*it).substr(0, 1) != "|") { // word tgtpd.push_back(*it); continue; } // end of hypothesis Word token((*it).substr(1, (*it).length()-2)); std::vector<Word> srctokenrange; // metadata boost::split( srctokenrange, token, boost::is_any_of("-"), boost::token_compress_on ); PhraseData srcpd; CoverageBitmap cov(sentence.size()); try { if(srctokenrange.size() != 2) { BOOST_THROW_EXCEPTION(FileFormatException()); } for(uint i = boost::lexical_cast<uint>(srctokenrange.front()); i <= boost::lexical_cast<uint>(srctokenrange.back()); ++i ) { srcpd.push_back(sentence[i]); cov.set(i); } } catch(boost::exception &) { LOG(logger_, error, "Invalid alignment data in raw-translation file " "(document " << documentNumber << ", " " sentence " << sentenceNumber << "): " << *it ); throw; } std::vector<AnchoredPhrasePair>::const_iterator appit = std::lower_bound( ppvec.begin(), ppvec.end(), CompareAnchoredPhrasePairs::PhrasePairKey(cov, srcpd, tgtpd), ppComparator ); seg.push_back(*appit); tgtpd.clear(); } return seg; }
void Importer::doImport(bool load_symbols_only, const QString& map_path) { import(load_symbols_only); // Object post processing: // - make sure that there is no object without symbol // - make sure that all area-only path objects are closed // - make sure that there are no special points in wrong places (e.g. curve starts inside curves) for (int p = 0; p < map->getNumParts(); ++p) { MapPart* part = map->getPart(p); for (int o = 0; o < part->getNumObjects(); ++o) { Object* object = part->getObject(o); if (object->getSymbol() == NULL) { addWarning(Importer::tr("Found an object without symbol.")); if (object->getType() == Object::Point) object->setSymbol(map->getUndefinedPoint(), true); else if (object->getType() == Object::Path) object->setSymbol(map->getUndefinedLine(), true); else { // There is no undefined symbol for this type of object, delete the object part->deleteObject(o, false); --o; continue; } } if (object->getType() == Object::Path) { PathObject* path = object->asPath(); Symbol::Type contained_types = path->getSymbol()->getContainedTypes(); if (contained_types & Symbol::Area && !(contained_types & Symbol::Line)) path->closeAllParts(); for (MapCoordVector::size_type i = 0; i < path->getCoordinateCount(); ++i) { if (path->getCoordinate(i).isCurveStart()) { if (i+3 >= path->getCoordinateCount()) { path->getCoordinate(i).setCurveStart(false); continue; } if (path->getCoordinate(i + 1).isClosePoint() || path->getCoordinate(i + 1).isHolePoint() || path->getCoordinate(i + 2).isClosePoint() || path->getCoordinate(i + 2).isHolePoint()) { path->getCoordinate(i).setCurveStart(false); continue; } path->getCoordinate(i + 1).setCurveStart(false); path->getCoordinate(i + 1).setDashPoint(false); path->getCoordinate(i + 2).setCurveStart(false); path->getCoordinate(i + 2).setDashPoint(false); i += 2; } if (i > 0 && path->getCoordinate(i).isHolePoint()) { if (path->getCoordinate(i-1).isHolePoint()) path->deleteCoordinate(i, false); } } } } } // Symbol post processing for (int i = 0; i < map->getNumSymbols(); ++i) { if (!map->getSymbol(i)->loadFinished(map)) throw FileFormatException(Importer::tr("Error during symbol post-processing.")); } // Template loading: try to find all template files bool have_lost_template = false; for (int i = 0; i < map->getNumTemplates(); ++i) { Template* temp = map->getTemplate(i); bool loaded_from_template_dir = false; temp->tryToFindAndReloadTemplateFile(map_path, &loaded_from_template_dir); if (loaded_from_template_dir) addWarning(Importer::tr("Template \"%1\" has been loaded from the map's directory instead of the relative location to the map file where it was previously.").arg(temp->getTemplateFilename())); if (temp->getTemplateState() != Template::Loaded) have_lost_template = true; } if (have_lost_template) { #if defined(Q_OS_ANDROID) addWarning(tr("At least one template file could not be found.")); #else addWarning(tr("At least one template file could not be found.") + " " + tr("Click the red template name(s) in the Templates -> Template setup window to locate the template file name(s).")); #endif } }