// ----------------------------------------------------------------------
//
// eof.cc - Does eof() behave properly on an istream?
//
// History:
//   06-Oct-1999  WEB  Initial draft, adapted from Scott Snyder's example
//   12-Oct-1999  WEB  Parameterize
//   25-Apr-2001  WEB  s/<ISOcxxSyntax.hh>/"ISOcxx\/ISOcxxSyntax.hh"/
//
// ----------------------------------------------------------------------


#include "ISOcxx/ISOcxxSyntax.hh"

#include <iostream>
#include <fstream>


int  main()  {

  // -------------------------
  // declare text of interest:
  // -------------------------

  char const  t[] = { "abc" };
  int const   s   = sizeof t;
  char const  nul = '\0';

  // ------------------------------
  // produce file for future input:
  // ------------------------------

  std::ofstream  os( "conftest.dat" );
  if ( ! os )  return  1;
  os.write( t, s );
  os.close();

  // ---------------------------------------
  // prepare to consume the file's contents:
  // ---------------------------------------

  char  buf[s];
  std::ifstream  is( "conftest.dat" );
  if ( ! is )  return  2;

  // -----------------------------------------------
  // consume and verify contents; verify file state:
  // -----------------------------------------------

  buf[0] = buf[1] = buf[2] = nul;

  if ( is.read( buf, s )
         .gcount() != s
     )
    return  3;
  if (   t[0] != buf[0]
     ||  t[1] != buf[1]
     ||  t[2] != buf[2]
     )
    return  4;
  if ( is.eof() )
    return  5;

  // ------
  // again:
  // ------

  is.clear();
  buf[0] = buf[1] = buf[2] = nul;

  if ( is.seekg( 0 )
         .read( buf, 1 )
         .gcount() != 1
     )
    return  6;
  if (   t[0] != buf[0]
     ||  nul  != buf[1]
     ||  nul  != buf[2]
     )
    return  7;
  if ( is.eof() )
    return  8;

  // --------
  // go home:
  // --------

  is.close();
  return  0;

}  // main()
