Logo ROOT   6.12/07
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 }
Class describing an archive file containing multiple sub-files, like a ZIP or TAR archive...
Definition: TArchiveFile.h:24
Int_t fMemberIndex
Index of sub-file in archive.
Definition: TArchiveFile.h:33
An array of TObjects.
Definition: TObjArray.h:37
virtual ~TArchiveFile()
Dtor.
TObjArray * fMembers
Members in this archive.
Definition: TArchiveFile.h:35
long long Long64_t
Definition: RtypesCore.h:69
Long64_t GetMemberFilePosition() const
Return position in archive of current member.
TString fComment
Comment field.
Definition: TArchiveFile.h:71
Collectable string class.
Definition: TObjString.h:28
virtual Int_t SetMember(const char *member)
Explicitely make the specified member the current member.
This class represents a WWW compatible URL.
Definition: TUrl.h:35
TFile * fFile
File stream used to access the archive.
Definition: TArchiveFile.h:34
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
static TArchiveFile * Open(const char *url, TFile *file)
Return proper archive file handler depending on passed url.
#define gROOT
Definition: TROOT.h:402
Long64_t fDsize
Decompressed size.
Definition: TArchiveFile.h:76
Int_t LoadPlugin()
Load the plugin library for this handler.
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TString fMemberName
Sub-file name.
Definition: TArchiveFile.h:32
const char * GetOptions() const
Definition: TUrl.h:74
TString fArchiveName
Archive file name.
Definition: TArchiveFile.h:31
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
TArchiveMember * fCurMember
Current archive member.
Definition: TArchiveFile.h:36
TString fComment
Archive comment.
Definition: TZIPFile.h:26
const char * GetFile() const
Definition: TUrl.h:72
TString fName
Name of member.
Definition: TArchiveFile.h:70
const char * GetAnchor() const
Definition: TUrl.h:73
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:271
Long64_t GetFilePosition() const
Definition: TArchiveFile.h:90
Long64_t fPosition
Byte position in archive.
Definition: TArchiveFile.h:73
Long_t ExecPlugin(int nargs, const T &... params)
Int_t GetNumberOfMembers() const
Returns number of members in archive.
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
TDatime fModTime
Modification time.
Definition: TArchiveFile.h:72
const Bool_t kFALSE
Definition: RtypesCore.h:88
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2251
#define ClassImp(name)
Definition: Rtypes.h:359
Bool_t fDirectory
Flag indicating this is a directory.
Definition: TArchiveFile.h:77
int type
Definition: TGX11.cxx:120
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.
TArchiveMember & operator=(const TArchiveMember &rhs)
Assignment operator.
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:396
virtual Int_t SetCurrentMember()=0
Mother of all ROOT objects.
Definition: TObject.h:37
Definition: file.py:1
Long64_t fCsize
Compressed size.
Definition: TArchiveFile.h:75
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1817
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
Long64_t fFilePosition
Byte position in archive where member data starts.
Definition: TArchiveFile.h:74
TArchiveMember()
Default ctor.
const Bool_t kTRUE
Definition: RtypesCore.h:87
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
const char * Data() const
Definition: TString.h:345