Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLeafC.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 17/03/97
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 /** \class TLeafC
13 \ingroup tree
14 
15 A TLeaf for a variable length string.
16 */
17 
18 #include "TLeafC.h"
19 #include "TBranch.h"
20 #include "TBasket.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 #include <string>
24 
25 ClassImp(TLeafC);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Default constructor for LeafC.
29 
30 TLeafC::TLeafC(): TLeaf()
31 {
32  fLenType = 1;
33  fMinimum = 0;
34  fMaximum = 0;
35  fValue = 0;
36  fPointer = 0;
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create a LeafC.
41 
42 TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
43  :TLeaf(parent, name,type)
44 {
45  fLenType = 1;
46  fMinimum = 0;
47  fMaximum = 0;
48  fValue = 0;
49  fPointer = 0;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Default destructor for a LeafC.
54 
55 TLeafC::~TLeafC()
56 {
57  if (ResetAddress(0,kTRUE)) delete [] fValue;
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Export element from local leaf buffer to ClonesArray.
62 
63 void TLeafC::Export(TClonesArray *list, Int_t n)
64 {
65  Int_t j = 0;
66  for (Int_t i=0;i<n;i++) {
67  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
68  j += fLen;
69  }
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Pack leaf elements in Basket output buffer.
74 
75 void TLeafC::FillBasket(TBuffer &b)
76 {
77  if (fPointer) fValue = *fPointer;
78  Int_t len = strlen(fValue);
79  if (len >= fMaximum) fMaximum = len+1;
80  if (len >= fLen) fLen = len+1;
81  b.WriteFastArrayString(fValue,len);
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Returns name of leaf type.
86 
87 const char *TLeafC::GetTypeName() const
88 {
89  if (fIsUnsigned) return "UChar_t";
90  return "Char_t";
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
95 
96 Bool_t TLeafC::IncludeRange(TLeaf *input)
97 {
98  if (input) {
99  if (input->GetMaximum() > this->GetMaximum())
100  this->SetMaximum( input->GetMaximum() );
101  if (input->GetMinimum() < this->GetMinimum())
102  this->SetMinimum( input->GetMinimum() );
103  return kTRUE;
104  } else {
105  return kFALSE;
106  }
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Import element from ClonesArray into local leaf buffer.
111 
112 void TLeafC::Import(TClonesArray *list, Int_t n)
113 {
114  Int_t j = 0;
115  for (Int_t i=0;i<n;i++) {
116  memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
117  j += fLen;
118  }
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Prints leaf value.
123 
124 void TLeafC::PrintValue(Int_t) const
125 {
126  char *value = (char*)GetValuePointer();
127  printf("%s",value);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Read leaf elements from Basket input buffer.
132 
133 void TLeafC::ReadBasket(TBuffer &b)
134 {
135  // Try to deal with the file written during the time where len was not
136  // written to disk when len was == 0.
137  Int_t readbasket = GetBranch()->GetReadBasket();
138  TBasket *basket = GetBranch()->GetBasket(readbasket);
139  if (!basket) {
140  fValue[0] = '\0';
141  return;
142  }
143  Int_t* entryOffset = basket->GetEntryOffset();
144  if (entryOffset) {
145  Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
146  Long64_t entry = GetBranch()->GetReadEntry();
147  if ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
148  ||
149  (readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
150  )
151  {
152  if ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
153  {
154  // Empty string
155  fValue[0] = '\0';
156  return;
157  }
158  }
159  else if ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
160  {
161  // Empty string
162  fValue[0] = '\0';
163  return;
164  }
165  }
166  b.ReadFastArrayString(fValue,fLen);
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Read leaf elements from Basket input buffer and export buffer to
171 /// TClonesArray objects.
172 
173 void TLeafC::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
174 {
175  UChar_t len;
176  b >> len;
177  if (len) {
178  if (len >= fLen) len = fLen-1;
179  b.ReadFastArray(fValue,len);
180  fValue[len] = 0;
181  } else {
182  fValue[0] = 0;
183  }
184 
185  Int_t j = 0;
186  for (Int_t i=0;i<n;i++) {
187  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
188  j += fLen;
189  }
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Read a string from std::istream s up to delimiter and store it into the branch
194 /// buffer.
195 
196 void TLeafC::ReadValue(std::istream &s, Char_t delim /*= ' '*/)
197 {
198  std::string temp;
199  std::getline(s, temp, delim);
200  if (TestBit(kNewValue) &&
201  (temp.length()+1 > ((UInt_t)fNdata))) {
202  // Grow buffer if needed and we created the buffer.
203  fNdata = ((UInt_t)temp.size()) + 1;
204  if (TestBit(kIndirectAddress) && fPointer) {
205  delete [] *fPointer;
206  *fPointer = new char[fNdata];
207  } else {
208  fValue = new char[fNdata];
209  }
210  }
211  strlcpy(fValue,temp.c_str(),fNdata);
212 }
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 /// Set leaf buffer data address.
216 
217 void TLeafC::SetAddress(void *add)
218 {
219  if (ResetAddress(add)) {
220  delete [] fValue;
221  }
222  if (add) {
223  if (TestBit(kIndirectAddress)) {
224  fPointer = (char**)add;
225  Int_t ncountmax = fLen;
226  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
227  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
228  ncountmax > fNdata || *fPointer == 0) {
229  if (*fPointer) delete [] *fPointer;
230  if (ncountmax > fNdata) fNdata = ncountmax;
231  *fPointer = new char[fNdata];
232  }
233  fValue = *fPointer;
234  } else {
235  fValue = (char*)add;
236  }
237  }
238  else {
239  fValue = new char[fNdata];
240  fValue[0] = 0;
241  }
242 }