#include "TLeafC.h"
#include "TBranch.h"
#include "TBasket.h"
#include "TClonesArray.h"
#include "Riostream.h"
#include <string>
ClassImp(TLeafC)
TLeafC::TLeafC(): TLeaf()
{
fLenType = 1;
fMinimum = 0;
fMaximum = 0;
fValue = 0;
fPointer = 0;
}
TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
:TLeaf(parent, name,type)
{
fLenType = 1;
fMinimum = 0;
fMaximum = 0;
fValue = 0;
fPointer = 0;
}
TLeafC::~TLeafC()
{
if (ResetAddress(0,kTRUE)) delete [] fValue;
}
void TLeafC::Export(TClonesArray *list, Int_t n)
{
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)
{
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
{
if (fIsUnsigned) return "UChar_t";
return "Char_t";
}
void TLeafC::Import(TClonesArray *list, Int_t n)
{
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
{
char *value = (char*)GetValuePointer();
printf("%s",value);
}
void TLeafC::ReadBasket(TBuffer &b)
{
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())
||
(readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] )
)
&&
( entryOffset[entry-first] == basket->GetLast() )
)
||
( entryOffset[entry-first] == entryOffset[entry-first+1] )
)
{
fValue[0] = '\0';
return;
}
}
b.ReadFastArrayString(fValue,fLen);
}
void TLeafC::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
{
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(istream &s)
{
string temp;
s >> temp;
if ( TestBit(kNewValue) &&
(temp.size()+1 > ((UInt_t)fNdata))) {
fNdata = temp.size() + 1;
if (TestBit(kIndirectAddress) && fPointer) {
delete [] *fPointer;
*fPointer = new char[fNdata];
} else {
fValue = new char[fNdata];
}
}
strlcpy(fValue,temp.c_str(),fNdata);
}
void TLeafC::SetAddress(void *add)
{
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;
}
}