mitab/mitab_tabfile.cpp
int TABFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType,
                            int nWidth /*=0*/, int nPrecision /*=0*/,
                            GBool bIndexed /*=FALSE*/, GBool /*bUnique=FALSE*/)
{
    OGRFieldDefn *poFieldDefn;
    int nStatus = 0;
    char *pszCleanName = NULL;

    if (m_eAccessMode != TABWrite)
    {
        CPLError(CE_Failure, CPLE_NotSupported,
                 "AddFieldNative() can be used only with Write access.");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Check that call happens at the right time in dataset's life.
     *----------------------------------------------------------------*/
    if (m_eAccessMode != TABWrite || 
        m_nLastFeatureId > 0 || m_poDATFile == NULL)
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "AddFieldNative() must be called after opening a new "
                 "dataset, but before writing the first feature to it.");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Create new OGRFeatureDefn if not done yet...
     *----------------------------------------------------------------*/
    if (m_poDefn== NULL)
    {
        char *pszFeatureClassName = TABGetBasename(m_pszFname);
        m_poDefn = new OGRFeatureDefn(pszFeatureClassName);
        CPLFree(pszFeatureClassName);
        // Ref count defaults to 0... set it to 1
        m_poDefn->Reference();
    }

    /*-----------------------------------------------------------------
     * Validate field width... must be <= 254
     *----------------------------------------------------------------*/
    if (nWidth > 254)
    {
        CPLError(CE_Warning, CPLE_IllegalArg,
                 "Invalid size (%d) for field '%s'.  "
                 "Size must be 254 or less.", nWidth, pszName);
        nWidth=254;
    }

    /*-----------------------------------------------------------------
     * Map fields with width=0 (variable length in OGR) to a valid default
     *----------------------------------------------------------------*/
    if (eMapInfoType == TABFDecimal && nWidth == 0)
        nWidth=20;
    else if (nWidth == 0)
        nWidth=254; /* char fields */

    /*-----------------------------------------------------------------
     * Make sure field name is valid... check for special chars, etc.
     * (pszCleanName will have to be freed.)
     *----------------------------------------------------------------*/
    pszCleanName = TABCleanFieldName(pszName);

    /*-----------------------------------------------------------------
     * Map MapInfo native types to OGR types
     *----------------------------------------------------------------*/
    poFieldDefn = NULL;

    switch(eMapInfoType)
    {
      case TABFChar:
        /*-------------------------------------------------
         * CHAR type
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(nWidth);
        break;
      case TABFInteger:
        /*-------------------------------------------------
         * INTEGER type
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger);
        break;
      case TABFSmallInt:
        /*-------------------------------------------------
         * SMALLINT type
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger);
        break;
      case TABFDecimal:
        /*-------------------------------------------------
         * DECIMAL type
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal);
        poFieldDefn->SetWidth(nWidth);
        poFieldDefn->SetPrecision(nPrecision);
        break;
      case TABFFloat:
        /*-------------------------------------------------
         * FLOAT type
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal);
        break;
      case TABFDate:
        /*-------------------------------------------------
         * DATE type (V450, returned as a string: "DD/MM/YYYY")
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(10);
        m_nVersion = MAX(m_nVersion, 450);
        break;
      case TABFTime:
        /*-------------------------------------------------
         * TIME type (V900, returned as a string: "HH:MM:SS")
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(8);
        m_nVersion = MAX(m_nVersion, 900);
        break;
      case TABFDateTime:
        /*-------------------------------------------------
         * DATETIME type (V900, returned as a string: "DD/MM/YYYY HH:MM:SS")
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(19);
        m_nVersion = MAX(m_nVersion, 900);
        break;
      case TABFLogical:
        /*-------------------------------------------------
         * LOGICAL type (value "T" or "F")
         *------------------------------------------------*/
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(1);
        break;
      default:
        CPLError(CE_Failure, CPLE_NotSupported,
                 "Unsupported type for field %s", pszCleanName);
        CPLFree(pszCleanName);
        return -1;
    }

    /*-----------------------------------------------------
     * Add the FieldDefn to the FeatureDefn 
     *----------------------------------------------------*/
    m_poDefn->AddFieldDefn(poFieldDefn);
    delete poFieldDefn;

    /*-----------------------------------------------------
     * ... and pass field info to the .DAT file.
     *----------------------------------------------------*/
    nStatus = m_poDATFile->AddField(pszCleanName, eMapInfoType, 
                                    nWidth, nPrecision);

    /*-----------------------------------------------------------------
     * Extend the array to keep track of indexed fields (default=NOT indexed)
     *----------------------------------------------------------------*/
    m_panIndexNo = (int *)CPLRealloc(m_panIndexNo,
                                     m_poDefn->GetFieldCount()*sizeof(int));
    m_panIndexNo[m_poDefn->GetFieldCount()-1] = 0;

     /*-----------------------------------------------------------------
     * Index the field if requested
     *----------------------------------------------------------------*/
    if (nStatus == 0 && bIndexed)
        nStatus = SetFieldIndexed(m_poDefn->GetFieldCount()-1);

    CPLFree(pszCleanName);
    return nStatus;
}
/**********************************************************************
 *                   TABFile::AddFieldNative()
 *
 * Create a new field using a native mapinfo data type... this is an 
 * alternative to defining fields through the OGR interface.
 * This function should be called after creating a new dataset, but before 
 * writing the first feature.
 *
 * This function will build/update the OGRFeatureDefn that will have to be
 * used when writing features to this dataset.
 *
 * A reference to the OGRFeatureDefn can be obtained using GetLayerDefn().
 *
 * Note: The bUnique flag has no effect on TABFiles.  See the TABView class.
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
:AddFieldNative()
Create a new field using a native mapinfo data type... this is an
alternative to defining fields through the OGR interface.
This function should be called after creating a new dataset, but before
writing the first feature.

This function will build/update the OGRFeatureDefn that will have to be
used when writing features to this dataset.

A reference to the OGRFeatureDefn can be obtained using GetLayerDefn().

The bUnique flag has no effect on TABFiles.  See the TABView class.

Returns 0 on success, -1 on error.
: AddFieldNative () Create a new field using a native mapinfo data type ... this is an alternative to defining fields through the OGR interface . This function should be called after creating a new dataset but before writing the first feature . This function will build / update the OGRFeatureDefn that will have to be used when writing features to this dataset . A reference to the OGRFeatureDefn can be obtained using GetLayerDefn () . The bUnique flag has no effect on TABFiles . See the TABView class . Returns 0 on success - 1 on error .
int TABFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType,
                            int nWidth , int nPrecision ,
                            GBool bIndexed , GBool )
{
    OGRFieldDefn *poFieldDefn;
    int nStatus = 0;
    char *pszCleanName = NULL;
    if (m_eAccessMode != TABWrite)
    {
        CPLError(CE_Failure, CPLE_NotSupported,
                 "AddFieldNative() can be used only with Write access.");
        return -1;
    }
    if (m_eAccessMode != TABWrite || 
        m_nLastFeatureId > 0 || m_poDATFile == NULL)
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "AddFieldNative() must be called after opening a new "
                 "dataset, but before writing the first feature to it.");
        return -1;
    }
    if (m_poDefn== NULL)
    {
        char *pszFeatureClassName = TABGetBasename(m_pszFname);
        m_poDefn = new OGRFeatureDefn(pszFeatureClassName);
        CPLFree(pszFeatureClassName);
        m_poDefn->Reference();
    }
    if (nWidth > 254)
    {
        CPLError(CE_Warning, CPLE_IllegalArg,
                 "Invalid size (%d) for field '%s'.  "
                 "Size must be 254 or less.", nWidth, pszName);
        nWidth=254;
    }
    if (eMapInfoType == TABFDecimal && nWidth == 0)
        nWidth=20;
    else if (nWidth == 0)
        nWidth=254; 
    pszCleanName = TABCleanFieldName(pszName);
    poFieldDefn = NULL;
    switch(eMapInfoType)
    {
      case TABFChar:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(nWidth);
        break;
      case TABFInteger:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger);
        break;
      case TABFSmallInt:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger);
        break;
      case TABFDecimal:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal);
        poFieldDefn->SetWidth(nWidth);
        poFieldDefn->SetPrecision(nPrecision);
        break;
      case TABFFloat:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal);
        break;
      case TABFDate:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(10);
        m_nVersion = MAX(m_nVersion, 450);
        break;
      case TABFTime:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(8);
        m_nVersion = MAX(m_nVersion, 900);
        break;
      case TABFDateTime:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(19);
        m_nVersion = MAX(m_nVersion, 900);
        break;
      case TABFLogical:
        poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString);
        poFieldDefn->SetWidth(1);
        break;
      default:
        CPLError(CE_Failure, CPLE_NotSupported,
                 "Unsupported type for field %s", pszCleanName);
        CPLFree(pszCleanName);
        return -1;
    }
    m_poDefn->AddFieldDefn(poFieldDefn);
    delete poFieldDefn;
    nStatus = m_poDATFile->AddField(pszCleanName, eMapInfoType, 
                                    nWidth, nPrecision);
    m_panIndexNo = (int *)CPLRealloc(m_panIndexNo,
                                     m_poDefn->GetFieldCount()*sizeof(int));
    m_panIndexNo[m_poDefn->GetFieldCount()-1] = 0;
    if (nStatus == 0 && bIndexed)
        nStatus = SetFieldIndexed(m_poDefn->GetFieldCount()-1);
    CPLFree(pszCleanName);
    return nStatus;
}
int TABFile :: AddFieldNative ( const char * pszName , TABFieldType eMapInfoType , int nWidth , int nPrecision , GBool bIndexed , GBool ) { OGRFieldDefn * poFieldDefn ; int nStatus = 0 ; char * pszCleanName = NULL ; if ( m_eAccessMode != TABWrite ) { CPLError ( CE_Failure , CPLE_NotSupported , " " ) ; return -1 ; } if ( m_eAccessMode != TABWrite || m_nLastFeatureId > 0 || m_poDATFile == NULL ) { CPLError ( CE_Failure , CPLE_AssertionFailed , " " " " ) ; return -1 ; } if ( m_poDefn == NULL ) { char * pszFeatureClassName = TABGetBasename ( m_pszFname ) ; m_poDefn = new OGRFeatureDefn ( pszFeatureClassName ) ; CPLFree ( pszFeatureClassName ) ; m_poDefn -> Reference ( ) ; } if ( nWidth > 254 ) { CPLError ( CE_Warning , CPLE_IllegalArg , " " " " , nWidth , pszName ) ; nWidth = 254 ; } if ( eMapInfoType == TABFDecimal && nWidth == 0 ) nWidth = 20 ; else if ( nWidth == 0 ) nWidth = 254 ; pszCleanName = TABCleanFieldName ( pszName ) ; poFieldDefn = NULL ; switch ( eMapInfoType ) { case TABFChar : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTString ) ; poFieldDefn -> SetWidth ( nWidth ) ; break ; case TABFInteger : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTInteger ) ; break ; case TABFSmallInt : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTInteger ) ; break ; case TABFDecimal : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTReal ) ; poFieldDefn -> SetWidth ( nWidth ) ; poFieldDefn -> SetPrecision ( nPrecision ) ; break ; case TABFFloat : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTReal ) ; break ; case TABFDate : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTString ) ; poFieldDefn -> SetWidth ( 10 ) ; m_nVersion = MAX ( m_nVersion , 450 ) ; break ; case TABFTime : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTString ) ; poFieldDefn -> SetWidth ( 8 ) ; m_nVersion = MAX ( m_nVersion , 900 ) ; break ; case TABFDateTime : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTString ) ; poFieldDefn -> SetWidth ( 19 ) ; m_nVersion = MAX ( m_nVersion , 900 ) ; break ; case TABFLogical : poFieldDefn = new OGRFieldDefn ( pszCleanName , OFTString ) ; poFieldDefn -> SetWidth ( 1 ) ; break ; default : CPLError ( CE_Failure , CPLE_NotSupported , " " , pszCleanName ) ; CPLFree ( pszCleanName ) ; return -1 ; } m_poDefn -> AddFieldDefn ( poFieldDefn ) ; delete poFieldDefn ; nStatus = m_poDATFile -> AddField ( pszCleanName , eMapInfoType , nWidth , nPrecision ) ; m_panIndexNo = ( int * ) CPLRealloc ( m_panIndexNo , m_poDefn -> GetFieldCount ( ) * sizeof ( int ) ) ; m_panIndexNo [ m_poDefn -> GetFieldCount ( ) - 1 ] = 0 ; if ( nStatus == 0 && bIndexed ) nStatus = SetFieldIndexed ( m_poDefn -> GetFieldCount ( ) - 1 ) ; CPLFree ( pszCleanName ) ; return nStatus ; }
TABFile::AddFieldNative()
 Create a new field using a native mapinfo data type... this is an
 alternative to defining fields through the OGR interface.
TABFile :: AddFieldNative () Create a new field using a native mapinfo data type ... this is an alternative to defining fields through the OGR interface .
/*=0*/ /*=0*/ /*=FALSE*/ /*bUnique=FALSE*/ /*-----------------------------------------------------------------
     * Check that call happens at the right time in dataset's life.
     *----------------------------------------------------------------*/ /*-----------------------------------------------------------------
     * Create new OGRFeatureDefn if not done yet...
     *----------------------------------------------------------------*/ // Ref count defaults to 0... set it to 1 /*-----------------------------------------------------------------
     * Validate field width... must be <= 254
     *----------------------------------------------------------------*/ /*-----------------------------------------------------------------
     * Map fields with width=0 (variable length in OGR) to a valid default
     *----------------------------------------------------------------*/ /* char fields */ /*-----------------------------------------------------------------
     * Make sure field name is valid... check for special chars, etc.
     * (pszCleanName will have to be freed.)
     *----------------------------------------------------------------*/ /*-----------------------------------------------------------------
     * Map MapInfo native types to OGR types
     *----------------------------------------------------------------*/ /*-------------------------------------------------
         * CHAR type
         *------------------------------------------------*/ /*-------------------------------------------------
         * INTEGER type
         *------------------------------------------------*/ /*-------------------------------------------------
         * SMALLINT type
         *------------------------------------------------*/ /*-------------------------------------------------
         * DECIMAL type
         *------------------------------------------------*/ /*-------------------------------------------------
         * FLOAT type
         *------------------------------------------------*/ /*-------------------------------------------------
         * DATE type (V450, returned as a string: "DD/MM/YYYY")
         *------------------------------------------------*/ /*-------------------------------------------------
         * TIME type (V900, returned as a string: "HH:MM:SS")
         *------------------------------------------------*/ /*-------------------------------------------------
         * DATETIME type (V900, returned as a string: "DD/MM/YYYY HH:MM:SS")
         *------------------------------------------------*/ /*-------------------------------------------------
         * LOGICAL type (value "T" or "F")
         *------------------------------------------------*/ /*-----------------------------------------------------
     * Add the FieldDefn to the FeatureDefn 
     *----------------------------------------------------*/ /*-----------------------------------------------------
     * ... and pass field info to the .DAT file.
     *----------------------------------------------------*/ /*-----------------------------------------------------------------
     * Extend the array to keep track of indexed fields (default=NOT indexed)
     *----------------------------------------------------------------*/ /*-----------------------------------------------------------------
     * Index the field if requested
     *----------------------------------------------------------------*/