1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/02/05 11:07:42 $
7 Version: $Revision: 1.3 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
22 #include "bbtkPackage.h"
23 #include "bbtkMessageManager.h"
24 #include "bbtkConfigurationFile.h"
27 #include "bbtkUtilities.h"
31 //==========================================================================
32 /// Ctor with the name of the package
33 Package::Package(const std::string& name,
34 const std::string& author,
35 const std::string& description,
36 const std::string& version,
37 const std::string& BBTKVersion)
40 mDescription(description),
42 mBBTKVersion(BBTKVersion)
44 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
45 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
46 std::string url = default_doc_dir;
47 if (c != '/' && c !='\\') url = url + "/";
48 url = url + "doc_tmp/" + name + "/index.html";
51 SetDocRelativeURL("Relative url not set");
54 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
55 relurl += "/packages/"+name+"/bbdoc/index.html";
56 std::string url = ConfigurationFile::GetInstance().Get_url()
59 SetDocRelativeURL(relurl);
62 // std::cout << " url=["<<url<<"]"<<std::endl;
63 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
64 bbtkDebugMessage("Core",7,"Package::Package(\""<<name<<"\")"<<bbtkendl);
66 //==========================================================================
70 //==========================================================================
74 bbtkDebugMessageInc("Core",7,"Package::~Package(\""<<mName<<"\")"<<bbtkendl);
75 BlackBoxMapType::const_iterator i;
76 for (i=mBlackBoxMap.begin();
77 i!=mBlackBoxMap.end();
82 // Adaptors are also stored in the black box map : hence already deleted
84 AdaptorMapType::const_iterator j;
85 for (j=mAdaptorMap.begin();
92 bbtkDebugDecTab("Core",7);
94 //==========================================================================
98 //==========================================================================
99 /// Creates an instance of a black box of type <type> with name <name>
100 BlackBox* Package::NewBlackBox(const std::string& type,
101 const std::string& name) const
103 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
105 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
106 if (i == mBlackBoxMap.end())
108 bbtkDebugDecTab("Core",8);
111 BlackBox* bb =i->second->CreateInstance(name);
112 bbtkDebugDecTab("Core",8);
116 //==========================================================================
120 //==========================================================================
121 /// Creates an instance of an adaptor of input type <typein> and
122 /// output type <typeout> with name <name>
123 BlackBox* Package::NewAdaptor(TypeInfo typein,
125 const std::string& name) const
127 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<
129 <<TypeName(typein)<<">,<"
130 <<TypeName(typeout)<<">,\""
131 <<name<<"\")"<<bbtkendl);
133 AdaptorKey key(typein,typeout);
134 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
135 if (i == mAdaptorMap.end())
137 bbtkDebugDecTab("Core",8);
140 BlackBox* bb =i->second->CreateInstance(name);
141 bbtkDebugDecTab("Core",8);
145 //==========================================================================
150 //==========================================================================
151 /// Registers a black box descriptor in the package
152 bool Package::RegisterBlackBox(BlackBoxDescriptor* d)
154 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
156 mBlackBoxMap[d->GetTypeName()] = d;
159 // If it is a default adaptor, also register it in the adaptors map
160 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR)
162 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
163 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
164 AdaptorKey key(typein,typeout);
165 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
166 if (i == mAdaptorMap.end())
168 bbtkDebugMessage("Core",8,"The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
169 mAdaptorMap[key] = d;
171 // If already an adaptor registered : error
174 bbtkError("Package <"<<GetName()<<
175 "> : trying to register black box <"
177 <<"> as default adaptor but there is already a default adaptor registered (<"
178 <<i->second->GetTypeName()<<">)");
182 bbtkDebugDecTab("Core",8);
186 //==========================================================================
189 //==========================================================================
190 /// UnRegisters a black box descriptor from the package
191 void Package::UnRegisterBlackBox(const std::string& name)
193 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
194 // Looking into the bb map
195 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
196 if (i == mBlackBoxMap.end())
198 bbtkDebugDecTab("Core",8);
199 bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
201 mBlackBoxMap.erase(i);
202 // Is it also in the adaptors map ?
204 AdaptorMapType::iterator j = mAdaptorMap.find(name);
205 if (j != mAdaptorMap.end())
207 mAdaptorMap.erase(j);
210 bbtkDebugDecTab("Core",8);
212 //==========================================================================
214 //==========================================================================
215 /// Changes the name of a black box type
216 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
218 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
219 // Looking into the bb map
220 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
221 if (i == mBlackBoxMap.end())
223 bbtkDebugDecTab("Core",8);
224 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
227 i->second->SetTypeName(newname);
228 mBlackBoxMap[newname] = i->second;
229 mBlackBoxMap.erase(i);
231 bbtkDebugDecTab("Core",8);
233 //==========================================================================
237 //==========================================================================
238 /// Registers an adaptor descriptor in the package
239 bool Package::RegisterAdaptor(BlackBoxDescriptor* d)
241 bbtkDebugMessage("Core",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
243 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
244 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
245 AdaptorKey key(typein,typeout);
247 mAdaptorMap[key] = d;
250 //==========================================================================
254 //==========================================================================
255 /// Displays the list of black boxes of the package
256 void Package::PrintBlackBoxes(bool description, bool adaptors) const
258 unsigned int lmax = 0;
259 std::vector<std::string> names;
260 std::vector<std::string> kinds;
261 std::vector<std::string> descrs;
263 BlackBoxMapType::const_iterator i;
264 for (i=mBlackBoxMap.begin();
265 i!=mBlackBoxMap.end();
269 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
271 std::string name(" ");
272 name += i->second->GetTypeName();
273 names.push_back(name);
276 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
278 kind = std::string("[A]");
280 else if ( i->second->GetKind() ==
281 BlackBoxDescriptor::DEFAULT_ADAPTOR )
283 kind = std::string("[DA]");
285 kinds.push_back(kind);
287 unsigned int l = name.size()+kind.size();
288 if (l>lmax) lmax = l;
294 descr += i->second->GetDescription();
296 descrs.push_back(descr);
302 offs.append(lmax+3,' ');
303 std::vector<std::string>::iterator ni,ci,di;
304 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
305 ni != names.end(); ++ni, ++ci, ++di)
308 space.append(lmax - ni->size() - ci->size(),' ');
309 bbtkMessage("Help",1,*ni << space << *ci );
311 unsigned int dmax = 75 - lmax;
312 // while (d.size() > dmax )
315 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
317 bbtkMessage("Help",1,d << std::endl);
318 // d = d.substr(dmax,d.size());
323 //==========================================================================
325 //==========================================================================
326 /// Displays the list of adaptors of the package
327 void Package::PrintAdaptors(bool description) const
329 BlackBoxMapType::const_iterator i;
330 for (i=mBlackBoxMap.begin();
331 i!=mBlackBoxMap.end();
334 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
336 bbtkMessage("Help",1,
337 " "<<i->second->GetTypeName());
338 if ( i->second->GetKind() ==
339 BlackBoxDescriptor::DEFAULT_ADAPTOR )
341 bbtkMessage("Help",1,
346 bbtkMessage("Help",1,
347 " : "<<i->second->GetDescription());
350 bbtkMessage("Help",1,std::endl);
354 AdaptorMapType::const_iterator i;
355 for (i=mAdaptorMap.begin();
356 i!=mAdaptorMap.end();
359 bbtkMessage("Help",1,
360 " "<<i->second->GetTypeName());
363 bbtkMessage("Help",1,
364 " : "<<i->second->GetDescription());
367 bbtkMessage("Help",1,std::endl);
371 //==========================================================================
373 //==========================================================================
374 /// Prints help on a black box
375 void Package::HelpBlackBox(const std::string& name, bool full) const
377 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
378 <<name<<"\")"<<bbtkendl);
380 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
381 if (i == mBlackBoxMap.end())
383 bbtkDebugDecTab("Core",8);
384 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
386 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
387 i->second->GetHelp(full);
388 bbtkDebugDecTab("Core",8);
391 //==========================================================================
394 //==========================================================================
395 /// Returns true iff the package contains the box of name boxname
396 bool Package::ContainsBlackBox(const std::string& name) const
398 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
399 <<name<<"\")"<<bbtkendl);
401 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
402 if (i == mBlackBoxMap.end())
404 bbtkDebugDecTab("Core",8);
407 bbtkDebugDecTab("Core",8);
410 //==========================================================================
414 //==========================================================================
415 void Package::CreateHtmlPage(const std::string& filename,
416 const std::string& caller,
417 const std::string& source,
418 const std::string& custom_header,
419 const std::string& custom_title,
422 bool relative_link ) const
424 bbtkDebugMessageInc("Core",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
425 <<filename<<"\")"<<bbtkendl);
427 //---------------------
430 s.open(filename.c_str());
433 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
436 //----------------------
438 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
440 if (custom_title.length() != 0) title = custom_title;
442 s << "<html lang=\"en\">\n";
444 s << "<title>" << title << "</title>\n";
445 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
446 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
447 s << "<meta name=\"generator\" content=\"\">\n";
448 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
449 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
450 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
451 s << "pre.display { font-family:inherit }\n";
452 s << "pre.format { font-family:inherit }\n";
453 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
454 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
455 s << "pre.smallexample { font-size:smaller }\n";
456 s << "pre.smalllisp { font-size:smaller }\n";
457 s << "span.sc { font-variant:small-caps }\n";
458 s << "span.roman { font-family:serif; font-weight:normal; } \n";
459 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
460 s << "--></style>\n";
462 //----------------------
464 //----------------------
467 s << "<a name=\"Top\"></a>\n";
469 //----------------------
471 if ( custom_header.length() != 0)
473 if ( custom_header != "none" )
476 in.open(custom_header.c_str());
479 bbtkError("Could not open file \""<<custom_header<<"\"");
484 in.getline(buffer,512);
485 std::string line(buffer);
492 s << "<object data=\"" << custom_header
493 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
494 << custom_header <<" could not be embedded.</object>\n";
503 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
504 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
505 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
506 << GetDescription() << "</TD></TR>\n";
507 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
508 << GetAuthor() << "</TD></TR>\n";
509 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
510 << GetKeyword() << "</TD></TR>\n";
511 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
512 << GetVersion() << "</TD></TR>\n";
513 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
514 << GetBBTKVersion() << "</TD></TR>\n";
518 //-------------------
521 // s << "<div class=\"contents\">\n";
522 s << "<p><b> Black Boxes : </b>\n";
525 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
527 BlackBoxMapType::const_iterator i;
528 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
530 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
533 std::string name = i->second->GetTypeName();
534 Utilities::html_format(name);
535 std::string descr = i->second->GetDescription();
538 s << "<TD style='vertical-align: top;'>";
539 s << "<li><a name=\"toc_"<<name
540 <<"\" href=\"#"<<name<<"\">"
543 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
552 //-------------------
554 if (mAdaptorMap.size()>0)
556 // s << "<div class=\"contents\">\n";
557 s << "<p><b> Adaptors : </b>\n";
560 // BlackBoxMapType::const_iterator i;
561 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
562 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
564 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
567 std::string name = i->second->GetTypeName();
568 Utilities::html_format(name);
569 std::string descr = i->second->GetDescription();
572 s << "<TD style='vertical-align: top;'>";
573 s << "<li><a name=\"toc_"<<name
574 <<"\" href=\"#"<<name<<"\">"
577 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
587 // s << "<div class=\"node\">\n";
590 // s << "<a name=\"Top\"></a>\n";
591 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
592 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
593 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
597 //----------------------
600 //-------------------
601 // Computes output directory from filename to pass it to
602 // BlackBoxDescriptor::InsertHtmlHelp
604 std::string::size_type slash_position =
605 filename.find_last_of(ConfigurationFile::GetInstance().Get_file_separator ());
606 if (slash_position != std::string::npos) {
607 if (slash_position == 0)
609 dir = filename.substr(0,slash_position);
612 for (i=mBlackBoxMap.begin();
613 i!=mBlackBoxMap.end();
616 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
619 //----------------------
624 ptm = gmtime ( &rawtime );
627 s << "Automatically generated by <b>"<<caller<<"</b> from <b>"
629 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
630 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
631 s << "</body></html>\n";
633 //----------------------
636 bbtkDebugDecTab("Core",9);
638 //==========================================================================