/******************************************************************************
 *
 * Project:  EPIInfo .REC Reader
 * Purpose:  Implements OGRRECLayer class.
 * Author:   Frank Warmerdam <warmerdam@pobox.com>
 *
 ******************************************************************************
 * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_rec.h"

CPL_CVSID("$Id: ogrreclayer.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")

/************************************************************************/
/*                            OGRRECLayer()                             */
/*                                                                      */
/*      Note that the OGRRECLayer assumes ownership of the passed       */
/*      file pointer.                                                   */
/************************************************************************/

OGRRECLayer::OGRRECLayer( const char *pszLayerNameIn,
                          FILE * fp, int nFieldCountIn ) :
    poFeatureDefn(new OGRFeatureDefn( pszLayerNameIn )),
    fpREC(fp),
    nStartOfData(0),
    bIsValid(FALSE),
    nFieldCount(0),
    panFieldOffset(static_cast<int *>(CPLCalloc(sizeof(int),nFieldCountIn))),
    panFieldWidth(static_cast<int *>(CPLCalloc(sizeof(int),nFieldCountIn))),
    nRecordLength(0),
    nNextFID(1)
{
    SetDescription( poFeatureDefn->GetName() );
    poFeatureDefn->Reference();
    poFeatureDefn->SetGeomType( wkbNone );

/* -------------------------------------------------------------------- */
/*      Read field definition lines.                                    */
/* -------------------------------------------------------------------- */
    for( int iField = 0; iField < nFieldCountIn; iField++ )
    {
        const char *pszLine = CPLReadLine( fp );

        if( pszLine == nullptr )
            return;

        if( strlen(pszLine) < 44 )
            return;

        // Extract field width.
        panFieldWidth[nFieldCount] = atoi( RECGetField( pszLine, 37, 4 ) );
        if( panFieldWidth[nFieldCount] < 0 )
            return;

        // Is this an real, integer or string field?  Default to string.
        int nTypeCode = atoi(RECGetField(pszLine,33,4));
        OGRFieldType eFType = OFTString;
        if( nTypeCode == 12 )
            eFType = OFTInteger;
        else if( nTypeCode > 100 && nTypeCode < 120 )
        {
            eFType = OFTReal;
        }
        else if( nTypeCode == 0 || nTypeCode == 6 || nTypeCode == 102 )
        {
            if( panFieldWidth[nFieldCount] < 3 )
                eFType = OFTInteger;
            else
                eFType = OFTReal;
        }
        else
            eFType = OFTString;

        OGRFieldDefn oField( RECGetField( pszLine, 2, 10 ), eFType );

        // Establish field offset.
        if( nFieldCount > 0 )
            panFieldOffset[nFieldCount]
                = panFieldOffset[nFieldCount-1] + panFieldWidth[nFieldCount-1];

        if( nTypeCode > 100 && nTypeCode < 120 )
        {
            oField.SetWidth( panFieldWidth[nFieldCount] );
            oField.SetPrecision( nTypeCode - 100 );
        }
        else if( eFType == OFTReal )
        {
            oField.SetWidth( panFieldWidth[nFieldCount]*2 );
            oField.SetPrecision( panFieldWidth[nFieldCount]-1 );
        }
        else
            oField.SetWidth( panFieldWidth[nFieldCount] );

        // Skip fields that are only screen labels.
        if( panFieldWidth[nFieldCount] == 0 )
            continue;

        poFeatureDefn->AddFieldDefn( &oField );
        nFieldCount++;
    }

    if( nFieldCount == 0 )
        return;

    nRecordLength = panFieldOffset[nFieldCount-1]+panFieldWidth[nFieldCount-1];
    bIsValid = TRUE;

    nStartOfData = static_cast<int>(VSIFTell( fp ));
}

/************************************************************************/
/*                           ~OGRRECLayer()                           */
/************************************************************************/

OGRRECLayer::~OGRRECLayer()

{
    if( m_nFeaturesRead > 0 && poFeatureDefn != nullptr )
    {
        CPLDebug( "REC", "%d features read on layer '%s'.",
                  static_cast<int>(m_nFeaturesRead),
                  poFeatureDefn->GetName() );
    }

    if( fpREC != nullptr )
        VSIFClose( fpREC );

    if( poFeatureDefn )
        poFeatureDefn->Release();

    CPLFree( panFieldOffset );
    CPLFree( panFieldWidth );
}

/************************************************************************/
/*                            ResetReading()                            */
/************************************************************************/

void OGRRECLayer::ResetReading()

{
    CPL_IGNORE_RET_VAL(VSIFSeek( fpREC, nStartOfData, SEEK_SET ));
    nNextFID = 1;
}

/************************************************************************/
/*                      GetNextUnfilteredFeature()                      */
/************************************************************************/

OGRFeature * OGRRECLayer::GetNextUnfilteredFeature()

{
/* -------------------------------------------------------------------- */
/*      Read and assemble the source data record.                       */
/* -------------------------------------------------------------------- */
    int        nDataLen = 0;
    char       *pszRecord = static_cast<char *>(CPLMalloc(nRecordLength + 2 ));

    while( nDataLen < nRecordLength )
    {
        const char *pszLine = CPLReadLine( fpREC );

        if( pszLine == nullptr )
        {
            CPLFree( pszRecord );
            return nullptr;
        }

        if( *pszLine == 0 || *pszLine == 26 /* Cntl-Z - DOS EOF */ )
        {
            CPLFree( pszRecord );
            return nullptr;
        }

        // If the end-of-line markers is '?' the record is deleted.
        int iSegLen = static_cast<int>(strlen(pszLine));
        if( pszLine[iSegLen-1] == '?' )
        {
            pszRecord[0] = '\0';
            nDataLen = 0;
            continue;
        }

        // Strip off end-of-line '!' marker.
        if( pszLine[iSegLen-1] != '!'
            && pszLine[iSegLen-1] != '^' )
        {
            CPLError( CE_Failure, CPLE_AppDefined,
                      "Apparent corrupt data line .. record FID=%d",
                      nNextFID );
            CPLFree( pszRecord );
            return nullptr;
        }

        iSegLen--;
        if( nDataLen + iSegLen > nRecordLength )
        {
            CPLError( CE_Failure, CPLE_AppDefined,
                      "Too much data for record %d.",
                      nNextFID );
            CPLFree( pszRecord );
            return nullptr;
        }

        memcpy( pszRecord+nDataLen, pszLine, iSegLen );
        pszRecord[nDataLen+iSegLen] = '\0';
        nDataLen += iSegLen;
    }

/* -------------------------------------------------------------------- */
/*      Create the OGR feature.                                         */
/* -------------------------------------------------------------------- */
    OGRFeature *poFeature = new OGRFeature( poFeatureDefn );

/* -------------------------------------------------------------------- */
/*      Set attributes for any indicated attribute records.             */
/* -------------------------------------------------------------------- */
    for( int iAttr = 0; iAttr < nFieldCount; iAttr++)
    {
        const char *pszFieldText =
            RECGetField( pszRecord,
                         panFieldOffset[iAttr] + 1,
                         panFieldWidth[iAttr] );

        if( strlen(pszFieldText) != 0 )
        {
            /* coverity[tainted_data] */
            poFeature->SetField( iAttr, pszFieldText );
        }
    }

/* -------------------------------------------------------------------- */
/*      Translate the record id.                                        */
/* -------------------------------------------------------------------- */
    poFeature->SetFID( nNextFID++ );
    m_nFeaturesRead++;

    CPLFree( pszRecord );

    return poFeature;
}

/************************************************************************/
/*                           GetNextFeature()                           */
/************************************************************************/

OGRFeature *OGRRECLayer::GetNextFeature()

{
    OGRFeature  *poFeature = nullptr;

/* -------------------------------------------------------------------- */
/*      Read features till we find one that satisfies our current       */
/*      spatial criteria.                                               */
/* -------------------------------------------------------------------- */
    while( true )
    {
        poFeature = GetNextUnfilteredFeature();
        if( poFeature == nullptr )
            break;

        if( m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate( poFeature ) )
            break;

        delete poFeature;
    }

    return poFeature;
}

/************************************************************************/
/*                           TestCapability()                           */
/************************************************************************/

int OGRRECLayer::TestCapability( const char * /* pszCap */)
{
    return FALSE;
}
