//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/io
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <stdio.h>

#include <libh/io.h>

//-----------------------------------------------------------------------------
// program	test_get_item
//
// purpose	Test the get_item_next() function.
//
// usage	test_get_item < datafile
//-----------------------------------------------------------------------------
int
main (
    int		argc
    ,
    char * *	argv
    )
{
    get_item_p		item_p		;

    int			rc		;
    char		item_space	[80]	;

    item_p = get_item_new_file( stdin );
    if ( ! item_p ) {
	fprintf(stderr,"get_item_new_file() failed\n");
	return 1;
    }

    if ( argc >= 2 && argv[1] ) {
	get_item_set_options( item_p, argv[1] );
    }

    for (;;) {

	rc = get_item_next( item_p, item_space, 80 );

	printf( "(c=%03lu,r=%03lu) .. (c=%03lu,r=%03lu) [b=%05llu .. b=%05llu]  ",
		get_item_begin_col( item_p ),
		get_item_begin_row( item_p ),
		get_item_end_col( item_p ),
		get_item_end_row( item_p ),
		get_item_begin_cnt( item_p ),
		get_item_end_cnt( item_p ) );

	if ( rc == -4 ) {
	    printf( "invalid arguments\n" );
	    break;
	}
	else if ( rc == -3 ) {
	    printf( "I/O error\n" );
	    break;
	}
	else if ( rc == -2 ) {
	    printf( "end of file\n" );
	    break;
	}
	else if ( rc == -1 ) {
	    printf( "end of line\n" );
	}
	else if ( rc == 0 ) {
	    printf( "item[%lu]: \"%s\"\n",
		    (unsigned long) get_item_actual_len( item_p ),
		    item_space );
	}
	else if ( rc == 1 ) {
	    printf( "truncated item[%lu,%lu]: \"%s\"\n",
		    (unsigned long) get_item_actual_len( item_p ),
		    (unsigned long) get_item_stored_len( item_p ),
		    item_space );
	}
	else {
	    printf( "unexpected return code %d\n", rc );
	    break;
	}
    }

    return 0;
}

