1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2009/01/27 14:22:56 $
6 Version: $Revision: 1.26 $
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
46 #if defined(MACOSX) // assume this is OSX
47 # include <sys/param.h>
48 # include <mach-o/dyld.h> // _NSGetExecutablePath : must add -framework CoreFoundation to link line
51 # define PATH_MAX MAXPATHLEN
55 #ifndef PATH_MAX // If not defined yet : do it
56 # define PATH_MAX 2048
63 //====================================================================
65 ConfigurationFile::ConfigurationFile()
68 mFile_separator = VALID_FILE_SEPARATOR;
70 // ==> Set system paths
71 mBin_path = Utilities::GetExecutablePath();
74 mInstall_path = mBin_path + "/../../../..";
76 mInstall_path = mBin_path + "/..";
80 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
81 // mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
82 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
83 // mDoc_path = mInstall_path + "/" + mDoc_rel_path;
84 // Have to create bbtk user's dirs if needed
86 // LG 27/01/09 : doc is now user dependent
87 // and located in user's .bbtk dir /doc
88 // All dirs under .bbtk user's dir are set and created by:
89 InitializeDotBbtkStructure();
91 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
92 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
93 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
94 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
96 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
98 Utilities::replace( mBin_path,
99 INVALID_FILE_SEPARATOR,
100 VALID_FILE_SEPARATOR);
101 Utilities::replace( mInstall_path,
102 INVALID_FILE_SEPARATOR,
103 VALID_FILE_SEPARATOR);
104 Utilities::replace( mBbs_path,
105 INVALID_FILE_SEPARATOR,
106 VALID_FILE_SEPARATOR);
107 Utilities::replace( mData_path,
108 INVALID_FILE_SEPARATOR,
109 VALID_FILE_SEPARATOR);
111 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
112 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
113 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
114 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
115 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
121 // always add "." (current working directory) at the begining
122 mBbs_paths.push_back( "." );
123 // add system bbs path
124 mBbs_paths.push_back(mBbs_path);
125 // add toolsbbtk/appli
126 //EED std::string toolsappli_rel_path(mFile_separator);
127 std::string toolsappli_rel_path("/");
128 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
129 toolsappli_rel_path += "toolsbbtk/appli";
131 //-----------------------------------------------------------
132 // LG : REMOVE BUGGY PATH WITH include *:
134 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
135 //-----------------------------------------------------------
136 int iStrVec,sizeStrVec;
138 sizeStrVec = mBbs_paths.size();
139 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
140 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
144 // always add "." (current working directory) at the begining
145 mPackage_paths.push_back(".");
146 // add system bin path (for build tree / standalone folder install)
147 mPackage_paths.push_back(mBin_path);
148 // add system lib path (for install tree)
149 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
150 mPackage_paths.push_back(mInstall_path + "/lib");
152 // add bin/Debug bin/Release paths (for build/install tree)
153 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
154 mPackage_paths.push_back(mBin_path + "/Debug");
155 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
156 mPackage_paths.push_back(mBin_path + "/Release");
159 sizeStrVec = mPackage_paths.size();
160 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
161 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
168 // ==> First we look for bbtk_config.xml in "."
170 const char *currentDir = getcwd(buf, 2048);
174 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
175 // \todo : what else?
176 // How abort a constructor and warn the caller function?
177 // LG : throw an exception
180 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
181 std::string configXmlFullPathName = currentDir ;
182 configXmlFullPathName += "/bbtk_config.xml";
183 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
185 if ( Utilities::FileExists( configXmlFullPathName ))
187 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
188 "] found in current directory" << std::endl);
189 //Read(configXmlFullPathName.c_str());
190 // traiter le fichier local
193 // ==> Then we look for bbtk_config.xml in ".bbtk"
196 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
197 if (!Utilities::FileExists( configXmlFullPathName ))
199 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
204 // In any case, deal with bbtk_config.xml!
205 Read(configXmlFullPathName.c_str());
207 //=========================================================================
209 //=========================================================================
211 ConfigurationFile::~ConfigurationFile()
214 //=========================================================================
216 //=========================================================================
217 void ConfigurationFile::InitializeDotBbtkStructure()
219 mDot_bbtk_path = Utilities::GetUserSettingsDir();
220 mDot_bbtk_is_new = false;
221 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
222 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
224 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
225 Utilities::CreateDirectoryIfNeeded(mDoc_path);
227 std::string bbdoc_path =
228 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
229 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
231 // Create help_contents.html if does not exist
232 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
233 if (!Utilities::FileExists(filename))
235 bbtkDebugMessage("Config",1,
236 "* Creating [" << filename << "]" << std::endl);
237 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
238 std::string doc_path = mInstall_path + "/"
239 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
240 Utilities::MakeValidFileName(doc_path);
242 f.open(filename.c_str(), std::ios::out );
243 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
244 << "HTML 4.01 Transitional//EN\">"
245 << "<html><head><title>Help Contents - bbtk "
246 << GetVersion() << "</title>"
247 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
248 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
249 << "<a href=\"bbdoc/index-category.html#demo\" "
250 << "target=\"information\">Demos</a><br>"
251 << "<a href=\"bbdoc/index-category.html#example\" "
252 << "target=\"information\">Examples</a>"
256 << "bbtkUsersGuide/bbtkUsersGuide.html"
257 << "\" target=\"information\">User's Guide</a><br>"
260 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.html"
261 << "\" target=\"information\">Package Developer's Guide</a><br>"
264 << "doxygen/bbtk/main.html"
265 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
267 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
268 << "Alphabetical list</a><br>"
269 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
270 << "List by package</a><br>"
271 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
272 << "List by category</a><br>"
273 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
274 << "List of adaptors</a><br>"
280 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
281 Utilities::CreateDirectoryIfNeeded(mTemp_path);
284 //=========================================================================
286 //=========================================================================
287 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
291 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
292 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
293 fp = fopen (configXml, "w");
294 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
295 fprintf(fp, "<config>\n");
296 fprintf(fp, " <bbs_path> </bbs_path>\n");
297 fprintf(fp, " <package_path> </package_path>\n");
298 fprintf(fp, "</config>\n");
301 //=========================================================================
306 //=========================================================================
307 void ConfigurationFile::InstallPath ()
310 /*--------------------------------------------------
311 New policy for bbtk_config.xml :
313 if bbtk_config.xml found in current directory (user is an aware user!)
316 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
319 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
320 copy it as .bbtk/bbtk_config.xml
322 else (nothing installed)
323 create a minimum version in HOME/.bbtk
324 ----------------------------------------------------*/
327 // -----------------------------------------------------------------
328 #if defined(__GNUC__)
330 // ------------------ create some usefull strings ----------------
331 // installed bbtk_path
333 strcpy(bbtk_path, "/usr/local/bin");
336 char rootDirectory[200];
337 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
340 char configPath[200];
341 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
344 char configXmlTmp[250];
345 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
350 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
352 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
353 CreateConfigXML( rootDirectory );// create .bbtk
357 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
358 if (!Utilities::FileExists(configPath))
365 // ------------------------------------------------------------------
369 // installed bbtk_path
371 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
372 char bbtk_path2[100];
373 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
376 char rootDirectory[200];
377 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
378 // std::cout << "[" << rootDirectory << "]" << std::endl;
381 char configPath[200];
382 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
386 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
389 char configXmlTmp[250];
390 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
395 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
397 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
398 CreateConfigXML( rootDirectory );// create .bbtk
402 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
404 int attribs = GetFileAttributes (rootDirectory);
405 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
406 if ( attribs != 0xFFFFFFFF)
408 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
410 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
422 // ------------------------------------------------------------------
424 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
430 //=========================================================================
434 //=========================================================================
435 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
438 void ConfigurationFile::Read(const std::string& filename)
441 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
443 mConfig_xml_full_path = filename;
444 XMLResults* res = new XMLResults;
445 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
447 if ( res->error != eXMLErrorNone )
449 std::string mess = GetErrorMessage(res,filename);
451 bbtkDebugMessage("Config",1,mess<< std::endl);
456 bbtkDebugMessage("Config",1,"OK" << std::endl);
461 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
464 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
469 if( BB.nChildNode((XMLCSTR)"url") )
470 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
473 if( BB.nChildNode((XMLCSTR)"data_path") )
474 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
477 // if( BB.nChildNode((XMLCSTR)"install_path") )
478 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
480 // add user bbs paths
481 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
484 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
485 mBbs_paths.push_back(val);
490 // add user package path
491 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
494 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
495 mPackage_paths.push_back(val);
500 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
501 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
503 if ( mDefault_temp_dir == "$") // no value found in config_xml
505 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
506 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
512 //=========================================================================
514 //=========================================================================
515 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
517 bbtkDebugMessageInc("Config",9,
518 "ConfigurationFile::AddPackagePathsAndWrite("
519 <<path<<")"<<std::endl);
521 XMLResults* res = new XMLResults;
523 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
524 (XMLCSTR)"config",res);
526 if ( res->error != eXMLErrorNone )
528 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
530 bbtkDebugMessage("Config",1,mess<< std::endl);
536 std::string bbs_path = path + "/bbs";
538 std::string bbs_path = path + "/share/bbtk/bbs" ;
540 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
541 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
542 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
543 mBbs_paths.push_back(bbs_path);
546 std::string pack_path = path + "/bin";
548 std::string pack_path = path ;
550 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
551 PACKPATH.addText((XMLCSTR)pack_path.c_str());
552 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
553 mPackage_paths.push_back(pack_path);
556 pack_path = path + "/Debug";
557 PACKPATH = BB.addChild((XMLCSTR)"package_path");
558 PACKPATH.addText((XMLCSTR)pack_path.c_str());
559 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
560 mPackage_paths.push_back(pack_path);
561 pack_path = path + "/Release";
562 PACKPATH = BB.addChild((XMLCSTR)"package_path");
563 PACKPATH.addText((XMLCSTR)pack_path.c_str());
564 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
565 mPackage_paths.push_back(pack_path);
569 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
570 if ( err != eXMLErrorNone )
572 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
573 bbtkDebugMessage("Config",1,mess<< std::endl);
579 //=========================================================================
581 //=========================================================================
582 void ConfigurationFile::GetHelp(int level) const
584 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
587 const std::string config_xml_full_path = Get_config_xml_full_path();
588 const std::string description = Get_description();
589 const std::string url = Get_doc_path();
590 const std::string data_path = Get_data_path();
591 const std::string default_temp_dir = Get_default_temp_dir();
592 const std::string file_separator = Get_file_separator();
593 const std::vector<std::string>bbs_paths = Get_bbs_paths();
594 const std::vector<std::string>package_paths = Get_package_paths();
596 bbtkMessage("Help",level, "=============" << std::endl);
597 bbtkMessage("Help",level, "Configuration" << std::endl);
598 bbtkMessage("Help",level, "=============" << std::endl);
599 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
600 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
601 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
602 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
603 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
605 std::vector<std::string>::const_iterator i;
607 bbtkMessage("Help",level, "BBS Paths " << std::endl);
608 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
610 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
613 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
614 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
616 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
619 bbtkDebugDecTab("Config",9);
621 //=========================================================================