Logo ROOT  
Reference Guide
TArchiveFile.cxx
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Fons Rademakers 30/6/04
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /**
13 \file TArchiveFile.cxx
14 \class TArchiveFile
15 \ingroup IO
16 
17 Class describing an archive file containing multiple sub-files, like a ZIP
18 or TAR archive.
19 */
20 
21 #include "TArchiveFile.h"
22 #include "TPluginManager.h"
23 #include "TROOT.h"
24 #include "TObjArray.h"
25 #include "TObjString.h"
26 #include "TError.h"
27 #include "TUrl.h"
28 #include <stdlib.h>
29 
30 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Specify the archive name and member name.
35 ///
36 /// \param[in] archive Name of the archive file
37 /// \param[in] member Name of the ROOT file or integer number
38 /// \param[in] file Address of the TFile instance from where the call takes place
39 ///
40 /// The member can be a decimal
41 /// number which allows to access the n-th sub-file. This method is
42 /// normally only called via TFile.
43 
44 TArchiveFile::TArchiveFile(const char *archive, const char *member, TFile *file)
45 {
46  if (!file)
47  Error("TArchiveFile", "must specify a valid TFile");
48 
49  fFile = file;
50  fArchiveName = archive;
51  fMemberName = member;
52  fMemberIndex = -1;
53  if (fMemberName.IsDigit())
54  fMemberIndex = atoi(fMemberName);
55  fMembers = new TObjArray;
56  fMembers->SetOwner();
57  fCurMember = 0;
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Dtor.
62 
64 {
65  delete fMembers;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Return position in archive of current member.
70 
72 {
73  return fCurMember ? fCurMember->GetFilePosition() : 0;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Returns number of members in archive.
78 
80 {
81  return fMembers->GetEntriesFast();
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Explicitely make the specified member the current member.
86 /// Returns -1 in case of error, 0 otherwise.
87 
88 Int_t TArchiveFile::SetMember(const char *member)
89 {
90  fMemberName = member;
91  fMemberIndex = -1;
92 
93  return SetCurrentMember();
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Explicitely make the member with the specified index the current member.
98 /// Returns -1 in case of error, 0 otherwise.
99 
101 {
102  fMemberName = "";
103  fMemberIndex = idx;
104 
105  return SetCurrentMember();
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Return proper archive file handler depending on passed url.
110 ///
111 /// The handler is loaded via the plugin manager and is triggered by
112 /// the extension of the archive file. In case no handler is found 0
113 /// is returned. The file argument is used to access the archive.
114 /// The archive should be specified as url with the member name as the
115 /// anchor, e.g. "root://pcsalo.cern.ch/alice/event_1.zip#tpc.root",
116 /// where tpc.root is the file in the archive to be opened.
117 /// Alternatively the sub-file can be specified via its index number,
118 /// e.g. "root://pcsalo.cern.ch/alice/event_1.zip#3".
119 /// This function is normally only called via TFile::Open().
120 
122 {
123  if (!file) {
124  ::Error("TArchiveFile::Open", "must specify a valid TFile to access %s",
125  url);
126  return 0;
127  }
128 
129  TString archive, member, type;
130 
131  if (!ParseUrl(url, archive, member, type))
132  return 0;
133 
134  TArchiveFile *f = 0;
135  TPluginHandler *h;
136  if ((h = gROOT->GetPluginManager()->FindHandler("TArchiveFile", type))) {
137  if (h->LoadPlugin() == -1)
138  return 0;
139  f = (TArchiveFile*) h->ExecPlugin(3, archive.Data(), member.Data(), file);
140  }
141 
142  return f;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Try to determine if url contains an anchor specifying an archive member.
147 /// Returns kFALSE in case of an error.
148 
149 Bool_t TArchiveFile::ParseUrl(const char *url, TString &archive, TString &member,
150  TString &type)
151 {
152  TUrl u(url, kTRUE);
153 
154  archive = "";
155  member = "";
156  type = "";
157 
158  // get the options and see, if the archive was specified by an option
159  // FIXME: hard coded for "zip" archive format
160  TString urloptions = u.GetOptions();
161  TObjArray *objOptions = urloptions.Tokenize("&");
162  for (Int_t n = 0; n < objOptions->GetEntries(); n++) {
163 
164  TString loption = ((TObjString*)objOptions->At(n))->GetName();
165  TObjArray *objTags = loption.Tokenize("=");
166  if (objTags->GetEntries() == 2) {
167 
168  TString key = ((TObjString*)objTags->At(0))->GetName();
169  TString value = ((TObjString*)objTags->At(1))->GetName();
170 
171  if (!key.CompareTo("zip", TString::kIgnoreCase)) {
172  archive = u.GetFile();
173  member = value;
174  type = "dummy.zip";
175  }
176  }
177  delete objTags;
178  }
179  delete objOptions;
180 
181  if (member != "") {
182  // member set by an option
183  return kTRUE;
184  }
185 
186  if (!strlen(u.GetAnchor())) {
187  archive = u.GetFile();
188  type = archive;
189  return kTRUE;
190  }
191 
192  archive = u.GetFile();
193  member = u.GetAnchor();
194  type = archive;
195 
196  if (archive == "" || member == "") {
197  archive = "";
198  member = "";
199  type = "";
200  return kFALSE;
201  }
202  return kTRUE;
203 }
204 
205 
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Default ctor.
210 
212 {
213  fName = "";
214  fComment = "";
215  fPosition = 0;
216  fFilePosition = 0;
217  fCsize = 0;
218  fDsize = 0;
219  fDirectory = kFALSE;
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Create an archive member file.
224 
226 {
227  fName = name;
228  fComment = "";
229  fPosition = 0;
230  fFilePosition = 0;
231  fCsize = 0;
232  fDsize = 0;
233  fDirectory = kFALSE;
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// Copy ctor.
238 
240  : TObject(member)
241 {
242  fName = member.fName;
243  fComment = member.fComment;
244  fModTime = member.fModTime;
245  fPosition = member.fPosition;
246  fFilePosition = member.fFilePosition;
247  fCsize = member.fCsize;
248  fDsize = member.fDsize;
249  fDirectory = member.fDirectory;
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Assignment operator.
254 
256 {
257  if (this != &rhs) {
258  TObject::operator=(rhs);
259  fName = rhs.fName;
260  fComment = rhs.fComment;
261  fModTime = rhs.fModTime;
262  fPosition = rhs.fPosition;
264  fCsize = rhs.fCsize;
265  fDsize = rhs.fDsize;
266  fDirectory = rhs.fDirectory;
267  }
268  return *this;
269 }
TArchiveFile.h
n
const Int_t n
Definition: legend1.C:16
TArchiveFile
Definition: TArchiveFile.h:24
TArchiveMember::fCsize
Long64_t fCsize
Compressed size.
Definition: TArchiveFile.h:75
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObjArray
Definition: TObjArray.h:37
f
#define f(i)
Definition: RSha256.hxx:122
TArchiveMember::fFilePosition
Long64_t fFilePosition
Byte position in archive where member data starts.
Definition: TArchiveFile.h:74
TArchiveFile::GetNumberOfMembers
Int_t GetNumberOfMembers() const
Returns number of members in archive.
Definition: TArchiveFile.cxx:79
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TObjString.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TString::kIgnoreCase
@ kIgnoreCase
Definition: TString.h:268
TArchiveMember
Definition: TArchiveFile.h:65
TCollection::SetOwner
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Definition: TCollection.cxx:746
TArchiveFile::ParseUrl
static Bool_t ParseUrl(const char *url, TString &archive, TString &member, TString &type)
Try to determine if url contains an anchor specifying an archive member.
Definition: TArchiveFile.cxx:149
TArchiveMember::fDsize
Long64_t fDsize
Decompressed size.
Definition: TArchiveFile.h:76
TObjArray::GetEntries
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
TObjArray::At
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
TString
Definition: TString.h:136
TArchiveFile::SetMember
virtual Int_t SetMember(const char *member)
Explicitely make the specified member the current member.
Definition: TArchiveFile.cxx:88
TArchiveFile::Open
static TArchiveFile * Open(const char *url, TFile *file)
Return proper archive file handler depending on passed url.
Definition: TArchiveFile.cxx:121
TArchiveMember::fDirectory
Bool_t fDirectory
Flag indicating this is a directory.
Definition: TArchiveFile.h:77
TArchiveFile::fFile
TFile * fFile
File stream used to access the archive.
Definition: TArchiveFile.h:34
bool
TROOT.h
TObjString
Definition: TObjString.h:28
TArchiveFile::fCurMember
TArchiveMember * fCurMember
Current archive member.
Definition: TArchiveFile.h:36
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TPluginHandler
Definition: TPluginManager.h:101
TArchiveMember::fComment
TString fComment
Comment field.
Definition: TArchiveFile.h:71
TString::Tokenize
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2197
TArchiveFile::TArchiveFile
TArchiveFile()
Definition: TArchiveFile.h:41
TArchiveMember::GetFilePosition
Long64_t GetFilePosition() const
Definition: TArchiveFile.h:90
TArchiveMember::fPosition
Long64_t fPosition
Byte position in archive.
Definition: TArchiveFile.h:73
h
#define h(i)
Definition: RSha256.hxx:124
TObjArray::GetEntriesFast
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TArchiveMember::TArchiveMember
TArchiveMember()
Default ctor.
Definition: TArchiveFile.cxx:211
TArchiveFile::GetMemberFilePosition
Long64_t GetMemberFilePosition() const
Return position in archive of current member.
Definition: TArchiveFile.cxx:71
TArchiveFile::~TArchiveFile
virtual ~TArchiveFile()
Dtor.
Definition: TArchiveFile.cxx:63
TArchiveMember::fModTime
TDatime fModTime
Modification time.
Definition: TArchiveFile.h:72
TArchiveFile::SetCurrentMember
virtual Int_t SetCurrentMember()=0
TArchiveMember::operator=
TArchiveMember & operator=(const TArchiveMember &rhs)
Assignment operator.
Definition: TArchiveFile.cxx:255
TUrl
Definition: TUrl.h:33
TString::IsDigit
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1763
TFile
Definition: TFile.h:54
TArchiveMember::fName
TString fName
Name of member.
Definition: TArchiveFile.h:70
TUrl::GetFile
const char * GetFile() const
Definition: TUrl.h:69
TString::CompareTo
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:418
TArchiveFile::fArchiveName
TString fArchiveName
Archive file name.
Definition: TArchiveFile.h:31
TObject::operator=
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:268
TPluginManager.h
TObjArray.h
file
Definition: file.py:1
TUrl::GetAnchor
const char * GetAnchor() const
Definition: TUrl.h:70
TObject
Definition: TObject.h:37
TArchiveFile::fMemberName
TString fMemberName
Sub-file name.
Definition: TArchiveFile.h:32
name
char name[80]
Definition: TGX11.cxx:110
TArchiveFile::fMemberIndex
Int_t fMemberIndex
Index of sub-file in archive.
Definition: TArchiveFile.h:33
TArchiveFile::fMembers
TObjArray * fMembers
Members in this archive.
Definition: TArchiveFile.h:35
TUrl::GetOptions
const char * GetOptions() const
Definition: TUrl.h:71
type
int type
Definition: TGX11.cxx:121
TUrl.h
gROOT
#define gROOT
Definition: TROOT.h:406
int
TError.h