Logo ROOT   6.07/09
Reference Guide
importCode.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -nodraw
4 /// Example of script showing how to create a ROOT file with subdirectories.
5 /// The script scans a given directory tree and recreates the same structure in the ROOT file.
6 /// All source files of type .h,cxx,c,dat,py are imported as TMacro objects.
7 /// See also the other tutorial readCode.C
8 /// \macro_code
9 ///
10 /// \author Rene Brun
11 
12 #include "TFile.h"
13 #include "TSystem.h"
14 #include "TMacro.h"
15 
16 void importdir(const char *dirname) {
17  char *slash = (char*)strrchr(dirname,'/');
18  char *locdir;
19  if (slash) locdir = slash+1;
20  else locdir = (char*)dirname;
21  printf("processing dir %s\n",dirname);
22  TDirectory *savdir = gDirectory;
23  TDirectory *adir = savdir->mkdir(locdir);
24  adir->cd();
25  void *dirp = gSystem->OpenDirectory(dirname);
26  if (!dirp) return;
27  char *direntry;
28  Long_t id, size,flags,modtime;
29  //loop on all entries of this directory
30  while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
31  TString afile = Form("%s/%s",dirname,direntry);
32  gSystem->GetPathInfo(afile,&id,&size,&flags,&modtime);
33  if (direntry[0] == '.') continue; //forget the "." and ".." special cases
34  if (!strcmp(direntry,"CVS")) continue; //forget some special directories
35  if (!strcmp(direntry,"htmldoc")) continue;
36  if (strstr(dirname,"root/include")) continue;
37  if (strstr(direntry,"G__")) continue;
38  if (strstr(direntry,".c") ||
39  strstr(direntry,".h") ||
40  strstr(direntry,".dat") ||
41  strstr(direntry,".py") ||
42  strstr(direntry,".C")) {
43  TMacro *m = new TMacro(afile);
44  m->Write(direntry);
45  delete m;
46  } else {
47  if (flags != 3) continue; //must be a directory
48  //we have found a valid sub-directory. Process it
49  importdir(afile);
50  }
51  }
52  gSystem->FreeDirectory(dirp);
53  savdir->cd();
54 }
55 void importCode() {
56  TFile *f = new TFile("code.root","recreate");
57  TString dir = gROOT->GetTutorialsDir();
58  importdir(dir); //change the directory as you like
59  delete f;
60 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:830
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1363
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
#define gROOT
Definition: TROOT.h:364
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:33
Basic string class.
Definition: TString.h:137
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory and return a pointer to the created directory.
Definition: TDirectory.cxx:957
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:848
XFontStruct * id
Definition: TGX11.cxx:108
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
TMarker * m
Definition: textangle.C:8
char * Form(const char *fmt,...)
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:840
long Long_t
Definition: RtypesCore.h:50
double f(double x)
Describe directory structure in memory.
Definition: TDirectory.h:44
TCanvas * slash()
Definition: slash.C:1
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:435
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:831
#define gDirectory
Definition: TDirectory.h:221