// =====================================================================
//  $Id: findelement.cc,v 1.3 2003/10/06 04:03:39 goiwai Exp $
//  $Name: CLDAQ-1-14-03 $
//  $Log: findelement.cc,v $
//  Revision 1.3  2003/10/06 04:03:39  goiwai
//  TArgumentを使用するように全てのプログラムを修整しました.
//
//  Revision 1.2  2003/07/30 16:21:38  goiwai
//  ファイルにコミットログをつけることにしました.
//
// =====================================================================
#include "TDataRecord.hh"
#include "TDataElement.hh"
#include "TInputObjectFile.hh"
#include "TBenchmark.hh"
#include "TArgument.hh"

int main( int argc, char** argv, char** envv )
{
  TArgument arg( argc, argv );
  if ( arg.GetNumberOfElements() != 1 || !isexist( arg[ 0 ] ) ) {
    arg.ShowUsage();
  }
  const Tstring filename = arg[ 0 ];

  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 {
    Tcout << "candidate: ofile.drec software.drec" << Tendl;
    return 0;
  }

  Tstring findtag[ 3 ] = { sectag, segtag, eletag };

  TInputObjectFile ifile( filename );

  TDataRecord record;
  TDataElement element;
  TBenchmark bench;
  bench.Start();
  Tint nelements = 0;
  while ( ( ifile >> record ) != 0 ) {
    if ( record.FindDataElement( findtag, element ) ) {
      Tcout << "Find element tagged \"" << element.GetID() << "\"." << Tendl;
      nelements ++;
    }
  }
  Tcout << nelements << " element(s) have been found out from ";
  Tcout << filename << "." << Tendl;
  bench.Stop();
  Tcout << bench << Tendl;

  return 0;
}
