TArchiveFile.cxx

Go to the documentation of this file.
00001 // @(#)root/io:$Id: TArchiveFile.cxx 28638 2009-05-15 12:31:05Z rdm $
00002 // Author: Fons Rademakers   30/6/04
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TArchiveFile                                                         //
00015 //                                                                      //
00016 // This is an abstract class that describes an archive file containing  //
00017 // multiple sub-files, like a ZIP or TAR archive.                       //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TArchiveFile.h"
00022 #include "TPluginManager.h"
00023 #include "TROOT.h"
00024 #include "TObjArray.h"
00025 #include "TObjString.h"
00026 #include "TError.h"
00027 #include "TUrl.h"
00028 #include <stdlib.h>
00029 
00030 
00031 ClassImp(TArchiveFile)
00032 
00033 //______________________________________________________________________________
00034 TArchiveFile::TArchiveFile(const char *archive, const char *member, TFile *file)
00035 {
00036    // Specify the archive name and member name. The member can be a decimal
00037    // number which allows to access the n-th sub-file. This method is
00038    // normally only called via TFile.
00039 
00040    if (!file)
00041       Error("TArchiveFile", "must specify a valid TFile");
00042 
00043    fFile        = file;
00044    fArchiveName = archive;
00045    fMemberName  = member;
00046    fMemberIndex = -1;
00047    if (fMemberName.IsDigit())
00048       fMemberIndex = atoi(fMemberName);
00049    fMembers     = new TObjArray;
00050    fMembers->SetOwner();
00051    fCurMember   = 0;
00052 }
00053 
00054 //______________________________________________________________________________
00055 TArchiveFile::~TArchiveFile()
00056 {
00057    // Dtor.
00058 
00059    delete fMembers;
00060 }
00061 
00062 //______________________________________________________________________________
00063 Long64_t TArchiveFile::GetMemberFilePosition() const
00064 {
00065    // Return position in archive of current member.
00066 
00067    return fCurMember ? fCurMember->GetFilePosition() : 0;
00068 }
00069 
00070 //______________________________________________________________________________
00071 Int_t TArchiveFile::GetNumberOfMembers() const
00072 {
00073    // Returns number of members in archive.
00074 
00075    return fMembers->GetEntriesFast();
00076 }
00077 
00078 //______________________________________________________________________________
00079 Int_t TArchiveFile::SetMember(const char *member)
00080 {
00081    // Explicitely make the specified member the current member.
00082    // Returns -1 in case of error, 0 otherwise.
00083 
00084    fMemberName  = member;
00085    fMemberIndex = -1;
00086 
00087    return SetCurrentMember();
00088 }
00089 
00090 //______________________________________________________________________________
00091 Int_t TArchiveFile::SetMember(Int_t idx)
00092 {
00093    // Explicitely make the member with the specified index the current member.
00094    // Returns -1 in case of error, 0 otherwise.
00095 
00096    fMemberName  = "";
00097    fMemberIndex = idx;
00098 
00099    return SetCurrentMember();
00100 }
00101 
00102 //______________________________________________________________________________
00103 TArchiveFile *TArchiveFile::Open(const char *url, TFile *file)
00104 {
00105    // Return proper archive file handler depending on passed url.
00106    // The handler is loaded via the plugin manager and is triggered by
00107    // the extension of the archive file. In case no handler is found 0
00108    // is returned. The file argument is used to access the archive.
00109    // The archive should be specified as url with the member name as the
00110    // anchor, e.g. "root://pcsalo.cern.ch/alice/event_1.zip#tpc.root",
00111    // where tpc.root is the file in the archive to be opened.
00112    // Alternatively the sub-file can be specified via its index number,
00113    // e.g. "root://pcsalo.cern.ch/alice/event_1.zip#3".
00114    // This function is normally only called via TFile::Open().
00115 
00116    if (!file) {
00117       ::Error("TArchiveFile::Open", "must specify a valid TFile to access %s",
00118               url);
00119       return 0;
00120    }
00121 
00122    TString archive, member, type;
00123 
00124    if (!ParseUrl(url, archive, member, type))
00125       return 0;
00126 
00127    TArchiveFile *f = 0;
00128    TPluginHandler *h;
00129    if ((h = gROOT->GetPluginManager()->FindHandler("TArchiveFile", type))) {
00130       if (h->LoadPlugin() == -1)
00131          return 0;
00132       f = (TArchiveFile*) h->ExecPlugin(3, archive.Data(), member.Data(), file);
00133    }
00134 
00135    return f;
00136 }
00137 
00138 //______________________________________________________________________________
00139 Bool_t TArchiveFile::ParseUrl(const char *url, TString &archive, TString &member,
00140                               TString &type)
00141 {
00142    // Try to determine if url contains an anchor specifying an archive member.
00143    // Returns kFALSE in case of an error.
00144 
00145    TUrl u(url, kTRUE);
00146 
00147    archive = "";
00148    member  = "";
00149    type    = "";
00150 
00151    // get the options and see, if the archive was specified by an option
00152    // FIXME: hard coded for "zip" archive format
00153    TString urloptions = u.GetOptions();
00154    TObjArray *objOptions = urloptions.Tokenize("&");
00155    for (Int_t n = 0; n < objOptions->GetEntries(); n++) {
00156 
00157       TString loption = ((TObjString*)objOptions->At(n))->GetName();
00158       TObjArray *objTags = loption.Tokenize("=");
00159       if (objTags->GetEntries() == 2) {
00160 
00161          TString key   = ((TObjString*)objTags->At(0))->GetName();
00162          TString value = ((TObjString*)objTags->At(1))->GetName();
00163 
00164          if (!key.CompareTo("zip", TString::kIgnoreCase)) {
00165             archive = u.GetFile();
00166             member = value;
00167             type = "dummy.zip";
00168          }
00169       }
00170       delete objTags;
00171    }
00172    delete objOptions;
00173 
00174    if (member != "") {
00175       // member set by an option
00176       return kTRUE;
00177    }
00178 
00179    if (!strlen(u.GetAnchor())) {
00180       archive = u.GetFile();
00181       type    = archive;
00182       return kTRUE;
00183    }
00184 
00185    archive = u.GetFile();
00186    member  = u.GetAnchor();
00187    type    = archive;
00188 
00189    if (archive == "" || member == "") {
00190       archive = "";
00191       member  = "";
00192       type    = "";
00193       return kFALSE;
00194    }
00195    return kTRUE;
00196 }
00197 
00198 
00199 ClassImp(TArchiveMember)
00200 
00201 //______________________________________________________________________________
00202 TArchiveMember::TArchiveMember()
00203 {
00204    // Default ctor.
00205 
00206    fName         = "";
00207    fComment      = "";
00208    fPosition     = 0;
00209    fFilePosition = 0;
00210    fCsize        = 0;
00211    fDsize        = 0;
00212    fDirectory    = kFALSE;
00213 }
00214 
00215 //______________________________________________________________________________
00216 TArchiveMember::TArchiveMember(const char *name)
00217 {
00218    // Create an archive member file.
00219 
00220    fName         = name;
00221    fComment      = "";
00222    fPosition     = 0;
00223    fFilePosition = 0;
00224    fCsize        = 0;
00225    fDsize        = 0;
00226    fDirectory    = kFALSE;
00227 }
00228 
00229 //______________________________________________________________________________
00230 TArchiveMember::TArchiveMember(const TArchiveMember &member)
00231    : TObject(member)
00232 {
00233    // Copy ctor.
00234 
00235    fName         = member.fName;
00236    fComment      = member.fComment;
00237    fModTime      = member.fModTime;
00238    fPosition     = member.fPosition;
00239    fFilePosition = member.fFilePosition;
00240    fCsize        = member.fCsize;
00241    fDsize        = member.fDsize;
00242    fDirectory    = member.fDirectory;
00243 }
00244 
00245 //______________________________________________________________________________
00246 TArchiveMember &TArchiveMember::operator=(const TArchiveMember &rhs)
00247 {
00248    // Assignment operator.
00249 
00250    if (this != &rhs) {
00251       TObject::operator=(rhs);
00252       fName         = rhs.fName;
00253       fComment      = rhs.fComment;
00254       fModTime      = rhs.fModTime;
00255       fPosition     = rhs.fPosition;
00256       fFilePosition = rhs.fFilePosition;
00257       fCsize        = rhs.fCsize;
00258       fDsize        = rhs.fDsize;
00259       fDirectory    = rhs.fDirectory;
00260    }
00261    return *this;
00262 }

Generated on Tue Jul 5 14:30:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1