/******************************************************************************
 * $Id: shpchk.c $
 *
 * Project:  Shapelib
 * Purpose:  Sample application to compare number of shp and dbf records
 * Author:   Steffen Macke, sdteffen@web.de
 *
 ******************************************************************************
 * 
 * Copyright (c) 2004, Steffen Macke
 * Copyright (c) 1999, Frank Wamderdam
 *
 * This software is available under the following "MIT Style" license,
 * or at the option of the licensee under the LGPL (see LICENSE.LGPL).  This
 * option is discussed in more detail in shapelib.html.
 *
 * --
 * 
 * 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.
 ******************************************************************************
 *
 * gcc shpchk.c dbfopen.o shpopen.o -o shpchk
 */

static char rcsid[] = 
  "$Id: shpchk.c $";

#include <stdlib.h>
#include <string.h>
#include "shapefil.h"

int main( int argc, char ** argv )

{
    DBFHandle	hDBF;
    SHPHandle   hSHP;
    int		nDBFRecords, nSHPRecords;
    char	*pszFilename = NULL;
    
/* -------------------------------------------------------------------- */
/*      Display a usage message.                                        */
/* -------------------------------------------------------------------- */
    if( argc != 2 )
    {
	printf( "shpchk shapefile\n" );
        printf( "        Compare number of records in shp and dbf files.\n" );
	exit( 1 );
    }

    
/* -------------------------------------------------------------------- */
/*      Handle arguments.                                               */
/* -------------------------------------------------------------------- */
    
    pszFilename = argv[1];

/* -------------------------------------------------------------------- */
/*      Open the DBF file.                                                  */
/* -------------------------------------------------------------------- */
    hDBF = DBFOpen( pszFilename, "rb" );
    if( hDBF == NULL )
    {
	printf( "DBFOpen(%s,\"r\") failed.\n", argv[1] );
	exit( 2 );
    }
    
/* -------------------------------------------------------------------- */
/*	If there is no data in this file let the user know.		*/
/* -------------------------------------------------------------------- */
     
    if( DBFGetFieldCount( hDBF ) == 0 )
    {
        printf( "There are no fields in table \"%s\"\n",argv[1] );
	DBFClose(hDBF);
	exit( 3 );
    }
    
    nDBFRecords = DBFGetRecordCount( hDBF );
/* -------------------------------------------------------------------- */
/*      Open the SHP file.                                                  */
/* -------------------------------------------------------------------- */

    hSHP = SHPOpen( pszFilename, "rb" );
    
    SHPGetInfo( hSHP, &nSHPRecords, NULL, NULL, NULL );
    
    if( nDBFRecords == nSHPRecords )
        printf("%s - OK\n", argv[1]);
    else
        printf("%s - %d DBF records, %d SHP records\n", argv[1], nDBFRecords, 
            nSHPRecords);  
    SHPClose( hSHP );
    DBFClose( hDBF );

    return( 0 );
}
