Example #1
0
FElem& FElem::operator=(FElem&& other) {
    if (fieldType() == other.fieldType() || fieldType() == AGNOSTIC) {
        elem_ = ::std::move(other.elem_);
    } else if (other.elem_->fieldType() != AGNOSTIC) {
        GADGETLIB_FATAL("Attempted to move assign field element of incorrect type");
    } else {
        *elem_ = dynamic_cast<FConst*>(other.elem_.get())->asLong();
    }
    return *this;
}
Example #2
0
/*!
 * Calculates the compnents of a field
 * @param[in] field field 
 * @return size of the field
 */
uint8_t VTKUnstructuredGrid::calcFieldComponents( const VTKField &field ){

    uint8_t comp ;
    std::string name( field.getName() ) ;

    if( name == "Points" ){
        comp = static_cast<int>(VTKFieldType::VECTOR) ; 

    } else if( name == "offsets" ){
        comp = 1 ;

    } else if( name == "types" ){
        comp = 1 ;

    } else if( name == "connectivity" ){
       if( m_homogeneousType != VTKElementType::UNDEFINED){
            comp = vtk::getNNodeInElement( m_homogeneousType ) ;

       } else {
           comp = 1;

       }

    } else{

        VTKFieldType fieldType( field.getFieldType() ) ;
        assert( fieldType != VTKFieldType::UNDEFINED) ;

        comp = static_cast<uint8_t>(fieldType) ;

    }

    return comp ;

};
 void FieldManager::parseCSV(const std::string& fileName) {
   if (fileName.rfind(".csv") != fileName.length() - 4) {
     throw wrongFileFormatException("file is not .csv file");
   }
   std::fstream fin(fileName.c_str());
   if (!fin.good()) {
     fin.close();
     throw fileNotFoundException();
   }
   std::string buf;
   field = fieldType();
   char symbol;
   std::vector<bool> line;
   while(std::getline(fin, buf, '\n')) {
     for(ll i = 0; i < buf.size(); i++) {
       symbol = buf[i];
       if (symbol != ',') {
         if (symbol == '1') {
           line.push_back(true);
         } else if (symbol == '0'){
           line.push_back(false);
         } else if (!std::isspace(symbol)) {
           fin.close();
           throw wrongFileFormatException("incorrect CSV file");
         }
       }
     }
     field.push_back(line);
     line.clear();
   }
   fin.close();
 }
Example #4
0
QString QDBCFile::getIntegerTextRepresentation(quint32 field, quint32 row, int base) const
{
    switch(fieldType(field)) {
    case FIELD_TYPE_INT:
        return QString::number(getInt(field,row),base);
    default:
        return QString::number(getUInt(field,row),base);
    }
}
Example #5
0
void ResultMetadata::init(MYSQL_STMT* stmt)
{
	ResultMetadataHandle h(stmt);

	if (!h)
	{
		// all right, it is normal
		// querys such an "INSERT INTO" just does not have result at all
		reset();
		return;
	}

	std::size_t count = mysql_num_fields(h);
	MYSQL_FIELD* fields = mysql_fetch_fields(h);

	std::size_t commonSize = 0;
	_columns.reserve(count);

	{for (std::size_t i = 0; i < count; i++)
	{
		std::size_t size = fieldSize(fields[i]);
		std::size_t zero = 0;
		if (size == ~zero) size = 0;

		_columns.push_back(MetaColumn(
			i,                               // position
			fields[i].name,                  // name
			fieldType(fields[i]),            // type
			size,                            // length
			0,                               // TODO: precision
			!IS_NOT_NULL(fields[i].flags)    // nullable
			));

		commonSize += _columns[i].length();
	}}

	_buffer.resize(commonSize);
	_row.resize(count);
	_lengths.resize(count);
	_isNull.resize(count);

	std::size_t offset = 0;

	for (std::size_t i = 0; i < count; i++)
	{
		std::memset(&_row[i], 0, sizeof(MYSQL_BIND));
		unsigned int len = static_cast<unsigned int>(_columns[i].length());
		_row[i].buffer_type   = fields[i].type;
		_row[i].buffer_length = len;
		_row[i].buffer        = (len > 0) ? (&_buffer[0] + offset) : 0;
		_row[i].length        = &_lengths[i];
		_row[i].is_null       = &_isNull[i];
		_row[i].is_unsigned   = (fields[i].flags & UNSIGNED_FLAG) > 0;
		
		offset += _row[i].buffer_length;
	}
}
 void FieldManager::generateField(ll wigth, ll height) {
   field = fieldType(height, std::vector<bool>(wigth,0));
   srand(time(0));
   for(int i = 0; i < height; i++) {
     for(int j = 0; j < wigth; j++) {
       field[i][j] = rand()%2;
     }
   }
 }
Example #7
0
double DBF::getNumber ( int nth )
{
	if ( fieldType(nth) != 'N' ) {
		_errNo = DB_FIELD_TYPE_MISSING ;
		return -1 ;
	}

	char	s[32]	;
	return atof(gets(nth,s,sizeof(s))) ;
}
Example #8
0
QString QDBCFile::getBitmaskTextRepresentation(quint32 field, quint32 row, BitmaskRepresentation view) const
{
    switch(view) {
    case BITMASK_VIEW_COMMA_SEPARATED_POSITIVE_BITS:
    case BITMASK_VIEW_COMMA_SEPARATED_POSITIVE_BITS_ALT:
        {
            quint32 index = 0;
            QString str = QString("Bitflags: ");
            if(fieldType(field) == FIELD_TYPE_INT) {
                qint32 i = getInt(field,row);
                if(i == 0)
                    return str.append("Empty.");
                if(i == 0x7FFFFFFF || i < -0x7FFFFFFF)
                    return str.append("All.");
                while(i != 0 || index <= BITMASK_MAX_BIT) {
                    qint32 value = qPow(2,index);
                    if((i & value) == value) {
                        i = (i & ~value);
                        (view == BITMASK_VIEW_COMMA_SEPARATED_POSITIVE_BITS) ? str.append(QString::number(index)) : str.append(QString::number(index+1));
                        if(i != 0)
                            str.append(",");
                        else
                            return str.append(".");
                    }
                    index++;
                }
            }
            else {
                quint32 u = getUInt(field,row);
                if(u == 0)
                    return str.append("Empty.");
                if(u == 0xffffffff)
                    return str.append("All.");
                while(u != 0 || index <= BITMASK_MAX_BIT) {
                    quint32 value = qPow(2,index);
                    if((u & value) == value) {
                        u = (u & ~value);
                        (view == BITMASK_VIEW_COMMA_SEPARATED_POSITIVE_BITS) ? str.append(QString::number(index)) : str.append(QString::number(index+1));
                        if(u != 0)
                            str.append(",");
                        else
                            return str.append(".");
                    }
                    index++;
                }
            }
        }
    }
    return QString();
}
void Foam::codedFixedValueFvPatchField<Type>::setFieldTemplates
(
    dynamicCode& dynCode
)
{
    word fieldType(pTraits<Type>::typeName);

    // template type for fvPatchField
    dynCode.setFilterVariable("TemplateType", fieldType);

    // Name for fvPatchField - eg, ScalarField, VectorField, ...
    fieldType[0] = toupper(fieldType[0]);
    dynCode.setFilterVariable("FieldType", fieldType + "Field");
}
Example #10
0
/*!
 * Calculates the compnents of a field
 * @param[in] field field 
 * @return size of the field
 */
uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ){

    uint8_t comp ;
    std::string name( field.getName() ) ;

    if( name == "x_Coord" || name == "y_Cooord" || name == "z_Coord" ){
        comp = 1 ;

    } else{
        VTKFieldType fieldType( field.getFieldType() ) ;
        assert( fieldType != VTKFieldType::UNDEFINED) ;

        comp = static_cast<uint8_t>(fieldType) ;

    }

    return comp ;

};
Example #11
0
QString PostgreSqlGenerator::diff(FieldModel *oldField, FieldModel *newField)
{
    QString sql = QString();
    if(oldField && newField)
        if(*oldField == *newField)
            return QString();

    if(!newField){
        sql = "DROP COLUMN " + oldField->name;
    }else{
        if(oldField){
            sql = "ALTER COLUMN ";
            sql.append(newField->name + " TYPE " + fieldType(newField));
        } else {
            sql = "ADD COLUMN ";
            sql.append(fieldDeclare(newField));
        }
    }
    return sql;
}
Example #12
0
/*!
 * Calculates the number of entries of a field
 * @param[in] field field 
 * @return size of the field
 */
uint64_t VTKUnstructuredGrid::calcFieldEntries( const VTKField &field ){

    uint64_t entries(0) ;
    std::string name( field.getName() ) ;

    if( name == "Points" ){
        entries = m_points *static_cast<int>(VTKFieldType::VECTOR) ; 

    } else if( name == "offsets" ){
        entries = m_cells ;

    } else if( name == "types" ){
        entries = m_cells ;

    } else if( name == "connectivity"){
        entries = m_connectivity ;

    } else{

        VTKLocation location( field.getLocation() ) ;
        assert( location != VTKLocation::UNDEFINED) ;

        if( location == VTKLocation::CELL ){
            entries = m_cells ;

        } else if( location == VTKLocation::POINT ){
            entries = m_points ;

        }

        VTKFieldType fieldType( field.getFieldType() ) ;
        assert( fieldType != VTKFieldType::UNDEFINED) ;

        entries *= static_cast<uint64_t>(fieldType) ;

    }

    return entries ;

};
Example #13
0
int DBF::putNumber ( int nth, double val )
{
	char	*p = fieldPtr(nth) ;
	int	l = fieldLength(nth) ;

	if ( fieldType(nth) != 'N' ) {
		_errNo = DB_FIELD_TYPE_MISSING ;
		return -1 ;
	}

	modify() ;

	char	s[32]	;
	sprintf(s,"%*.*f",fieldLength(nth),fieldDecimal(nth),val) ;
	if ( strlen(s) != fieldLength(nth) ) {	/* overflow */
		memset(p,'*',l) ;
		_errNo = DB_FIELD_OVERFLOW ;
		return -1 ;
	}

	memcpy(p,s,fieldLength(nth)) ;
	return 0 ;
}
Example #14
0
/*!
 * Calculates the number of entries of a field
 * @param[in] field field 
 * @return size of the field
 */
uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ){

    uint64_t entries(0) ;
    std::string name( field.getName() ) ;

    if( name == "x_Coord" ){
        entries = m_localIndex[0][1] -m_localIndex[0][0] +1 ;

    } else if( name == "y_Coord" ){
        entries = m_localIndex[1][1] -m_localIndex[1][0] +1 ;

    } else if( name == "z_Coord" ){
        entries = m_localIndex[2][1] -m_localIndex[2][0] +1 ;

    } else{

        VTKLocation location( field.getLocation() ) ;
        assert( location != VTKLocation::UNDEFINED) ;

        if( location == VTKLocation::CELL ){
            entries = m_cells ;

        } else if( location == VTKLocation::POINT ){
            entries = m_points ;

        }

        VTKFieldType fieldType( field.getFieldType() ) ;
        assert( fieldType != VTKFieldType::UNDEFINED) ;

        entries *= static_cast<uint64_t>(fieldType) ;

    }

    return entries ;

};
Example #15
0
ResultSet::FieldType SQLiteResult::fieldType(const String &fieldname)
{
	int num=fieldNum(fieldname);
	return fieldType(num);
}
QgsVirtualLayerDefinition QgsVirtualLayerDefinition::fromUrl( const QUrl& url )
{
  QgsVirtualLayerDefinition def;

  def.setFilePath( url.path() );

  // regexp for column name
  const QString columnNameRx( "[a-zA-Z_\x80-\xFF][a-zA-Z0-9_\x80-\xFF]*" );

  QgsFields fields;

  int layerIdx = 0;
  QList<QPair<QString, QString> > items = url.queryItems();
  for ( int i = 0; i < items.size(); i++ )
  {
    QString key = items.at( i ).first;
    QString value = items.at( i ).second;
    if ( key == "layer_ref" )
    {
      layerIdx++;
      // layer id, with optional layer_name
      int pos = value.indexOf( ':' );
      QString layerId, vlayerName;
      if ( pos == -1 )
      {
        layerId = value;
        vlayerName = QString( "vtab%1" ).arg( layerIdx );
      }
      else
      {
        layerId = value.left( pos );
        vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
      }
      // add the layer to the list
      def.addSource( vlayerName, layerId );
    }
    else if ( key == "layer" )
    {
      layerIdx++;
      // syntax: layer=provider:url_encoded_source_URI(:name(:encoding)?)?
      int pos = value.indexOf( ':' );
      if ( pos != -1 )
      {
        QString providerKey, source, vlayerName, encoding = "UTF-8";

        providerKey = value.left( pos );
        int pos2 = value.indexOf( ':', pos + 1 );
        if ( pos2 != -1 )
        {
          source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
          int pos3 = value.indexOf( ':', pos2 + 1 );
          if ( pos3 != -1 )
          {
            vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
            encoding = value.mid( pos3 + 1 );
          }
          else
          {
            vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
          }
        }
        else
        {
          source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
          vlayerName = QString( "vtab%1" ).arg( layerIdx );
        }

        def.addSource( vlayerName, source, providerKey, encoding );
      }
    }
    else if ( key == "geometry" )
    {
      // geometry field definition, optional
      // geometry_column(:wkb_type:srid)?
      QRegExp reGeom( "(" + columnNameRx + ")(?::([a-zA-Z0-9]+):(\\d+))?" );
      int pos = reGeom.indexIn( value );
      if ( pos >= 0 )
      {
        def.setGeometryField( reGeom.cap( 1 ) );
        if ( reGeom.captureCount() > 1 )
        {
          // not used by the spatialite provider for now ...
          QgsWKBTypes::Type wkbType = QgsWKBTypes::parseType( reGeom.cap( 2 ) );
          if ( wkbType == QgsWKBTypes::Unknown )
          {
            wkbType = static_cast<QgsWKBTypes::Type>( reGeom.cap( 2 ).toLong() );
          }
          def.setGeometryWkbType( wkbType );
          def.setGeometrySrid( reGeom.cap( 3 ).toLong() );
        }
      }
    }
    else if ( key == "nogeometry" )
    {
      def.setGeometryWkbType( QgsWKBTypes::NoGeometry );
    }
    else if ( key == "uid" )
    {
      def.setUid( value );
    }
    else if ( key == "query" )
    {
      // url encoded query
      def.setQuery( value );
    }
    else if ( key == "field" )
    {
      // field_name:type (int, real, text)
      QRegExp reField( "(" + columnNameRx + "):(int|real|text)" );
      int pos = reField.indexIn( value );
      if ( pos >= 0 )
      {
        QString fieldName( reField.cap( 1 ) );
        QString fieldType( reField.cap( 2 ) );
        if ( fieldType == "int" )
        {
          fields.append( QgsField( fieldName, QVariant::Int, fieldType ) );
        }
        else if ( fieldType == "real" )
        {
          fields.append( QgsField( fieldName, QVariant::Double, fieldType ) );
        }
        if ( fieldType == "text" )
        {
          fields.append( QgsField( fieldName, QVariant::String, fieldType ) );
        }
      }
    }
  }
  def.setFields( fields );

  return def;
}
bool QgsNewGeoPackageLayerDialog::apply()
{
  QString fileName( mDatabase->filePath() );
  if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
    fileName += QLatin1String( ".gpkg" );

  bool createNewDb = false;

  if ( QFile( fileName ).exists( fileName ) )
  {
    bool overwrite = false;

    switch ( mBehavior )
    {
      case Prompt:
      {
        QMessageBox msgBox;
        msgBox.setIcon( QMessageBox::Question );
        msgBox.setWindowTitle( tr( "The File Already Exists." ) );
        msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
        QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
        QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
        msgBox.setStandardButtons( QMessageBox::Cancel );
        msgBox.setDefaultButton( addNewLayerButton );
        bool cancel = false;
        if ( property( "hideDialogs" ).toBool() )
        {
          overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
          if ( !overwrite )
            cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
        }
        else
        {
          int ret = msgBox.exec();
          if ( ret == QMessageBox::Cancel )
            cancel = true;
          if ( msgBox.clickedButton() == overwriteButton )
            overwrite = true;
        }
        if ( cancel )
        {
          return false;
        }
        break;
      }

      case Overwrite:
        overwrite = true;
        break;

      case AddNewLayer:
        overwrite = false;
        break;
    }

    if ( overwrite )
    {
      QFile( fileName ).remove();
      createNewDb = true;
    }
  }
  else
  {
    createNewDb = true;
  }

  OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" );
  if ( !hGpkgDriver )
  {
    if ( !property( "hideDialogs" ).toBool() )
      QMessageBox::critical( this, tr( "Layer creation failed" ),
                             tr( "GeoPackage driver not found" ) );
    return false;
  }

  gdal::ogr_datasource_unique_ptr hDS;
  if ( createNewDb )
  {
    hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(), nullptr ) );
    if ( !hDS )
    {
      QString msg( tr( "Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
      if ( !property( "hideDialogs" ).toBool() )
        QMessageBox::critical( this, tr( "Layer creation failed" ), msg );
      return false;
    }
  }
  else
  {
    OGRSFDriverH hDriver = nullptr;
    hDS.reset( OGROpen( fileName.toUtf8().constData(), true, &hDriver ) );
    if ( !hDS )
    {
      QString msg( tr( "Opening of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
      if ( !property( "hideDialogs" ).toBool() )
        QMessageBox::critical( this, tr( "Layer creation failed" ), msg );
      return false;
    }
    if ( hDriver != hGpkgDriver )
    {
      QString msg( tr( "Opening of file succeeded, but this is not a GeoPackage database" ) );
      if ( !property( "hideDialogs" ).toBool() )
        QMessageBox::critical( this, tr( "Layer creation failed" ), msg );
      return false;
    }
  }

  QString tableName( mTableNameEdit->text() );

  bool overwriteTable = false;
  if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) )
  {
    if ( property( "hideDialogs" ).toBool() )
    {
      overwriteTable = property( "question_existing_layer_answer_overwrite" ).toBool();
    }
    else if ( QMessageBox::question( this, tr( "Existing layer" ),
                                     tr( "A table with the same name already exists. Do you want to overwrite it?" ),
                                     QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
    {
      overwriteTable = true;
    }

    if ( !overwriteTable )
    {
      return false;
    }
  }

  QString layerIdentifier( mLayerIdentifierEdit->text() );
  QString layerDescription( mLayerDescriptionEdit->text() );

  OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>
                               ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );

  // z-coordinate & m-value.
  if ( mGeometryWithZCheckBox->isChecked() )
    wkbType = OGR_GT_SetZ( wkbType );

  if ( mGeometryWithMCheckBox->isChecked() )
    wkbType = OGR_GT_SetM( wkbType );

  OGRSpatialReferenceH hSRS = nullptr;
  // consider spatial reference system of the layer
  QgsCoordinateReferenceSystem srs = mCrsSelector->crs();
  if ( wkbType != wkbNone && srs.isValid() )
  {
    QString srsWkt = srs.toWkt();
    hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
  }

  // Set options
  char **options = nullptr;

  if ( overwriteTable )
    options = CSLSetNameValue( options, "OVERWRITE", "YES" );
  if ( !layerIdentifier.isEmpty() )
    options = CSLSetNameValue( options, "IDENTIFIER", layerIdentifier.toUtf8().constData() );
  if ( !layerDescription.isEmpty() )
    options = CSLSetNameValue( options, "DESCRIPTION", layerDescription.toUtf8().constData() );

  QString featureId( mFeatureIdColumnEdit->text() );
  if ( !featureId.isEmpty() )
    options = CSLSetNameValue( options, "FID", featureId.toUtf8().constData() );

  QString geometryColumn( mGeometryColumnEdit->text() );
  if ( wkbType != wkbNone && !geometryColumn.isEmpty() )
    options = CSLSetNameValue( options, "GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() );

  if ( wkbType != wkbNone )
    options = CSLSetNameValue( options, "SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ? "YES" : "NO" );

  OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options );
  CSLDestroy( options );
  if ( hSRS )
    OSRRelease( hSRS );
  if ( !hLayer )
  {
    QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
    if ( !property( "hideDialogs" ).toBool() )
      QMessageBox::critical( this, tr( "Layer creation failed" ), msg );
    return false;
  }

  QTreeWidgetItemIterator it( mAttributeView );
  while ( *it )
  {
    QString fieldName( ( *it )->text( 0 ) );
    QString fieldType( ( *it )->text( 1 ) );
    QString fieldWidth( ( *it )->text( 2 ) );

    OGRFieldType ogrType( OFTString );
    if ( fieldType == QLatin1String( "text" ) )
      ogrType = OFTString;
    else if ( fieldType == QLatin1String( "integer" ) )
      ogrType = OFTInteger;
    else if ( fieldType == QLatin1String( "integer64" ) )
      ogrType = OFTInteger64;
    else if ( fieldType == QLatin1String( "real" ) )
      ogrType = OFTReal;
    else if ( fieldType == QLatin1String( "date" ) )
      ogrType = OFTDate;
    else if ( fieldType == QLatin1String( "datetime" ) )
      ogrType = OFTDateTime;

    int ogrWidth = fieldWidth.toInt();

    gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( fieldName.toUtf8().constData(), ogrType ) );
    OGR_Fld_SetWidth( fld.get(), ogrWidth );

    if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE )
    {
      if ( !property( "hideDialogs" ).toBool() )
      {
        QMessageBox::critical( this, tr( "Layer creation failed" ),
                               tr( "Creation of field %1 failed (OGR error: %2)" )
                               .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
      }
      return false;
    }

    ++it;
  }

  // In GDAL >= 2.0, the driver implements a deferred creation strategy, so
  // issue a command that will force table creation
  CPLErrorReset();
  OGR_L_ResetReading( hLayer );
  if ( CPLGetLastErrorType() != CE_None )
  {
    QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
    if ( !property( "hideDialogs" ).toBool() )
      QMessageBox::critical( this, tr( "Layer creation failed" ), msg );
    return false;
  }
  hDS.reset();

  QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
  QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
  QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
  if ( layer->isValid() )
  {
    // register this layer with the central layers registry
    QList<QgsMapLayer *> myList;
    myList << layer;
    //addMapLayers returns a list of all successfully added layers
    //so we compare that to our original list.
    if ( myList == QgsProject::instance()->addMapLayers( myList ) )
      return true;
  }
  else
  {
    if ( !property( "hideDialogs" ).toBool() )
      QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
    delete layer;
  }

  return false;
}
nsresult
nsDirIndexParser::ParseData(nsIDirIndex *aIdx, char* aDataStr) {
  // Parse a "201" data line, using the field ordering specified in
  // mFormat.

  if (!mFormat) {
    // Ignore if we haven't seen a format yet.
    return NS_OK;
  }

  nsresult rv = NS_OK;

  nsCAutoString filename;

  for (PRInt32 i = 0; mFormat[i] != -1; ++i) {
    // If we've exhausted the data before we run out of fields, just
    // bail.
    if (! *aDataStr)
      break;

    while (*aDataStr && nsCRT::IsAsciiSpace(*aDataStr))
      ++aDataStr;

    char    *value = aDataStr;

    if (*aDataStr == '"' || *aDataStr == '\'') {
      // it's a quoted string. snarf everything up to the next quote character
      const char quotechar = *(aDataStr++);
      ++value;
      while (*aDataStr && *aDataStr != quotechar)
        ++aDataStr;
      *aDataStr++ = '\0';

      if (! aDataStr) {
        NS_WARNING("quoted value not terminated");
      }
    } else {
      // it's unquoted. snarf until we see whitespace.
      value = aDataStr;
      while (*aDataStr && (!nsCRT::IsAsciiSpace(*aDataStr)))
        ++aDataStr;
      *aDataStr++ = '\0';
    }

    fieldType t = fieldType(mFormat[i]);
    switch (t) {
    case FIELD_FILENAME: {
      // don't unescape at this point, so that UnEscapeAndConvert() can
      filename = value;
      
      PRBool  success = PR_FALSE;
      
      nsAutoString entryuri;
      
      if (gTextToSubURI) {
        PRUnichar   *result = nsnull;
        if (NS_SUCCEEDED(rv = gTextToSubURI->UnEscapeAndConvert(mEncoding.get(), filename.get(),
                                                                &result)) && (result)) {
          if (*result) {
            aIdx->SetLocation(filename.get());
            if (!mHasDescription)
              aIdx->SetDescription(result);
            success = PR_TRUE;
          }
          NS_Free(result);
        } else {
          NS_WARNING("UnEscapeAndConvert error");
        }
      }
      
      if (!success) {
        // if unsuccessfully at charset conversion, then
        // just fallback to unescape'ing in-place
        // XXX - this shouldn't be using UTF8, should it?
        // when can we fail to get the service, anyway? - bbaetz
        aIdx->SetLocation(filename.get());
        if (!mHasDescription) {
          aIdx->SetDescription(NS_ConvertUTF8toUTF16(value).get());
        }
      }
    }
      break;
    case FIELD_DESCRIPTION:
      nsUnescape(value);
      aIdx->SetDescription(NS_ConvertUTF8toUTF16(value).get());
      break;
    case FIELD_CONTENTLENGTH:
      {
        PRInt64 len;
        PRInt32 status = PR_sscanf(value, "%lld", &len);
        if (status == 1)
          aIdx->SetSize(len);
        else
          aIdx->SetSize(LL_MAXUINT); // LL_MAXUINT means unknown
      }
      break;
    case FIELD_LASTMODIFIED:
      {
        PRTime tm;
        nsUnescape(value);
        if (PR_ParseTimeString(value, PR_FALSE, &tm) == PR_SUCCESS) {
          aIdx->SetLastModified(tm);
        }
      }
      break;
    case FIELD_CONTENTTYPE:
      aIdx->SetContentType(value);
      break;
    case FIELD_FILETYPE:
      // unescape in-place
      nsUnescape(value);
      if (!nsCRT::strcasecmp(value, "directory")) {
        aIdx->SetType(nsIDirIndex::TYPE_DIRECTORY);
      } else if (!nsCRT::strcasecmp(value, "file")) {
        aIdx->SetType(nsIDirIndex::TYPE_FILE);
      } else if (!nsCRT::strcasecmp(value, "symbolic-link")) {
        aIdx->SetType(nsIDirIndex::TYPE_SYMLINK);
      } else {
        aIdx->SetType(nsIDirIndex::TYPE_UNKNOWN);
      }
      break;
    case FIELD_UNKNOWN:
      // ignore
      break;
    }
  }

  return NS_OK;
}
Example #19
0
bool QDBCFile::detectFieldType(quint32 field)
{
    if(field < m_fieldCount && field >= 0) {
        if(field == 0) {
            //the first field seems to be always uint32, so set it asap to save some time
            setFieldType(field,FIELD_TYPE_ID);
            setFieldTypeSQL(field,FIELD_TYPE_SQL_ID);
            return true;
        }
        quint32 strings = 0;
        quint32 floats = 0;
        quint32 uints = 0;
        quint32 ints = 0;
        quint32 nulls = 0;
        quint32 ones = 0;
        quint32 biguints = 0;
        quint32 maxuints = 0;
        quint32 timeuints = 0;
        quint32 bitvaluints = 0;
        quint32 bigints = 0;
        quint32 maxints = 0;
        quint32 timeints = 0;
        quint32 bitvalints = 0;
        quint32 unk = 0;
        //
        DBCFieldType fieldtype = FIELD_TYPE_DEFAULT;
        SQLFieldType fieldtypesql = FIELD_TYPE_SQL_DEFAULT;
        quint8 uintsizelevel = 0;
        quint8 intsizelevel = 0;
        quint8 textsizelevel = 0;
        /*
        sql field size level:
        0 - tiny (tinyint, tinytext)
        1 - small (smallint, text)
        2 - medium (mediumint, mediumtext)
        3 - large (int, longtext)
        */

        for(quint32 record=0; record < m_recordCount; record++) {
            switch(m_fieldSize) {
            case 1:
            case 2:
            case 4:
                {
                    if(hasUsualFloat(field,record)) {
                        float f = getFloat(field,record);
                        if(f != 0.0f)
                            floats++;
                        else
                            nulls++;
                    }
                    else if(hasFullString(field,record)) {
                        strings++;
                        if(getInt(field,record) == 1)
                            ones++;
                        //
                        if(getString(field,record).size() > SQL_TINYTEXT && textsizelevel < 1)
                            textsizelevel = 1;
                        if(getString(field,record).size() > SQL_TEXT && textsizelevel < 2)
                            textsizelevel = 2;
                        if(getString(field,record).size() > SQL_MEDIUMTEXT && textsizelevel < 3)
                            textsizelevel = 3;
                    }
                    else if(hasInteger(field,record,false)) {
                        quint32 u = getUInt(field,record);
                        quint32 uMinusOne = u-1;
                        bool isDegreeOfTwo = !(uMinusOne&u);
                        if(u == 0)
                            nulls++;
                        else
                            uints++;
                        if(u == 1)
                            ones++;
                        /*
                    BITMASK DETECTION TEMPORARY HACKS: wild bitmask guessing. Test in progress.
                    The insteresting thing about it is the fact that blizzards used same shit
                    themselves at least once. Current precision is about 60-70%.
                    */
                        if(u > qPow(2,19)) {
                            biguints++;
                            if(u%10 == 0)
                                timeuints++;
                        }
                        if(u == 0xffffffff)
                            maxuints++;
                        if(isDegreeOfTwo && u > 8)
                            bitvaluints++;
                        //
                        if(u > SQL_TINYINT_UNSIGNED_HI && uintsizelevel < 1)
                            uintsizelevel = 1;
                        if(u > SQL_SMALLINT_UNSIGNED_HI && uintsizelevel < 2)
                            uintsizelevel = 2;
                        if(u > SQL_MEDIUMINT_UNSIGNED_HI && uintsizelevel < 3)
                            uintsizelevel = 3;
                    }
                    else if(hasInteger(field,record,true)) {
                        qint32 i = getInt(field,record);
                        quint32 absI = abs(i);
                        quint32 absIMinusOne = absI-1;
                        bool isDegreeOfTwo = !(absIMinusOne&absI);
                        if(i == 0)
                            nulls++;
                        else
                            ints++;
                        if(i == 1)
                            ones++;
                        /*
                    BITMASK DETECTION TEMPORARY HACKS: wild bitmask guessing. Test in progress.
                    The insteresting thing about it is the fact that blizzards used same shit
                    themselves at least once. Current precision is about 60-70%.
                    */
                        if(i == -1 || i == -2)
                            maxuints++;
                        if(absI > qPow(2,19)) {
                            bigints++;
                            if(absI%10 == 0)
                                timeints++;
                        }
                        if(absI >= 0x7FFFFFFF)
                            maxints++;
                        if(isDegreeOfTwo && absI > 8)
                            bitvalints++;
                        //
                        if((i < SQL_TINYINT_LOW || i > SQL_TINYINT_HI) && intsizelevel < 1)
                            intsizelevel = 1;
                        if((i < SQL_SMALLINT_LOW || i > SQL_SMALLINT_HI) && intsizelevel < 2)
                            intsizelevel = 2;
                        if((i < SQL_MEDIUMINT_LOW || i > SQL_MEDIUMINT_HI) && intsizelevel < 3)
                            intsizelevel = 3;
                    }
                    else {
                        //OMG WTF?!
                        unk++;
                    }
                    break;
                }
            default:
                //Unsupported field size, make error and get out of here
                return makeDetectionError(DBCFILE_ERROR_AUTO_DETECT_FAILED_UNK_FIELD_SIZE,field);
            }
        }
        //Auto-Detecting Generic Field Type...
       if( unk > 0 )
           //OMG WTF?! Normally this thing should NEVER EVER happen.
           fieldtype = FIELD_TYPE_UNKNOWN;
       else if( ones + nulls == m_recordCount && ones > 0 && nulls > 0 )
           fieldtype = FIELD_TYPE_BOOL;
       else if( strings + nulls == m_recordCount && strings > 0 )
           fieldtype = FIELD_TYPE_TEXT;
       else if( nulls == m_recordCount
                && (fieldType(field-1) == FIELD_TYPE_TEXT || fieldType(field-1) == FIELD_TYPE_NULLTEXT))
           //TODO: add some field count correction? Got one more excess column with nulls as nulltext somewhere.
           fieldtype = FIELD_TYPE_NULLTEXT;
       else if( nulls == m_recordCount )
           fieldtype = FIELD_TYPE_NULLS;
       else if( floats + nulls == m_recordCount
                || (floats > m_recordCount - nulls - floats && floats > 0))
           fieldtype = FIELD_TYPE_FLOAT;
       else if( (biguints > uints*0.10f && biguints != timeuints && biguints > 1)
                || (bitvaluints == uints && bitvaluints != ones  && (ones <= uints*0.09f || biguints > 0) && (ints == 0 || (uints > ints && ints == maxuints)) && m_recordCount > 1 && uints > 0)
                || (bitvaluints >= uints*0.30f && strings < uints*0.09f && bitvaluints != ones  && (ones <= uints*0.09f || biguints > 0) && (ints == 0 || (uints > ints && ints == maxuints)) && m_recordCount > 1 && uints > 0)
                || (bigints > ints*0.10f && bigints != timeints && bigints > 1)
                || (bitvalints == ints && bitvalints != ones  && (ones <= ints*0.09f || bigints > 0) && (uints == 0 || (ints > uints && uints == maxuints)) && m_recordCount > 1 && ints > 0)
                || (bitvalints >= ints*0.30f && strings < ints*0.09f && bitvalints != ones  && (ones <= ints*0.09f || bigints > 0) && (uints == 0 || (ints > uints && uints == maxuints)) && m_recordCount > 1 && ints > 0))
           fieldtype = FIELD_TYPE_BITMASK;//Yeah, i know. xD
       else if( ints == 0 || (uints > ints && ints == maxuints && maxuints > uints*0.30f) )
           fieldtype = FIELD_TYPE_UINT;
       else
           fieldtype = FIELD_TYPE_INT;

       switch(fieldtype) {
        case FIELD_TYPE_TEXT:
            switch(textsizelevel) {
            case 0:
                fieldtypesql = FIELD_TYPE_SQL_TINYTEXT;
                break;
            case 1:
                fieldtypesql = FIELD_TYPE_SQL_TEXT;
                break;
            case 2:
                fieldtypesql = FIELD_TYPE_SQL_MEDIUMTEXT;
                break;
            default:
                fieldtypesql = FIELD_TYPE_SQL_LONGTEXT;
            }
            break;
        case FIELD_TYPE_UINT:
            switch(uintsizelevel) {
            case 0:
                fieldtypesql = FIELD_TYPE_SQL_TINYINT_UNSIGNED;
                break;
            case 1:
                fieldtypesql = FIELD_TYPE_SQL_SMALLINT_UNSIGNED;
                break;
            case 2:
                fieldtypesql = FIELD_TYPE_SQL_MEDIUMINT_UNSIGNED;
                break;
            default:
                fieldtypesql = FIELD_TYPE_SQL_INT_UNSIGNED;
            }
            break;
        case FIELD_TYPE_INT:
            switch(intsizelevel) {
            case 0:
                fieldtypesql = FIELD_TYPE_SQL_TINYINT;
                break;
            case 1:
                fieldtypesql = FIELD_TYPE_SQL_SMALLINT;
                break;
            case 2:
                fieldtypesql = FIELD_TYPE_SQL_MEDIUMINT;
                break;
            default:
                fieldtypesql = FIELD_TYPE_SQL_INT;
            }
            break;
        case FIELD_TYPE_FLOAT:
            fieldtypesql = FIELD_TYPE_SQL_FLOAT;
            break;
        case FIELD_TYPE_BOOL:
            fieldtypesql = FIELD_TYPE_SQL_BOOL;
            break;
        case FIELD_TYPE_NULLS:
            fieldtypesql = FIELD_TYPE_SQL_TINYINT_UNSIGNED;
            break;
        case FIELD_TYPE_NULLTEXT:
            fieldtypesql = FIELD_TYPE_SQL_TINYTEXT;
            break;
        case FIELD_TYPE_BITMASK:
            fieldtypesql = FIELD_TYPE_SQL_BITMASK;
            break;
        case FIELD_TYPE_UNKNOWN:
            fieldtypesql = FIELD_TYPE_SQL_UNKNOWN;
            break;
        }
        setFieldType(field,fieldtype);
        setFieldTypeSQL(field,fieldtypesql);
        return true;
    }
    //OK, we are totally screwed if we are right here
    return makeDetectionError(DBCFILE_ERROR_AUTO_DETECT_FAILED_INVALID_FIELD,field);
}