2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.38 $
34 =========================================================================*/
40 *\brief Class bbtk::ConfigurationFile
43 #include "bbtkConfigurationFile.h"
44 #include "bbtkMessageManager.h"
46 #include "bbtkUtilities.h"
48 #include "creaSystem.h"
51 # include <direct.h> // for getcwd
58 //====================================================================
60 ConfigurationFile::ConfigurationFile()
62 mFile_separator = VALID_FILE_SEPARATOR;
64 // ==> Set system paths
65 mBin_path = Utilities::GetExecutablePath();
70 mInstall_path = mBin_path + "/../../../..";
72 mInstall_path = mBin_path + "/..";
76 std::string macPath("Contents/MacOS");
77 int sbp = mBin_path.length();
78 int smp = macPath.length();
79 if (mBin_path.compare( sbp-smp, smp, macPath )==0 )
81 mBin_path = mBin_path + "/../../..";
86 ///\TODO : better use ??
87 mInstall_path = mBin_path + "/..";
89 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
90 // mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
91 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
92 // mDoc_path = mInstall_path + "/" + mDoc_rel_path;
93 // Have to create bbtk user's dirs if needed
95 // LG 27/01/09 : doc is now user dependent
96 // and located in user's .bbtk dir /doc
97 // All dirs under .bbtk user's dir are set and created by:
98 InitializeDotBbtkStructure();
100 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
101 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
102 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
103 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
105 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
107 Utilities::replace( mBin_path,
108 INVALID_FILE_SEPARATOR,
109 VALID_FILE_SEPARATOR);
110 Utilities::replace( mInstall_path,
111 INVALID_FILE_SEPARATOR,
112 VALID_FILE_SEPARATOR);
113 Utilities::replace( mBbs_path,
114 INVALID_FILE_SEPARATOR,
115 VALID_FILE_SEPARATOR);
116 Utilities::replace( mData_path,
117 INVALID_FILE_SEPARATOR,
118 VALID_FILE_SEPARATOR);
120 bbtkMessage("config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
121 bbtkMessage("config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
122 bbtkMessage("config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
123 bbtkMessage("config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
124 bbtkMessage("config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
127 // always add "." (current working directory) at the begining
128 mBbs_paths.push_back( "." );
129 // add system bbs path
130 mBbs_paths.push_back(mBbs_path);
131 // add toolsbbtk/appli
132 //EED std::string toolsappli_rel_path(mFile_separator);
133 std::string toolsappli_rel_path("/");
134 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
135 toolsappli_rel_path += "toolsbbtk/appli";
137 //-----------------------------------------------------------
138 // LG : REMOVE BUGGY PATH WITH include *:
140 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
141 //-----------------------------------------------------------
142 int iStrVec,sizeStrVec;
144 sizeStrVec = mBbs_paths.size();
145 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
146 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
150 // always add "." (current working directory) at the begining
151 mPackage_paths.push_back(".");
152 // add system bin path (for build tree / standalone folder install)
153 mPackage_paths.push_back(mBin_path);
154 // add system lib path (for install tree)
155 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
157 //mPackage_paths.push_back(mInstall_path + "/lib"); // JPR
158 //mPackage_paths.push_back(mInstall_path + "/lib64");// JPR
160 ///\TODO : better use BBTK_PACKAGE_LIB_PATH // JPR
162 mPackage_paths.push_back(mInstall_path + "/lib/creatools");
163 mPackage_paths.push_back(mInstall_path + "/lib64/creatools");
165 // add bin/Debug bin/Release paths (for build/install tree)
166 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
167 mPackage_paths.push_back(mBin_path + "/Debug");
168 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
169 mPackage_paths.push_back(mBin_path + "/Release");
172 sizeStrVec = mPackage_paths.size();
173 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
174 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
179 // ==> First we look for bbtk_config.xml in "."
181 const char *currentDir = getcwd(buf, 2048);
185 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
186 // \todo : what else?
187 // How abort a constructor and warn the caller function?
188 // LG : throw an exception
191 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
192 std::string configXmlFullPathName = currentDir ;
193 configXmlFullPathName += "/bbtk_config.xml";
194 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
196 if ( Utilities::FileExists( configXmlFullPathName ))
198 bbtkMessage("config",1, "ConfigurationFile : [" << configXmlFullPathName <<
199 "] found in current directory" << std::endl);
200 //Read(configXmlFullPathName.c_str());
201 // traiter le fichier local
204 // ==> Then we look for bbtk_config.xml in ".bbtk"
207 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
208 if (!Utilities::FileExists( configXmlFullPathName ))
210 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
215 // In any case, deal with bbtk_config.xml!
216 Read(configXmlFullPathName.c_str());
218 //=========================================================================
220 //=========================================================================
222 ConfigurationFile::~ConfigurationFile()
225 //=========================================================================
227 //=========================================================================
228 void ConfigurationFile::InitializeDotBbtkStructure()
230 mDot_bbtk_path = Utilities::GetUserSettingsDir();
231 mDot_bbtk_is_new = false;
232 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
233 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
236 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
237 Utilities::CreateDirectoryIfNeeded(mDoc_path);
239 std::string bbdoc_path =
240 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
241 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
243 // Create help_contents.html if does not exist
244 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
245 if (!Utilities::FileExists(filename))
247 bbtkDebugMessage("config",1,
248 "* Creating [" << filename << "]" << std::endl);
249 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
250 std::string doc_path = mInstall_path + "/"
251 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
252 Utilities::MakeValidFileName(doc_path);
255 f.open(filename.c_str(), std::ios::out );
256 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
257 << "HTML 4.01 Transitional//EN\">"
258 << "<html><head><title>Help Contents - bbtk "
259 << GetVersion() << "</title>"
260 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
261 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
262 << "<a href=\"bbdoc/index-category.html#demo\" "
263 << "target=\"information\">Demos</a><br>"
264 << "<a href=\"bbdoc/index-category.html#example\" "
265 << "target=\"information\">Examples</a>"
269 << "bbtkUsersGuide/bbtkUsersGuide.pdf"
270 << "\" target=\"information\">User's Guide</a><br>"
273 << "bbtkDevelopersGuide/bbtkDevelopersGuide.pdf"
274 << "\" target=\"information\">Developer's Guide</a><br>"
277 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.pdf"
278 << "\" target=\"information\">Package Developer's Guide</a><br>"
281 << "doxygen/bbtk/main.html"
282 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
284 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
285 << "Alphabetical list</a><br>"
286 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
287 << "List by package</a><br>"
288 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
289 << "List by category</a><br>"
290 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
291 << "List of adaptors</a><br>"
296 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
297 Utilities::CreateDirectoryIfNeeded(mTemp_path);
301 //=========================================================================
303 //=========================================================================
304 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
308 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
309 bbtkDebugMessage("config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
310 fp = fopen (configXml, "w");
311 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
312 fprintf(fp, "<config>\n");
313 fprintf(fp, " <bbs_path> </bbs_path>\n");
314 fprintf(fp, " <package_path> </package_path>\n");
315 fprintf(fp, "</config>\n");
318 //=========================================================================
323 //=========================================================================
324 void ConfigurationFile::InstallPath ()
327 /*--------------------------------------------------
328 New policy for bbtk_config.xml :
330 if bbtk_config.xml found in current directory (user is an aware user!)
333 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
336 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
337 copy it as .bbtk/bbtk_config.xml
339 else (nothing installed)
340 create a minimum version in HOME/.bbtk
341 ----------------------------------------------------*/
344 // -----------------------------------------------------------------
345 #if defined(__GNUC__)
347 // ------------------ create some usefull strings ----------------
348 // installed bbtk_path
349 char bbtk_path[1000];
350 strcpy(bbtk_path, Utilities::GetExecutablePath().c_str()); // JPR
351 //strcpy(bbtk_path, "/usr/local/bin");
354 std::string str_home=Utilities::GetEnvHome();
358 char rootDirectory[200];
359 sprintf( rootDirectory, "%s/.bbtk", str_home.c_str());
362 char configPath[200];
363 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
366 char configXmlTmp[250];
367 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
372 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
374 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
375 CreateConfigXML( rootDirectory );// create .bbtk
379 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
380 if (!Utilities::FileExists(configPath))
387 // ------------------------------------------------------------------
391 // installed bbtk_path
393 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
394 char bbtk_path2[100];
395 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
398 char rootDirectory[200];
399 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
400 // std::cout << "[" << rootDirectory << "]" << std::endl;
403 char configPath[200];
404 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
408 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
411 char configXmlTmp[250];
412 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
417 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
419 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
420 CreateConfigXML( rootDirectory );// create .bbtk
424 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
426 int attribs = GetFileAttributes (rootDirectory);
427 bbtkMessage("config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
428 if ( attribs != 0xFFFFFFFF)
430 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
432 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
444 // ------------------------------------------------------------------
446 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
452 //=========================================================================
456 //=========================================================================
457 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
460 void ConfigurationFile::Read(const std::string& filename)
463 bbtkDebugMessage("config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
465 mConfig_xml_full_path = filename;
466 XMLResults* res = new XMLResults;
467 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
469 if ( res->error != eXMLErrorNone )
471 std::string mess = GetErrorMessage(res,filename);
473 bbtkDebugMessage("config",1,mess<< std::endl);
478 bbtkDebugMessage("config",1,"OK" << std::endl);
483 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
486 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
491 if( BB.nChildNode((XMLCSTR)"url") )
492 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
495 if( BB.nChildNode((XMLCSTR)"data_path") )
496 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
499 // if( BB.nChildNode((XMLCSTR)"install_path") )
500 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
502 // add user bbs paths
503 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
506 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
507 mBbs_paths.push_back(val);
512 // add user package path
513 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
516 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
517 mPackage_paths.push_back(val);
522 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
523 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
525 if ( mDefault_temp_dir == "$") // no value found in config_xml
527 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
528 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
534 //=========================================================================
536 //=========================================================================
537 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
539 bbtkDebugMessageInc("config",9,
540 "ConfigurationFile::AddPackagePathsAndWrite("
541 <<path<<")"<<std::endl);
543 XMLResults* res = new XMLResults;
545 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
546 (XMLCSTR)"config",res);
548 if ( res->error != eXMLErrorNone )
550 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
552 bbtkDebugMessage("config",1,mess<< std::endl);
558 std::string bbs_path = path + "/bbs";
560 std::string bbs_path = path + "/share/bbtk/bbs" ;
562 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
563 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
564 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
565 mBbs_paths.push_back(bbs_path);
568 std::string pack_path = path + "/bin";
570 std::string pack_path = path ;
572 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
573 PACKPATH.addText((XMLCSTR)pack_path.c_str());
574 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
575 mPackage_paths.push_back(pack_path);
578 pack_path = path + "/RelWithDebInfo";
579 PACKPATH = BB.addChild((XMLCSTR)"package_path");
580 PACKPATH.addText((XMLCSTR)pack_path.c_str());
581 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
582 mPackage_paths.push_back(pack_path);
583 pack_path = path + "/Debug";
584 PACKPATH = BB.addChild((XMLCSTR)"package_path");
585 PACKPATH.addText((XMLCSTR)pack_path.c_str());
586 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
587 mPackage_paths.push_back(pack_path);
588 pack_path = path + "/Release";
589 PACKPATH = BB.addChild((XMLCSTR)"package_path");
590 PACKPATH.addText((XMLCSTR)pack_path.c_str());
591 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
592 mPackage_paths.push_back(pack_path);
596 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
597 if ( err != eXMLErrorNone )
599 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
600 bbtkDebugMessage("config",1,mess<< std::endl);
606 //=========================================================================
608 //=========================================================================
609 void ConfigurationFile::GetHelp(int level) const
611 bbtkDebugMessageInc("config",9,"ConfigurationFile::GetHelp("<<level
614 const std::string config_xml_full_path = Get_config_xml_full_path();
615 const std::string description = Get_description();
616 const std::string url = Get_doc_path();
617 const std::string data_path = Get_data_path();
618 const std::string default_temp_dir = Get_default_temp_dir();
619 const std::string file_separator = Get_file_separator();
620 const std::vector<std::string>bbs_paths = Get_bbs_paths();
621 const std::vector<std::string>package_paths = Get_package_paths();
623 bbtkMessage("help",level, "=============" << std::endl);
624 bbtkMessage("help",level, "Configuration" << std::endl);
625 bbtkMessage("help",level, "=============" << std::endl);
626 bbtkMessage("help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
627 bbtkMessage("help",level, "Documentation Path : [" << url << "]" << std::endl);
628 bbtkMessage("help",level, "Data Path : [" << data_path << "]" << std::endl);
629 bbtkMessage("help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
630 bbtkMessage("help",level, "File Separator : [" << file_separator << "]" << std::endl);
632 std::vector<std::string>::const_iterator i;
634 bbtkMessage("help",level, "BBS Paths " << std::endl);
635 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
637 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
640 bbtkMessage("help",level, "PACKAGE Paths : " << std::endl);
641 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
643 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
646 bbtkDebugDecTab("config",9);
648 //=========================================================================