1 /*=========================================================================
4 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
6 Date: $Date: 2008/03/03 08:06:36 $
7 Version: $Revision: 1.12 $
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()
42 mFile_separator = "\\";
44 mFile_separator = "/";
47 // ==> Set system paths
48 mBin_path = GetExecutablePath();
49 mInstall_path = mBin_path + mFile_separator + "..";
50 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
51 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
52 //EED mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(doc);
53 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
54 mDoc_path = mInstall_path + mFile_separator + mDoc_rel_path;
55 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
56 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
57 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
58 mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
59 // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
60 // mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
61 // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
62 // mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
63 // The path to the bbtk data folder
64 // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
65 // But can be overriden by value read from bbtk_config.xml
66 mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
68 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
69 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
70 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
71 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
72 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
76 // always add "." (current working directory) at the begining
77 mBbs_paths.push_back(".");
78 // add system bbs path
79 mBbs_paths.push_back(mBbs_path);
80 // add toolsbbtk/appli
81 std::string toolsappli_rel_path(mFile_separator);
82 toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
83 mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
85 //EED for windows BUILD tree
86 std::string winbbspath = mInstall_path + mFile_separator + ".." + mFile_separator + mBbs_rel_path;
87 mBbs_paths.push_back(winbbspath);
88 // add toolsbbtk/appli
89 mBbs_paths.push_back(winbbspath + toolsappli_rel_path);
94 // always add "." (current working directory) at the begining
95 mPackage_paths.push_back(".");
96 // add system bin path (for build tree / standalone folder install)
97 mPackage_paths.push_back(mBin_path);
98 // add system lib path (for install tree)
99 mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
101 // add bin/Debug bin/Release paths (for build/install tree)
102 mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
103 mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
108 // ==> First we look for bbtk_config.xml in "."
110 const char *currentDir = getcwd(buf, 2048);
114 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
115 // \todo : what else?
116 // How abort a constructor and warn the caller function?
117 // LG : throw an exception
120 std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
122 if ( Utilities::FileExists( configXmlFullPathName ))
124 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
125 "] found in current directory" << std::endl);
126 //Read(configXmlFullPathName.c_str());
127 // traiter le fichier local
130 // ==> Then we look for bbtk_config.xml in ".bbtk"
133 #if defined(__GNUC__)
134 std::string str_home(getenv("HOME"));
135 #elif defined(_WIN32)
136 std::string str_home(getenv("USERPROFILE"));
138 configXmlFullPathName = str_home + mFile_separator + ".bbtk/bbtk_config.xml";
139 if (!Utilities::FileExists( configXmlFullPathName ))
141 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
146 // In any case, deal with bbtk_config.xml!
147 Read(configXmlFullPathName.c_str());
149 //=========================================================================
151 //=========================================================================
153 ConfigurationFile::~ConfigurationFile()
156 //=========================================================================
161 //=========================================================================
162 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
166 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
167 bbtkMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
168 fp = fopen (configXml, "w");
169 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
170 // fprintf(fp, "<!DOCTYPE config SYSTEM \"/mnt/windows/bbtk/bbtk_config.xml>\n");
171 fprintf(fp, "<config>\n");
172 fprintf(fp, " <description> </description>\n");
173 // fprintf(fp, " <url> http://www.creatis.insa-lyon.fr/software/bbtk </url>\n");
174 // fprintf(fp, " <install_path> </install_path>\n");
175 fprintf(fp, " <bbs_path> </bbs_path>\n");
176 // fprintf(fp, " <ext_dll_path> </ext_dll_path>\n");
177 fprintf(fp, " <package_path> </package_path>\n");
178 fprintf(fp, " <data_path> </data_path>\n");//, BBTK_DATA_PATH);
179 /// \todo find a decent default value !
180 ///fprintf(fp, " <default_temp_dir> %s </default_temp_dir>\n", " ");
181 // when $ will be found, default_temp_dir value will be replaced
182 fprintf(fp, " <default_temp_dir>$</default_temp_dir>\n");
183 fprintf(fp, "</config>\n");
186 //=========================================================================
189 //=========================================================================
190 std::string ConfigurationFile::GetExecutablePath()
192 /// \todo : Think to delete it!
193 char *buf = (char *)malloc(512);
197 GetModuleFileName(NULL, buf, 511);
198 slash = strrchr(buf, '\\');
203 #elif defined(__GNUC__)
205 res = readlink("/proc/self/exe", buf, 512);
209 slash = strrchr(buf, '/');
217 std::string ret(buf);
221 //=========================================================================
224 //=========================================================================
225 void ConfigurationFile::InstallPath ()
228 /*--------------------------------------------------
229 New policy for bbtk_config.xml :
231 if bbtk_config.xml found in current directory (user is an aware user!)
234 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
237 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
238 copy it as .bbtk/bbtk_config.xml
240 else (nothing installed)
241 create a minimum version in HOME/.bbtk
242 ----------------------------------------------------*/
245 // -----------------------------------------------------------------
246 #if defined(__GNUC__)
248 // ------------------ create some usefull strings ----------------
249 // installed bbtk_path
251 strcpy(bbtk_path, "/usr/local/bin");
254 char rootDirectory[200];
255 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
258 char configPath[200];
259 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
263 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
266 char configXmlTmp[250];
267 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
272 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
274 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
276 system(makeDir); // create .bbtk
279 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
280 CreateConfigXML( rootDirectory );// create .bbtk
284 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
285 if (!Utilities::FileExists(rootDirectory))
287 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
291 if (!Utilities::FileExists(configPath))
298 // ------------------------------------------------------------------
302 // installed bbtk_path
304 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
305 char bbtk_path2[100];
306 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
309 char rootDirectory[200];
310 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
311 // std::cout << "[" << rootDirectory << "]" << std::endl;
314 char configPath[200];
315 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
319 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
322 char configXmlTmp[250];
323 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
328 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
330 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
332 system(makeDir); // create .bbtk
335 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
336 CreateConfigXML( rootDirectory );// create .bbtk
340 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
342 int attribs = GetFileAttributes (rootDirectory);
343 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
344 if ( attribs != 0xFFFFFFFF)
346 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
348 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
360 // ------------------------------------------------------------------
362 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
368 //=========================================================================
372 //=========================================================================
373 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
376 void ConfigurationFile::Read(const std::string& filename)
379 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
381 mConfig_xml_full_path = filename;
382 XMLResults* res = new XMLResults;
383 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
385 if ( res->error != eXMLErrorNone )
387 std::string mess = GetErrorMessage(res,filename);
389 bbtkDebugMessage("Config",1,mess<< std::endl);
394 bbtkDebugMessage("Config",1,"OK" << std::endl);
399 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
402 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
407 if( BB.nChildNode((XMLCSTR)"url") )
408 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
411 if( BB.nChildNode((XMLCSTR)"data_path") )
412 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
415 // if( BB.nChildNode((XMLCSTR)"install_path") )
416 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
418 // add user bbs paths
419 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
422 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
423 mBbs_paths.push_back(val);
428 // add user package path
429 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
432 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
433 mPackage_paths.push_back(val);
437 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
438 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
440 if ( mDefault_temp_dir == "$") // no value found in config_xml
442 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
443 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
448 //=========================================================================
451 //=========================================================================
452 void ConfigurationFile::GetHelp(int level) const
454 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
457 const std::string config_xml_full_path = Get_config_xml_full_path();
458 const std::string description = Get_description();
459 const std::string url = Get_doc_path();
460 const std::string data_path = Get_data_path();
461 const std::string default_temp_dir = Get_default_temp_dir();
462 const std::string file_separator = Get_file_separator();
463 const std::vector<std::string>bbs_paths = Get_bbs_paths();
464 const std::vector<std::string>package_paths = Get_package_paths();
466 bbtkMessage("Help",level, "=============" << std::endl);
467 bbtkMessage("Help",level, "Configuration" << std::endl);
468 bbtkMessage("Help",level, "=============" << std::endl);
469 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
470 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
471 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
472 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
473 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
475 std::vector<std::string>::const_iterator i;
477 bbtkMessage("Help",level, "BBS Paths " << std::endl);
478 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
480 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
483 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
484 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
486 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
489 bbtkDebugDecTab("Config",9);
491 //=========================================================================