1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
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->GetCategory() == 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()<<">)");
184 bbtkDebugDecTab("Core",8);
188 //==========================================================================
191 //==========================================================================
192 /// UnRegisters a black box descriptor from the package
193 void Package::UnRegisterBlackBox(const std::string& name)
195 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
196 // Looking into the bb map
197 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
198 if (i == mBlackBoxMap.end())
200 bbtkDebugDecTab("Core",8);
201 bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
203 mBlackBoxMap.erase(i);
204 // Is it also in the adaptors map ?
206 AdaptorMapType::iterator j = mAdaptorMap.find(name);
207 if (j != mAdaptorMap.end())
209 mAdaptorMap.erase(j);
212 bbtkDebugDecTab("Core",8);
214 //==========================================================================
216 //==========================================================================
217 /// Changes the name of a black box type
218 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
220 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
221 // Looking into the bb map
222 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
223 if (i == mBlackBoxMap.end())
225 bbtkDebugDecTab("Core",8);
226 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
229 i->second->SetTypeName(newname);
230 mBlackBoxMap[newname] = i->second;
231 mBlackBoxMap.erase(i);
233 bbtkDebugDecTab("Core",8);
235 //==========================================================================
239 //==========================================================================
240 /// Registers an adaptor descriptor in the package
241 bool Package::RegisterAdaptor(BlackBoxDescriptor* d)
243 bbtkDebugMessage("Core",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
245 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
246 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
247 AdaptorKey key(typein,typeout);
249 mAdaptorMap[key] = d;
252 //==========================================================================
256 //==========================================================================
257 /// Displays the list of black boxes of the package
258 void Package::PrintBlackBoxes(bool description, bool adaptors) const
260 unsigned int lmax = 0;
261 std::vector<std::string> names;
262 std::vector<std::string> categs;
263 std::vector<std::string> descrs;
265 BlackBoxMapType::const_iterator i;
266 for (i=mBlackBoxMap.begin();
267 i!=mBlackBoxMap.end();
271 ( i->second->GetCategory() == BlackBoxDescriptor::STANDARD) )
273 std::string name(" ");
274 name += i->second->GetTypeName();
275 names.push_back(name);
278 if ( i->second->GetCategory() == BlackBoxDescriptor::ADAPTOR )
280 categ = std::string("[A]");
282 else if ( i->second->GetCategory() ==
283 BlackBoxDescriptor::DEFAULT_ADAPTOR )
285 categ = std::string("[DA]");
287 categs.push_back(categ);
289 unsigned int l = name.size()+categ.size();
290 if (l>lmax) lmax = l;
296 descr += i->second->GetDescription();
298 descrs.push_back(descr);
304 offs.append(lmax+3,' ');
305 std::vector<std::string>::iterator ni,ci,di;
306 for (ni = names.begin(), ci = categs.begin(), di = descrs.begin();
307 ni != names.end(); ++ni, ++ci, ++di)
310 space.append(lmax - ni->size() - ci->size(),' ');
311 bbtkMessage("Help",1,*ni << space << *ci );
313 unsigned int dmax = 75 - lmax;
314 // while (d.size() > dmax )
317 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
319 bbtkMessage("Help",1,d << std::endl);
320 // d = d.substr(dmax,d.size());
325 //==========================================================================
327 //==========================================================================
328 /// Displays the list of adaptors of the package
329 void Package::PrintAdaptors(bool description) const
331 BlackBoxMapType::const_iterator i;
332 for (i=mBlackBoxMap.begin();
333 i!=mBlackBoxMap.end();
336 if ( i->second->GetCategory() != BlackBoxDescriptor::STANDARD )
338 bbtkMessage("Help",1,
339 " "<<i->second->GetTypeName());
340 if ( i->second->GetCategory() ==
341 BlackBoxDescriptor::DEFAULT_ADAPTOR )
343 bbtkMessage("Help",1,
348 bbtkMessage("Help",1,
349 " : "<<i->second->GetDescription());
352 bbtkMessage("Help",1,std::endl);
356 AdaptorMapType::const_iterator i;
357 for (i=mAdaptorMap.begin();
358 i!=mAdaptorMap.end();
361 bbtkMessage("Help",1,
362 " "<<i->second->GetTypeName());
365 bbtkMessage("Help",1,
366 " : "<<i->second->GetDescription());
369 bbtkMessage("Help",1,std::endl);
373 //==========================================================================
375 //==========================================================================
376 /// Prints help on a black box
377 void Package::HelpBlackBox(const std::string& name, bool full) const
379 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
380 <<name<<"\")"<<bbtkendl);
382 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
383 if (i == mBlackBoxMap.end())
385 bbtkDebugDecTab("Core",8);
386 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
388 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
389 i->second->GetHelp(full);
390 bbtkDebugDecTab("Core",8);
393 //==========================================================================
396 //==========================================================================
397 /// Returns true iff the package contains the box of name boxname
398 bool Package::ContainsBlackBox(const std::string& name) const
400 bbtkDebugMessageInc("Core",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
401 <<name<<"\")"<<bbtkendl);
403 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
404 if (i == mBlackBoxMap.end())
406 bbtkDebugDecTab("Core",8);
409 bbtkDebugDecTab("Core",8);
412 //==========================================================================
416 //==========================================================================
417 void Package::CreateHtmlPage(const std::string& filename,
418 const std::string& caller,
419 const std::string& source,
420 const std::string& custom_header,
421 const std::string& custom_title,
424 bool relative_link ) const
426 bbtkDebugMessageInc("Core",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
427 <<filename<<"\")"<<bbtkendl);
429 //---------------------
432 s.open(filename.c_str());
435 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
438 //----------------------
440 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
442 if (custom_title.length() != 0) title = custom_title;
444 s << "<html lang=\"en\">\n";
446 s << "<title>" << title << "</title>\n";
447 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
448 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
449 s << "<meta name=\"generator\" content=\"\">\n";
450 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
451 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
452 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
453 s << "pre.display { font-family:inherit }\n";
454 s << "pre.format { font-family:inherit }\n";
455 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
456 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
457 s << "pre.smallexample { font-size:smaller }\n";
458 s << "pre.smalllisp { font-size:smaller }\n";
459 s << "span.sc { font-variant:small-caps }\n";
460 s << "span.roman { font-family:serif; font-weight:normal; } \n";
461 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
462 s << "--></style>\n";
464 //----------------------
466 //----------------------
469 s << "<a name=\"Top\"></a>\n";
471 //----------------------
473 if ( custom_header.length() != 0)
475 if ( custom_header != "none" )
478 in.open(custom_header.c_str());
481 bbtkError("Could not open file \""<<custom_header<<"\"");
486 in.getline(buffer,512);
487 std::string line(buffer);
494 s << "<object data=\"" << custom_header
495 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
496 << custom_header <<" could not be embedded.</object>\n";
505 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
506 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
507 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
508 << GetDescription() << "</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 << GetAuthor() << "</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->GetCategory() != 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->GetCategory() == 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 //==========================================================================