void Wiegand37WithFacilityFormat::getLinearDataWithoutParity(void* data, size_t dataLengthBytes) const { unsigned int pos = 1; convertField(data, dataLengthBytes, &pos, getFacilityCode(), 16); convertField(data, dataLengthBytes, &pos, getUid(), 19); }
QString KicadSchematic2Svg::convertText(const QString & line) { QStringList fs = splitLine(line); if (fs.count() < 8) { DebugDialog::debug("bad text " + line); return ""; } return convertField(fs[2], fs[3], fs[4], fs[1], "C", "C", fs[8]); }
QString KicadSchematic2Svg::convertField(const QString & line) { QStringList fs = splitLine(line); if (fs.count() < 7) { DebugDialog::debug("bad field " + line); return ""; } if (fs[6] == "I") { // invisible return ""; } while (fs.count() < 9) { fs.append(""); } return convertField(fs[2], fs[3], fs[4], fs[5], fs[7], fs[8], fs[1]); }
QString KicadSchematic2Svg::convert(const QString & filename, const QString & defName) { initLimits(); QFile file(filename); if (!file.open(QFile::ReadOnly)) { throw QObject::tr("unable to open %1").arg(filename); } QTextStream textStream(&file); QString metadata = makeMetadata(filename, "schematic part", defName); metadata += endMetadata(); QString reference; int textOffset = 0; bool drawPinNumber = true; bool drawPinName = true; bool gotDef = false; while (true) { QString line = textStream.readLine(); if (line.isNull()) { break; } if (line.startsWith("DEF") && line.contains(defName, Qt::CaseInsensitive)) { QStringList defs = splitLine(line); if (defs.count() < 8) { throw QObject::tr("bad schematic definition %1").arg(filename); } reference = defs[2]; textOffset = defs[4].toInt(); drawPinName = defs[6] == "Y"; drawPinNumber = defs[5] == "Y"; gotDef = true; break; } } if (!gotDef) { throw QObject::tr("schematic part %1 not found in %2").arg(defName).arg(filename); } QString contents = "<g id='schematic'>\n"; bool inFPLIST = false; while (true) { QString fline = textStream.readLine(); if (fline.isNull()) { throw QObject::tr("schematic %1 unexpectedly ends (1) in %2").arg(defName).arg(filename); } if (fline.contains("ENDDEF")) { throw QObject::tr("schematic %1 unexpectedly ends (2) in %2").arg(defName).arg(filename); } if (fline.startsWith("DRAW")) { break; } if (fline.startsWith("ALIAS")) continue; if (fline.startsWith("F")) { contents += convertField(fline); continue; } if (fline.startsWith("$FPLIST")) { inFPLIST = true; break; } } while (inFPLIST) { QString fline = textStream.readLine(); if (fline.isNull()) { throw QObject::tr("schematic %1 unexpectedly ends (1) in %2").arg(defName).arg(filename); } if (fline.startsWith("$ENDFPLIST")) { inFPLIST = false; break; } if (fline.contains("ENDDEF")) { throw QObject::tr("schematic %1 unexpectedly ends (2) in %2").arg(defName).arg(filename); } } int pinIndex = 0; while (true) { QString line = textStream.readLine(); if (line.isNull()) { throw QObject::tr("schematic %1 unexpectedly ends (3) in %2").arg(defName).arg(filename); } if (line.startsWith("DRAW")) { continue; } if (line.contains("ENDDEF")) { break; } if (line.contains("ENDDRAW")) { break; } if (line.startsWith("S")) { contents += convertRect(line); } else if (line.startsWith("X")) { // need to look at them all before formatting (I think) contents += convertPin(line, textOffset, drawPinName, drawPinNumber, pinIndex++); } else if (line.startsWith("C")) { contents += convertCircle(line); } else if (line.startsWith("P")) { contents += convertPoly(line); } else if (line.startsWith("A")) { contents += convertArc(line); } else if (line.startsWith("T")) { contents += convertText(line); } else { DebugDialog::debug("Unknown line " + line); } } contents += "</g>\n"; QString svg = TextUtils::makeSVGHeader(GraphicsUtils::StandardFritzingDPI, GraphicsUtils::StandardFritzingDPI, m_maxX - m_minX, m_maxY - m_minY) + m_title + m_description + metadata + offsetMin(contents) + "</svg>"; return svg; }
QString KicadSchematic2Svg::convertPin(const QString & line, int textOffset, bool drawPinName, bool drawPinNumber, int pinIndex) { QStringList l = splitLine(line); if (l.count() < 12) { DebugDialog::debug(QString("bad line %1").arg(line)); return ""; } if (l[6].length() != 1) { DebugDialog::debug(QString("bad orientation %1").arg(line)); return ""; } if (l.count() > 12 && l[12] == "N") { // don't draw this return ""; } int unit = l[9].toInt(); if (unit > 1) { // don't draw this return ""; } QChar orientation = l[6].at(0); QString name = l[1]; if (name == "~") { name = ""; } bool pinNumberOK; int pinNumber = l[2].toInt(&pinNumberOK); if (!pinNumberOK) { pinNumber = pinIndex; } int nFontSize = l[7].toInt(); int x1 = l[3].toInt(); int y1 = -l[4].toInt(); // KiCad flips y-axis w.r.t. svg int length = l[5].toInt(); int x2 = x1; int y2 = y1; int x3 = x1; int y3 = y1; int x4 = x1; int y4 = y1; QString justify = "C"; bool rotate = false; switch (orientation.toLatin1()) { case 'D': y2 = y1 + length; y3 = y1 + (length / 2); if (textOffset == 0) { x3 += nFontSize / 2; x4 -= nFontSize / 2; y4 = y3; justify = "C"; } else { x3 -= nFontSize / 2; y4 = y2; justify = "R"; } rotate = true; break; case 'U': y2 = y1 - length; y3 = y1 - (length / 2); if (textOffset == 0) { x3 += nFontSize / 2; x4 -= nFontSize / 2; y4 = y3; justify = "C"; } else { x3 -= nFontSize / 2; y4 = y2; justify = "L"; } rotate = true; break; case 'L': x2 = x1 - length; x3 = x1 - (length / 2); if (textOffset == 0) { y3 += nFontSize / 2; y4 -= nFontSize / 2; x4 = x3; justify = "C"; } else { y3 -= nFontSize / 2; x4 = x2; justify = "R"; } break; case 'R': x2 = x1 + length; x3 = x1 + (length / 2); if (textOffset == 0) { y3 += nFontSize / 2; y4 -= nFontSize / 2; x4 = x3; justify = "C"; } else { y3 -= nFontSize / 2; x4 = x2; justify = "L"; } break; default: DebugDialog::debug(QString("bad orientation %1").arg(line)); break; } checkXLimit(x1); checkXLimit(x2); checkYLimit(y1); checkYLimit(y2); int thickness = 1; QString pin = QString("<line fill='none' stroke='#000000' x1='%1' y1='%2' x2='%3' y2='%4' stroke-width='%5' id='connector%6pin' connectorname='%7' />\n") .arg(x1) .arg(y1) .arg(x2) .arg(y2) .arg(thickness) .arg(pinNumber) .arg(TextUtils::escapeAnd(name)); pin += QString("<rect fill='none' x='%1' y='%2' width='0' height='0' stroke-width='0' id='connector%3terminal' />\n") .arg(x1) .arg(y1) .arg(pinNumber); if (drawPinNumber) { pin += convertField(QString::number(x3), QString::number(-y3), l[7], rotate ? "V" : "H", "C", "C", l[2]); } if (drawPinName && !name.isEmpty()) { pin += convertField(QString::number(x4), QString::number(-y4), l[8], rotate ? "V" : "H", justify, "C", name); } return pin; }
void FASCN200BitFormat::getLinearData(void* data, size_t dataLengthBytes) const { unsigned int pos = 0; convertField(data, dataLengthBytes, &pos, FASCN_SS, 4); convertField(data, dataLengthBytes, &pos, getAgencyCode(), 16); convertField(data, dataLengthBytes, &pos, FASCN_FS, 4); convertField(data, dataLengthBytes, &pos, getSystemCode(), 16); convertField(data, dataLengthBytes, &pos, FASCN_FS, 4); convertField(data, dataLengthBytes, &pos, getUid(), 24); convertField(data, dataLengthBytes, &pos, FASCN_FS, 4); convertField(data, dataLengthBytes, &pos, d_formatLinear.d_serieCode, 4); convertField(data, dataLengthBytes, &pos, FASCN_FS, 4); convertField(data, dataLengthBytes, &pos, d_formatLinear.d_credentialCode, 4); convertField(data, dataLengthBytes, &pos, FASCN_FS, 4); convertField(data, dataLengthBytes, &pos, getPersonIdentifier(), 40); convertField(data, dataLengthBytes, &pos, getOrganizationalCategory(), 4); convertField(data, dataLengthBytes, &pos, getOrganizationalIdentifier(), 16); convertField(data, dataLengthBytes, &pos, getPOACategory(), 4); convertField(data, dataLengthBytes, &pos, FASCN_ES, 4); unsigned char lrc = calculateLRC(data, pos); convertField(data, dataLengthBytes, &pos, lrc, 4); }
void Wiegand37Format::getLinearDataWithoutParity(void* data, size_t dataLengthBytes) const { unsigned int pos = 1; convertField(data, dataLengthBytes, &pos, getUid(), 35); }
QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer( const QString& uri, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem& srs, bool overwrite, QMap<int, int> *oldToNewAttrIdxMap, QString *errorMessage, const QMap<QString, QVariant> *options ) { Q_UNUSED( options ); // populate members from the uri structure QgsDataSourceUri dsUri( uri ); QString connInfo = dsUri.connectionInfo(); QString errMsg; QString srsName; QgsDebugMsg( "uri: " + uri ); // connect to database QSqlDatabase db = QgsDb2Provider::getDatabase( connInfo, errMsg ); if ( !errMsg.isEmpty() ) { if ( errorMessage ) *errorMessage = errMsg; return QgsVectorLayerImport::ErrConnectionFailed; } // Get the SRS name using srid, needed to register the spatial column // srs->posgisSrid() seems to return the authority id which is // most often the EPSG id. Hopefully DB2 has defined an SRS using this // value as the srid / srs_id. If not, we are out of luck. QgsDebugMsg( "srs: " + srs.toWkt() ); long srid = srs.postgisSrid(); QgsDebugMsg( QString( "srid: %1" ).arg( srid ) ); if ( srid >= 0 ) { QSqlQuery query( db ); QString statement = QString( "SELECT srs_name FROM db2gse.st_spatial_reference_systems where srs_id=%1" ) .arg( srid ); QgsDebugMsg( statement ); if ( !query.exec( statement ) || !query.isActive() ) { QgsDebugMsg( query.lastError().text() ); } if ( query.next() ) { srsName = query.value( 0 ).toString(); QgsDebugMsg( QString( "srs_name: %1" ).arg( srsName ) ); } else { QgsDebugMsg( "Couldn't get srs_name from db2gse.st_spatial_reference_systems" ); } } QString schemaName = dsUri.schema().toUpper(); QString tableName = dsUri.table().toUpper(); QString fullName; if ( schemaName.isEmpty() ) { schemaName = dsUri.username().toUpper(); // set schema to user name } fullName = schemaName + "." + tableName; QString geometryColumn = dsUri.geometryColumn().toUpper(); QString primaryKey = dsUri.keyColumn().toUpper(); QString primaryKeyType; // TODO - this is a bad hack to cope with shapefiles. // The wkbType from the shapefile header is usually a multi-type // even if all the data is a single-type. If we create the column as // a multi-type, the insert will fail if the actual data is a single-type // due to type mismatch. // We could potentially defer adding the spatial column until addFeatures is // called the first time, but QgsVectorLayerImport doesn't pass the CRS/srid // information to the DB2 provider and we need this information to register // the spatial column. // This hack is problematic because the drag/drop will fail if the // actual data is a multi-type which is possible with a shapefile or // other data source. QgsWkbTypes::Type wkbTypeSingle; wkbTypeSingle = QgsWkbTypes::singleType( wkbType ); if ( wkbType != QgsWkbTypes::NoGeometry && geometryColumn.isEmpty() ) geometryColumn = "GEOM"; if ( primaryKey.isEmpty() ) primaryKey = "QGS_FID"; // get the pk's name and type // if no pk name was passed, define the new pk field name int fieldCount = fields.size(); if ( primaryKey.isEmpty() ) { int index = 0; QString pk = primaryKey = "QGS_FID"; for ( int i = 0; i < fieldCount; ++i ) { if ( fields.at( i ).name() == primaryKey ) { // it already exists, try again with a new name primaryKey = QString( "%1_%2" ).arg( pk ).arg( index++ ); i = 0; } } } else { // search for the passed field for ( int i = 0; i < fieldCount; ++i ) { if ( fields.at( i ).name() == primaryKey ) { // found, get the field type QgsField fld = fields.at( i ); if ( convertField( fld ) ) { primaryKeyType = fld.typeName(); } } } } QgsDebugMsg( "primaryKeyType: '" + primaryKeyType + "'" ); QString sql; QSqlQuery q = QSqlQuery( db ); q.setForwardOnly( true ); // get wkb type and dimension QString geometryType; int dim = 2; db2WkbTypeAndDimension( wkbTypeSingle, geometryType, dim ); QgsDebugMsg( QString( "wkbTypeSingle: %1; geometryType: %2" ).arg( wkbTypeSingle ).arg( geometryType ) ); if ( overwrite ) { // remove the old table with the same name sql = "DROP TABLE " + fullName; if ( !q.exec( sql ) ) { if ( q.lastError().number() != -206 ) // -206 is "not found" just ignore { QString lastError = q.lastError().text(); QgsDebugMsg( lastError ); if ( errorMessage ) { *errorMessage = lastError; } return QgsVectorLayerImport::ErrCreateLayer; } } } // add fields to the layer if ( oldToNewAttrIdxMap ) oldToNewAttrIdxMap->clear(); QString attr2Create = ""; if ( fields.size() > 0 ) { int offset = 0; // get the list of fields QgsDebugMsg( "PrimaryKey: '" + primaryKey + "'" ); for ( int i = 0; i < fieldCount; ++i ) { QgsField fld = fields.field( i ); QgsDebugMsg( QString( "i: %1; fldIdx: %2; offset: %3" ) .arg( i ).arg( fields.lookupField( fld.name() ) ).arg( offset ) ); if ( oldToNewAttrIdxMap && fld.name() == primaryKey ) { oldToNewAttrIdxMap->insert( i , 0 ); continue; } if ( fld.name() == geometryColumn ) { // Found a field with the same name of the geometry column. Skip it! continue; } QString db2Field = qgsFieldToDb2Field( fld ); if ( db2Field.isEmpty() ) { if ( errorMessage ) { *errorMessage = QObject::tr( "Unsupported type for field %1" ).arg( fld.name() ); } return QgsVectorLayerImport::ErrAttributeTypeUnsupported; } if ( oldToNewAttrIdxMap ) { oldToNewAttrIdxMap->insert( fields.lookupField( fld.name() ), offset++ ); } attr2Create += ',' + db2Field.toUpper(); } QgsDebugMsg( attr2Create ); if ( !geometryColumn.isEmpty() ) { sql = QString( // need to set specific geometry type "CREATE TABLE %1(%2 BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, " "%3 DB2GSE.%4 %5) " ) .arg( fullName, primaryKey, geometryColumn, geometryType, attr2Create ); } else { //geometryless table sql = QString( // need to set specific geometry type "CREATE TABLE %1.%2(%3 INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS %4) " ) .arg( schemaName, tableName, primaryKey, attr2Create ); } QgsDebugMsg( sql ); if ( !q.exec( sql ) ) { QString lastError = q.lastError().text(); QgsDebugMsg( lastError ); if ( errorMessage ) { *errorMessage = lastError; } return QgsVectorLayerImport::ErrCreateLayer; } if ( !geometryColumn.isEmpty() ) { int computeExtents = 0; int msgCode = 0; int outCode; int outMsg; QVariant msgText( " " ); QSqlQuery query( db ); int db2Environment = ENV_LUW; // get the environment QgsDb2GeometryColumns gc( db ); int rc = gc.open( schemaName, tableName ); // returns SQLCODE if failure if ( rc == 0 ) { db2Environment = gc.db2Environment(); } if ( ENV_LUW == db2Environment ) { sql = QString( "CALL DB2GSE.ST_Register_Spatial_Column(?, ?, ?, ?, ?, ?, ?)" ); outCode = 5; outMsg = 6; } else // z/OS doesn't support 'computeExtents' parameter and has different schema { sql = QString( "CALL SYSPROC.ST_Register_Spatial_Column(?, ?, ?, ?, ?, ?)" ); outCode = 4; outMsg = 5; } query.prepare( sql ); query.bindValue( 0, schemaName ); query.bindValue( 1, tableName ); query.bindValue( 2, geometryColumn ); query.bindValue( 3, srsName ); if ( ENV_LUW == db2Environment ) { query.bindValue( 4, computeExtents ); } query.bindValue( outCode, msgCode, QSql::Out ); query.bindValue( outMsg, msgText, QSql::Out ); if ( !query.exec() ) { QgsDebugMsg( QString( "error: %1; sql: %2" ).arg( query.lastError().text(), query.lastQuery() ) ); } else { msgCode = query.boundValue( outCode ).toInt(); msgText = query.boundValue( outMsg ).toString(); // never gets a value... if ( 0 != msgCode ) { QgsDebugMsg( QString( "Register failed with code: %1; text: '%2'" ).arg( msgCode ).arg( msgText.toString() ) ); } else { QgsDebugMsg( "Register successful" ); } } QList<QVariant> list = query.boundValues().values(); for ( int i = 0; i < list.size(); ++i ) { QgsDebugMsg( QString( "i: %1; value: %2; type: %3" ) .arg( i ).arg( list.at( i ).toString().toLatin1().data() ).arg( list.at( i ).typeName() ) ); } } // clear any resources hold by the query q.clear(); q.setForwardOnly( true ); } QgsDebugMsg( "successfully created empty layer" ); return QgsVectorLayerImport::NoError; }