/*========================================================================= Program: bbtk Module: $RCSfile: bbtkPackage.cxx,v $ Language: C++ Date: $Date: 2008/01/22 15:02:00 $ Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ /** *\file *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered. */ #include "bbtkPackage.h" #include "bbtkMessageManager.h" #include "bbtkConfigurationFile.h" #include #include #include "bbtkUtilities.h" namespace bbtk { //========================================================================== /// Ctor with the name of the package Package::Package(const std::string& name, const std::string& author, const std::string& description, const std::string& version, const std::string& BBTKVersion) : mName(name), mAuthor(author), mDescription(description), mVersion(version), mBBTKVersion(BBTKVersion) { std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp(); char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1]; std::string url = default_doc_dir; if (c != '/' && c !='\\') url = url + "/"; url = url + "doc_tmp/" + name + "/index.html"; SetDocURL(url); SetDocRelativeURL("Relative url not set"); /* std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)); relurl += "/packages/"+name+"/bbdoc/index.html"; std::string url = ConfigurationFile::GetInstance().Get_url() + relurl; SetDocURL(url); SetDocRelativeURL(relurl); */ // std::cout << " url=["<second; } // Adaptors are also stored in the black box map : hence already deleted /* AdaptorMapType::const_iterator j; for (j=mAdaptorMap.begin(); j!=mAdaptorMap.end(); ++j) { delete j->second; } */ bbtkDebugDecTab("Core",7); } //========================================================================== //========================================================================== /// Creates an instance of a black box of type with name BlackBox* Package::NewBlackBox(const std::string& type, const std::string& name) const { bbtkDebugMessageInc("Core",8,"Package<"<::NewBlackBox(\""<second->CreateInstance(name); bbtkDebugDecTab("Core",8); return bb; } //========================================================================== //========================================================================== /// Creates an instance of an adaptor of input type and /// output type with name BlackBox* Package::NewAdaptor(TypeInfo typein, TypeInfo typeout, const std::string& name) const { bbtkDebugMessageInc("Core",8,"Package<"<::NewAdaptor(<" <,<" <,\"" <second->CreateInstance(name); bbtkDebugDecTab("Core",8); return bb; } //========================================================================== //========================================================================== /// Registers a black box descriptor in the package bool Package::RegisterBlackBox(BlackBoxDescriptor* d) { bbtkDebugMessageInc("Core",8,"Package<"<::RegisterBlackBox(\""<GetTypeName()<<"\")"<GetTypeName()] = d; d->SetPackage(this); // If it is a default adaptor, also register it in the adaptors map if ( d->GetCategory() == BlackBoxDescriptor::DEFAULT_ADAPTOR) { TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo(); TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo(); AdaptorKey key(typein,typeout); AdaptorMapType::const_iterator i = mAdaptorMap.find(key); if (i == mAdaptorMap.end()) { bbtkDebugMessage("Core",8,"The box is an adaptor, inserting it in adaptors map ..."< : trying to register black box <" <GetTypeName() <<"> as default adaptor but there is already a default adaptor registered (<" <second->GetTypeName()<<">)"); } } bbtkDebugDecTab("Core",8); return true; } //========================================================================== //========================================================================== /// UnRegisters a black box descriptor from the package void Package::UnRegisterBlackBox(const std::string& name) { bbtkDebugMessageInc("Core",8,"Package<"<::UnRegisterBlackBox(\""< does not contains the black box <"<"); } mBlackBoxMap.erase(i); // Is it also in the adaptors map ? /* AdaptorMapType::iterator j = mAdaptorMap.find(name); if (j != mAdaptorMap.end()) { mAdaptorMap.erase(j); } */ bbtkDebugDecTab("Core",8); } //========================================================================== //========================================================================== /// Changes the name of a black box type void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname ) { bbtkDebugMessageInc("Core",8,"Package<"<::ChangeBlackBoxName(\""< does not contains the black box <"<"); } i->second->SetTypeName(newname); mBlackBoxMap[newname] = i->second; mBlackBoxMap.erase(i); bbtkDebugDecTab("Core",8); } //========================================================================== /* //========================================================================== /// Registers an adaptor descriptor in the package bool Package::RegisterAdaptor(BlackBoxDescriptor* d) { bbtkDebugMessage("Core",8,"Package<"<::RegisterAdaptor(\""<GetTypeName()<<"\")"<GetInputDescriptor("In")->GetTypeInfo(); TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo(); AdaptorKey key(typein,typeout); mAdaptorMap[key] = d; return true; } //========================================================================== */ //========================================================================== /// Displays the list of black boxes of the package void Package::PrintBlackBoxes(bool description, bool adaptors) const { unsigned int lmax = 0; std::vector names; std::vector categs; std::vector descrs; BlackBoxMapType::const_iterator i; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { if ( adaptors || ( i->second->GetCategory() == BlackBoxDescriptor::STANDARD) ) { std::string name(" "); name += i->second->GetTypeName(); names.push_back(name); std::string categ; if ( i->second->GetCategory() == BlackBoxDescriptor::ADAPTOR ) { categ = std::string("[A]"); } else if ( i->second->GetCategory() == BlackBoxDescriptor::DEFAULT_ADAPTOR ) { categ = std::string("[DA]"); } categs.push_back(categ); unsigned int l = name.size()+categ.size(); if (l>lmax) lmax = l; std::string descr; if (description) { descr += " : "; descr += i->second->GetDescription(); } descrs.push_back(descr); } } std::string offs; offs.append(lmax+3,' '); std::vector::iterator ni,ci,di; for (ni = names.begin(), ci = categs.begin(), di = descrs.begin(); ni != names.end(); ++ni, ++ci, ++di) { std::string space; space.append(lmax - ni->size() - ci->size(),' '); bbtkMessage("Help",1,*ni << space << *ci ); std::string d(*di); unsigned int dmax = 75 - lmax; // while (d.size() > dmax ) // { if (d.size()>dmax) bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl); else bbtkMessage("Help",1,d << std::endl); // d = d.substr(dmax,d.size()); // } } } //========================================================================== //========================================================================== /// Displays the list of adaptors of the package void Package::PrintAdaptors(bool description) const { BlackBoxMapType::const_iterator i; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { if ( i->second->GetCategory() != BlackBoxDescriptor::STANDARD ) { bbtkMessage("Help",1, " "<second->GetTypeName()); if ( i->second->GetCategory() == BlackBoxDescriptor::DEFAULT_ADAPTOR ) { bbtkMessage("Help",1, " [default]"); } if (description) { bbtkMessage("Help",1, " : "<second->GetDescription()); } bbtkMessage("Help",1,std::endl); } } /* AdaptorMapType::const_iterator i; for (i=mAdaptorMap.begin(); i!=mAdaptorMap.end(); ++i) { bbtkMessage("Help",1, " "<second->GetTypeName()); if (detail_level>0) { bbtkMessage("Help",1, " : "<second->GetDescription()); } bbtkMessage("Help",1,std::endl); } */ } //========================================================================== //========================================================================== /// Prints help on a black box void Package::HelpBlackBox(const std::string& name, bool full) const { bbtkDebugMessageInc("Core",8,"Package<"<::HelpBlackBox(\"" < does not contains the black box <"<"); } // bbtkMessage("Help",1,"["<second->GetHelp(full); bbtkDebugDecTab("Core",8); } //========================================================================== //========================================================================== /// Returns true iff the package contains the box of name boxname bool Package::ContainsBlackBox(const std::string& name) const { bbtkDebugMessageInc("Core",8,"Package<"<::HelpBlackBox(\"" <::CreateHtmlPage(\"" <\n"; s << "\n"; s << "" << title << "\n"; s << "\n"; s << "\n"; s << "\n"; s << "\n"; // s << "\n"; s << "\n"; //---------------------- //---------------------- // Html body s << "\n"; s << "\n"; //---------------------- // Header if ( custom_header.length() != 0) { if ( custom_header != "none" ) { std::ifstream in; in.open(custom_header.c_str()); if (!in.good()) { bbtkError("Could not open file \""<\n"; /* s << " Warning: " << custom_header <<" could not be embedded.\n"; s << "
\n"; */ } } else { s << "

"<\n"; s << "

\n"; s << "\n"; s << "\n"; s << "\n"; s << "\n"; s << "
Description : " << GetDescription() << "
Author(s) : " << GetAuthor() << "
Version : " << GetVersion() << "
bbtk Version : " << GetBBTKVersion() << "
\n"; } //------------------- // Table of contents // Black boxes list // s << "

\n"; s << "

Black Boxes : \n"; s << "

    \n"; s << "

    \n"; BlackBoxMapType::const_iterator i; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { if ( i->second->GetCategory() != BlackBoxDescriptor::STANDARD) continue; std::string name = i->second->GetTypeName(); Utilities::html_format(name); std::string descr = i->second->GetDescription(); s << ""; s << " "; s << " "; s << "\n"; } s << "
    "; s << "
  • " <"; s << "
  • " << descr << "
    \n"; s << "

\n"; s << "
\n"; //------------------- // Adaptors list if (mAdaptorMap.size()>0) { // s << "
\n"; s << "

Adaptors : \n"; s << "

    \n"; // BlackBoxMapType::const_iterator i; s << "

    \n"; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i) { if ( i->second->GetCategory() == BlackBoxDescriptor::STANDARD) continue; std::string name = i->second->GetTypeName(); Utilities::html_format(name); std::string descr = i->second->GetDescription(); s << ""; s << " "; s << " "; s << "\n"; } s << "
    "; s << "
  • " <"; s << "
  • " << descr << "
    \n"; s << "

\n"; s << "
\n"; } // s << "
\n"; // s << "


\n"; // s << "\n"; // s << "Top: Top\n"; // s << "Previous: (dir), // s << "Up: (dir) // s << "
\n"; //---------------------- // Boxes doc //------------------- // Computes output directory from filename to pass it to // BlackBoxDescriptor::InsertHtmlHelp std::string dir; std::string::size_type slash_position = filename.find_last_of(ConfigurationFile::GetInstance().Get_file_separator ()); if (slash_position != std::string::npos) { if (slash_position == 0) slash_position = 1; dir = filename.substr(0,slash_position); } for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { i->second->InsertHtmlHelp(s,detail,level,dir,relative_link); } //---------------------- // Footer time_t rawtime; tm * ptm; time ( &rawtime ); ptm = gmtime ( &rawtime ); s << "


\n"; s << "Automatically generated by "< from " < on " << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n"; s << "\n"; s.close(); //---------------------- // End bbtkDebugDecTab("Core",9); } //========================================================================== }