//-----------------------------------------------------------------------------
// 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	get_line_file
//
// purpose	Get the next line, ended by the newline character (or formfeed
//		or vertical tab), from an open FILE, with any carriage returns
//		truncated, copied to the specified target buffer up to one
//		one character less than the size given (plus a null character
//		always stored at the end of the stored string even if the line
//		is too long and the stored string is truncated).
//
// arguments	1 (char *) pointer to target buffer
//		2 (size_t) size of target buffer
//		3 (FILE *) pointer to open file to read from, or stdin
//
// returns	(ssize_t) == -2 : error
//		(ssize_t) == -1 : end of file
//              (ssize_t) >=  0 : length of line
//
// note		The length returned is the length of the actual line, up to
//		any trailing newline-class characters, NOT the length of the
//		string actually stored.  Use strlen() to get the stored
//		length.
//-----------------------------------------------------------------------------
ssize_t
get_line_file (
    char *	arg_target
    ,
    size_t	arg_length
    ,
    FILE *	arg_file
    )
__PROTO_END__
{
    char *		target_end	;
    char *		target_ptr	;
    char *		trunc_ptr	;
    size_t		line_len	;
    size_t		return_len	;
    int			ch		;

    if ( ! arg_target ) return -2;
    if ( arg_length < 1 ) return -2;
    if ( ! arg_file ) arg_file = stdin;

    trunc_ptr = target_ptr = arg_target;
    target_end = target_ptr + arg_length - 1;
    return_len = line_len = 0;
    for (;;) {
	ch = fgetc( arg_file );
	if ( ch == EOF || ch == '\n' || ch == '\f' || ch == '\v' ) break;
	++ line_len;
	if ( ch != '\r' ) return_len = line_len;
	if ( target_ptr == target_end ) continue;
	* target_ptr ++ = ch;
	if ( ch != '\r' ) trunc_ptr = target_ptr;
    }
    * trunc_ptr = 0;
    if ( ch == EOF && target_ptr == arg_target ) return -1;

    return return_len;
}

