//-----------------------------------------------------------------------------
// 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	sub_copy
//
// purpose	Copy an open file to another open file, with parsing for
//		substitutions that begin and end with specified strings,
//		returning to the caller for each substitution encountered.
//		The caller can output whatever it needs to output, and
//		resume by calling sub_copy() again.
//
// arguments	1 (FILE *) open input file
//		2 (FILE *) open output file
//		3 (char *) pointer to where to store substitution string
//		4 (size_t) size of space to store substitution string
//		5 (const char *) substitution marker prefix string
//		6 (const char *) substitution marker suffix string
//
// returns	(int) -2 : error
//		(int) -1 : EOF collecting substitution (stored)
//		(int)  0 : EOF complete
//		(int)  1 : substitution found, complete
//		(int)  2 : substitution found, truncated
//
// note		The marker prefix and suffix are not store with the string to
//		be substituted.
//-----------------------------------------------------------------------------
int
sub_copy (
    FILE *		arg_file_in
    ,
    FILE *		arg_file_out
    ,
    char *		arg_ptr
    ,
    size_t		arg_len
    ,
    const char *	arg_prefix
    ,
    const char *	arg_suffix
    )
__PROTO_END__
{
    const char *	match_ptr	;
    int			match_state	;
    int			ch		;


    //-----------------
    // Check arguments.
    //-----------------
    if ( ! arg_file_in ) return -2;
    if ( ! arg_file_out ) return -2;
    if ( ! arg_ptr ) return -2;
    if ( arg_len == ~0 ) return -2;
    if ( ! arg_prefix ) return -2;
    if ( ! arg_suffix ) return -2;

    //---------------------------------------------------
    // Copy input to output, looking for matching prefix.
    //---------------------------------------------------
    match_ptr = arg_prefix;
    match_state = 0;
    for (;;) {
	ch = fgetc( arg_file_in );
	if ( ch == EOF ) return 0;

	//-- If another matching character, count it.
	if ( ch == * match_ptr ) {
	    ++ match_ptr;
	    ++ match_state;
	    //-- If prefix is done, collecting string is next.
	    if ( * match_ptr == 0 ) break;
	    continue;
	}

	//-- If incomplete match exists, flush it.
	if ( match_state > 0 ) {
	    match_ptr = arg_prefix;
	    while ( match_state ) {
		putc( * match_ptr, arg_file_out );
		++ match_ptr;
		-- match_state;
	    }
	    match_ptr = arg_prefix;
	}

	//-- Output non-matching character.
	putc( ch, arg_file_out );
    }

    //------------------------------------------------------------
    // Collect input after matching prefix, until matching suffix.
    //------------------------------------------------------------
    match_ptr = arg_suffix;
    match_state = 0;
    for (;;) {
	ch = fgetc( arg_file_in );
	if ( ch == EOF ) return 0;

	//-- If another matching character, count it.
	if ( ch == * match_ptr ) {
	    ++ match_ptr;
	    ++ match_state;
	    //-- If suffix is done, return to caller is next.
	    if ( * match_ptr == 0 ) break;
	    continue;
	}

	//-- If incomplete match exists, store it as data.
	if ( match_state > 0 ) {
	    match_ptr = arg_suffix;
	    while ( match_state ) {
		if ( arg_len ) {
		    * arg_ptr = * match_ptr;
		    ++ arg_ptr;
		    -- arg_len;
		}
		++ match_ptr;
		-- match_state;
	    }
	    match_ptr = arg_suffix;
	}

	//-- Collect non-matching character.
	if ( arg_len ) {
	    * arg_ptr = ch;
	    ++ arg_ptr;
	    -- arg_len;
	}
    }

    //--------------------------------------------------------------
    // Return to caller with string found between prefix and suffix.
    //--------------------------------------------------------------
    if ( arg_len == 0 ) {
	-- arg_ptr;
	* arg_ptr = 0;
	return 2;
    }
    * arg_ptr = 0;
    return 1;
}

