62 TMacro::TMacro(): TNamed(), fLines(0)
73 TMacro::TMacro(
const char *name,
const char *title): TNamed(name,title)
77 Int_t nch = strlen(name);
78 char *s =
new char[nch+1];
79 strlcpy(s,name,nch+1);
80 char *slash = (
char*)strrchr(s,
'/');
81 if (!slash) slash = s;
83 char *dot = (
char*)strchr(slash,
'.');
87 if (fTitle.Length() == 0) fTitle = name;
96 TMacro::TMacro(
const TMacro ¯o): TNamed(macro)
99 TIter next(macro.GetListOfLines());
101 while ((obj = (TObjString*) next())) {
102 fLines->Add(
new TObjString(obj->GetName()));
104 fParams = macro.fParams;
112 if (fLines) fLines->Delete();
119 TMacro& TMacro::operator=(
const TMacro ¯o)
122 TNamed::operator=(macro);
123 if (fLines) fLines->Delete();
125 fLines =
new TList();
126 TIter next(macro.GetListOfLines());
128 while ((obj = (TObjString*) next())) {
129 fLines->Add(
new TObjString(obj->GetName()));
131 fParams = macro.fParams;
139 TObjString *TMacro::AddLine(
const char *text)
141 if (!fLines) fLines =
new TList();
142 TObjString *obj =
new TObjString(text);
172 void TMacro::Browse(TBrowser * )
174 TString opt = gEnv->GetValue(
"TMacro.Browse",
"");
179 if (opt ==
"Print") {
183 if (opt.Contains(
".C")) {
184 const char *cmd = Form(
".x %s((TMacro*)0x%lx)",opt.Data(),(ULong_t)
this);
185 gROOT->ProcessLine(cmd);
194 TMD5 *TMacro::Checksum()
196 if (!fLines || fLines->GetSize() <= 0)
199 TMD5 *md5 =
new TMD5;
202 const Int_t bufSize = 8192;
203 UChar_t buf[bufSize];
205 Long64_t left = bufSize;
209 while ((l = (TObjString *) nxl())) {
210 TString line = l->GetString();
212 Int_t len = line.Length();
213 char *p = (
char *) line.Data();
215 strlcpy((
char *)&buf[pos], p, len+1);
218 }
else if (left == len) {
219 strlcpy((
char *)&buf[pos], p, len+1);
220 md5->Update(buf, bufSize);
224 strlcpy((
char *)&buf[pos], p, left+1);
225 md5->Update(buf, bufSize);
230 strlcpy((
char *)&buf[pos], p, len+1);
235 md5->Update(buf, pos);
247 Bool_t TMacro::Load()
const
249 std::stringstream ss;
253 while ((obj = (TObjString*) next())) {
254 ss << obj->GetName() << std::endl;
256 return gInterpreter->LoadText(ss.str().c_str());
266 Long_t TMacro::Exec(
const char *params, Int_t* error)
268 if ( !gROOT->GetGlobalFunction(GetName(), 0, kTRUE) ) {
270 if (error) *error = 1;
278 if ( gROOT->GetGlobalFunction(GetName(), 0, kTRUE) ) {
279 gROOT->SetExecutingMacro(kTRUE);
280 TString exec = GetName();
282 if (p ==
"") p = fParams;
284 exec +=
"(" + p +
")";
287 Long_t ret = gROOT->ProcessLine(exec, error);
289 gROOT->SetExecutingMacro(kFALSE);
293 Error(
"Exec",
"Macro does not contains function named %s.",GetName());
294 if (error) *error = 1;
301 TObjString *TMacro::GetLineWith(
const char *text)
const
303 if (!fLines)
return 0;
306 while ((obj = (TObjString*) next())) {
307 if (strstr(obj->GetName(),text))
return obj;
315 void TMacro::Paint(Option_t *option)
323 void TMacro::Print(Option_t * )
const
328 while ((obj = (TObjString*) next())) {
329 printf(
"%s\n",obj->GetName());
336 Int_t TMacro::ReadFile(
const char *filename)
338 if (!fLines) fLines =
new TList();
342 Error(
"ReadFile",
"Cannot open file: %s",filename);
345 char *line =
new char[10000];
348 in.getline(line,10000);
349 if (!in.good())
break;
351 fLines->Add(
new TObjString(line));
361 void TMacro::SaveSource(
const char *filename)
364 out.open(filename, std::ios::out);
366 Printf(
"SaveSource cannot open file: %s",filename);
369 if (!fLines) {out.close();
return;}
372 while ((obj = (TObjString*) next())) {
373 out<<obj->GetName()<<std::endl;
381 void TMacro::SaveSource(FILE *fp)
383 if (!fLines) {fclose(fp);
return;}
386 while ((obj = (TObjString*) next())) {
387 fprintf(fp,
"%s\n", obj->GetName());
395 void TMacro::SavePrimitive(std::ostream &out, Option_t *option )
399 if (gROOT->ClassSaved(TMacro::Class())) {
402 out<<
" "<<ClassName()<<
" *";
404 out<<
"macro = new "<<ClassName()<<
"("<<quote<<GetName()<<quote<<
","<<quote<<GetTitle()<<quote<<
");"<<std::endl;
408 while ((obj = (TObjString*) next())) {
409 TString s = obj->GetName();
410 s.ReplaceAll(
"\"",
"\\\"");
411 out<<
" macro->AddLine("<<quote<<s.Data()<<quote<<
");"<<std::endl;
413 out<<
" macro->Draw("<<quote<<option<<quote<<
");"<<std::endl;
419 void TMacro::SetParams(
const char *params)
421 if (params) fParams = params;