/* ************************************************************************
 * *                                                                      *
 * *                Pipe to filter catchment data for Turkey              *
 * *                                                                      *
 * ************************************************************************
 * File: utils/pipes_out/TurkeyCat.C
 *
 *                         COPYRIGHT (c) 2003  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

/*
 * Note if you are modifying this program, and wish to insert a diagnostic
 * printf, be sure to write it to the error output.
 */

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

#include "sigtypes.h"

/* ===============================================================
 * Main program starts here.
 */
int main(int iArgc, char *sArgv[])
{
  int ierror, iLength, iLine;
#define LINE_SIZE (80)
  char *psLine, *sToken, *sNextToken, *sEnd;
  char sLine[LINE_SIZE], sScratchLine[LINE_SIZE];

  setlocale( LC_ALL, "" ) ;

  /* Read in one line */
  iLine=0;
  psLine =  fgets( sLine, LINE_SIZE, stdin ); iLine++;
  while (psLine)
    {
      iLength = strlen(psLine);
      if (iLength>=(LINE_SIZE-1))
	fprintf(stderr, "ERROR: Line overflow, line %d\n", iLine );

      /* The pipe conversion is quit simple:  If the first token is a number, 
       * then remove it.  If not, then keep the original line.
       */
      /* Copy line to a scratch buffer because the strtok damages it */
      strcpy( sScratchLine, sLine );
      sToken = strtok_r( sScratchLine, " ", &sNextToken );
      strtol( sToken, &sEnd, 10 );

      /* The end point will match the input only if the number is
       * not a base 10 number.
       */
      if (sToken == sEnd)
	ierror = fputs( sLine, stdout );
      else
	ierror = fputs( sNextToken, stdout );
	
      if (ierror<0)
	{
	  perror("fputs");
	  exit(EXIT_FAILURE);
	}
      psLine =  fgets( sLine, LINE_SIZE, stdin ); iLine++;
    }

  exit( EXIT_SUCCESS );
}


