TLeafC.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TLeafC.cxx 36416 2010-10-23 16:23:41Z brun $
00002 // Author: Rene Brun   17/03/97
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, 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 // A TLeaf for a variable length string.                                //
00015 //////////////////////////////////////////////////////////////////////////
00016 
00017 #include "TLeafC.h"
00018 #include "TBranch.h"
00019 #include "TBasket.h"
00020 #include "TClonesArray.h"
00021 #include "Riostream.h"
00022 #include <string>
00023 
00024 ClassImp(TLeafC)
00025 
00026 //______________________________________________________________________________
00027 TLeafC::TLeafC(): TLeaf()
00028 {
00029 //*-*-*-*-*-*Default constructor for LeafC*-*-*-*-*-*-*-*-*-*-*-*-*-*
00030 //*-*        ============================
00031 
00032    fLenType = 1;
00033    fMinimum = 0;
00034    fMaximum = 0;
00035    fValue   = 0;
00036    fPointer = 0;
00037 }
00038 
00039 //______________________________________________________________________________
00040 TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
00041    :TLeaf(parent, name,type)
00042 {
00043 //*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafC*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00044 //*-*                      ==============
00045 //*-*
00046 
00047    fLenType = 1;
00048    fMinimum = 0;
00049    fMaximum = 0;
00050    fValue   = 0;
00051    fPointer = 0;
00052 }
00053 
00054 //______________________________________________________________________________
00055 TLeafC::~TLeafC()
00056 {
00057 //*-*-*-*-*-*Default destructor for a LeafC*-*-*-*-*-*-*-*-*-*-*-*
00058 //*-*        ===============================
00059 
00060    if (ResetAddress(0,kTRUE)) delete [] fValue;
00061 }
00062 
00063 
00064 //______________________________________________________________________________
00065 void TLeafC::Export(TClonesArray *list, Int_t n)
00066 {
00067 //*-*-*-*-*-*Export element from local leaf buffer to ClonesArray*-*-*-*-*
00068 //*-*        ====================================================
00069 
00070    Int_t j = 0;
00071    for (Int_t i=0;i<n;i++) {
00072       memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
00073       j += fLen;
00074    }
00075 }
00076 
00077 
00078 //______________________________________________________________________________
00079 void TLeafC::FillBasket(TBuffer &b)
00080 {
00081 //*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
00082 //*-*                  ==========================================
00083 
00084    if (fPointer) fValue = *fPointer;
00085    Int_t len = strlen(fValue);
00086    if (len >= fMaximum) fMaximum = len+1;
00087    if (len >= fLen)     fLen = len+1;
00088    b.WriteFastArrayString(fValue,len);
00089 }
00090 
00091 //______________________________________________________________________________
00092 const char *TLeafC::GetTypeName() const
00093 {
00094 //*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
00095 //*-*            =========================
00096 
00097    if (fIsUnsigned) return "UChar_t";
00098    return "Char_t";
00099 }
00100 
00101 
00102 //______________________________________________________________________________
00103 void TLeafC::Import(TClonesArray *list, Int_t n)
00104 {
00105 //*-*-*-*-*-*Import element from ClonesArray into local leaf buffer*-*-*-*-*
00106 //*-*        ======================================================
00107 
00108    Int_t j = 0;
00109    for (Int_t i=0;i<n;i++) {
00110       memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
00111       j += fLen;
00112    }
00113 }
00114 
00115 //______________________________________________________________________________
00116 void TLeafC::PrintValue(Int_t) const
00117 {
00118    // Prints leaf value.
00119 
00120    char *value = (char*)GetValuePointer();
00121    printf("%s",value);
00122 }
00123 
00124 //______________________________________________________________________________
00125 void TLeafC::ReadBasket(TBuffer &b)
00126 {
00127    // Read leaf elements from Basket input buffer.
00128 
00129    // Try to deal with the file written during the time where len was not
00130    // written to disk when len was == 0.
00131    Int_t readbasket = GetBranch()->GetReadBasket();
00132    TBasket *basket = GetBranch()->GetBasket(readbasket);
00133    Int_t* entryOffset = basket->GetEntryOffset();
00134    if (entryOffset) {
00135       Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
00136       Long64_t entry = GetBranch()->GetReadEntry();
00137       if ( ( ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
00138                || 
00139                (readbasket <  GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
00140              )
00141              && 
00142              ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
00143            )
00144            || 
00145            ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
00146          ) 
00147       {
00148          // Empty string
00149          fValue[0] = '\0';
00150          return;
00151       }
00152    }
00153    b.ReadFastArrayString(fValue,fLen);
00154 }
00155 
00156 //______________________________________________________________________________
00157 void TLeafC::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
00158 {
00159    // Read leaf elements from Basket input buffer
00160    // and export buffer to TClonesArray objects.
00161 
00162    UChar_t len;
00163    b >> len;
00164    if (len) {
00165       if (len >= fLen) len = fLen-1;
00166       b.ReadFastArray(fValue,len);
00167       fValue[len] = 0;
00168    } else {
00169       fValue[0] = 0;
00170    }
00171 
00172    Int_t j = 0;
00173    for (Int_t i=0;i<n;i++) {
00174       memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
00175       j += fLen;
00176    }
00177 }
00178 
00179 //______________________________________________________________________________
00180 void TLeafC::ReadValue(istream &s)
00181 {
00182    // Read a string from istream s and store it into the branch buffer.
00183 
00184    string temp;
00185    s >> temp;
00186    if ( TestBit(kNewValue) &&
00187         (temp.size()+1 > ((UInt_t)fNdata))) {
00188       // Grow buffer if needed and we created the buffer.
00189       fNdata = temp.size() + 1;
00190       if (TestBit(kIndirectAddress) && fPointer) {
00191          delete [] *fPointer;
00192          *fPointer = new char[fNdata];
00193       } else {
00194          fValue = new char[fNdata];
00195       }
00196    }
00197    strlcpy(fValue,temp.c_str(),fNdata);
00198 }
00199 
00200 //______________________________________________________________________________
00201 void TLeafC::SetAddress(void *add)
00202 {
00203 //*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
00204 //*-*                  ============================
00205 
00206    if (ResetAddress(add)) {
00207       delete [] fValue;
00208    }
00209    if (add) {
00210       if (TestBit(kIndirectAddress)) {
00211          fPointer = (char**)add;
00212          Int_t ncountmax = fLen;
00213          if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
00214          if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
00215              ncountmax > fNdata || *fPointer == 0) {
00216             if (*fPointer) delete [] *fPointer;
00217             if (ncountmax > fNdata) fNdata = ncountmax;
00218             *fPointer = new char[fNdata];
00219          }
00220          fValue = *fPointer;
00221       } else {
00222          fValue = (char*)add;
00223       }
00224    }
00225    else {
00226       fValue = new char[fNdata];
00227       fValue[0] = 0;
00228    }
00229 }

Generated on Tue Jul 5 15:34:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1