1 /*=========================================================================
4 Module: $RCSfile: bbtkFactory.cxx,v $
7 Date: $Date: 2008/02/05 12:16:55 $
8 Version: $Revision: 1.13 $
11 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
12 l'Image). All rights reserved. See doc/license.txt or
13 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
15 This software is distributed WITHOUT ANY WARRANTY; without even
16 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 PURPOSE. See the above copyright notices for more information.
19 =========================================================================*/
22 *\brief Class bbtk::Factory : can load and unload dynamic libraries containing
23 * black boxes packages and create instances of the black boxes registered
24 * in the packages loaded.
26 #include "bbtkFactory.h"
27 #include "bbtkMessageManager.h"
28 #include "bbtkConnection.h"
29 #include "bbtkConfigurationFile.h"
30 #include "bbtkUtilities.h"
32 #include <sys/stat.h> // for struct stat stFileInfo
35 #include <direct.h> // for getcwd
38 #include <cctype> // std::toupper
43 # define getcwd _getcwd
46 #if defined(_MSC_VER) || defined(__BORLANDC__)
57 typedef Package* (*PackageAccessor)();
58 typedef void (*PackageDeleteFunction)();
61 //===================================================================
65 bbtkDebugMessage("Core",7,"Factory::Factory()"<<std::endl);
67 //===================================================================
69 //===================================================================
73 bbtkDebugMessageInc("Core",7,"Factory::~Factory()"<<std::endl);
75 bbtkDebugDecTab("Core",7);
77 //===================================================================
80 //===================================================================
81 void Factory::CloseAllPackages()
83 bbtkDebugMessageInc("Core",7,"Factory::CloseAllPackages()"<<std::endl);
84 while (mPackageMap.begin() != mPackageMap.end())
86 PackageMapType::iterator i = mPackageMap.begin();
89 bbtkDebugDecTab("Core",7);
91 //===================================================================
93 //===================================================================
96 bbtkDebugMessageInc("Core",7,"Factory::Reset()"<<std::endl);
98 bbtkDebugDecTab("Core",7);
100 //===================================================================
103 // ===================================================================================
105 bool Factory::DoLoadPackage(std::string libname,
111 #if defined(__GNUC__)
114 handler = dlopen(libname.c_str(),
115 BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
118 // The following is *NOT* a debug time message :
119 // It's a user intended message.
120 // Please don't remove it.
122 std::cout <<"[" <<libname<<"] can't be open" << std::endl;
123 std::cout << " " <<dlerror() << std::endl;
125 return false; // try next path
128 // The following is *NOT* a debug time message :
129 // It's a user intended message.
130 // Please don't remove it.
132 std::cout <<" -->[" <<libname<<"] found" << std::endl;
134 // Loads the Package accessor
136 std::string getpackname(pkgname);
137 getpackname += "GetPackage";
138 void *getpack = dlsym(handler, getpackname.c_str());
142 bbtkError("GetPackage : could not load package \""<<pkgname
143 <<"\" [symbol "<<getpackname<<"] :"<<dlerror());
146 // Verifies that the Package delete function is present
147 std::string delfname(pkgname);
148 delfname += "DeletePackage";
149 void *delf = dlsym(handler, delfname.c_str());
153 bbtkError("DeletePackage : could not load package \""<<pkgname
154 <<"\" [symbol "<<delfname<<"] :"<<dlerror());
157 #elif defined(_WIN32)
162 handler = LoadLibrary(libname.c_str());
165 // The following is *NOT* a debug time message :
166 // It's a user intended message.
167 // Please don't remove it.
168 std::cout <<" no handler for [" <<libname<<"];" << std::endl;
169 return false;// Problem with the found library
172 std::cout <<" --->[" <<libname<<"] found" << std::endl;
174 // Loads the Package accessor
176 std::string getpackname(pkgname);
177 getpackname += "GetPackage";
178 void *getpack = GetProcAddress(handler, getpackname.c_str());
181 FreeLibrary(handler);
182 bbtkError("[1]could not load package \""<<pkgname
183 <<"\" : "<<getpackname<<" symbol not found (is it a bbtk package lib ?)");
184 // look how to get the error message on win
187 // Verifies that the Package delete function is present
188 std::string delfname(pkgname);
189 delfname += "DeletePackage";
190 void *delf = GetProcAddress(handler, delfname.c_str());
193 FreeLibrary(handler);
194 bbtkError("[2]could not load package \""<<pkgname
195 <<"\" : "<<delfname<<" symbol not found (is it a bbtk package lib ?)");
196 // look how to get the error message on win
200 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
203 // Stores the package
204 PackageInfoType pack;
205 pack.mDynamicLibraryHandler = handler;
206 // Invokes the accessor to the PackageUnit pointer
207 pack.mPackage = ((PackageAccessor)getpack)();
209 mPackageMap[pkgname] = pack;
211 // Test bbtk build version ok
212 if ( pack.mPackage->GetBBTKVersion() != bbtk::GetVersion() )
214 std::string v(pack.mPackage->GetBBTKVersion());
215 UnLoadPackage(pkgname);
216 bbtkError(" package build with bbtk version "
218 << " whereas application build with version "
219 << bbtk::GetVersion());
222 std::string separator =
223 ConfigurationFile::GetInstance().Get_file_separator ();
224 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
225 std::string docreldoc = separator + "packages" + separator + pkgname
226 + separator + "bbdoc" + separator + "index.html";
227 std::string reldoc = ".." + separator + ".." + separator
229 std::string doc = path + separator + ".." + separator
230 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
233 pack.mPackage->SetDocURL(doc);
234 pack.mPackage->SetDocRelativeURL(reldoc);
236 //===================================================================
237 bbtkMessage("Output",2,pack.mPackage->GetName()<<" "
238 <<pack.mPackage->GetVersion()
240 <<pack.mPackage->GetBBTKVersion()<<") "
241 <<pack.mPackage->GetAuthor() << " Category(s) :"
242 <<pack.mPackage->GetCategory()
244 bbtkMessage("Output",2,pack.mPackage->GetDescription()<<std::endl);
245 //===================================================================
247 bbtkDebugDecTab("Core",7);
251 //===================================================================
252 /// \brief Loads a package.
254 /// The name is the system-independant name of the package (the name of the instance of bbtk::Package).
255 /// Tries to open the dynamic library :
256 /// - "libbb<name>.so" for linux systems,
257 /// - "bb<name>.dll" for windows systems.
258 /// If it succeeds, then tries to load the symbols "<name>GetPackage" and "<name>DeletePackage".
259 /// "<name>GetPackage" is called to get the pointer on the bbtk::Package of the library
260 /// ("<name>DeletePackage" is not used, its presence is just checked before loading the package).
262 /// now, filename is only the last name (no longer the full name!)
263 /// it will be searched within *all* the paths given in bbtk_config.xml
265 /// verbose = true (set by "config v") displays the loading process
267 void Factory::LoadPackage( const std::string& name,
268 bool use_configuration_file, bool verbose)
270 // Note : in the following :
271 // name : the user supplied name
272 // - abreviated name e.g. pkg pkg.so libbpkg libbbpkg.so
273 // - relative full name e.g. ./libbbpkg.so ../../libbbpkg.so
274 // - absolute full name e.g. /home/usrname/proj/lib/libbbpkg.so
275 // same for Windows, with c:, d: ...
277 // lastname : string before the last / (if any), or user supplied name
279 bbtkDebugMessageInc("Core",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
280 bbtkMessage("Debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
281 bbtkMessage("Debug",1,"use_configuration_file ["
282 << use_configuration_file << "]" << std::endl);
284 std::vector<std::string> package_paths;
285 std::string libname; // full path library name
286 std::string pkgname; // e.g. libbb<pkgname>.so
289 pkgname = Utilities::ExtractPackageName(name,upath);
291 bbtkMessage("Debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
292 bbtkMessage("Debug",1,"Package path ["<<upath<<"]"<<std::endl);
294 // no loading package if already loaded
295 PackageMapType::iterator iUnload;
296 iUnload = mPackageMap.find(pkgname);
297 if (iUnload != mPackageMap.end())
299 bbtkMessage("Output",2,"["<< pkgname <<"] already loaded" << std::endl);
303 // =================================================
304 // The following structure was checked to work
305 // with any type of relative/absolute path.
306 // Please don't modify it without checking
307 // *all* the cases. JP
308 //==================================================
310 //std::cout << "upath [" << upath << "]" << std::endl;
313 bool foundFile = false;
315 // If path provided by user will be the first scanned :
316 // push it into vector of paths
317 if (upath.length()>0) // ------------------------------------- check user supplied location
319 if (name[0] != '.' && name[0] != '/' && name[1]!= ':')
321 bbtkError("Use absolute or relative path name! ["<<name<<"] is an illegal name");
325 // std::string path = Utilities::ExpandLibName(upath, verbose);
326 std::string path = Utilities::ExpandLibName(name, verbose); // keep last item, here.
331 Utilities::ExtractPackageName(path,p2);
332 //libname = Utilities::MakeLibnameFromPath(path, pkgname);
333 libname = Utilities::MakeLibnameFromPath(p2, pkgname); // remove last item
334 // Check if library exists
335 if ( !Utilities::FileExists(libname) )
337 // The following is *NOT* a debug time message :
338 // It's a user intended message.
339 // Please don't remove it.
341 std::cout <<" [" <<libname <<"] : doesn't exist" <<std::endl;
345 ok = DoLoadPackage( libname, pkgname, path, verbose);
350 bbtkError("Path ["<<upath<<"] doesn't exist");
354 else // ----------------------------------------------------- iterate on the paths
358 package_paths = ConfigurationFile::GetInstance().Get_package_paths();
359 std::vector<std::string>::iterator i;
360 for (i=package_paths.begin();i!=package_paths.end();++i)
365 // we *really* want '.' to be the current working directory
368 char buf[2048]; // for getcwd
369 char * currentDir = getcwd(buf, 2048);
370 std::string cwd(currentDir);
374 libname = Utilities::MakeLibnameFromPath(path, pkgname);
376 bbtkMessage("Debug",2,"-> Trying to load [" << libname << "]" <<std::endl);
378 // Check if library exists
379 if ( !Utilities::FileExists(libname) )
381 // The following is *NOT* a debug time message :
382 // It's a user intended message.
383 // Please don't remove it.
385 std::cout <<" [" <<libname <<"] : doesn't exist" <<std::endl;
386 continue; // try next path
390 // Try to Load the library
392 ok = DoLoadPackage( libname, pkgname, path, verbose);
395 bbtkMessage("Debug",2," OK"<<std::endl);
396 break; // a package was found; we stop iterating
398 } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
402 if( !ok ) // nothing was loaded
406 bbtkError("could not find package ["<<pkgname<< "]");
410 #if defined(__GNUC__)
411 bbtkError("could not load package ["<< pkgname
412 <<"] :" << std::endl << " " << dlerror());
413 #elif defined(_WIN32)
414 bbtkError("could not load package ["<<pkgname
415 <<"] : " << std::endl << " " <<libname<<" not found");
417 // look how to get the error message on win
419 // it is the bordel !! (the bloody fucking bordel, you mean?)
420 // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
424 bbtkMessage("Output",2,"[" << libname << "] loaded" << std::endl);
428 //===================================================================
429 /// \brief UnLoads a package.
431 /// The package must have been previously loaded by LoadPackage.
432 /// If the entry is found in the map, calls ClosePackage
433 void Factory::UnLoadPackage( const std::string& name )
435 bbtkDebugMessageInc("Core",7,"Factory::UnLoadPackage(\""
436 <<name<<"\")"<<std::endl);
438 PackageMapType::iterator i;
439 i = mPackageMap.find(name);
440 if (i == mPackageMap.end())
442 bbtkError("cannot unload package \""<<name
443 <<"\" : package not loaded !");
446 bbtkDebugDecTab("Core",7);
448 //===================================================================
451 //===================================================================
452 /// \brief Close the package referenced by the iterator
454 /// If it is a dynamically loaded package
455 /// - Loads and calls the function "<name>DeletePackage" of the dynamic library (responsible for package desallocation)
456 /// - Closes the dynamic library
457 /// - Erases the package entry in the packages map
459 /// Else simply erases the package entry in the packages map
460 void Factory::ClosePackage(PackageMapType::iterator& i)
462 bbtkDebugMessageInc("Core",7,"Factory::ClosePackage(\""
463 <<i->second.mPackage->GetName()
466 if (i->second.mDynamicLibraryHandler)
469 // If it is a dynamically loaded package
470 // Loads the Package delete function
472 std::string delfname(i->second.mPackage->GetName());
473 delfname += "DeletePackage";
474 #if defined(__GNUC__)
475 void *delf = dlsym(i->second.mDynamicLibraryHandler, delfname.c_str());
478 bbtkError("could not close package \""
479 <<i->second.mPackage->GetName()
480 <<"\" :"<<dlerror());
482 #elif defined(_WIN32)
483 void *delf = GetProcAddress(i->second.mDynamicLibraryHandler,
487 bbtkError("could not close package \""
488 <<i->second.mPackage->GetName()
490 <<" symbol not found (how did you open it ???");
491 //<<"\" :"<<dlerror());
495 // deletes the package
496 ((PackageDeleteFunction)delf)();
498 // closes the dl handler
499 #if defined(__GNUC__)
500 dlclose(i->second.mDynamicLibraryHandler);
501 #elif defined(_WIN32)
503 FreeLibrary(i->second.mDynamicLibraryHandler);
508 // If it is a manually inserted package
509 delete i->second.mPackage;
512 // remove the entry in the map
513 mPackageMap.erase(i);
514 bbtkDebugDecTab("Core",7);
516 //===================================================================
520 //===================================================================
521 /// Displays the list of packages loaded
522 void Factory::PrintPackages(bool details, bool adaptors) const
524 bbtkDebugMessageInc("Core",9,"Factory::PrintPackages"<<std::endl);
526 PackageMapType::const_iterator i;
527 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
529 bbtkMessage("Help",1, i->first << std::endl);
531 i->second.mPackage->PrintBlackBoxes(false,adaptors);
535 bbtkDebugDecTab("Core",9);
537 //===================================================================
539 //===================================================================
540 /// Displays help on a package
541 void Factory::HelpPackage(const std::string& name, bool adaptors) const
543 bbtkDebugMessageInc("Core",9,"Factory::HelpPackage(\""<<name<<"\")"
546 PackageMapType::const_iterator i = mPackageMap.find(name);
547 if ( i != mPackageMap.end() )
549 bbtkMessage("Help",1, "Package "<<i->first<<" ");
551 if (i->second.mPackage->GetVersion().length()>0)
552 bbtkMessageCont("Help",1,"v" <<i->second.mPackage->GetVersion());
554 if (i->second.mPackage->GetAuthor().length()>0)
555 bbtkMessageCont("Help",1,"- "<<i->second.mPackage->GetAuthor());
557 if (i->second.mPackage->GetCategory().length()>0)
558 bbtkMessageCont("Help",1,"- "<<i->second.mPackage->GetCategory());
560 bbtkMessageCont("Help",1,std::endl);
561 bbtkIncTab("Help",1);
562 bbtkMessage("Help",1,i->second.mPackage->GetDescription()<<std::endl);
563 if (i->second.mPackage->GetNumberOfBlackBoxes()>0)
565 bbtkMessage("Help",1, "Black boxes : "<<std::endl);
566 i->second.mPackage->PrintBlackBoxes(true,adaptors);
570 bbtkMessage("Help",1, "No black boxes"<<std::endl);
572 bbtkDecTab("Help",1);
576 bbtkDebugDecTab("Core",9);
577 bbtkError("package \""<<name<<"\" unknown");
580 bbtkDebugDecTab("Core",9);
582 //===================================================================
584 //===================================================================
585 /// Prints help on the black box of type <name>
586 void Factory::HelpBlackBox(const std::string& name, bool full) const
588 bbtkDebugMessageInc("Core",9,"Factory::HelpBlackBox(\""<<name<<"\")"
592 PackageMapType::const_iterator i;
593 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
595 if (i->second.mPackage->ContainsBlackBox(name))
597 i->second.mPackage->HelpBlackBox(name,full);
602 bbtkDebugDecTab("Core",9);
605 bbtkError("No package of the factory contains any black box <"
609 //===================================================================
612 //===================================================================
613 /// Inserts a package in the factory
614 void Factory::InsertPackage( Package* p )
616 bbtkDebugMessageInc("Core",9,"Factory::InsertPackage(\""<<
617 p->GetName()<<"\")"<<std::endl);
619 PackageInfoType pack;
620 pack.mDynamicLibraryHandler = 0;
624 mPackageMap[p->GetName()] = pack;
625 bbtkDebugDecTab("Core",9);
627 //===================================================================
629 //===================================================================
630 /// Removes a package from the factory (and deletes it)
631 void Factory::RemovePackage( Package* p )
633 bbtkDebugMessageInc("Core",9,"Factory::RemovePackage(\""<<
634 p->GetName()<<"\")"<<std::endl);
636 PackageMapType::iterator i;
637 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
639 if (i->second.mPackage == p) break;
642 if (i!=mPackageMap.end())
648 bbtkError("Factory::RemovePackage(\""<<
649 p->GetName()<<"\") : package absent from factory");
652 bbtkDebugDecTab("Core",9);
654 //===================================================================
657 //===================================================================
658 /// Creates an instance of a black box of type <type> with name <name>
659 BlackBox* Factory::NewBlackBox(const std::string& type,
660 const std::string& name) const
662 bbtkDebugMessageInc("Core",7,"Factory::NewBlackBox(\""
663 <<type<<"\",\""<<name<<"\")"<<std::endl);
666 PackageMapType::const_iterator i;
667 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
669 b = i->second.mPackage->NewBlackBox(type,name);
674 bbtkError("black box type \""<<type<<"\" unknown");
677 bbtkDebugDecTab("Core",7);
680 //===================================================================
682 //===================================================================
683 /// Creates an instance of a black box of type <type> with name <name>
684 BlackBox* Factory::NewAdaptor(TypeInfo typein,
686 const std::string& name) const
688 bbtkDebugMessageInc("Core",8,"Factory::NewAdaptor(<"
689 <<TypeName(typein)<<">,<"
690 <<TypeName(typeout)<<">,\""
691 <<name<<"\")"<<bbtkendl);
695 PackageMapType::const_iterator i;
696 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
698 b = i->second.mPackage->NewAdaptor(typein,typeout,name);
704 <<TypeName(typein)<<"> to <"
706 <<"> adaptor available");
709 bbtkDebugDecTab("Core",7);
712 //===================================================================
714 //===================================================================
715 /// Creates an instance of a connection
716 Connection* Factory::NewConnection(BlackBox* from,
717 const std::string& output,
719 const std::string& input) const
721 bbtkDebugMessage("Core",7,"Factory::NewConnection(\""
722 <<from->bbGetName()<<"\",\""<<output<<"\",\""
723 <<to->bbGetName()<<"\",\""<<input
726 return new Connection(from,output,to,input);
729 // !!! WARNING : WE NEED TO TEST THE TYPE NAME EQUALITY
730 // BECAUSE IN DIFFERENT DYN LIBS THE type_info EQUALITY CAN
731 // BE FALSE (DIFFERENT INSTANCES !)
733 std::string t1 ( from->bbGetOutputType(output).name() );
734 std::string t2 ( to->bbGetInputType(input).name() );
738 //from->bbGetOutputType(output) ==
739 // to->bbGetInputType(input) )
741 c = new Connection(from,output,to,input);
745 // std::cout << "Adaptive connection "<<std::endl;
747 name = from->bbGetName() + "." + output + "-"
748 + to->bbGetName() + "." + input;
751 PackageMapType::const_iterator i;
752 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
754 b = i->second.mPackage->NewAdaptor(from->bbGetOutputType(output),
755 to->bbGetInputType(input),
761 bbtkError("did not find any <"
762 <<TypeName(from->bbGetOutputType(output))
764 <<TypeName(to->bbGetInputType(input))
767 c = new AdaptiveConnection(from,output,to,input,b);
769 bbtkDebugDecTab("Core",7);
774 //===================================================================
778 //===================================================================
779 const Package* Factory::GetPackage(const std::string& name) const
781 bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
784 PackageMapType::const_iterator i = mPackageMap.find(name);
785 if ( i != mPackageMap.end() )
787 bbtkDebugDecTab("Core",9);
788 return i->second.mPackage;
792 bbtkDebugDecTab("Core",9);
793 bbtkError("package \""<<name<<"\" unknown");
796 bbtkDebugDecTab("Core",9);
798 //===================================================================
800 //===================================================================
801 Package* Factory::GetPackage(const std::string& name)
803 bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
806 PackageMapType::const_iterator i = mPackageMap.find(name);
807 if ( i != mPackageMap.end() )
809 bbtkDebugDecTab("Core",9);
810 return i->second.mPackage;
814 bbtkDebugDecTab("Core",9);
815 bbtkError("package \""<<name<<"\" unknown");
818 bbtkDebugDecTab("Core",9);
820 //===================================================================
822 //===================================================================
823 void Factory::WriteDotFilePackagesList(FILE *ff)
825 bbtkDebugMessageInc("Core",9,"Factory::WriteDotFilePackagesList()"
829 fprintf( ff , "subgraph cluster_FACTORY {\n");
830 fprintf( ff , " label = \"PACKAGES\"%s\n", ";");
831 fprintf( ff , " style=filled%s\n",";");
832 fprintf( ff , " color=lightgrey%s\n",";");
833 fprintf( ff , " rankdir=TB%s\n",";");
836 PackageMapType::const_iterator i;
837 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
839 url=GetPackage(i->first)->GetDocURL();
840 fprintf(ff," %s [shape=ellipse, URL=\"%s\"]%s\n",i->first.c_str(),url.c_str(),";" );
842 fprintf( ff , "}\n\n");
843 bbtkDebugDecTab("Core",9);
845 //===================================================================
848 void Factory::ShowGraphTypes(const std::string& name) const
851 PackageMapType::const_iterator i;
852 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
854 if (i->second.mPackage->ContainsBlackBox(name))
856 std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
858 // Don't pollute the file store with "doc_tmp" directories ...
859 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
860 std::string directory = "\"" + default_doc_dir + separator + "doc_tmp" +separator + "\"";
861 std::string filename2 = default_doc_dir + separator + "doc_tmp" + separator + "tmp.html";
864 std::string command("start \"Titre\" /D ");
866 std::string command("gnome-open ");
868 command=command + directory +" tmp.html";
870 ff=fopen(filename2.c_str(),"w");
872 fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
873 fprintf(ff," window.location=\"%s#%s\";\n" , i->second.mPackage->GetDocURL().c_str(),name.c_str() );
874 fprintf(ff,"//--></script></head><body></body></html>\n");
877 //fprintf(ff, "<a href=\"%s#%s\">Link</a>\n", i->second.mPackage->GetDocURL().c_str(),name.c_str() );
879 system( command.c_str() );
884 bbtkDebugDecTab("Core",9);
887 bbtkError("No package of the factory contains any black box <"
895 void Factory::CreateHtmlIndex(IndexEntryType type,
896 const std::string& filename)
898 bbtkDebugMessageInc("Core",9,"Factory::CreateHtmlIndex(\""
899 <<filename<<"\")"<<bbtkendl);
903 typedef std::map<std::string, std::vector<BlackBoxDescriptor*> > IndexType;
905 // Builds the index map
906 PackageMapType::const_iterator i;
907 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
909 Package* pack = i->second.mPackage;
910 if (pack->GetName()=="user") continue;
911 Package::BlackBoxMapType::const_iterator j;
912 for (j = pack->GetBlackBoxMap().begin();
913 j!= pack->GetBlackBoxMap().end();
917 std::vector<std::string> keys;
921 k += pack->GetName();
923 title = "List by package";
925 else if (type==Initials)
927 std::string init(" ");
928 init[0] = std::toupper(j->second->GetTypeName()[0]);
929 keys.push_back(init);
930 title = "Alphabetical list";
932 else if (type==Categories)
935 // Split the category string
936 //std::vector<std::string> categories;
938 std::string delimiters = ";,";
939 Utilities::SplitString(j->second->GetCategory(),
942 //std::vector<std::string>::iterator si;
943 //for (si=categories.begin();si!=categories.end;++si)
945 // keys.push_back(*si);
947 title = "Index by category";
950 keys.push_back(" NONE");
951 title = "List by category";
954 std::vector<std::string>::const_iterator k;
955 for (k=keys.begin(); k!=keys.end(); ++k )
957 IndexType::iterator p;
959 if (p != index.end())
961 p->second.push_back(j->second);
965 std::vector<BlackBoxDescriptor*> v;
966 v.push_back(j->second);
974 //---------------------
977 s.open(filename.c_str());
980 bbtkError("Factory::CreateHtmlIndex : could not open file '"
984 //----------------------
986 s << "<html lang=\"en\">\n";
988 s << "<title>"<<title<<"</title>\n";
989 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
990 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
991 s << "<meta name=\"generator\" content=\"\">\n";
992 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
993 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
994 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
995 s << "pre.display { font-family:inherit }\n";
996 s << "pre.format { font-family:inherit }\n";
997 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
998 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
999 s << "pre.smallexample { font-size:smaller }\n";
1000 s << "pre.smalllisp { font-size:smaller }\n";
1001 s << "span.sc { font-variant:small-caps }\n";
1002 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1003 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1004 s << "--></style>\n";
1006 //----------------------
1008 //----------------------
1011 s << "<a name=\"Top\"></a>\n";
1012 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1014 IndexType::iterator ii;
1015 for (ii=index.begin();ii!=index.end();++ii)
1017 s << "<a href=\"#"<<ii->first<<"\">"<<ii->first<<"</a> ";
1020 for (ii=index.begin();ii!=index.end();++ii)
1023 s << "<p><a href=\"#Top\">Top</a>";
1024 s << "<a name=\""<<ii->first<<"\"></a>\n";
1025 s << "<p><b>"<<ii->first<<"</b>\n";
1028 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1030 std::vector<BlackBoxDescriptor*>::iterator di;
1031 for (di=ii->second.begin();di!=ii->second.end();++di)
1033 std::string pack = (*di)->GetPackage()->GetName();
1034 std::string name = (*di)->GetTypeName();
1035 std::string descr = (*di)->GetDescription();
1037 s << "<TD style='vertical-align: top;'>";
1038 s << "<li><a href=\""<<pack
1039 <<"/index.html#"<<name<<"\">"
1040 <<pack<<"::"<<name<<"</a>";
1042 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1046 s << "</li></ul>\n";
1049 //----------------------
1054 ptm = gmtime ( &rawtime );
1057 s << "Automatically generated by <b>bbi</b> on "
1058 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1059 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1060 s << "</body></html>\n";
1062 //----------------------
1065 bbtkDebugDecTab("Core",9);