// $Id$
// Author: Sergey Linev   22/12/2013

#ifndef ROOT_TRootSniffer
#define ROOT_TRootSniffer

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

#ifndef ROOT_TList
#include "TList.h"
#endif

enum {
   mask_Scan        = 0x0001,  ///< normal scan of hierarchy
   mask_Expand      = 0x0002,  ///< expand of specified item - allowed to scan object members
   mask_Search      = 0x0004,  ///< search for specified item (only objects and collections)
   mask_CheckChld   = 0x0008,  ///< check if there childs, very similar to search
   mask_Actions     = 0x000F,  ///< mask for actions, only actions copied to child rec
   mask_ExtraFolder = 0x0010   ///< bit marks folder where all childs can be expanded
};

class TMemFile;
class TBufferFile;
class TDataMember;
class TRootSnifferStore;

class TRootSnifferScanRec {
public:

   TRootSnifferScanRec *parent; //! pointer on parent record
   UInt_t mask;                 //! defines operation kind
   const char *searchpath;      //! current path searched
   Int_t lvl;                   //! current level of hierarchy
   TList fItemsNames;           //! list of created items names, need to avoid duplication

   TRootSnifferStore *store;  //! object to store results
   Bool_t has_more;           //! indicates that potentially there are more items can be found
   TString started_node;      //! name of node stared
   Int_t num_fields;          //! number of fields
   Int_t num_childs;          //! number of childs

   TRootSnifferScanRec();
   virtual ~TRootSnifferScanRec();

   void CloseNode();

   /** return true when fields could be set to the hierarchy item */
   Bool_t CanSetFields()
   {
      return (mask & mask_Scan) && (store != 0);
   }

   /** Starts new node, must be closed at the end */
   void CreateNode(const char *_node_name);

   void BeforeNextChild();

   /** Set item field only when creating is specified */
   void SetField(const char *name, const char *value, Bool_t with_quotes = kTRUE);

   /** Mark item with ROOT class and correspondent streamer info */
   void SetRootClass(TClass *cl);

   /** Returns true when item can be expanded */
   Bool_t CanExpandItem();

   /** Checks if result will be accepted. Used to verify if sniffer should read object from the file */
   Bool_t IsReadyForResult() const;

   /** Set result pointer and return true if result is found */
   Bool_t SetResult(void *obj, TClass *cl, TDataMember *member = 0,  Int_t chlds = -1);

   /** Returns depth of hierarchy */
   Int_t Depth() const;

   /** Returns level till extra folder, marked as mask_ExtraFolder */
   Int_t ExtraFolderLevel();

   /** Method indicates that scanning can be interrupted while result is set */
   Bool_t Done() const;

   /** Construct item name, using object name as basis */
   void MakeItemName(const char *objname, TString& itemname);

   Bool_t GoInside(TRootSnifferScanRec &super, TObject *obj, const char *obj_name = 0);

   ClassDef(TRootSnifferScanRec, 0) // Scan record for objects sniffer
};


class TRootSniffer : public TNamed {

protected:

   TString     fObjectsPath; //! path for registered objects
   TMemFile   *fMemFile;     //! file used to manage streamer infos
   Int_t       fSinfoSize;   //! number of elements in streamer info, used as version
   Bool_t      fReadOnly;    //! indicate if sniffer allowed to change ROOT structures - for instance, read objects from files

   void ScanObjectMemebers(TRootSnifferScanRec &rec, TClass *cl, char *ptr, unsigned long int cloffset);

   void ScanObject(TRootSnifferScanRec &rec, TObject *obj);

   virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject* &obj, TClass* &obj_class);

   virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj);

   void ScanCollection(TRootSnifferScanRec &rec, TCollection *lst,
                       const char *foldername = 0, Bool_t extra = kFALSE, TCollection* keys_lst = 0);

   /* Method is used to scan ROOT objects.
    * Can be reimplemented to extend scanning */
   virtual void ScanRoot(TRootSnifferScanRec &rec);

   void CreateMemFile();

   TString DecodeUrlOptionValue(const char* value, Bool_t remove_quotes = kTRUE);

public:

   TRootSniffer(const char *name, const char *objpath = "online");
   virtual ~TRootSniffer();

   static Bool_t IsDrawableClass(TClass *cl);

   /** When readonly on (default), sniffer is not allowed to change ROOT structures.
    * For instance, it is not allowed to read new objects from files */
   void  SetReadOnly(Bool_t on = kTRUE) { fReadOnly = on; }

   /** Return readonly mode */
   Bool_t IsReadOnly() const { return fReadOnly; }

   Bool_t RegisterObject(const char *subfolder, TObject *obj);

   Bool_t UnregisterObject(TObject *obj);

   /** Method scans normal objects, registered in ROOT */
   void ScanHierarchy(const char *topname, const char *path, TRootSnifferStore *store);

   TObject *FindTObjectInHierarchy(const char *path);

   virtual void *FindInHierarchy(const char *path, TClass **cl = 0, TDataMember **member = 0, Int_t *chld = 0);

   Bool_t CanDrawItem(const char *path);

   Bool_t CanExploreItem(const char *path);

   Bool_t IsStreamerInfoItem(const char *itemname);

   ULong_t GetStreamerInfoHash();

   ULong_t GetItemHash(const char *itemname);

   Bool_t ProduceJson(const char *path, const char *options, TString &res);

   Bool_t ProduceXml(const char *path, const char *options, TString &res);

   Bool_t ProduceBinary(const char *path, const char *options, void *&ptr, Long_t &length);

   Bool_t ProduceImage(Int_t kind, const char *path, const char *options, void *&ptr, Long_t &length);

   Bool_t ProduceExe(const char *path, const char *options, TString &res, Bool_t astxt = kFALSE);

   Bool_t Produce(const char *path, const char *file, const char *options, void *&ptr, Long_t &length);

   ClassDef(TRootSniffer, 0) // Sniffer of ROOT objects
};

#endif
 TRootSniffer.h:1
 TRootSniffer.h:2
 TRootSniffer.h:3
 TRootSniffer.h:4
 TRootSniffer.h:5
 TRootSniffer.h:6
 TRootSniffer.h:7
 TRootSniffer.h:8
 TRootSniffer.h:9
 TRootSniffer.h:10
 TRootSniffer.h:11
 TRootSniffer.h:12
 TRootSniffer.h:13
 TRootSniffer.h:14
 TRootSniffer.h:15
 TRootSniffer.h:16
 TRootSniffer.h:17
 TRootSniffer.h:18
 TRootSniffer.h:19
 TRootSniffer.h:20
 TRootSniffer.h:21
 TRootSniffer.h:22
 TRootSniffer.h:23
 TRootSniffer.h:24
 TRootSniffer.h:25
 TRootSniffer.h:26
 TRootSniffer.h:27
 TRootSniffer.h:28
 TRootSniffer.h:29
 TRootSniffer.h:30
 TRootSniffer.h:31
 TRootSniffer.h:32
 TRootSniffer.h:33
 TRootSniffer.h:34
 TRootSniffer.h:35
 TRootSniffer.h:36
 TRootSniffer.h:37
 TRootSniffer.h:38
 TRootSniffer.h:39
 TRootSniffer.h:40
 TRootSniffer.h:41
 TRootSniffer.h:42
 TRootSniffer.h:43
 TRootSniffer.h:44
 TRootSniffer.h:45
 TRootSniffer.h:46
 TRootSniffer.h:47
 TRootSniffer.h:48
 TRootSniffer.h:49
 TRootSniffer.h:50
 TRootSniffer.h:51
 TRootSniffer.h:52
 TRootSniffer.h:53
 TRootSniffer.h:54
 TRootSniffer.h:55
 TRootSniffer.h:56
 TRootSniffer.h:57
 TRootSniffer.h:58
 TRootSniffer.h:59
 TRootSniffer.h:60
 TRootSniffer.h:61
 TRootSniffer.h:62
 TRootSniffer.h:63
 TRootSniffer.h:64
 TRootSniffer.h:65
 TRootSniffer.h:66
 TRootSniffer.h:67
 TRootSniffer.h:68
 TRootSniffer.h:69
 TRootSniffer.h:70
 TRootSniffer.h:71
 TRootSniffer.h:72
 TRootSniffer.h:73
 TRootSniffer.h:74
 TRootSniffer.h:75
 TRootSniffer.h:76
 TRootSniffer.h:77
 TRootSniffer.h:78
 TRootSniffer.h:79
 TRootSniffer.h:80
 TRootSniffer.h:81
 TRootSniffer.h:82
 TRootSniffer.h:83
 TRootSniffer.h:84
 TRootSniffer.h:85
 TRootSniffer.h:86
 TRootSniffer.h:87
 TRootSniffer.h:88
 TRootSniffer.h:89
 TRootSniffer.h:90
 TRootSniffer.h:91
 TRootSniffer.h:92
 TRootSniffer.h:93
 TRootSniffer.h:94
 TRootSniffer.h:95
 TRootSniffer.h:96
 TRootSniffer.h:97
 TRootSniffer.h:98
 TRootSniffer.h:99
 TRootSniffer.h:100
 TRootSniffer.h:101
 TRootSniffer.h:102
 TRootSniffer.h:103
 TRootSniffer.h:104
 TRootSniffer.h:105
 TRootSniffer.h:106
 TRootSniffer.h:107
 TRootSniffer.h:108
 TRootSniffer.h:109
 TRootSniffer.h:110
 TRootSniffer.h:111
 TRootSniffer.h:112
 TRootSniffer.h:113
 TRootSniffer.h:114
 TRootSniffer.h:115
 TRootSniffer.h:116
 TRootSniffer.h:117
 TRootSniffer.h:118
 TRootSniffer.h:119
 TRootSniffer.h:120
 TRootSniffer.h:121
 TRootSniffer.h:122
 TRootSniffer.h:123
 TRootSniffer.h:124
 TRootSniffer.h:125
 TRootSniffer.h:126
 TRootSniffer.h:127
 TRootSniffer.h:128
 TRootSniffer.h:129
 TRootSniffer.h:130
 TRootSniffer.h:131
 TRootSniffer.h:132
 TRootSniffer.h:133
 TRootSniffer.h:134
 TRootSniffer.h:135
 TRootSniffer.h:136
 TRootSniffer.h:137
 TRootSniffer.h:138
 TRootSniffer.h:139
 TRootSniffer.h:140
 TRootSniffer.h:141
 TRootSniffer.h:142
 TRootSniffer.h:143
 TRootSniffer.h:144
 TRootSniffer.h:145
 TRootSniffer.h:146
 TRootSniffer.h:147
 TRootSniffer.h:148
 TRootSniffer.h:149
 TRootSniffer.h:150
 TRootSniffer.h:151
 TRootSniffer.h:152
 TRootSniffer.h:153
 TRootSniffer.h:154
 TRootSniffer.h:155
 TRootSniffer.h:156
 TRootSniffer.h:157
 TRootSniffer.h:158
 TRootSniffer.h:159
 TRootSniffer.h:160
 TRootSniffer.h:161
 TRootSniffer.h:162
 TRootSniffer.h:163
 TRootSniffer.h:164
 TRootSniffer.h:165
 TRootSniffer.h:166
 TRootSniffer.h:167
 TRootSniffer.h:168
 TRootSniffer.h:169
 TRootSniffer.h:170
 TRootSniffer.h:171