Logo ROOT   6.18/05
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
17Class describing an archive file containing multiple sub-files, like a ZIP
18or 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
44TArchiveFile::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())
55 fMembers = new TObjArray;
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{
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
88Int_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;
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
149Bool_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;
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;
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;
247 fCsize = member.fCsize;
248 fDsize = member.fDsize;
249 fDirectory = member.fDirectory;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Assignment operator.
254
256{
257 if (this != &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;
267 }
268 return *this;
269}
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
long long Long64_t
Definition: RtypesCore.h:69
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
#define gROOT
Definition: TROOT.h:414
Class describing an archive file containing multiple sub-files, like a ZIP or TAR archive.
Definition: TArchiveFile.h:24
virtual ~TArchiveFile()
Dtor.
virtual Int_t SetCurrentMember()=0
TArchiveMember * fCurMember
Current archive member.
Definition: TArchiveFile.h:36
TString fMemberName
Sub-file name.
Definition: TArchiveFile.h:32
static TArchiveFile * Open(const char *url, TFile *file)
Return proper archive file handler depending on passed url.
TString fArchiveName
Archive file name.
Definition: TArchiveFile.h:31
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.
Int_t fMemberIndex
Index of sub-file in archive.
Definition: TArchiveFile.h:33
Long64_t GetMemberFilePosition() const
Return position in archive of current member.
virtual Int_t SetMember(const char *member)
Explicitely make the specified member the current member.
Int_t GetNumberOfMembers() const
Returns number of members in archive.
TObjArray * fMembers
Members in this archive.
Definition: TArchiveFile.h:35
TFile * fFile
File stream used to access the archive.
Definition: TArchiveFile.h:34
TString fComment
Comment field.
Definition: TArchiveFile.h:71
TDatime fModTime
Modification time.
Definition: TArchiveFile.h:72
Long64_t fCsize
Compressed size.
Definition: TArchiveFile.h:75
Long64_t fPosition
Byte position in archive.
Definition: TArchiveFile.h:73
Long64_t fFilePosition
Byte position in archive where member data starts.
Definition: TArchiveFile.h:74
TArchiveMember & operator=(const TArchiveMember &rhs)
Assignment operator.
TString fName
Name of member.
Definition: TArchiveFile.h:70
TArchiveMember()
Default ctor.
Bool_t fDirectory
Flag indicating this is a directory.
Definition: TArchiveFile.h:77
Long64_t fDsize
Decompressed size.
Definition: TArchiveFile.h:76
Long64_t GetFilePosition() const
Definition: TArchiveFile.h:90
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
An array of TObjects.
Definition: TObjArray.h:37
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
Collectable string class.
Definition: TObjString.h:28
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:268
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
Basic string class.
Definition: TString.h:131
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:418
const char * Data() const
Definition: TString.h:364
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1763
@ kIgnoreCase
Definition: TString.h:263
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2197
This class represents a WWW compatible URL.
Definition: TUrl.h:35
const char * GetAnchor() const
Definition: TUrl.h:73
const char * GetFile() const
Definition: TUrl.h:72
const char * GetOptions() const
Definition: TUrl.h:74
const Int_t n
Definition: legend1.C:16
Definition: file.py:1