//-----------------------------------------------------------------------------
// 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 Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; 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 "io_lib.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	eput_bytes_end
//
// purpose	Output bytes, escaped as needed, for the specified length,
//		to stderr.
//
// arguments	1 (const char *) point to beginning of bytes
//		2 (const char *) point to end of bytes (after last)
//
// returns	(int) 0
//-----------------------------------------------------------------------------
#define eput_bytes_end(p,e) fput_bytes_end((p),(e),(stderr))

__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	put_bytes_end
//
// purpose	Output bytes, escaped as needed, for the specified length,
//		to stdout.
//
// arguments	1 (const char *) point to beginning of bytes
//		2 (const char *) point to end of bytes (after last)
//
// returns	(int) 0
//-----------------------------------------------------------------------------
#define put_bytes_end(p,e) fput_bytes_end((p),(e),(stdout))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	fput_bytes_end
//
// purpose	Output bytes, escaped as needed, for the specified length,
//		to the specified open file.
//
// arguments	1 (const char *) point to beginning of bytes
//		2 (const char *) point to end of bytes (after last)
//		3 (FILE *) file to output to
//
// returns	(int) total characters output (extra for escaped)
//-----------------------------------------------------------------------------
int
fput_bytes_end (
    const char *	arg_ptr
    ,
    const char *	arg_end
    ,
    FILE *		arg_file
    )
    __PROTO_END__
{
    int count;
    count = 0;
    while ( arg_ptr < arg_end ) {
	int ch;
	int es;

	ch = * arg_ptr ++;
	es = 0;
	if ( ch == '\\' )	es = '\\';
	else if ( ch == '\a' )	es = 'a';
	else if ( ch == '\b' )	es = 'b';
	else if ( ch == '\f' )	es = 'f';
	else if ( ch == '\n' )	es = 'n';
	else if ( ch == '\r' )	es = 'r';
	else if ( ch == '\t' )	es = 't';
	else if ( ch == '\v' )	es = 'v';
	if ( es ) {
	    fputc( '\\', arg_file );
	    fputc( es, arg_file );
	    count += 2;
	    continue;
	}
	if ( 32 <= ch && ch <= 126 ) {
	    fputc( ch, arg_file );
	    count ++;
	    continue;
	}
	fputc( '\\', arg_file );
	fputc( ( ( ch >> 6 ) & 7 ) + '0', arg_file );
	fputc( ( ( ch >> 3 ) & 7 ) + '0', arg_file );
	fputc( ( ch & 7 ) + '0', arg_file );
	count += 4;
    }
    return count;
}

