1 /*=========================================================================
4 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
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"
25 //#include "xmlParser.h"
26 #include <sys/stat.h> // for struct stat stFileInfo
29 #include <direct.h> // for getcwd
35 ConfigurationFile::ConfigurationFile()
37 char *execPath = GetExecutablePath();
40 std::string slash("\\");
42 std::string slash("/");
45 // ==> First we look for bbtk_config.xml in "."
48 const char *currentDir = getcwd(buf, 2048);
52 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
53 /// \todo : what else?
54 // How abort a constructor and warn the caller function?
57 std::string configXmlFullPathName = currentDir + slash + "bbtk_config.xml";
59 if ( FileExists( configXmlFullPathName ))
61 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
62 "] found in current directory" << std::endl);
63 //Read(configXmlFullPathName.c_str());
64 // traiter le fichier local
67 // ==> Then we look for bbtk_config.xml in ".bbtk"
71 std::string str_home(getenv("HOME"));
73 std::string str_home(getenv("USERPROFILE"));
75 configXmlFullPathName = str_home + slash + ".bbtk/bbtk_config.xml";
76 if (!FileExists( configXmlFullPathName ))
78 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
83 // In any case, deal with bbtk_config.xml!
84 Read(configXmlFullPathName.c_str());
88 ConfigurationFile::~ConfigurationFile()
92 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
93 bool ConfigurationFile::FileExists(std::string strFilename) {
94 struct stat stFileInfo;
98 // Attempt to get the file attributes
99 intStat = stat(strFilename.c_str(),&stFileInfo);
101 // We were able to get the file attributes
102 // so the file obviously exists.
105 // We were not able to get the file attributes.
106 // This may mean that we don't have permission to
107 // access the folder which contains this file. If you
108 // need to do that level of checking, lookup the
109 // return values of stat which will give you
110 // more details on why stat failed.
118 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
122 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
123 bbtkMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
124 fp = fopen (configXml, "w");
125 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
126 fprintf(fp, "<!DOCTYPE config SYSTEM \"/mnt/windows/bbtk/bbtk_config.xml>\n");
127 fprintf(fp, "<config>\n");
128 fprintf(fp, " <description> </description>\n");
129 fprintf(fp, " <url> http://www.creatis.insa-lyon.fr/software/bbtk </url>\n");
130 fprintf(fp, " <install_path> </install_path>\n");
131 fprintf(fp, " <bbs_path> </bbs_path>\n");
132 // fprintf(fp, " <ext_dll_path> </ext_dll_path>\n");
133 fprintf(fp, " <package_path> </package_path>\n");
134 fprintf(fp, " <data_path> %s </data_path>\n", BBTK_DATA_PATH);
135 /// \todo find a decent default value !
136 ///fprintf(fp, " <default_doc_tmp> %s </default_doc_tmp>\n", " ");
137 // when $ will be found, default_doc_tmp value will be replaced
138 fprintf(fp, " <default_doc_tmp>$</default_doc_tmp>\n");
139 fprintf(fp, "</config>\n");
143 char *ConfigurationFile::GetExecutablePath()
145 /// \todo : Think to delete it!
146 char *buf = (char *)malloc(512);
150 GetModuleFileName(NULL, buf, 511);
151 slash = strrchr(buf, '\\');
156 #elif defined(__GNUC__)
158 res = readlink("/proc/self/exe", buf, 512);
162 slash = strrchr(buf, '/');
176 void ConfigurationFile::InstallPath ()
179 /*--------------------------------------------------
180 New policy for bbtk_config.xml :
182 if bbtk_config.xml found in current directory (user is an aware user!)
185 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
188 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
189 copy it as .bbtk/bbtk_config.xml
191 else (nothing installed)
192 create a minimum version in HOME/.bbtk
193 ----------------------------------------------------*/
196 // -----------------------------------------------------------------
197 #if defined(__GNUC__)
199 // ------------------ create some usefull strings ----------------
200 // installed bbtk_path
202 strcpy(bbtk_path, "/usr/local/bin");
205 char rootDirectory[200];
206 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
209 char configPath[200];
210 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
214 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
217 char configXmlTmp[250];
218 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
223 if (!FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
225 if (!FileExists(rootDirectory)) // .bbtk not found
227 system(makeDir); // create .bbtk
230 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
231 CreateConfigXML( rootDirectory );// create .bbtk
235 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
236 if (!FileExists(rootDirectory))
238 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
242 if (!FileExists(configPath))
249 // ------------------------------------------------------------------
253 // installed bbtk_path
255 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
256 char bbtk_path2[100];
257 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
260 char rootDirectory[200];
261 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
262 // std::cout << "[" << rootDirectory << "]" << std::endl;
265 char configPath[200];
266 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
270 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
273 char configXmlTmp[250];
274 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
279 if (!FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
281 if (!FileExists(rootDirectory)) // .bbtk not found
283 system(makeDir); // create .bbtk
286 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
287 CreateConfigXML( rootDirectory );// create .bbtk
291 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
293 int attribs = GetFileAttributes (rootDirectory);
294 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
295 if ( attribs != 0xFFFFFFFF){
296 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
298 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
308 // ------------------------------------------------------------------
310 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
317 void ConfigurationFile::GetTextOrClear(const XMLNode& node, std::string& var)
321 var = node.getText();
323 else if (node.nClear()>0)
325 var = node.getClear().lpszValue;
329 std::string mess("Error : element <");
330 mess += node.getName();
331 mess += "> : no text nor <PRE></PRE> clear tag found";
336 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
339 void ConfigurationFile::Read(const std::string& filename)
342 //std::cout << "=======================in ConfigurationFile::Read filename [" <<filename << "]" << std::endl;
344 mConfig_xml_full_path = filename;
345 XMLResults* res = new XMLResults;
346 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
348 if ( res->error != eXMLErrorNone )
350 std::ostringstream str;
351 str << XMLNode::getError(res->error);
352 str << " [line " << res->nLine << ", col "<<res->nColumn<<"] ";
353 str << " file "<<filename;
355 bbtkError(str.str());
356 // throw ConfigurationException(str.str());
363 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
366 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
371 if( BB.nChildNode((XMLCSTR)"url") )
372 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
375 if( BB.nChildNode((XMLCSTR)"data_path") )
376 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
379 if( BB.nChildNode((XMLCSTR)"install_path") )
380 GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
382 // always add "." (current working directory) at the begining
385 mBbs_paths.push_back(".");
386 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
389 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
390 mBbs_paths.push_back(val);
394 mPackage_paths.push_back(".");
395 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
398 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
399 mPackage_paths.push_back(val);
403 if( BB.nChildNode((XMLCSTR)"default_doc_tmp") )
404 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_doc_tmp"),mDefault_doc_tmp);
406 if ( mDefault_doc_tmp == "$") // no value found in config_xml
408 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
409 mDefault_doc_tmp = mConfig_xml_full_path.substr (0,pos);
413 mFile_separator = "\\";
415 mFile_separator = "/";