//-----------------------------------------------------------------------------
// 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_map
//
// purpose	Copy an open file to another open file, with parsing for
//		substitutions that begin and end with specified strings,
//		substituting each with the string found in the given mapping.
//
// arguments	1 (FILE *) open input file
//		2 (FILE *) open output file
//		3 (MAP) mapping to get replacement data from
//		4 (const char *) substitution marker prefix string
//		5 (const char *) substitution marker suffix string
//
// returns	(int) -2 : error
//		(int) -1 : EOF collecting substitution
//		(int)  0 : EOF complete
//-----------------------------------------------------------------------------
int
sub_copy_map (
    FILE *		arg_file_in
    ,
    FILE *		arg_file_out
    ,
    MAP			arg_map
    ,
    const char *	arg_prefix
    ,
    const char *	arg_suffix
    )
__PROTO_END__
{
    const char *	data_ptr	;
    int			rc		;
    char		sub_data	[PATH_MAX+1];


    //-----------------
    // Check arguments.
    //-----------------
    if ( ! arg_file_in ) return -2;
    if ( ! arg_file_out ) return -2;
    if ( ! arg_map ) return -2;
    if ( ! arg_prefix ) return -2;
    if ( ! arg_suffix ) return -2;

    //-------------------------------------------------------
    // Cycle through each substitution returned by sub_copy()
    // and output the data found in the mapping.
    //-------------------------------------------------------
    for (;;) {
	rc = sub_copy( arg_file_in, arg_file_out, sub_data, sizeof sub_data, arg_prefix, arg_suffix );
	if ( rc <= 0 ) return rc;
	if ( rc == 1 ) {
	    if ( map_str_find_exact( arg_map, sub_data ) == MAP_DATA_STR ) {
		data_ptr = map_str_fetch_ptr( arg_map );
		if ( data_ptr ) fputs( data_ptr, arg_file_out );
	    }
	}
    }
}

