1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2010/11/02 10:15:43 $
6 Version: $Revision: 1.34 $
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/creatools");
159 mPackage_paths.push_back(mInstall_path + "/lib64/creatools");
161 // add bin/Debug bin/Release paths (for build/install tree)
162 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
163 mPackage_paths.push_back(mBin_path + "/Debug");
164 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
165 mPackage_paths.push_back(mBin_path + "/Release");
168 sizeStrVec = mPackage_paths.size();
169 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
170 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
177 // ==> First we look for bbtk_config.xml in "."
179 const char *currentDir = getcwd(buf, 2048);
183 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
184 // \todo : what else?
185 // How abort a constructor and warn the caller function?
186 // LG : throw an exception
189 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
190 std::string configXmlFullPathName = currentDir ;
191 configXmlFullPathName += "/bbtk_config.xml";
192 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
194 if ( Utilities::FileExists( configXmlFullPathName ))
196 bbtkMessage("config",1, "ConfigurationFile : [" << configXmlFullPathName <<
197 "] found in current directory" << std::endl);
198 //Read(configXmlFullPathName.c_str());
199 // traiter le fichier local
202 // ==> Then we look for bbtk_config.xml in ".bbtk"
205 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
206 if (!Utilities::FileExists( configXmlFullPathName ))
208 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
213 // In any case, deal with bbtk_config.xml!
214 Read(configXmlFullPathName.c_str());
216 //=========================================================================
218 //=========================================================================
220 ConfigurationFile::~ConfigurationFile()
223 //=========================================================================
225 //=========================================================================
226 void ConfigurationFile::InitializeDotBbtkStructure()
228 mDot_bbtk_path = Utilities::GetUserSettingsDir();
229 mDot_bbtk_is_new = false;
230 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
231 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
233 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
234 Utilities::CreateDirectoryIfNeeded(mDoc_path);
236 std::string bbdoc_path =
237 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
238 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
240 // Create help_contents.html if does not exist
241 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
242 if (!Utilities::FileExists(filename))
244 bbtkDebugMessage("config",1,
245 "* Creating [" << filename << "]" << std::endl);
246 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
247 std::string doc_path = mInstall_path + "/"
248 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
249 Utilities::MakeValidFileName(doc_path);
253 f.open(filename.c_str(), std::ios::out );
254 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
255 << "HTML 4.01 Transitional//EN\">"
256 << "<html><head><title>Help Contents - bbtk "
257 << GetVersion() << "</title>"
258 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
259 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
260 << "<a href=\"bbdoc/index-category.html#demo\" "
261 << "target=\"information\">Demos</a><br>"
262 << "<a href=\"bbdoc/index-category.html#example\" "
263 << "target=\"information\">Examples</a>"
267 << "bbtkUsersGuide/bbtkUsersGuide.pdf"
268 << "\" target=\"information\">User's Guide</a><br>"
271 << "bbtkDevelopersGuide/bbtkDevelopersGuide.pdf"
272 << "\" target=\"information\">Developer's Guide</a><br>"
275 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.pdf"
276 << "\" target=\"information\">Package Developer's Guide</a><br>"
279 << "doxygen/bbtk/main.html"
280 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
282 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
283 << "Alphabetical list</a><br>"
284 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
285 << "List by package</a><br>"
286 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
287 << "List by category</a><br>"
288 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
289 << "List of adaptors</a><br>"
294 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
295 Utilities::CreateDirectoryIfNeeded(mTemp_path);
298 //=========================================================================
300 //=========================================================================
301 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
305 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
306 bbtkDebugMessage("config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
307 fp = fopen (configXml, "w");
308 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
309 fprintf(fp, "<config>\n");
310 fprintf(fp, " <bbs_path> </bbs_path>\n");
311 fprintf(fp, " <package_path> </package_path>\n");
312 fprintf(fp, "</config>\n");
315 //=========================================================================
320 //=========================================================================
321 void ConfigurationFile::InstallPath ()
324 /*--------------------------------------------------
325 New policy for bbtk_config.xml :
327 if bbtk_config.xml found in current directory (user is an aware user!)
330 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
333 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
334 copy it as .bbtk/bbtk_config.xml
336 else (nothing installed)
337 create a minimum version in HOME/.bbtk
338 ----------------------------------------------------*/
341 // -----------------------------------------------------------------
342 #if defined(__GNUC__)
344 // ------------------ create some usefull strings ----------------
345 // installed bbtk_path
347 strcpy(bbtk_path, "/usr/local/bin");
350 char rootDirectory[200];
351 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
354 char configPath[200];
355 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
358 char configXmlTmp[250];
359 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
364 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
366 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
367 CreateConfigXML( rootDirectory );// create .bbtk
371 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
372 if (!Utilities::FileExists(configPath))
379 // ------------------------------------------------------------------
383 // installed bbtk_path
385 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
386 char bbtk_path2[100];
387 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
390 char rootDirectory[200];
391 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
392 // std::cout << "[" << rootDirectory << "]" << std::endl;
395 char configPath[200];
396 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
400 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
403 char configXmlTmp[250];
404 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
409 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
411 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
412 CreateConfigXML( rootDirectory );// create .bbtk
416 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
418 int attribs = GetFileAttributes (rootDirectory);
419 bbtkMessage("config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
420 if ( attribs != 0xFFFFFFFF)
422 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
424 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
436 // ------------------------------------------------------------------
438 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
444 //=========================================================================
448 //=========================================================================
449 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
452 void ConfigurationFile::Read(const std::string& filename)
455 bbtkDebugMessage("config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
457 mConfig_xml_full_path = filename;
458 XMLResults* res = new XMLResults;
459 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
461 if ( res->error != eXMLErrorNone )
463 std::string mess = GetErrorMessage(res,filename);
465 bbtkDebugMessage("config",1,mess<< std::endl);
470 bbtkDebugMessage("config",1,"OK" << std::endl);
475 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
478 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
483 if( BB.nChildNode((XMLCSTR)"url") )
484 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
487 if( BB.nChildNode((XMLCSTR)"data_path") )
488 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
491 // if( BB.nChildNode((XMLCSTR)"install_path") )
492 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
494 // add user bbs paths
495 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
498 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
499 mBbs_paths.push_back(val);
504 // add user package path
505 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
508 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
509 mPackage_paths.push_back(val);
514 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
515 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
517 if ( mDefault_temp_dir == "$") // no value found in config_xml
519 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
520 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
526 //=========================================================================
528 //=========================================================================
529 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
531 bbtkDebugMessageInc("config",9,
532 "ConfigurationFile::AddPackagePathsAndWrite("
533 <<path<<")"<<std::endl);
535 XMLResults* res = new XMLResults;
537 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
538 (XMLCSTR)"config",res);
540 if ( res->error != eXMLErrorNone )
542 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
544 bbtkDebugMessage("config",1,mess<< std::endl);
550 std::string bbs_path = path + "/bbs";
552 std::string bbs_path = path + "/share/bbtk/bbs" ;
554 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
555 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
556 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
557 mBbs_paths.push_back(bbs_path);
560 std::string pack_path = path + "/bin";
562 std::string pack_path = path ;
564 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
565 PACKPATH.addText((XMLCSTR)pack_path.c_str());
566 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
567 mPackage_paths.push_back(pack_path);
570 pack_path = path + "/RelWithDebInfo";
571 PACKPATH = BB.addChild((XMLCSTR)"package_path");
572 PACKPATH.addText((XMLCSTR)pack_path.c_str());
573 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
574 mPackage_paths.push_back(pack_path);
575 pack_path = path + "/Debug";
576 PACKPATH = BB.addChild((XMLCSTR)"package_path");
577 PACKPATH.addText((XMLCSTR)pack_path.c_str());
578 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
579 mPackage_paths.push_back(pack_path);
580 pack_path = path + "/Release";
581 PACKPATH = BB.addChild((XMLCSTR)"package_path");
582 PACKPATH.addText((XMLCSTR)pack_path.c_str());
583 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
584 mPackage_paths.push_back(pack_path);
588 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
589 if ( err != eXMLErrorNone )
591 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
592 bbtkDebugMessage("config",1,mess<< std::endl);
598 //=========================================================================
600 //=========================================================================
601 void ConfigurationFile::GetHelp(int level) const
603 bbtkDebugMessageInc("config",9,"ConfigurationFile::GetHelp("<<level
606 const std::string config_xml_full_path = Get_config_xml_full_path();
607 const std::string description = Get_description();
608 const std::string url = Get_doc_path();
609 const std::string data_path = Get_data_path();
610 const std::string default_temp_dir = Get_default_temp_dir();
611 const std::string file_separator = Get_file_separator();
612 const std::vector<std::string>bbs_paths = Get_bbs_paths();
613 const std::vector<std::string>package_paths = Get_package_paths();
615 bbtkMessage("help",level, "=============" << std::endl);
616 bbtkMessage("help",level, "Configuration" << std::endl);
617 bbtkMessage("help",level, "=============" << std::endl);
618 bbtkMessage("help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
619 bbtkMessage("help",level, "Documentation Path : [" << url << "]" << std::endl);
620 bbtkMessage("help",level, "Data Path : [" << data_path << "]" << std::endl);
621 bbtkMessage("help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
622 bbtkMessage("help",level, "File Separator : [" << file_separator << "]" << std::endl);
624 std::vector<std::string>::const_iterator i;
626 bbtkMessage("help",level, "BBS Paths " << std::endl);
627 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
629 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
632 bbtkMessage("help",level, "PACKAGE Paths : " << std::endl);
633 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
635 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
638 bbtkDebugDecTab("config",9);
640 //=========================================================================