1 /*=========================================================================
4 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
6 Date: $Date: 2008/02/14 09:32:54 $
7 Version: $Revision: 1.4 $
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);
65 //MessageManager::SetMessageLevel("Config",1);
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)
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");
89 // add bin/Debug bin/Release paths (for build/install tree)
90 mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
91 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 //std::cout << "=======================in ConfigurationFile::Read filename [" <<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::ostringstream str;
375 str << XMLNode::getError(res->error);
376 str << " [line " << res->nLine << ", col "<<res->nColumn<<"] ";
377 str << " file "<<filename;
379 bbtkError(str.str());
380 // throw ConfigurationException(str.str());
388 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
391 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
396 if( BB.nChildNode((XMLCSTR)"url") )
397 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
400 if( BB.nChildNode((XMLCSTR)"data_path") )
401 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
404 // if( BB.nChildNode((XMLCSTR)"install_path") )
405 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
407 // add user bbs paths
408 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
411 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
412 mBbs_paths.push_back(val);
417 // add user package path
418 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
421 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
422 mPackage_paths.push_back(val);
426 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
427 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
429 if ( mDefault_temp_dir == "$") // no value found in config_xml
431 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
432 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
436 //=========================================================================