// ============================================================================
/*!
//
//  \file TUtilities.hh
//  \brief This file contains the TUtilities class.
//  \author Go IWAI <goiwai@users.sourceforge.jp>
//
//  $Id: TUtilities.hh,v 1.7 2004/03/07 10:30:34 goiwai Exp $
//  $Name: CLDAQ-1-14-06 $
//  $Log: TUtilities.hh,v $
//  Revision 1.7  2004/03/07 10:30:34  goiwai
//  ROOTに組みこむためのおまじないマクロを埋めこみました。
//  全てにおいて完全に動作するわけではありません。
//
//  Revision 1.6  2004/02/26 09:11:33  goiwai
//  タイプミスでコンパイル通らなくなってたところを修正
//
//  Revision 1.5  2004/02/06 07:33:31  goiwai
//  split,index,subst,のパターンを指定しなかった場合の振舞を変更しました.
//
//  Revision 1.4  2004/01/29 04:25:07  goiwai
//  いくつかの関数で微妙に仕様変更しました.
//  あとは,substituteをconst sourceのconstをとろうかどうしようか迷ってます.
//
//  Revision 1.3  2004/01/19 21:09:43  goiwai
//  プリプロセッサの接頭辞をCLDAQ__に変更しました.
//
//  Revision 1.2  2003/11/03 08:35:15  goiwai
//  Doxygen用のタグとコメントをうちこんでいく試みを開始しました.
//
//  Revision 1.1  2003/10/06 16:34:36  goiwai
//  データの変換や正規表現等,頻繁に使用される関数群のためのラッパークラス
//  です.あたかもCのライブラリ関数の如く使用できます.
//
*/
// ============================================================================
#ifndef CLDAQ__TUTILITIES_HH
#define CLDAQ__TUTILITIES_HH

#include "Ttypes.h"


/*!
// \example
//
// If example code exists.
//
*/

/*!
// \class TUtilities
// \brief This is the TUtilities class.
// \author Go IWAI <goiwai@users.sourceforge.jp>
//
// The TUtilities class detail description.
// Write it down ASAP!
//
// \todo None?
// \bug None?
// \ingroup SystemCategory
*/
class TUtilities
{

  public:
    static Tstring ConvertIntegerToString( Tint i, Tint digits );
    static Tstring ConvertLongToString( Tlong l, Tint digits );
    static Tstring ConvertUnsignedLongToString( TUlong ul, Tint digits );
    static Tstring ConvertDoubleToString( Tdouble d, Tint precision );
    static Tstring ConvertFloatToString( Tfloat f, Tint precision );
    static Tint ConvertStringToInteger( const Tstring& nptr, Tchar** endptr, Tint base );
    static Tdouble ConvertStringToDouble( const Tstring& nptr, Tchar** endptr );
    static Tvoid ShowBitPattern( Tint bit );
    static Tbool FileExist( const Tstring& filename, Tint mode );
    static TstringList Split( const Tstring& source, const Tstring& pattern, Tint pos );
    static TintList Index( const Tstring& source, const Tstring& pattern, Tint pos );
    static TintList Size( const Tstring& source, const Tstring& pattern, Tint pos );
    static Tstring Substitute( const Tstring& source, const Tstring& pattern, const Tstring& substr, Tint pos );
    static Tstring SubstituteAll( const Tstring& source, const Tstring& pattern, const Tstring& substr, Tint pos );
    static Tbool IsMatch( const Tstring& source, const Tstring& pattern, Tbool igcase, Tint pos );
    static Tint GetNumberOfMatches( const Tstring& source, const Tstring& pattern, Tint pos );
    static Tstring GetSubMatch( Tint index, const Tstring& source, const Tstring& pattern, Tint pos );
    static TstringList GetSubMatch( const Tstring& source, const Tstring& pattern, Tint pos );
    static Tstring GetBaseName( const Tstring& path );
    static Tstring GetDirectoryName( const Tstring& path );

  public:
    friend Tstring itostr( Tint i, Tint digits = 6 );
    friend Tstring ltostr( Tlong l, Tint digits = 6 );
    friend Tstring ultostr( TUlong ul, Tint digits = 6 );
    friend Tstring dtostr( Tdouble d, Tint precision = 6 );
    friend Tstring ftostr( Tfloat f, Tint precision = 6 );
    friend Tint strtoi( const Tstring& nptr, Tchar** endptr = 0, Tint base = 0 );
    friend Tdouble strtod( const Tstring& nptr, Tchar** endptr = 0 );
    friend Tvoid showbit( Tint bit );
    friend Tbool isexist( const Tstring& filename, Tint mode = R_OK|F_OK );
    friend TstringList split( const Tstring& source, const Tstring& pattern = "(\t| |\\s)+", Tint pos = 0 );
    friend TintList index( const Tstring& source, const Tstring& pattern = "(\t| |\\s)+", Tint pos = 0 );
    friend TintList size( const Tstring& source, const Tstring& pattern, Tint pos = 0 );
    friend Tstring subst( const Tstring& source, const Tstring& pattern = "(#.*)|(//.*)", const Tstring& substr = "", Tbool all = Ttrue, Tint pos = 0 );
    friend Tbool match( const Tstring& source, const Tstring& pattern, Tbool igcase = Tfalse, Tint pos = 0 );
    friend Tint nmatch( const Tstring& source, const Tstring& pattern, Tint pos = 0 );
    friend Tstring submatch( Tint index, const Tstring& source, const Tstring& pattern, Tint pos = 0 );
    friend TstringList submatch( const Tstring& source, const Tstring& pattern, Tint pos = 0 );
    friend Tbool grep( const Tstring& source, const Tstring& pattern, Tbool igcase = Tfalse, Tint pos = 0 );
    friend Tstring basename( const Tstring& path );
    friend Tstring dirname( const Tstring& path );
 
#ifdef __CLDAQ_ROOT_DLL
    ClassDef(TUtilities,0)
#endif

};

inline Tstring itostr( Tint i, Tint digits )
{
  return TUtilities::ConvertIntegerToString( i, digits );
}

inline Tstring ltostr( Tlong l, Tint digits )
{
  return TUtilities::ConvertLongToString( l, digits );
}

inline Tstring ultostr( TUlong ul, Tint digits )
{
  return TUtilities::ConvertUnsignedLongToString( ul, digits );
}

inline Tstring dtostr( Tdouble d, Tint precision )
{
  return TUtilities::ConvertDoubleToString( d, precision );
}

inline Tstring ftostr( Tfloat f, Tint precision )
{
  return TUtilities::ConvertFloatToString( f, precision );
}

inline Tint strtoi( const Tstring& nptr, Tchar** endptr, Tint base )
{
  return TUtilities::ConvertStringToInteger( nptr, endptr, base );
}

inline Tdouble strtod( const Tstring& nptr, Tchar** endptr )
{
  return TUtilities::ConvertStringToDouble( nptr, endptr );
}

inline Tvoid showbit( Tint bit )
{
  TUtilities::ShowBitPattern( bit );
  return;
}

inline Tbool isexist( const Tstring& filename, Tint mode )
{
  return TUtilities::FileExist( filename, mode );
}

inline TstringList split( const Tstring& source, const Tstring& pattern, Tint pos )
{
  return TUtilities::Split( source, pattern, pos );
}

inline TintList index( const Tstring& source, const Tstring& pattern, Tint pos )
{
  return TUtilities::Index( source, pattern, pos );
}

inline TintList size( const Tstring& source, const Tstring& pattern, Tint pos )
{
  return TUtilities::Size( source, pattern, pos );
}

inline Tstring subst( const Tstring& source, const Tstring& pattern, const Tstring& substr, Tbool all, Tint pos )
{
  if ( all ) {
    return TUtilities::SubstituteAll( source, pattern, substr, pos );
  } else {
    return TUtilities::Substitute( source, pattern, substr, pos );
  }
}

inline Tbool match( const Tstring& source, const Tstring& pattern, Tbool igcase, Tint pos )
{
  return TUtilities::IsMatch( source, pattern, igcase, pos );
}

inline Tbool grep( const Tstring& source, const Tstring& pattern, Tbool igcase, Tint pos )
{
  return TUtilities::IsMatch( source, pattern, igcase, pos );
}

inline Tstring submatch( Tint index, const Tstring& source, const Tstring& pattern, Tint pos )
{
  return TUtilities::GetSubMatch( index, source, pattern, pos );
}

inline TstringList submatch( const Tstring& source, const Tstring& pattern, Tint pos )
{
  return TUtilities::GetSubMatch( source, pattern, pos );
}

inline Tstring basename( const Tstring& path )
{
  return TUtilities::GetBaseName( path );
}

inline Tstring dirname( const Tstring& path )
{
  return TUtilities::GetDirectoryName( path );
}

#endif
