//-----------------------------------------------------------------------------
// 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 "io_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	skip_line_file
//
// purpose	Skip to the next line delimited by a combination of end of line
//		characters, discarding the contents.
//
// arguments	1 (FILE *) pointer to open file to read from
//
// returns	(int) -3 : error
//		(int) -2 : line not skipped due to immediate end of file
//              (int)  0 : line skipped OK
//
// note		An end of line is considered to be any combination of the four
//		line breaking characters:
//			'\n' (newline)
//			'\r' (carriage return)
//			'\f' (form feed)
//			'\v' (vertical tab)
//		with not more than one instance of any one character making up
//		one line ending.  If a character is encountered a 2nd time, it
//		is considered to begin a 2nd line ending.
//-----------------------------------------------------------------------------
int
skip_line_file (
    FILE *	arg_file
    )
__PROTO_END__
{
    int			ch		;
    int			seen_nl		;
    int			seen_cr		;
    int			seen_ff		;
    int			seen_vt		;


    //-----------------
    // Check arguments.
    //-----------------
    if ( ! arg_file ) arg_file = stdin;

    //----------------------------------------
    // Skip the line until EOF or end of line.
    //----------------------------------------
    if ( ( ch = fgetc( arg_file ) ) == EOF ) return -2;
    do {
	if ( ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v' ) break;
    } while ( ( ch = fgetc( arg_file ) ) != EOF );

    //------------------------------------------------------
    // Skip all possible end of line characters no more than
    // once each, stopping if EOF, to reach the next line.
    //------------------------------------------------------
    seen_nl = seen_cr = seen_ff = seen_vt = 0;
    if ( ch != EOF ) {
	do {
	    if ( ch == '\n' ) { if ( ++ seen_nl > 1 ) break; else continue; }
	    if ( ch == '\r' ) { if ( ++ seen_cr > 1 ) break; else continue; }
	    if ( ch == '\f' ) { if ( ++ seen_ff > 1 ) break; else continue; }
	    if ( ch == '\v' ) { if ( ++ seen_vt > 1 ) break; else continue; }
	    break;
	} while ( ( ch = fgetc( arg_file ) ) != EOF );
    }

    //------------------------------------
    // Unget first character of next line.
    //------------------------------------
    if ( ch != EOF ) ungetc( ch, arg_file );

    //-----------
    // Return OK.
    //-----------
    return 0;
}

