1 /*=========================================================================
4 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
6 Date: $Date: 2008/03/17 10:51:35 $
7 Version: $Revision: 1.14 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief Class bbtk::ConfigurationFile
23 #include "bbtkConfigurationFile.h"
24 #include "bbtkMessageManager.h"
26 #include "bbtkUtilities.h"
29 #include <direct.h> // for getcwd
37 //====================================================================
39 ConfigurationFile::ConfigurationFile()
42 mFile_separator = VALID_FILE_SEPARATOR;
44 // ==> Set system paths
45 mBin_path = GetExecutablePath();
46 //EED mInstall_path = mBin_path + mFile_separator + "..";
47 mInstall_path = mBin_path + "/..";
48 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
49 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
50 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
51 //EED mDoc_path = mInstall_path + mFile_separator + mDoc_rel_path;
52 mDoc_path = mInstall_path + "/" + mDoc_rel_path;
53 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
54 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
55 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
56 //EED mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
57 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
58 // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
59 // mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
60 // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
61 // mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
62 // The path to the bbtk data folder
63 // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
64 // But can be overriden by value read from bbtk_config.xml
65 //EED mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
66 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
68 Utilities::replace( mBin_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
69 Utilities::replace( mInstall_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
70 Utilities::replace( mDoc_rel_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
71 Utilities::replace( mDoc_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
72 Utilities::replace( mBbs_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
73 Utilities::replace( mData_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
75 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
76 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
77 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
78 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
79 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
85 // always add "." (current working directory) at the begining
86 mBbs_paths.push_back( "." );
87 // add system bbs path
88 mBbs_paths.push_back(mBbs_path);
89 // add toolsbbtk/appli
90 //EED std::string toolsappli_rel_path(mFile_separator);
91 std::string toolsappli_rel_path("/");
92 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
93 toolsappli_rel_path += "toolsbbtk/appli";
95 //-----------------------------------------------------------
96 // LG : REMOVE BUGGY PATH WITH include *:
98 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
99 //-----------------------------------------------------------
100 int iStrVec,sizeStrVec;
102 sizeStrVec = mBbs_paths.size();
103 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
104 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
108 // always add "." (current working directory) at the begining
109 mPackage_paths.push_back(".");
110 // add system bin path (for build tree / standalone folder install)
111 mPackage_paths.push_back(mBin_path);
112 // add system lib path (for install tree)
113 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
114 mPackage_paths.push_back(mInstall_path + "/lib");
116 // add bin/Debug bin/Release paths (for build/install tree)
117 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
118 mPackage_paths.push_back(mBin_path + "/Debug");
119 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
120 mPackage_paths.push_back(mBin_path + "/Release");
123 sizeStrVec = mPackage_paths.size();
124 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
125 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
132 // ==> First we look for bbtk_config.xml in "."
134 const char *currentDir = getcwd(buf, 2048);
138 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
139 // \todo : what else?
140 // How abort a constructor and warn the caller function?
141 // LG : throw an exception
144 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
145 std::string configXmlFullPathName = currentDir ;
146 configXmlFullPathName += "/bbtk_config.xml";
147 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
149 if ( Utilities::FileExists( configXmlFullPathName ))
151 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
152 "] found in current directory" << std::endl);
153 //Read(configXmlFullPathName.c_str());
154 // traiter le fichier local
157 // ==> Then we look for bbtk_config.xml in ".bbtk"
160 #if defined(__GNUC__)
161 std::string str_home(getenv("HOME"));
162 #elif defined(_WIN32)
163 std::string str_home(getenv("USERPROFILE"));
165 //EED configXmlFullPathName = str_home + mFile_separator + ".bbtk/bbtk_config.xml";
166 configXmlFullPathName = str_home + "/.bbtk/bbtk_config.xml";
167 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
169 if (!Utilities::FileExists( configXmlFullPathName ))
171 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
176 // In any case, deal with bbtk_config.xml!
177 Read(configXmlFullPathName.c_str());
179 //=========================================================================
181 //=========================================================================
183 ConfigurationFile::~ConfigurationFile()
186 //=========================================================================
191 //=========================================================================
192 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
196 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
197 bbtkMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
198 fp = fopen (configXml, "w");
199 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
200 // fprintf(fp, "<!DOCTYPE config SYSTEM \"/mnt/windows/bbtk/bbtk_config.xml>\n");
201 fprintf(fp, "<config>\n");
202 fprintf(fp, " <description> </description>\n");
203 // fprintf(fp, " <url> http://www.creatis.insa-lyon.fr/software/bbtk </url>\n");
204 // fprintf(fp, " <install_path> </install_path>\n");
205 fprintf(fp, " <bbs_path> </bbs_path>\n");
206 // fprintf(fp, " <ext_dll_path> </ext_dll_path>\n");
207 fprintf(fp, " <package_path> </package_path>\n");
208 fprintf(fp, " <data_path> </data_path>\n");//, BBTK_DATA_PATH);
209 /// \todo find a decent default value !
210 ///fprintf(fp, " <default_temp_dir> %s </default_temp_dir>\n", " ");
211 // when $ will be found, default_temp_dir value will be replaced
212 fprintf(fp, " <default_temp_dir>$</default_temp_dir>\n");
213 fprintf(fp, "</config>\n");
216 //=========================================================================
219 //=========================================================================
220 std::string ConfigurationFile::GetExecutablePath()
222 /// \todo : Think to delete it!
223 char *buf = (char *)malloc(512);
227 GetModuleFileName(NULL, buf, 511);
228 slash = strrchr(buf, '\\');
233 #elif defined(__GNUC__)
235 res = readlink("/proc/self/exe", buf, 512);
239 slash = strrchr(buf, '/');
247 std::string ret(buf);
251 //=========================================================================
254 //=========================================================================
255 void ConfigurationFile::InstallPath ()
258 /*--------------------------------------------------
259 New policy for bbtk_config.xml :
261 if bbtk_config.xml found in current directory (user is an aware user!)
264 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
267 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
268 copy it as .bbtk/bbtk_config.xml
270 else (nothing installed)
271 create a minimum version in HOME/.bbtk
272 ----------------------------------------------------*/
275 // -----------------------------------------------------------------
276 #if defined(__GNUC__)
278 // ------------------ create some usefull strings ----------------
279 // installed bbtk_path
281 strcpy(bbtk_path, "/usr/local/bin");
284 char rootDirectory[200];
285 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
288 char configPath[200];
289 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
293 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
296 char configXmlTmp[250];
297 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
302 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
304 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
306 system(makeDir); // create .bbtk
309 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
310 CreateConfigXML( rootDirectory );// create .bbtk
314 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
315 if (!Utilities::FileExists(rootDirectory))
317 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
321 if (!Utilities::FileExists(configPath))
328 // ------------------------------------------------------------------
332 // installed bbtk_path
334 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
335 char bbtk_path2[100];
336 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
339 char rootDirectory[200];
340 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
341 // std::cout << "[" << rootDirectory << "]" << std::endl;
344 char configPath[200];
345 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
349 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
352 char configXmlTmp[250];
353 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
358 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
360 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
362 system(makeDir); // create .bbtk
365 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
366 CreateConfigXML( rootDirectory );// create .bbtk
370 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
372 int attribs = GetFileAttributes (rootDirectory);
373 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
374 if ( attribs != 0xFFFFFFFF)
376 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
378 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
390 // ------------------------------------------------------------------
392 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
398 //=========================================================================
402 //=========================================================================
403 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
406 void ConfigurationFile::Read(const std::string& filename)
409 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
411 mConfig_xml_full_path = filename;
412 XMLResults* res = new XMLResults;
413 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
415 if ( res->error != eXMLErrorNone )
417 std::string mess = GetErrorMessage(res,filename);
419 bbtkDebugMessage("Config",1,mess<< std::endl);
424 bbtkDebugMessage("Config",1,"OK" << std::endl);
429 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
432 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
437 if( BB.nChildNode((XMLCSTR)"url") )
438 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
441 if( BB.nChildNode((XMLCSTR)"data_path") )
442 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
445 // if( BB.nChildNode((XMLCSTR)"install_path") )
446 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
448 // add user bbs paths
449 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
452 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
453 mBbs_paths.push_back(val);
458 // add user package path
459 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
462 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
463 mPackage_paths.push_back(val);
467 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
468 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
470 if ( mDefault_temp_dir == "$") // no value found in config_xml
472 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
473 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
478 //=========================================================================
481 //=========================================================================
482 void ConfigurationFile::GetHelp(int level) const
484 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
487 const std::string config_xml_full_path = Get_config_xml_full_path();
488 const std::string description = Get_description();
489 const std::string url = Get_doc_path();
490 const std::string data_path = Get_data_path();
491 const std::string default_temp_dir = Get_default_temp_dir();
492 const std::string file_separator = Get_file_separator();
493 const std::vector<std::string>bbs_paths = Get_bbs_paths();
494 const std::vector<std::string>package_paths = Get_package_paths();
496 bbtkMessage("Help",level, "=============" << std::endl);
497 bbtkMessage("Help",level, "Configuration" << std::endl);
498 bbtkMessage("Help",level, "=============" << std::endl);
499 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
500 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
501 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
502 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
503 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
505 std::vector<std::string>::const_iterator i;
507 bbtkMessage("Help",level, "BBS Paths " << std::endl);
508 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
510 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
513 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
514 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
516 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
519 bbtkDebugDecTab("Config",9);
521 //=========================================================================