// @(#)root/table:$Id$
// Author: Valery Fine(fine@bnl.gov)   01/03/2001

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * Copyright (C) 2001 [BNL] Brookhaven National Laboratory.              *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TFileIter
#define ROOT_TFileIter

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Class to iterate (read / write ) the events written to /from TFile.   //
//                                                                       //
//  - set the current internal cursor directly by different means        //
//  - set the current cursor to the "next" position if available         //
//  - gain extra information of the TKey object at the current position  //
//  - read TObject object from the TFile defined by TKey at the current  //
//         position                                                      //
//                                                                       //
//  - Read "next" object from the file                                   //
//  - n-th object from the file                                          //
//  - object that is in n object on the file                             //
//  - read current object                                                //
//  - return the name of the key of the current object                   //
//  - return the current position                                        //
//  - set the current position by the absolute position number           //
//  - set the current position by relative position number               //
//  - get the number of keys in the file                                 //
//                                                                       //
// The event is supposed to assign an unique ID in form of               //
//                                                                       //
// TKey name ::= event Id ::= eventName "." run_number "." event_number  //
//                                                                       //
//                                                                       //
// and stored as the TKey name of the object written                     //
//                                                                       //
//  author Valeri Fine                                                   //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef ROOT_TIterator
#include "TIterator.h"
#endif
#ifndef ROOT_TList
#include "TList.h"
#endif
#ifndef ROOT_TFile
#include "TFile.h"
#endif


class TFileIter : public TListIter {

private:

   TFileIter *fNestedIterator;   //! The inner TFidrectory interator;

   virtual TIterator &operator=(const TIterator &) { return *this; }
   virtual Bool_t operator!=(const TIterator &it) const { return TListIter::operator!=(it);}

protected:
   TDirectory   *fRootFile;       // TDirectory/TFile to be iterated over
   TString  fEventName;           // current key name
   UInt_t   fRunNumber;           // current "run number"
   UInt_t   fEventNumber;         // current "event number"
   Int_t    fCursorPosition;      // the position of the current key in the sorted TKey list
   Bool_t   fOwnTFile;            // Bit whether this classs creates TFile on its own to delete

   void     Initialize();
   TObject *ReadObj(const TKey *key) const;
   TKey    *NextEventKey(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");

public:

   TFileIter(const char *name, Option_t *option = "",
             const char *ftitle = "", Int_t compress = 1,
             Int_t netopt = 0);
   TFileIter(TFile *file=0);
   TFileIter(TDirectory *directory);
   TFileIter(const TFileIter &);
   virtual ~TFileIter();
// --- draft !!!     virtual Int_t Copy(TFile *destFile);
   Int_t   CurrentCursorPosition() const;
   virtual const TFile *GetTFile() const;
   virtual const TDirectory *GetTDirectory() const;
   static  TString MapName(const char *name, const char *localSystemKey = 0
                                            , const char *mountedFileSystemKey = 0);
   static  const char *GetResourceName();
   static  const char *GetDefaultMapFileName();
   static  const char *GetLocalFileNameKey();
   static  const char *GetForeignFileSystemKey();
   static  void  PurgeKeys(TList *listOfKeys);
   virtual Bool_t      IsOpen() const;
   virtual TObject    *NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");
   virtual Int_t       NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0);
   void                SetCursorPosition(Int_t cursorPosition);
   void                SetCursorPosition(const char *keyNameToFind);
   Int_t               GetObjlen() const;
   virtual Int_t       TotalKeys() const;
   virtual TKey       *SkipObjects(Int_t  nSkip=1);
   virtual TObject    *GetObject() const;
   virtual Int_t       GetDepth() const;

   TKey               *GetCurrentKey() const;
   const char         *GetKeyName() const;

   TFileIter &operator=(Int_t cursorPosition);
   TFileIter &operator=(const char *keyNameToFind);
   TFileIter &operator+=(Int_t shift);
   TFileIter &operator-=(Int_t shift);
   TFileIter &operator++();
   TFileIter &operator--();

   TObject *operator*() const;
   operator const char *() const;
   operator const TFile *() const;
   operator const TDirectory *() const;
   operator int () const;
   int operator==(const char *name) const;
   int operator!=(const char *name) const;

public:  // abstract TIterator methods implementations:

   virtual TObject *Next();
   virtual TObject *Next(Int_t  nSkip);
   virtual void Reset();
   virtual void Rewind();
   TObject *operator()(Int_t  nSkip);
   TObject *operator()();

   ClassDef(TFileIter,0) // TFile class iterator
};

//__________________________________________________________________________
inline const char *TFileIter::GetResourceName()        {return "ForeignFileMap";}
//__________________________________________________________________________
inline const char *TFileIter::GetDefaultMapFileName()  {return "io.config";}
//__________________________________________________________________________
inline const char *TFileIter::GetLocalFileNameKey()    {return "LocalFileSystem";}
//__________________________________________________________________________
inline const char *TFileIter::GetForeignFileSystemKey(){return "MountedFileSystem";}

//__________________________________________________________________________
inline Int_t TFileIter::CurrentCursorPosition() const
{
   // return the current
   return fNestedIterator ? fNestedIterator->CurrentCursorPosition() : fCursorPosition;
}

//__________________________________________________________________________
inline const TFile *TFileIter::GetTFile() const { return GetTDirectory()->GetFile(); }
//__________________________________________________________________________
inline const TDirectory *TFileIter::GetTDirectory() const
{ return fNestedIterator ? fNestedIterator->GetTDirectory() : fRootFile; }

//__________________________________________________________________________
inline TObject *TFileIter::Next()
{
   // Make 1 step over the file objects and returns its pointer
   // or 0, if there is no object left in the container
   return Next(1);
}

//__________________________________________________________________________
inline void TFileIter::Rewind()
{
   // Alias for "Reset" method
   Reset();
}
//__________________________________________________________________________
inline void  TFileIter::SetCursorPosition(Int_t cursorPosition)
{
   // Make <cursorPosition> steps (>0 - forward) over the file
   // objects to skip it
   if (fNestedIterator)
      fNestedIterator->SetCursorPosition(cursorPosition);
   else
      SkipObjects(cursorPosition - fCursorPosition);
}

//__________________________________________________________________________
inline TFileIter &TFileIter::operator=(const char *keyNameToFind)
{
   // Iterate unless the name of the object matches <keyNameToFind>
   SetCursorPosition(keyNameToFind); return *this;}

//__________________________________________________________________________
inline TFileIter &TFileIter::operator=(Int_t cursorPosition)
{
  // Iterate over <cursorPosition>
  SetCursorPosition(cursorPosition);
  return *this;
}
//__________________________________________________________________________
inline TFileIter::operator const TDirectory *() const
{ return GetTDirectory();  }

//__________________________________________________________________________
inline TFileIter::operator const TFile *() const
{ return GetTFile (); }
//__________________________________________________________________________
inline TFileIter &TFileIter::operator+=(Int_t shift)
{ SkipObjects(shift); return *this;}
//__________________________________________________________________________
inline TFileIter &TFileIter::operator-=(Int_t shift)
{ return operator+=(-shift);}
//__________________________________________________________________________
inline TFileIter &TFileIter::operator++()
{ SkipObjects( 1); return *this;}
//__________________________________________________________________________
inline TFileIter &TFileIter::operator--()
{ SkipObjects(-1); return *this;}
//__________________________________________________________________________
inline TObject *TFileIter::operator*() const
{ return GetObject();}
//__________________________________________________________________________
inline TFileIter::operator int () const
{ return CurrentCursorPosition(); }
//__________________________________________________________________________
inline TFileIter::operator const char *() const
{
   // return the current key name
   return GetKeyName();
}
//__________________________________________________________________________
inline int TFileIter::operator==(const char *name) const
{ return name ? !strcmp(name,GetKeyName()):0;}

//__________________________________________________________________________
inline int TFileIter::operator!=(const char *name) const
{ return !(operator==(name)); }

//__________________________________________________________________________
inline TObject *TFileIter::operator()(){ return Next(); }
//__________________________________________________________________________
inline TObject *TFileIter::operator()(Int_t  nSkip){ return Next(nSkip);}

#endif
 TFileIter.h:1
 TFileIter.h:2
 TFileIter.h:3
 TFileIter.h:4
 TFileIter.h:5
 TFileIter.h:6
 TFileIter.h:7
 TFileIter.h:8
 TFileIter.h:9
 TFileIter.h:10
 TFileIter.h:11
 TFileIter.h:12
 TFileIter.h:13
 TFileIter.h:14
 TFileIter.h:15
 TFileIter.h:16
 TFileIter.h:17
 TFileIter.h:18
 TFileIter.h:19
 TFileIter.h:20
 TFileIter.h:21
 TFileIter.h:22
 TFileIter.h:23
 TFileIter.h:24
 TFileIter.h:25
 TFileIter.h:26
 TFileIter.h:27
 TFileIter.h:28
 TFileIter.h:29
 TFileIter.h:30
 TFileIter.h:31
 TFileIter.h:32
 TFileIter.h:33
 TFileIter.h:34
 TFileIter.h:35
 TFileIter.h:36
 TFileIter.h:37
 TFileIter.h:38
 TFileIter.h:39
 TFileIter.h:40
 TFileIter.h:41
 TFileIter.h:42
 TFileIter.h:43
 TFileIter.h:44
 TFileIter.h:45
 TFileIter.h:46
 TFileIter.h:47
 TFileIter.h:48
 TFileIter.h:49
 TFileIter.h:50
 TFileIter.h:51
 TFileIter.h:52
 TFileIter.h:53
 TFileIter.h:54
 TFileIter.h:55
 TFileIter.h:56
 TFileIter.h:57
 TFileIter.h:58
 TFileIter.h:59
 TFileIter.h:60
 TFileIter.h:61
 TFileIter.h:62
 TFileIter.h:63
 TFileIter.h:64
 TFileIter.h:65
 TFileIter.h:66
 TFileIter.h:67
 TFileIter.h:68
 TFileIter.h:69
 TFileIter.h:70
 TFileIter.h:71
 TFileIter.h:72
 TFileIter.h:73
 TFileIter.h:74
 TFileIter.h:75
 TFileIter.h:76
 TFileIter.h:77
 TFileIter.h:78
 TFileIter.h:79
 TFileIter.h:80
 TFileIter.h:81
 TFileIter.h:82
 TFileIter.h:83
 TFileIter.h:84
 TFileIter.h:85
 TFileIter.h:86
 TFileIter.h:87
 TFileIter.h:88
 TFileIter.h:89
 TFileIter.h:90
 TFileIter.h:91
 TFileIter.h:92
 TFileIter.h:93
 TFileIter.h:94
 TFileIter.h:95
 TFileIter.h:96
 TFileIter.h:97
 TFileIter.h:98
 TFileIter.h:99
 TFileIter.h:100
 TFileIter.h:101
 TFileIter.h:102
 TFileIter.h:103
 TFileIter.h:104
 TFileIter.h:105
 TFileIter.h:106
 TFileIter.h:107
 TFileIter.h:108
 TFileIter.h:109
 TFileIter.h:110
 TFileIter.h:111
 TFileIter.h:112
 TFileIter.h:113
 TFileIter.h:114
 TFileIter.h:115
 TFileIter.h:116
 TFileIter.h:117
 TFileIter.h:118
 TFileIter.h:119
 TFileIter.h:120
 TFileIter.h:121
 TFileIter.h:122
 TFileIter.h:123
 TFileIter.h:124
 TFileIter.h:125
 TFileIter.h:126
 TFileIter.h:127
 TFileIter.h:128
 TFileIter.h:129
 TFileIter.h:130
 TFileIter.h:131
 TFileIter.h:132
 TFileIter.h:133
 TFileIter.h:134
 TFileIter.h:135
 TFileIter.h:136
 TFileIter.h:137
 TFileIter.h:138
 TFileIter.h:139
 TFileIter.h:140
 TFileIter.h:141
 TFileIter.h:142
 TFileIter.h:143
 TFileIter.h:144
 TFileIter.h:145
 TFileIter.h:146
 TFileIter.h:147
 TFileIter.h:148
 TFileIter.h:149
 TFileIter.h:150
 TFileIter.h:151
 TFileIter.h:152
 TFileIter.h:153
 TFileIter.h:154
 TFileIter.h:155
 TFileIter.h:156
 TFileIter.h:157
 TFileIter.h:158
 TFileIter.h:159
 TFileIter.h:160
 TFileIter.h:161
 TFileIter.h:162
 TFileIter.h:163
 TFileIter.h:164
 TFileIter.h:165
 TFileIter.h:166
 TFileIter.h:167
 TFileIter.h:168
 TFileIter.h:169
 TFileIter.h:170
 TFileIter.h:171
 TFileIter.h:172
 TFileIter.h:173
 TFileIter.h:174
 TFileIter.h:175
 TFileIter.h:176
 TFileIter.h:177
 TFileIter.h:178
 TFileIter.h:179
 TFileIter.h:180
 TFileIter.h:181
 TFileIter.h:182
 TFileIter.h:183
 TFileIter.h:184
 TFileIter.h:185
 TFileIter.h:186
 TFileIter.h:187
 TFileIter.h:188
 TFileIter.h:189
 TFileIter.h:190
 TFileIter.h:191
 TFileIter.h:192
 TFileIter.h:193
 TFileIter.h:194
 TFileIter.h:195
 TFileIter.h:196
 TFileIter.h:197
 TFileIter.h:198
 TFileIter.h:199
 TFileIter.h:200
 TFileIter.h:201
 TFileIter.h:202
 TFileIter.h:203
 TFileIter.h:204
 TFileIter.h:205
 TFileIter.h:206
 TFileIter.h:207
 TFileIter.h:208
 TFileIter.h:209
 TFileIter.h:210
 TFileIter.h:211
 TFileIter.h:212
 TFileIter.h:213
 TFileIter.h:214
 TFileIter.h:215
 TFileIter.h:216
 TFileIter.h:217
 TFileIter.h:218
 TFileIter.h:219
 TFileIter.h:220
 TFileIter.h:221
 TFileIter.h:222
 TFileIter.h:223
 TFileIter.h:224
 TFileIter.h:225
 TFileIter.h:226
 TFileIter.h:227
 TFileIter.h:228
 TFileIter.h:229
 TFileIter.h:230
 TFileIter.h:231
 TFileIter.h:232
 TFileIter.h:233
 TFileIter.h:234
 TFileIter.h:235
 TFileIter.h:236
 TFileIter.h:237
 TFileIter.h:238
 TFileIter.h:239
 TFileIter.h:240
 TFileIter.h:241
 TFileIter.h:242
 TFileIter.h:243
 TFileIter.h:244
 TFileIter.h:245
 TFileIter.h:246
 TFileIter.h:247