1 /*=========================================================================
4 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
6 Date: $Date: 2008/02/15 08:46:17 $
7 Version: $Revision: 1.8 $
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
35 //====================================================================
37 ConfigurationFile::ConfigurationFile()
41 mFile_separator = "\\";
43 mFile_separator = "/";
46 // ==> Set system paths
47 mBin_path = GetExecutablePath();
48 mInstall_path = mBin_path + mFile_separator + "..";
49 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
50 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
51 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
52 mDoc_path = mInstall_path + mFile_separator + 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 mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
57 // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
58 // mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
59 // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
60 // mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
61 // The path to the bbtk data folder
62 // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
63 // But can be overriden by value read from bbtk_config.xml
64 mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
66 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
67 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
68 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
69 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
70 // bbtkMessage("Config",1," ==> rsc : '"<<mRsc_path<<"'"<<std::endl);
71 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
75 // always add "." (current working directory) at the begining
76 mBbs_paths.push_back(".");
77 // add system bbs path
78 mBbs_paths.push_back(mBbs_path);
80 // always add "." (current working directory) at the begining
81 mPackage_paths.push_back(".");
83 // add system bin path (for build tree / standalone folder install)
84 mPackage_paths.push_back(mBin_path);
85 // add system lib path (for install tree)
86 mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
88 // add bin/Debug bin/Release paths (for build/install tree)
89 mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
90 mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
95 // ==> First we look for bbtk_config.xml in "."
97 const char *currentDir = getcwd(buf, 2048);
101 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
102 // \todo : what else?
103 // How abort a constructor and warn the caller function?
104 // LG : throw an exception
107 std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
109 if ( Utilities::FileExists( configXmlFullPathName ))
111 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
112 "] found in current directory" << std::endl);
113 //Read(configXmlFullPathName.c_str());
114 // traiter le fichier local
117 // ==> Then we look for bbtk_config.xml in ".bbtk"
120 #if defined(__GNUC__)
121 std::string str_home(getenv("HOME"));
122 #elif defined(_WIN32)
123 std::string str_home(getenv("USERPROFILE"));
125 configXmlFullPathName = str_home + mFile_separator + ".bbtk/bbtk_config.xml";
126 if (!Utilities::FileExists( configXmlFullPathName ))
128 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
133 // In any case, deal with bbtk_config.xml!
134 Read(configXmlFullPathName.c_str());
136 //=========================================================================
138 //=========================================================================
140 ConfigurationFile::~ConfigurationFile()
143 //=========================================================================
148 //=========================================================================
149 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
153 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
154 bbtkMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
155 fp = fopen (configXml, "w");
156 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
157 // fprintf(fp, "<!DOCTYPE config SYSTEM \"/mnt/windows/bbtk/bbtk_config.xml>\n");
158 fprintf(fp, "<config>\n");
159 fprintf(fp, " <description> </description>\n");
160 // fprintf(fp, " <url> http://www.creatis.insa-lyon.fr/software/bbtk </url>\n");
161 // fprintf(fp, " <install_path> </install_path>\n");
162 fprintf(fp, " <bbs_path> </bbs_path>\n");
163 // fprintf(fp, " <ext_dll_path> </ext_dll_path>\n");
164 fprintf(fp, " <package_path> </package_path>\n");
165 fprintf(fp, " <data_path> </data_path>\n");//, BBTK_DATA_PATH);
166 /// \todo find a decent default value !
167 ///fprintf(fp, " <default_temp_dir> %s </default_temp_dir>\n", " ");
168 // when $ will be found, default_temp_dir value will be replaced
169 fprintf(fp, " <default_temp_dir>$</default_temp_dir>\n");
170 fprintf(fp, "</config>\n");
173 //=========================================================================
176 //=========================================================================
177 std::string ConfigurationFile::GetExecutablePath()
179 /// \todo : Think to delete it!
180 char *buf = (char *)malloc(512);
184 GetModuleFileName(NULL, buf, 511);
185 slash = strrchr(buf, '\\');
190 #elif defined(__GNUC__)
192 res = readlink("/proc/self/exe", buf, 512);
196 slash = strrchr(buf, '/');
204 std::string ret(buf);
208 //=========================================================================
211 //=========================================================================
212 void ConfigurationFile::InstallPath ()
215 /*--------------------------------------------------
216 New policy for bbtk_config.xml :
218 if bbtk_config.xml found in current directory (user is an aware user!)
221 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
224 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
225 copy it as .bbtk/bbtk_config.xml
227 else (nothing installed)
228 create a minimum version in HOME/.bbtk
229 ----------------------------------------------------*/
232 // -----------------------------------------------------------------
233 #if defined(__GNUC__)
235 // ------------------ create some usefull strings ----------------
236 // installed bbtk_path
238 strcpy(bbtk_path, "/usr/local/bin");
241 char rootDirectory[200];
242 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
245 char configPath[200];
246 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
250 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
253 char configXmlTmp[250];
254 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
259 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
261 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
263 system(makeDir); // create .bbtk
266 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
267 CreateConfigXML( rootDirectory );// create .bbtk
271 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
272 if (!Utilities::FileExists(rootDirectory))
274 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
278 if (!Utilities::FileExists(configPath))
285 // ------------------------------------------------------------------
289 // installed bbtk_path
291 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
292 char bbtk_path2[100];
293 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
296 char rootDirectory[200];
297 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
298 // std::cout << "[" << rootDirectory << "]" << std::endl;
301 char configPath[200];
302 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
306 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
309 char configXmlTmp[250];
310 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
315 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
317 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
319 system(makeDir); // create .bbtk
322 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
323 CreateConfigXML( rootDirectory );// create .bbtk
327 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
329 int attribs = GetFileAttributes (rootDirectory);
330 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
331 if ( attribs != 0xFFFFFFFF)
333 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
335 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
347 // ------------------------------------------------------------------
349 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
355 //=========================================================================
359 //=========================================================================
360 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
363 void ConfigurationFile::Read(const std::string& filename)
366 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
368 mConfig_xml_full_path = filename;
369 XMLResults* res = new XMLResults;
370 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
372 if ( res->error != eXMLErrorNone )
374 std::string mess = GetErrorMessage(res,filename);
376 bbtkDebugMessage("Config",1,mess<< std::endl);
381 bbtkDebugMessage("Config",1,"OK" << std::endl);
386 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
389 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
394 if( BB.nChildNode((XMLCSTR)"url") )
395 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
398 if( BB.nChildNode((XMLCSTR)"data_path") )
399 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
402 // if( BB.nChildNode((XMLCSTR)"install_path") )
403 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
405 // add user bbs paths
406 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
409 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
410 mBbs_paths.push_back(val);
415 // add user package path
416 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
419 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
420 mPackage_paths.push_back(val);
424 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
425 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
427 if ( mDefault_temp_dir == "$") // no value found in config_xml
429 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
430 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
435 //=========================================================================
438 //=========================================================================
439 void ConfigurationFile::GetHelp(int level) const
441 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
444 const std::string config_xml_full_path = Get_config_xml_full_path();
445 const std::string description = Get_description();
446 const std::string url = Get_doc_path();
447 const std::string data_path = Get_data_path();
448 const std::string default_temp_dir = Get_default_temp_dir();
449 const std::string file_separator = Get_file_separator();
450 const std::vector<std::string>bbs_paths = Get_bbs_paths();
451 const std::vector<std::string>package_paths = Get_package_paths();
453 bbtkMessage("Help",level, "=============" << std::endl);
454 bbtkMessage("Help",level, "Configuration" << std::endl);
455 bbtkMessage("Help",level, "=============" << std::endl);
456 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
457 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
458 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
459 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
460 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
462 std::vector<std::string>::const_iterator i;
464 bbtkMessage("Help",level, "BBS Paths " << std::endl);
465 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
467 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
470 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
471 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
473 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
476 bbtkDebugDecTab("Config",9);
478 //=========================================================================