// =====================================================================
//  $Id: findprimitive.cc,v 1.1.1.1 2003/06/27 02:56:41 goiwai Exp $
//  $Name: CLDAQ-1-06-02 $
// =====================================================================
#include "TDataRecord.hh"
#include "TDataElement.hh"
#include "TInputObjectFile.hh"
#include "TBenchmark.hh"

int main( int argc, char** argv, char** envv )
{
  if ( argc != 2 ) {
    Tcerr << "usage: " << argv[ 0 ] << Tspace << "<filename>" << Tendl;
    return( -1 );
  }

  const Tstring filename = argv[ 1 ];
  Tstring sectag;
  Tstring segtag;
  Tstring eletag;
  if ( filename == "ofile.drec" ) {
    sectag = "This is Data Section.";
    segtag = "This is a Data Segement identified 0.";
    eletag = "This is unsigned short data array.";
  } else if ( filename == "software.drec" ) {
    sectag = "This section is for software modules.";
    segtag = "ADC";
    eletag = "channel 5";
  } else {
    Tcerr << argv[ 0 ] << ": unkown tags." << Tendl;
    return( -1 );
  }

  Tstring findtag[ 3 ] = { sectag, segtag, eletag };
  TInputObjectFile ifile( filename );

  // どんな型でも受けつけるけど、問答無用でつっこむので、
  // 安全に実行したければ、element.StorePrimitives( buffer, narray );
  const Tint narray = 10000;
  Tstring buffer[ narray ];
  // Tfloat buffer[ narray ];
  // Tdouble buffer[ narray ];
  // Tint buffer[ narray ];
  // Tshort buffer[ narray ];
  // Tlong buffer[ narray ];

  TDataRecord record;
  TDataElement element;
  TBenchmark bench;
  bench.Start();
  while ( ( ifile >> record ) != 0 ) {
    if ( record.FindDataElement( findtag, element ) ) {
      Tint n = element.StorePrimitives( buffer );
      for ( Tint i = 0; i < n; i ++ ) {
        Tcout << "Primitive[" << i << "]: " << buffer[ i ] << Tendl;
      }
    }
  }
  bench.Stop();
  Tcout << bench << Tendl;

  return( 0 );
}
