1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2009/01/28 13:20:32 $
6 Version: $Revision: 1.28 $
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();
63 mInstall_path = mBin_path + "/../../../..";
65 mInstall_path = mBin_path + "/..";
69 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
70 // mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
71 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
72 // mDoc_path = mInstall_path + "/" + mDoc_rel_path;
73 // Have to create bbtk user's dirs if needed
75 // LG 27/01/09 : doc is now user dependent
76 // and located in user's .bbtk dir /doc
77 // All dirs under .bbtk user's dir are set and created by:
78 InitializeDotBbtkStructure();
80 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
81 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
82 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
83 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
85 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
87 Utilities::replace( mBin_path,
88 INVALID_FILE_SEPARATOR,
89 VALID_FILE_SEPARATOR);
90 Utilities::replace( mInstall_path,
91 INVALID_FILE_SEPARATOR,
92 VALID_FILE_SEPARATOR);
93 Utilities::replace( mBbs_path,
94 INVALID_FILE_SEPARATOR,
95 VALID_FILE_SEPARATOR);
96 Utilities::replace( mData_path,
97 INVALID_FILE_SEPARATOR,
98 VALID_FILE_SEPARATOR);
100 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
101 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
102 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
103 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
104 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
110 // always add "." (current working directory) at the begining
111 mBbs_paths.push_back( "." );
112 // add system bbs path
113 mBbs_paths.push_back(mBbs_path);
114 // add toolsbbtk/appli
115 //EED std::string toolsappli_rel_path(mFile_separator);
116 std::string toolsappli_rel_path("/");
117 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
118 toolsappli_rel_path += "toolsbbtk/appli";
120 //-----------------------------------------------------------
121 // LG : REMOVE BUGGY PATH WITH include *:
123 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
124 //-----------------------------------------------------------
125 int iStrVec,sizeStrVec;
127 sizeStrVec = mBbs_paths.size();
128 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
129 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
133 // always add "." (current working directory) at the begining
134 mPackage_paths.push_back(".");
135 // add system bin path (for build tree / standalone folder install)
136 mPackage_paths.push_back(mBin_path);
137 // add system lib path (for install tree)
138 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
139 mPackage_paths.push_back(mInstall_path + "/lib");
141 // add bin/Debug bin/Release paths (for build/install tree)
142 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
143 mPackage_paths.push_back(mBin_path + "/Debug");
144 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
145 mPackage_paths.push_back(mBin_path + "/Release");
148 sizeStrVec = mPackage_paths.size();
149 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
150 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
157 // ==> First we look for bbtk_config.xml in "."
159 const char *currentDir = getcwd(buf, 2048);
163 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
164 // \todo : what else?
165 // How abort a constructor and warn the caller function?
166 // LG : throw an exception
169 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
170 std::string configXmlFullPathName = currentDir ;
171 configXmlFullPathName += "/bbtk_config.xml";
172 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
174 if ( Utilities::FileExists( configXmlFullPathName ))
176 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
177 "] found in current directory" << std::endl);
178 //Read(configXmlFullPathName.c_str());
179 // traiter le fichier local
182 // ==> Then we look for bbtk_config.xml in ".bbtk"
185 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
186 if (!Utilities::FileExists( configXmlFullPathName ))
188 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
193 // In any case, deal with bbtk_config.xml!
194 Read(configXmlFullPathName.c_str());
196 //=========================================================================
198 //=========================================================================
200 ConfigurationFile::~ConfigurationFile()
203 //=========================================================================
205 //=========================================================================
206 void ConfigurationFile::InitializeDotBbtkStructure()
208 mDot_bbtk_path = Utilities::GetUserSettingsDir();
209 mDot_bbtk_is_new = false;
210 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
211 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
213 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
214 Utilities::CreateDirectoryIfNeeded(mDoc_path);
216 std::string bbdoc_path =
217 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
218 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
220 // Create help_contents.html if does not exist
221 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
222 if (!Utilities::FileExists(filename))
224 bbtkDebugMessage("Config",1,
225 "* Creating [" << filename << "]" << std::endl);
226 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
227 std::string doc_path = mInstall_path + "/"
228 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
229 Utilities::MakeValidFileName(doc_path);
231 f.open(filename.c_str(), std::ios::out );
232 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
233 << "HTML 4.01 Transitional//EN\">"
234 << "<html><head><title>Help Contents - bbtk "
235 << GetVersion() << "</title>"
236 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
237 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
238 << "<a href=\"bbdoc/index-category.html#demo\" "
239 << "target=\"information\">Demos</a><br>"
240 << "<a href=\"bbdoc/index-category.html#example\" "
241 << "target=\"information\">Examples</a>"
245 << "bbtkUsersGuide/bbtkUsersGuide.html"
246 << "\" target=\"information\">User's Guide</a><br>"
249 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.html"
250 << "\" target=\"information\">Package Developer's Guide</a><br>"
253 << "doxygen/bbtk/main.html"
254 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
256 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
257 << "Alphabetical list</a><br>"
258 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
259 << "List by package</a><br>"
260 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
261 << "List by category</a><br>"
262 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
263 << "List of adaptors</a><br>"
269 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
270 Utilities::CreateDirectoryIfNeeded(mTemp_path);
273 //=========================================================================
275 //=========================================================================
276 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
280 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
281 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
282 fp = fopen (configXml, "w");
283 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
284 fprintf(fp, "<config>\n");
285 fprintf(fp, " <bbs_path> </bbs_path>\n");
286 fprintf(fp, " <package_path> </package_path>\n");
287 fprintf(fp, "</config>\n");
290 //=========================================================================
295 //=========================================================================
296 void ConfigurationFile::InstallPath ()
299 /*--------------------------------------------------
300 New policy for bbtk_config.xml :
302 if bbtk_config.xml found in current directory (user is an aware user!)
305 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
308 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
309 copy it as .bbtk/bbtk_config.xml
311 else (nothing installed)
312 create a minimum version in HOME/.bbtk
313 ----------------------------------------------------*/
316 // -----------------------------------------------------------------
317 #if defined(__GNUC__)
319 // ------------------ create some usefull strings ----------------
320 // installed bbtk_path
322 strcpy(bbtk_path, "/usr/local/bin");
325 char rootDirectory[200];
326 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
329 char configPath[200];
330 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
333 char configXmlTmp[250];
334 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
339 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
341 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
342 CreateConfigXML( rootDirectory );// create .bbtk
346 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
347 if (!Utilities::FileExists(configPath))
354 // ------------------------------------------------------------------
358 // installed bbtk_path
360 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
361 char bbtk_path2[100];
362 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
365 char rootDirectory[200];
366 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
367 // std::cout << "[" << rootDirectory << "]" << std::endl;
370 char configPath[200];
371 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
375 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
378 char configXmlTmp[250];
379 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
384 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
386 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
387 CreateConfigXML( rootDirectory );// create .bbtk
391 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
393 int attribs = GetFileAttributes (rootDirectory);
394 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
395 if ( attribs != 0xFFFFFFFF)
397 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
399 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
411 // ------------------------------------------------------------------
413 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
419 //=========================================================================
423 //=========================================================================
424 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
427 void ConfigurationFile::Read(const std::string& filename)
430 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
432 mConfig_xml_full_path = filename;
433 XMLResults* res = new XMLResults;
434 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
436 if ( res->error != eXMLErrorNone )
438 std::string mess = GetErrorMessage(res,filename);
440 bbtkDebugMessage("Config",1,mess<< std::endl);
445 bbtkDebugMessage("Config",1,"OK" << std::endl);
450 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
453 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
458 if( BB.nChildNode((XMLCSTR)"url") )
459 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
462 if( BB.nChildNode((XMLCSTR)"data_path") )
463 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
466 // if( BB.nChildNode((XMLCSTR)"install_path") )
467 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
469 // add user bbs paths
470 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
473 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
474 mBbs_paths.push_back(val);
479 // add user package path
480 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
483 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
484 mPackage_paths.push_back(val);
489 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
490 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
492 if ( mDefault_temp_dir == "$") // no value found in config_xml
494 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
495 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
501 //=========================================================================
503 //=========================================================================
504 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
506 bbtkDebugMessageInc("Config",9,
507 "ConfigurationFile::AddPackagePathsAndWrite("
508 <<path<<")"<<std::endl);
510 XMLResults* res = new XMLResults;
512 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
513 (XMLCSTR)"config",res);
515 if ( res->error != eXMLErrorNone )
517 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
519 bbtkDebugMessage("Config",1,mess<< std::endl);
525 std::string bbs_path = path + "/bbs";
527 std::string bbs_path = path + "/share/bbtk/bbs" ;
529 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
530 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
531 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
532 mBbs_paths.push_back(bbs_path);
535 std::string pack_path = path + "/bin";
537 std::string pack_path = path ;
539 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
540 PACKPATH.addText((XMLCSTR)pack_path.c_str());
541 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
542 mPackage_paths.push_back(pack_path);
545 pack_path = path + "/RelWithDebInfo";
546 PACKPATH = BB.addChild((XMLCSTR)"package_path");
547 PACKPATH.addText((XMLCSTR)pack_path.c_str());
548 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
549 mPackage_paths.push_back(pack_path);
550 pack_path = path + "/Debug";
551 PACKPATH = BB.addChild((XMLCSTR)"package_path");
552 PACKPATH.addText((XMLCSTR)pack_path.c_str());
553 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
554 mPackage_paths.push_back(pack_path);
555 pack_path = path + "/Release";
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);
563 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
564 if ( err != eXMLErrorNone )
566 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
567 bbtkDebugMessage("Config",1,mess<< std::endl);
573 //=========================================================================
575 //=========================================================================
576 void ConfigurationFile::GetHelp(int level) const
578 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
581 const std::string config_xml_full_path = Get_config_xml_full_path();
582 const std::string description = Get_description();
583 const std::string url = Get_doc_path();
584 const std::string data_path = Get_data_path();
585 const std::string default_temp_dir = Get_default_temp_dir();
586 const std::string file_separator = Get_file_separator();
587 const std::vector<std::string>bbs_paths = Get_bbs_paths();
588 const std::vector<std::string>package_paths = Get_package_paths();
590 bbtkMessage("Help",level, "=============" << std::endl);
591 bbtkMessage("Help",level, "Configuration" << std::endl);
592 bbtkMessage("Help",level, "=============" << std::endl);
593 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
594 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
595 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
596 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
597 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
599 std::vector<std::string>::const_iterator i;
601 bbtkMessage("Help",level, "BBS Paths " << std::endl);
602 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
604 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
607 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
608 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
610 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
613 bbtkDebugDecTab("Config",9);
615 //=========================================================================