ROOT logo
// @(#)root/tree:$Id: TLeafC.cxx 25939 2008-10-23 21:18:11Z pcanal $
// Author: Rene Brun   17/03/97

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// A TLeaf for a variable length string.                                //
//////////////////////////////////////////////////////////////////////////

#include "TLeafC.h"
#include "TBranch.h"
#include "TBasket.h"
#include "TClonesArray.h"
#include "Riostream.h"
#include <string>

ClassImp(TLeafC)

//______________________________________________________________________________
TLeafC::TLeafC(): TLeaf()
{
//*-*-*-*-*-*Default constructor for LeafC*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ============================

   fValue = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
   :TLeaf(parent, name,type)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafC*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ==============
//*-*

   fLenType = 1;
   fMinimum = 0;
   fMaximum = 0;
   fValue   = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafC::~TLeafC()
{
//*-*-*-*-*-*Default destructor for a LeafC*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ===============================

   if (ResetAddress(0,kTRUE)) delete [] fValue;
}


//______________________________________________________________________________
void TLeafC::Export(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Export element from local leaf buffer to ClonesArray*-*-*-*-*
//*-*        ====================================================

   Int_t j = 0;
   for (Int_t i=0;i<n;i++) {
      memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
      j += fLen;
   }
}


//______________________________________________________________________________
void TLeafC::FillBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
//*-*                  ==========================================

   if (fPointer) fValue = *fPointer;
   Int_t len = strlen(fValue);
   if (len >= fMaximum) fMaximum = len+1;
   if (len >= fLen)     fLen = len+1;
   b.WriteFastArrayString(fValue,len);
}

//______________________________________________________________________________
const char *TLeafC::GetTypeName() const
{
//*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
//*-*            =========================

   if (fIsUnsigned) return "UChar_t";
   return "Char_t";
}


//______________________________________________________________________________
void TLeafC::Import(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Import element from ClonesArray into local leaf buffer*-*-*-*-*
//*-*        ======================================================

   Int_t j = 0;
   for (Int_t i=0;i<n;i++) {
      memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
      j += fLen;
   }
}

//______________________________________________________________________________
void TLeafC::PrintValue(Int_t) const
{
   // Prints leaf value.

   char *value = (char*)GetValuePointer();
   printf("%s",value);
}

//______________________________________________________________________________
void TLeafC::ReadBasket(TBuffer &b)
{
   // Read leaf elements from Basket input buffer.

   // Try to deal with the file written during the time where len was not
   // written to disk when len was == 0.
   Int_t readbasket = GetBranch()->GetReadBasket();
   TBasket *basket = GetBranch()->GetBasket(readbasket);
   Int_t* entryOffset = basket->GetEntryOffset();
   if (entryOffset) {
      Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
      Long64_t entry = GetBranch()->GetReadEntry();
      if ( ( ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1)==GetBranch()->GetEntries())
              || (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1])
             && ( entryOffset[entry-first] == basket->GetLast() ) )
           || ( entryOffset[entry-first] == entryOffset[entry-first+1] ) ) 
      {
         // Empty string
         fValue[0] = '\0';
         return;
      }
   }
   b.ReadFastArrayString(fValue,fLen);
}

//______________________________________________________________________________
void TLeafC::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
{
   // Read leaf elements from Basket input buffer
   // and export buffer to TClonesArray objects.

   UChar_t len;
   b >> len;
   if (len) {
      if (len >= fLen) len = fLen-1;
      b.ReadFastArray(fValue,len);
      fValue[len] = 0;
   } else {
      fValue[0] = 0;
   }

   Int_t j = 0;
   for (Int_t i=0;i<n;i++) {
      memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
      j += fLen;
   }
}

//______________________________________________________________________________
void TLeafC::ReadValue(ifstream &s)
{
   // Read a string from ifstream s and store it into the branch buffer.

   string temp;
   s >> temp;
   if ( TestBit(kNewValue) &&
        (temp.size()+1 > ((UInt_t)fNdata))) {
      // Grow buffer if needed and we created the buffer.
      fNdata = temp.size() + 1;
      if (TestBit(kIndirectAddress) && fPointer) {
         delete [] *fPointer;
         *fPointer = new char[fNdata];
      } else {
         fValue = new char[fNdata];
      }
   }
   strcpy(fValue,temp.c_str());
}

//______________________________________________________________________________
void TLeafC::SetAddress(void *add)
{
//*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
//*-*                  ============================

   if (ResetAddress(add)) {
      delete [] fValue;
   }
   if (add) {
      if (TestBit(kIndirectAddress)) {
         fPointer = (char**)add;
         Int_t ncountmax = fLen;
         if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
         if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
             ncountmax > fNdata || *fPointer == 0) {
            if (*fPointer) delete [] *fPointer;
            if (ncountmax > fNdata) fNdata = ncountmax;
            *fPointer = new char[fNdata];
         }
         fValue = *fPointer;
      } else {
         fValue = (char*)add;
      }
   }
   else {
      fValue = new char[fNdata];
      fValue[0] = 0;
   }
}
 TLeafC.cxx:1
 TLeafC.cxx:2
 TLeafC.cxx:3
 TLeafC.cxx:4
 TLeafC.cxx:5
 TLeafC.cxx:6
 TLeafC.cxx:7
 TLeafC.cxx:8
 TLeafC.cxx:9
 TLeafC.cxx:10
 TLeafC.cxx:11
 TLeafC.cxx:12
 TLeafC.cxx:13
 TLeafC.cxx:14
 TLeafC.cxx:15
 TLeafC.cxx:16
 TLeafC.cxx:17
 TLeafC.cxx:18
 TLeafC.cxx:19
 TLeafC.cxx:20
 TLeafC.cxx:21
 TLeafC.cxx:22
 TLeafC.cxx:23
 TLeafC.cxx:24
 TLeafC.cxx:25
 TLeafC.cxx:26
 TLeafC.cxx:27
 TLeafC.cxx:28
 TLeafC.cxx:29
 TLeafC.cxx:30
 TLeafC.cxx:31
 TLeafC.cxx:32
 TLeafC.cxx:33
 TLeafC.cxx:34
 TLeafC.cxx:35
 TLeafC.cxx:36
 TLeafC.cxx:37
 TLeafC.cxx:38
 TLeafC.cxx:39
 TLeafC.cxx:40
 TLeafC.cxx:41
 TLeafC.cxx:42
 TLeafC.cxx:43
 TLeafC.cxx:44
 TLeafC.cxx:45
 TLeafC.cxx:46
 TLeafC.cxx:47
 TLeafC.cxx:48
 TLeafC.cxx:49
 TLeafC.cxx:50
 TLeafC.cxx:51
 TLeafC.cxx:52
 TLeafC.cxx:53
 TLeafC.cxx:54
 TLeafC.cxx:55
 TLeafC.cxx:56
 TLeafC.cxx:57
 TLeafC.cxx:58
 TLeafC.cxx:59
 TLeafC.cxx:60
 TLeafC.cxx:61
 TLeafC.cxx:62
 TLeafC.cxx:63
 TLeafC.cxx:64
 TLeafC.cxx:65
 TLeafC.cxx:66
 TLeafC.cxx:67
 TLeafC.cxx:68
 TLeafC.cxx:69
 TLeafC.cxx:70
 TLeafC.cxx:71
 TLeafC.cxx:72
 TLeafC.cxx:73
 TLeafC.cxx:74
 TLeafC.cxx:75
 TLeafC.cxx:76
 TLeafC.cxx:77
 TLeafC.cxx:78
 TLeafC.cxx:79
 TLeafC.cxx:80
 TLeafC.cxx:81
 TLeafC.cxx:82
 TLeafC.cxx:83
 TLeafC.cxx:84
 TLeafC.cxx:85
 TLeafC.cxx:86
 TLeafC.cxx:87
 TLeafC.cxx:88
 TLeafC.cxx:89
 TLeafC.cxx:90
 TLeafC.cxx:91
 TLeafC.cxx:92
 TLeafC.cxx:93
 TLeafC.cxx:94
 TLeafC.cxx:95
 TLeafC.cxx:96
 TLeafC.cxx:97
 TLeafC.cxx:98
 TLeafC.cxx:99
 TLeafC.cxx:100
 TLeafC.cxx:101
 TLeafC.cxx:102
 TLeafC.cxx:103
 TLeafC.cxx:104
 TLeafC.cxx:105
 TLeafC.cxx:106
 TLeafC.cxx:107
 TLeafC.cxx:108
 TLeafC.cxx:109
 TLeafC.cxx:110
 TLeafC.cxx:111
 TLeafC.cxx:112
 TLeafC.cxx:113
 TLeafC.cxx:114
 TLeafC.cxx:115
 TLeafC.cxx:116
 TLeafC.cxx:117
 TLeafC.cxx:118
 TLeafC.cxx:119
 TLeafC.cxx:120
 TLeafC.cxx:121
 TLeafC.cxx:122
 TLeafC.cxx:123
 TLeafC.cxx:124
 TLeafC.cxx:125
 TLeafC.cxx:126
 TLeafC.cxx:127
 TLeafC.cxx:128
 TLeafC.cxx:129
 TLeafC.cxx:130
 TLeafC.cxx:131
 TLeafC.cxx:132
 TLeafC.cxx:133
 TLeafC.cxx:134
 TLeafC.cxx:135
 TLeafC.cxx:136
 TLeafC.cxx:137
 TLeafC.cxx:138
 TLeafC.cxx:139
 TLeafC.cxx:140
 TLeafC.cxx:141
 TLeafC.cxx:142
 TLeafC.cxx:143
 TLeafC.cxx:144
 TLeafC.cxx:145
 TLeafC.cxx:146
 TLeafC.cxx:147
 TLeafC.cxx:148
 TLeafC.cxx:149
 TLeafC.cxx:150
 TLeafC.cxx:151
 TLeafC.cxx:152
 TLeafC.cxx:153
 TLeafC.cxx:154
 TLeafC.cxx:155
 TLeafC.cxx:156
 TLeafC.cxx:157
 TLeafC.cxx:158
 TLeafC.cxx:159
 TLeafC.cxx:160
 TLeafC.cxx:161
 TLeafC.cxx:162
 TLeafC.cxx:163
 TLeafC.cxx:164
 TLeafC.cxx:165
 TLeafC.cxx:166
 TLeafC.cxx:167
 TLeafC.cxx:168
 TLeafC.cxx:169
 TLeafC.cxx:170
 TLeafC.cxx:171
 TLeafC.cxx:172
 TLeafC.cxx:173
 TLeafC.cxx:174
 TLeafC.cxx:175
 TLeafC.cxx:176
 TLeafC.cxx:177
 TLeafC.cxx:178
 TLeafC.cxx:179
 TLeafC.cxx:180
 TLeafC.cxx:181
 TLeafC.cxx:182
 TLeafC.cxx:183
 TLeafC.cxx:184
 TLeafC.cxx:185
 TLeafC.cxx:186
 TLeafC.cxx:187
 TLeafC.cxx:188
 TLeafC.cxx:189
 TLeafC.cxx:190
 TLeafC.cxx:191
 TLeafC.cxx:192
 TLeafC.cxx:193
 TLeafC.cxx:194
 TLeafC.cxx:195
 TLeafC.cxx:196
 TLeafC.cxx:197
 TLeafC.cxx:198
 TLeafC.cxx:199
 TLeafC.cxx:200
 TLeafC.cxx:201
 TLeafC.cxx:202
 TLeafC.cxx:203
 TLeafC.cxx:204
 TLeafC.cxx:205
 TLeafC.cxx:206
 TLeafC.cxx:207
 TLeafC.cxx:208
 TLeafC.cxx:209
 TLeafC.cxx:210
 TLeafC.cxx:211
 TLeafC.cxx:212
 TLeafC.cxx:213
 TLeafC.cxx:214
 TLeafC.cxx:215
 TLeafC.cxx:216
 TLeafC.cxx:217
 TLeafC.cxx:218
 TLeafC.cxx:219
 TLeafC.cxx:220