1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2009/03/23 13:06:41 $
6 Version: $Revision: 1.29 $
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 + "/..";
73 mBin_path = mBin_path + "/../../..";
75 mInstall_path = mBin_path + "/..";
79 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
80 // mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
81 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
82 // mDoc_path = mInstall_path + "/" + mDoc_rel_path;
83 // Have to create bbtk user's dirs if needed
85 // LG 27/01/09 : doc is now user dependent
86 // and located in user's .bbtk dir /doc
87 // All dirs under .bbtk user's dir are set and created by:
88 InitializeDotBbtkStructure();
90 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
91 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
92 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
93 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
95 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
97 Utilities::replace( mBin_path,
98 INVALID_FILE_SEPARATOR,
99 VALID_FILE_SEPARATOR);
100 Utilities::replace( mInstall_path,
101 INVALID_FILE_SEPARATOR,
102 VALID_FILE_SEPARATOR);
103 Utilities::replace( mBbs_path,
104 INVALID_FILE_SEPARATOR,
105 VALID_FILE_SEPARATOR);
106 Utilities::replace( mData_path,
107 INVALID_FILE_SEPARATOR,
108 VALID_FILE_SEPARATOR);
110 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
111 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
112 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
113 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
114 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
120 // always add "." (current working directory) at the begining
121 mBbs_paths.push_back( "." );
122 // add system bbs path
123 mBbs_paths.push_back(mBbs_path);
124 // add toolsbbtk/appli
125 //EED std::string toolsappli_rel_path(mFile_separator);
126 std::string toolsappli_rel_path("/");
127 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
128 toolsappli_rel_path += "toolsbbtk/appli";
130 //-----------------------------------------------------------
131 // LG : REMOVE BUGGY PATH WITH include *:
133 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
134 //-----------------------------------------------------------
135 int iStrVec,sizeStrVec;
137 sizeStrVec = mBbs_paths.size();
138 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
139 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
143 // always add "." (current working directory) at the begining
144 mPackage_paths.push_back(".");
145 // add system bin path (for build tree / standalone folder install)
146 mPackage_paths.push_back(mBin_path);
147 // add system lib path (for install tree)
148 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
149 mPackage_paths.push_back(mInstall_path + "/lib");
151 // add bin/Debug bin/Release paths (for build/install tree)
152 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
153 mPackage_paths.push_back(mBin_path + "/Debug");
154 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
155 mPackage_paths.push_back(mBin_path + "/Release");
158 sizeStrVec = mPackage_paths.size();
159 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
160 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
167 // ==> First we look for bbtk_config.xml in "."
169 const char *currentDir = getcwd(buf, 2048);
173 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
174 // \todo : what else?
175 // How abort a constructor and warn the caller function?
176 // LG : throw an exception
179 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
180 std::string configXmlFullPathName = currentDir ;
181 configXmlFullPathName += "/bbtk_config.xml";
182 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
184 if ( Utilities::FileExists( configXmlFullPathName ))
186 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
187 "] found in current directory" << std::endl);
188 //Read(configXmlFullPathName.c_str());
189 // traiter le fichier local
192 // ==> Then we look for bbtk_config.xml in ".bbtk"
195 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
196 if (!Utilities::FileExists( configXmlFullPathName ))
198 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
203 // In any case, deal with bbtk_config.xml!
204 Read(configXmlFullPathName.c_str());
206 //=========================================================================
208 //=========================================================================
210 ConfigurationFile::~ConfigurationFile()
213 //=========================================================================
215 //=========================================================================
216 void ConfigurationFile::InitializeDotBbtkStructure()
218 mDot_bbtk_path = Utilities::GetUserSettingsDir();
219 mDot_bbtk_is_new = false;
220 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
221 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
223 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
224 Utilities::CreateDirectoryIfNeeded(mDoc_path);
226 std::string bbdoc_path =
227 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
228 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
230 // Create help_contents.html if does not exist
231 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
232 if (!Utilities::FileExists(filename))
234 bbtkDebugMessage("Config",1,
235 "* Creating [" << filename << "]" << std::endl);
236 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
237 std::string doc_path = mInstall_path + "/"
238 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
239 Utilities::MakeValidFileName(doc_path);
241 f.open(filename.c_str(), std::ios::out );
242 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
243 << "HTML 4.01 Transitional//EN\">"
244 << "<html><head><title>Help Contents - bbtk "
245 << GetVersion() << "</title>"
246 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
247 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
248 << "<a href=\"bbdoc/index-category.html#demo\" "
249 << "target=\"information\">Demos</a><br>"
250 << "<a href=\"bbdoc/index-category.html#example\" "
251 << "target=\"information\">Examples</a>"
255 << "bbtkUsersGuide/bbtkUsersGuide.html"
256 << "\" target=\"information\">User's Guide</a><br>"
259 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.html"
260 << "\" target=\"information\">Package Developer's Guide</a><br>"
263 << "doxygen/bbtk/main.html"
264 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
266 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
267 << "Alphabetical list</a><br>"
268 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
269 << "List by package</a><br>"
270 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
271 << "List by category</a><br>"
272 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
273 << "List of adaptors</a><br>"
279 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
280 Utilities::CreateDirectoryIfNeeded(mTemp_path);
283 //=========================================================================
285 //=========================================================================
286 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
290 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
291 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
292 fp = fopen (configXml, "w");
293 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
294 fprintf(fp, "<config>\n");
295 fprintf(fp, " <bbs_path> </bbs_path>\n");
296 fprintf(fp, " <package_path> </package_path>\n");
297 fprintf(fp, "</config>\n");
300 //=========================================================================
305 //=========================================================================
306 void ConfigurationFile::InstallPath ()
309 /*--------------------------------------------------
310 New policy for bbtk_config.xml :
312 if bbtk_config.xml found in current directory (user is an aware user!)
315 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
318 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
319 copy it as .bbtk/bbtk_config.xml
321 else (nothing installed)
322 create a minimum version in HOME/.bbtk
323 ----------------------------------------------------*/
326 // -----------------------------------------------------------------
327 #if defined(__GNUC__)
329 // ------------------ create some usefull strings ----------------
330 // installed bbtk_path
332 strcpy(bbtk_path, "/usr/local/bin");
335 char rootDirectory[200];
336 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
339 char configPath[200];
340 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
343 char configXmlTmp[250];
344 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
349 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
351 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
352 CreateConfigXML( rootDirectory );// create .bbtk
356 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
357 if (!Utilities::FileExists(configPath))
364 // ------------------------------------------------------------------
368 // installed bbtk_path
370 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
371 char bbtk_path2[100];
372 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
375 char rootDirectory[200];
376 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
377 // std::cout << "[" << rootDirectory << "]" << std::endl;
380 char configPath[200];
381 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
385 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
388 char configXmlTmp[250];
389 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
394 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
396 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
397 CreateConfigXML( rootDirectory );// create .bbtk
401 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
403 int attribs = GetFileAttributes (rootDirectory);
404 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
405 if ( attribs != 0xFFFFFFFF)
407 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
409 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
421 // ------------------------------------------------------------------
423 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
429 //=========================================================================
433 //=========================================================================
434 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
437 void ConfigurationFile::Read(const std::string& filename)
440 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
442 mConfig_xml_full_path = filename;
443 XMLResults* res = new XMLResults;
444 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
446 if ( res->error != eXMLErrorNone )
448 std::string mess = GetErrorMessage(res,filename);
450 bbtkDebugMessage("Config",1,mess<< std::endl);
455 bbtkDebugMessage("Config",1,"OK" << std::endl);
460 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
463 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
468 if( BB.nChildNode((XMLCSTR)"url") )
469 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
472 if( BB.nChildNode((XMLCSTR)"data_path") )
473 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
476 // if( BB.nChildNode((XMLCSTR)"install_path") )
477 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
479 // add user bbs paths
480 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
483 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
484 mBbs_paths.push_back(val);
489 // add user package path
490 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
493 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
494 mPackage_paths.push_back(val);
499 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
500 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
502 if ( mDefault_temp_dir == "$") // no value found in config_xml
504 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
505 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
511 //=========================================================================
513 //=========================================================================
514 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
516 bbtkDebugMessageInc("Config",9,
517 "ConfigurationFile::AddPackagePathsAndWrite("
518 <<path<<")"<<std::endl);
520 XMLResults* res = new XMLResults;
522 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
523 (XMLCSTR)"config",res);
525 if ( res->error != eXMLErrorNone )
527 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
529 bbtkDebugMessage("Config",1,mess<< std::endl);
535 std::string bbs_path = path + "/bbs";
537 std::string bbs_path = path + "/share/bbtk/bbs" ;
539 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
540 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
541 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
542 mBbs_paths.push_back(bbs_path);
545 std::string pack_path = path + "/bin";
547 std::string pack_path = path ;
549 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
550 PACKPATH.addText((XMLCSTR)pack_path.c_str());
551 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
552 mPackage_paths.push_back(pack_path);
555 pack_path = path + "/RelWithDebInfo";
556 PACKPATH = BB.addChild((XMLCSTR)"package_path");
557 PACKPATH.addText((XMLCSTR)pack_path.c_str());
558 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
559 mPackage_paths.push_back(pack_path);
560 pack_path = path + "/Debug";
561 PACKPATH = BB.addChild((XMLCSTR)"package_path");
562 PACKPATH.addText((XMLCSTR)pack_path.c_str());
563 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
564 mPackage_paths.push_back(pack_path);
565 pack_path = path + "/Release";
566 PACKPATH = BB.addChild((XMLCSTR)"package_path");
567 PACKPATH.addText((XMLCSTR)pack_path.c_str());
568 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
569 mPackage_paths.push_back(pack_path);
573 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
574 if ( err != eXMLErrorNone )
576 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
577 bbtkDebugMessage("Config",1,mess<< std::endl);
583 //=========================================================================
585 //=========================================================================
586 void ConfigurationFile::GetHelp(int level) const
588 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
591 const std::string config_xml_full_path = Get_config_xml_full_path();
592 const std::string description = Get_description();
593 const std::string url = Get_doc_path();
594 const std::string data_path = Get_data_path();
595 const std::string default_temp_dir = Get_default_temp_dir();
596 const std::string file_separator = Get_file_separator();
597 const std::vector<std::string>bbs_paths = Get_bbs_paths();
598 const std::vector<std::string>package_paths = Get_package_paths();
600 bbtkMessage("Help",level, "=============" << std::endl);
601 bbtkMessage("Help",level, "Configuration" << std::endl);
602 bbtkMessage("Help",level, "=============" << std::endl);
603 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
604 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
605 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
606 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
607 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
609 std::vector<std::string>::const_iterator i;
611 bbtkMessage("Help",level, "BBS Paths " << std::endl);
612 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
614 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
617 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
618 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
620 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
623 bbtkDebugDecTab("Config",9);
625 //=========================================================================