/**********************************************************************
*
*  module   : Hfd  (Dilib Acces Method)
*  fichier  : HfdCat.c
*  Auteur   : Jacques DUCLOY
*  Date     : Octobre 93
*  Modification : Fevrier 94
*  $Id: HfdCat.c,v 1.5 2005/06/22 12:23:26 parmentf Exp $
*
***********************************************************************
* 
*     Copyleft
*
***********************************************************************/
/**
   @file

   @brief HfdCat command

   @section HfdCatSYNOPSIS SYNOPSIS

   @code
   HfdCat [-] [fileName...]
   @endcode

   @section HfdCatDESCRIPTION DESCRIPTION

   @p HfdCat reads each @p filename in sequence and displays it on the
   standard output.

   If no @p filename argument is given, or if the argument `-' is
   given, @p HfdCat reads from the standard input.  If the standard
   input is a terminal, input is terminated by an @p EOF condition.

   If @p filename is terminated by suffix ".hfd", @p HfdCat reads each
   leaf of the HFD File in sequence.  Nodes and leaves suffixes must
   be conformed to DamHfd(5).

   @see DamHfd, cat

   @author &copy; INIST-CNRS
   @author Jacques DUCLOY
           <DilibMaster@inist.fr>
   @since  November 1993

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Hfd.h"

/***************   HfdCat *********************************************/

void HfdCopyFile(FILE *inputFile, 
		 HfdStreamIterator *listInputStream)
{
  int c; /* char c; */
  int c1;/* char c1;*/

  if((listInputStream->hfdExploring)
     &&(listInputStream->hfdIterator->isMultiGroup))
    printf("%s:",BufferString(listInputStream->hfdIterator->multiBegin));

  while ((c=getc(inputFile))!=EOF)
    {
      putchar(c);
      if((c=='\n')
	 &&(listInputStream->hfdExploring)
	 &&(listInputStream->hfdIterator->isMultiGroup))
	{
	  if ((c=getc(inputFile))!=EOF)
	    {
	      printf("%s:",BufferString(listInputStream->hfdIterator->multiBegin));
	      putchar(c);
	      continue;
	    }
	  else return;
	}
      c1=c;
    }
  if (c1!='\n')putchar ('\n');
  return ;
}

int main (int argc, char **argv)
{
  FILE              *inputFile;
  HfdStreamIterator *listInputStream;

  listInputStream=HfdStreamIteratorCreate('h');
  HfdStreamIteratorSetStreams(listInputStream, argv+1);
  
  while ((inputFile=HfdNextStream(listInputStream)))
    {
      HfdCopyFile(inputFile, listInputStream);
    }
  exit(EXIT_SUCCESS);
}
