1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2009/03/24 13:42:59 $
6 Version: $Revision: 1.30 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 *\brief Class bbtk::ConfigurationFile
36 #include "bbtkConfigurationFile.h"
37 #include "bbtkMessageManager.h"
39 #include "bbtkUtilities.h"
42 # include <direct.h> // for getcwd
52 //====================================================================
54 ConfigurationFile::ConfigurationFile()
57 mFile_separator = VALID_FILE_SEPARATOR;
59 // ==> Set system paths
60 mBin_path = Utilities::GetExecutablePath();
67 mInstall_path = mBin_path + "/../../../..";
69 mInstall_path = mBin_path + "/..";
74 std::string macPath("Contents/MacOS");
75 int sbp = mBin_path.length();
76 int smp = macPath.length();
77 if (mBin_path.compare( sbp-smp, smp, macPath )==0 )
79 mBin_path = mBin_path + "/../../..";
84 mInstall_path = mBin_path + "/..";
88 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
89 // mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
90 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
91 // mDoc_path = mInstall_path + "/" + mDoc_rel_path;
92 // Have to create bbtk user's dirs if needed
94 // LG 27/01/09 : doc is now user dependent
95 // and located in user's .bbtk dir /doc
96 // All dirs under .bbtk user's dir are set and created by:
97 InitializeDotBbtkStructure();
99 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
100 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
101 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
102 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
104 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
106 Utilities::replace( mBin_path,
107 INVALID_FILE_SEPARATOR,
108 VALID_FILE_SEPARATOR);
109 Utilities::replace( mInstall_path,
110 INVALID_FILE_SEPARATOR,
111 VALID_FILE_SEPARATOR);
112 Utilities::replace( mBbs_path,
113 INVALID_FILE_SEPARATOR,
114 VALID_FILE_SEPARATOR);
115 Utilities::replace( mData_path,
116 INVALID_FILE_SEPARATOR,
117 VALID_FILE_SEPARATOR);
119 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
120 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
121 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
122 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
123 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
129 // always add "." (current working directory) at the begining
130 mBbs_paths.push_back( "." );
131 // add system bbs path
132 mBbs_paths.push_back(mBbs_path);
133 // add toolsbbtk/appli
134 //EED std::string toolsappli_rel_path(mFile_separator);
135 std::string toolsappli_rel_path("/");
136 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
137 toolsappli_rel_path += "toolsbbtk/appli";
139 //-----------------------------------------------------------
140 // LG : REMOVE BUGGY PATH WITH include *:
142 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
143 //-----------------------------------------------------------
144 int iStrVec,sizeStrVec;
146 sizeStrVec = mBbs_paths.size();
147 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
148 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
152 // always add "." (current working directory) at the begining
153 mPackage_paths.push_back(".");
154 // add system bin path (for build tree / standalone folder install)
155 mPackage_paths.push_back(mBin_path);
156 // add system lib path (for install tree)
157 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
158 mPackage_paths.push_back(mInstall_path + "/lib");
160 // add bin/Debug bin/Release paths (for build/install tree)
161 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
162 mPackage_paths.push_back(mBin_path + "/Debug");
163 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
164 mPackage_paths.push_back(mBin_path + "/Release");
167 sizeStrVec = mPackage_paths.size();
168 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
169 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
176 // ==> First we look for bbtk_config.xml in "."
178 const char *currentDir = getcwd(buf, 2048);
182 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
183 // \todo : what else?
184 // How abort a constructor and warn the caller function?
185 // LG : throw an exception
188 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
189 std::string configXmlFullPathName = currentDir ;
190 configXmlFullPathName += "/bbtk_config.xml";
191 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
193 if ( Utilities::FileExists( configXmlFullPathName ))
195 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
196 "] found in current directory" << std::endl);
197 //Read(configXmlFullPathName.c_str());
198 // traiter le fichier local
201 // ==> Then we look for bbtk_config.xml in ".bbtk"
204 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
205 if (!Utilities::FileExists( configXmlFullPathName ))
207 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
212 // In any case, deal with bbtk_config.xml!
213 Read(configXmlFullPathName.c_str());
215 //=========================================================================
217 //=========================================================================
219 ConfigurationFile::~ConfigurationFile()
222 //=========================================================================
224 //=========================================================================
225 void ConfigurationFile::InitializeDotBbtkStructure()
227 mDot_bbtk_path = Utilities::GetUserSettingsDir();
228 mDot_bbtk_is_new = false;
229 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
230 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
232 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
233 Utilities::CreateDirectoryIfNeeded(mDoc_path);
235 std::string bbdoc_path =
236 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
237 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
239 // Create help_contents.html if does not exist
240 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
241 if (!Utilities::FileExists(filename))
243 bbtkDebugMessage("Config",1,
244 "* Creating [" << filename << "]" << std::endl);
245 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
246 std::string doc_path = mInstall_path + "/"
247 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
248 Utilities::MakeValidFileName(doc_path);
250 f.open(filename.c_str(), std::ios::out );
251 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
252 << "HTML 4.01 Transitional//EN\">"
253 << "<html><head><title>Help Contents - bbtk "
254 << GetVersion() << "</title>"
255 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
256 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
257 << "<a href=\"bbdoc/index-category.html#demo\" "
258 << "target=\"information\">Demos</a><br>"
259 << "<a href=\"bbdoc/index-category.html#example\" "
260 << "target=\"information\">Examples</a>"
264 << "bbtkUsersGuide/bbtkUsersGuide.html"
265 << "\" target=\"information\">User's Guide</a><br>"
268 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.html"
269 << "\" target=\"information\">Package Developer's Guide</a><br>"
272 << "doxygen/bbtk/main.html"
273 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
275 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
276 << "Alphabetical list</a><br>"
277 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
278 << "List by package</a><br>"
279 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
280 << "List by category</a><br>"
281 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
282 << "List of adaptors</a><br>"
288 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
289 Utilities::CreateDirectoryIfNeeded(mTemp_path);
292 //=========================================================================
294 //=========================================================================
295 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
299 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
300 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
301 fp = fopen (configXml, "w");
302 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
303 fprintf(fp, "<config>\n");
304 fprintf(fp, " <bbs_path> </bbs_path>\n");
305 fprintf(fp, " <package_path> </package_path>\n");
306 fprintf(fp, "</config>\n");
309 //=========================================================================
314 //=========================================================================
315 void ConfigurationFile::InstallPath ()
318 /*--------------------------------------------------
319 New policy for bbtk_config.xml :
321 if bbtk_config.xml found in current directory (user is an aware user!)
324 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
327 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
328 copy it as .bbtk/bbtk_config.xml
330 else (nothing installed)
331 create a minimum version in HOME/.bbtk
332 ----------------------------------------------------*/
335 // -----------------------------------------------------------------
336 #if defined(__GNUC__)
338 // ------------------ create some usefull strings ----------------
339 // installed bbtk_path
341 strcpy(bbtk_path, "/usr/local/bin");
344 char rootDirectory[200];
345 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
348 char configPath[200];
349 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
352 char configXmlTmp[250];
353 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
358 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
360 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
361 CreateConfigXML( rootDirectory );// create .bbtk
365 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
366 if (!Utilities::FileExists(configPath))
373 // ------------------------------------------------------------------
377 // installed bbtk_path
379 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
380 char bbtk_path2[100];
381 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
384 char rootDirectory[200];
385 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
386 // std::cout << "[" << rootDirectory << "]" << std::endl;
389 char configPath[200];
390 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
394 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
397 char configXmlTmp[250];
398 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
403 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
405 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
406 CreateConfigXML( rootDirectory );// create .bbtk
410 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
412 int attribs = GetFileAttributes (rootDirectory);
413 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
414 if ( attribs != 0xFFFFFFFF)
416 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
418 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
430 // ------------------------------------------------------------------
432 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
438 //=========================================================================
442 //=========================================================================
443 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
446 void ConfigurationFile::Read(const std::string& filename)
449 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
451 mConfig_xml_full_path = filename;
452 XMLResults* res = new XMLResults;
453 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
455 if ( res->error != eXMLErrorNone )
457 std::string mess = GetErrorMessage(res,filename);
459 bbtkDebugMessage("Config",1,mess<< std::endl);
464 bbtkDebugMessage("Config",1,"OK" << std::endl);
469 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
472 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
477 if( BB.nChildNode((XMLCSTR)"url") )
478 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
481 if( BB.nChildNode((XMLCSTR)"data_path") )
482 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
485 // if( BB.nChildNode((XMLCSTR)"install_path") )
486 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
488 // add user bbs paths
489 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
492 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
493 mBbs_paths.push_back(val);
498 // add user package path
499 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
502 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
503 mPackage_paths.push_back(val);
508 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
509 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
511 if ( mDefault_temp_dir == "$") // no value found in config_xml
513 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
514 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
520 //=========================================================================
522 //=========================================================================
523 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
525 bbtkDebugMessageInc("Config",9,
526 "ConfigurationFile::AddPackagePathsAndWrite("
527 <<path<<")"<<std::endl);
529 XMLResults* res = new XMLResults;
531 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
532 (XMLCSTR)"config",res);
534 if ( res->error != eXMLErrorNone )
536 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
538 bbtkDebugMessage("Config",1,mess<< std::endl);
544 std::string bbs_path = path + "/bbs";
546 std::string bbs_path = path + "/share/bbtk/bbs" ;
548 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
549 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
550 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
551 mBbs_paths.push_back(bbs_path);
554 std::string pack_path = path + "/bin";
556 std::string pack_path = path ;
558 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
559 PACKPATH.addText((XMLCSTR)pack_path.c_str());
560 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
561 mPackage_paths.push_back(pack_path);
564 pack_path = path + "/RelWithDebInfo";
565 PACKPATH = BB.addChild((XMLCSTR)"package_path");
566 PACKPATH.addText((XMLCSTR)pack_path.c_str());
567 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
568 mPackage_paths.push_back(pack_path);
569 pack_path = path + "/Debug";
570 PACKPATH = BB.addChild((XMLCSTR)"package_path");
571 PACKPATH.addText((XMLCSTR)pack_path.c_str());
572 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
573 mPackage_paths.push_back(pack_path);
574 pack_path = path + "/Release";
575 PACKPATH = BB.addChild((XMLCSTR)"package_path");
576 PACKPATH.addText((XMLCSTR)pack_path.c_str());
577 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
578 mPackage_paths.push_back(pack_path);
582 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
583 if ( err != eXMLErrorNone )
585 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
586 bbtkDebugMessage("Config",1,mess<< std::endl);
592 //=========================================================================
594 //=========================================================================
595 void ConfigurationFile::GetHelp(int level) const
597 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
600 const std::string config_xml_full_path = Get_config_xml_full_path();
601 const std::string description = Get_description();
602 const std::string url = Get_doc_path();
603 const std::string data_path = Get_data_path();
604 const std::string default_temp_dir = Get_default_temp_dir();
605 const std::string file_separator = Get_file_separator();
606 const std::vector<std::string>bbs_paths = Get_bbs_paths();
607 const std::vector<std::string>package_paths = Get_package_paths();
609 bbtkMessage("Help",level, "=============" << std::endl);
610 bbtkMessage("Help",level, "Configuration" << std::endl);
611 bbtkMessage("Help",level, "=============" << std::endl);
612 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
613 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
614 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
615 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
616 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
618 std::vector<std::string>::const_iterator i;
620 bbtkMessage("Help",level, "BBS Paths " << std::endl);
621 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
623 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
626 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
627 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
629 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
632 bbtkDebugDecTab("Config",9);
634 //=========================================================================