1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2008/10/21 08:37:04 $
6 Version: $Revision: 1.19 $
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
50 //====================================================================
52 ConfigurationFile::ConfigurationFile()
55 mFile_separator = VALID_FILE_SEPARATOR;
57 // ==> Set system paths
58 mBin_path = GetExecutablePath();
59 //EED mInstall_path = mBin_path + mFile_separator + "..";
60 mInstall_path = mBin_path + "/..";
61 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
62 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
63 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
64 //EED mDoc_path = mInstall_path + mFile_separator + mDoc_rel_path;
65 mDoc_path = mInstall_path + "/" + mDoc_rel_path;
66 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
67 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
68 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
69 //EED mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
70 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
71 // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
72 // mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
73 // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
74 // mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
75 // The path to the bbtk data folder
76 // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
77 // But can be overriden by value read from bbtk_config.xml
78 //EED mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
79 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
81 Utilities::replace( mBin_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
82 Utilities::replace( mInstall_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
83 Utilities::replace( mDoc_rel_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
84 Utilities::replace( mDoc_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
85 Utilities::replace( mBbs_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
86 Utilities::replace( mData_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
88 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
89 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
90 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
91 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
92 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
98 // always add "." (current working directory) at the begining
99 mBbs_paths.push_back( "." );
100 // add system bbs path
101 mBbs_paths.push_back(mBbs_path);
102 // add toolsbbtk/appli
103 //EED std::string toolsappli_rel_path(mFile_separator);
104 std::string toolsappli_rel_path("/");
105 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
106 toolsappli_rel_path += "toolsbbtk/appli";
108 //-----------------------------------------------------------
109 // LG : REMOVE BUGGY PATH WITH include *:
111 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
112 //-----------------------------------------------------------
113 int iStrVec,sizeStrVec;
115 sizeStrVec = mBbs_paths.size();
116 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
117 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
121 // always add "." (current working directory) at the begining
122 mPackage_paths.push_back(".");
123 // add system bin path (for build tree / standalone folder install)
124 mPackage_paths.push_back(mBin_path);
125 // add system lib path (for install tree)
126 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
127 mPackage_paths.push_back(mInstall_path + "/lib");
129 // add bin/Debug bin/Release paths (for build/install tree)
130 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
131 mPackage_paths.push_back(mBin_path + "/Debug");
132 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
133 mPackage_paths.push_back(mBin_path + "/Release");
136 sizeStrVec = mPackage_paths.size();
137 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
138 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
145 // ==> First we look for bbtk_config.xml in "."
147 const char *currentDir = getcwd(buf, 2048);
151 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
152 // \todo : what else?
153 // How abort a constructor and warn the caller function?
154 // LG : throw an exception
157 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
158 std::string configXmlFullPathName = currentDir ;
159 configXmlFullPathName += "/bbtk_config.xml";
160 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
162 if ( Utilities::FileExists( configXmlFullPathName ))
164 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
165 "] found in current directory" << std::endl);
166 //Read(configXmlFullPathName.c_str());
167 // traiter le fichier local
170 // ==> Then we look for bbtk_config.xml in ".bbtk"
173 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
174 if (!Utilities::FileExists( configXmlFullPathName ))
176 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
181 // In any case, deal with bbtk_config.xml!
182 Read(configXmlFullPathName.c_str());
184 //=========================================================================
186 //=========================================================================
188 ConfigurationFile::~ConfigurationFile()
191 //=========================================================================
196 //=========================================================================
197 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
201 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
202 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
203 fp = fopen (configXml, "w");
204 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
205 fprintf(fp, "<config>\n");
206 fprintf(fp, " <bbs_path> </bbs_path>\n");
207 fprintf(fp, " <package_path> </package_path>\n");
208 fprintf(fp, "</config>\n");
211 //=========================================================================
214 //=========================================================================
215 std::string ConfigurationFile::GetExecutablePath()
217 /// \todo : Think to delete it!
218 char *buf = (char *)malloc(512);
222 GetModuleFileName(NULL, buf, 511);
223 slash = strrchr(buf, '\\');
228 #elif defined(__GNUC__)
230 res = readlink("/proc/self/exe", buf, 512);
234 slash = strrchr(buf, '/');
242 std::string ret(buf);
246 //=========================================================================
249 //=========================================================================
250 void ConfigurationFile::InstallPath ()
253 /*--------------------------------------------------
254 New policy for bbtk_config.xml :
256 if bbtk_config.xml found in current directory (user is an aware user!)
259 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
262 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
263 copy it as .bbtk/bbtk_config.xml
265 else (nothing installed)
266 create a minimum version in HOME/.bbtk
267 ----------------------------------------------------*/
270 // -----------------------------------------------------------------
271 #if defined(__GNUC__)
273 // ------------------ create some usefull strings ----------------
274 // installed bbtk_path
276 strcpy(bbtk_path, "/usr/local/bin");
279 char rootDirectory[200];
280 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
283 char configPath[200];
284 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
288 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
291 char configXmlTmp[250];
292 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
297 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
299 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
301 system(makeDir); // create .bbtk
304 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
305 CreateConfigXML( rootDirectory );// create .bbtk
309 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
310 if (!Utilities::FileExists(rootDirectory))
312 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
316 if (!Utilities::FileExists(configPath))
323 // ------------------------------------------------------------------
327 // installed bbtk_path
329 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
330 char bbtk_path2[100];
331 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
334 char rootDirectory[200];
335 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
336 // std::cout << "[" << rootDirectory << "]" << std::endl;
339 char configPath[200];
340 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
344 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
347 char configXmlTmp[250];
348 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
353 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
355 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
357 system(makeDir); // create .bbtk
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,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
367 int attribs = GetFileAttributes (rootDirectory);
368 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
369 if ( attribs != 0xFFFFFFFF)
371 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
373 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
385 // ------------------------------------------------------------------
387 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
393 //=========================================================================
397 //=========================================================================
398 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
401 void ConfigurationFile::Read(const std::string& filename)
404 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
406 mConfig_xml_full_path = filename;
407 XMLResults* res = new XMLResults;
408 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
410 if ( res->error != eXMLErrorNone )
412 std::string mess = GetErrorMessage(res,filename);
414 bbtkDebugMessage("Config",1,mess<< std::endl);
419 bbtkDebugMessage("Config",1,"OK" << std::endl);
424 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
427 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
432 if( BB.nChildNode((XMLCSTR)"url") )
433 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
436 if( BB.nChildNode((XMLCSTR)"data_path") )
437 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
440 // if( BB.nChildNode((XMLCSTR)"install_path") )
441 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
443 // add user bbs paths
444 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
447 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
448 mBbs_paths.push_back(val);
453 // add user package path
454 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
457 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
458 mPackage_paths.push_back(val);
462 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
463 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
465 if ( mDefault_temp_dir == "$") // no value found in config_xml
467 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
468 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
473 //=========================================================================
475 //=========================================================================
476 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
478 bbtkDebugMessageInc("Config",9,
479 "ConfigurationFile::AddPackagePathsAndWrite("
480 <<path<<")"<<std::endl);
482 XMLResults* res = new XMLResults;
484 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
485 (XMLCSTR)"config",res);
487 if ( res->error != eXMLErrorNone )
489 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
491 bbtkDebugMessage("Config",1,mess<< std::endl);
497 std::string bbs_path = path + "/bbs";
499 std::string bbs_path = path + "/share/bbtk/bbs" ;
501 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
502 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
503 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
504 mBbs_paths.push_back(bbs_path);
507 std::string pack_path = path + "/bin";
509 std::string pack_path = path ;
511 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
512 PACKPATH.addText((XMLCSTR)pack_path.c_str());
513 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
514 mPackage_paths.push_back(pack_path);
517 pack_path = path + "/Debug";
518 PACKPATH = BB.addChild((XMLCSTR)"package_path");
519 PACKPATH.addText((XMLCSTR)pack_path.c_str());
520 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
521 mPackage_paths.push_back(pack_path);
522 pack_path = path + "/Release";
523 PACKPATH = BB.addChild((XMLCSTR)"package_path");
524 PACKPATH.addText((XMLCSTR)pack_path.c_str());
525 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
526 mPackage_paths.push_back(pack_path);
530 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
531 if ( err != eXMLErrorNone )
533 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
534 bbtkDebugMessage("Config",1,mess<< std::endl);
540 //=========================================================================
542 //=========================================================================
543 void ConfigurationFile::GetHelp(int level) const
545 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
548 const std::string config_xml_full_path = Get_config_xml_full_path();
549 const std::string description = Get_description();
550 const std::string url = Get_doc_path();
551 const std::string data_path = Get_data_path();
552 const std::string default_temp_dir = Get_default_temp_dir();
553 const std::string file_separator = Get_file_separator();
554 const std::vector<std::string>bbs_paths = Get_bbs_paths();
555 const std::vector<std::string>package_paths = Get_package_paths();
557 bbtkMessage("Help",level, "=============" << std::endl);
558 bbtkMessage("Help",level, "Configuration" << std::endl);
559 bbtkMessage("Help",level, "=============" << std::endl);
560 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
561 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
562 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
563 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
564 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
566 std::vector<std::string>::const_iterator i;
568 bbtkMessage("Help",level, "BBS Paths " << std::endl);
569 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
571 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
574 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
575 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
577 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
580 bbtkDebugDecTab("Config",9);
582 //=========================================================================