1 /*=========================================================================
3 Module: $RCSfile: bbtkConfigurationFile.cxx,v $
5 Date: $Date: 2008/11/21 15:26:44 $
6 Version: $Revision: 1.22 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 *\brief Class bbtk::ConfigurationFile
36 #include "bbtkConfigurationFile.h"
37 #include "bbtkMessageManager.h"
39 #include "bbtkUtilities.h"
42 # include <direct.h> // for getcwd
46 #if defined(MACOSX) // assume this is OSX
47 # include <sys/param.h>
48 # include <mach-o/dyld.h> // _NSGetExecutablePath : must add -framework CoreFoundation to link line
51 # define PATH_MAX MAXPATHLEN
55 #ifndef PATH_MAX // If not defined yet : do it
56 # define PATH_MAX 2048
63 //====================================================================
65 ConfigurationFile::ConfigurationFile()
68 mFile_separator = VALID_FILE_SEPARATOR;
70 // ==> Set system paths
71 mBin_path = GetExecutablePath();
72 //EED mInstall_path = mBin_path + mFile_separator + "..";
74 mInstall_path = mBin_path + "/../../../..";
76 mInstall_path = mBin_path + "/..";
78 // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
79 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
80 // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
81 //EED mDoc_path = mInstall_path + mFile_separator + mDoc_rel_path;
82 mDoc_path = mInstall_path + "/" + mDoc_rel_path;
83 // The relative path to the doc folder (=BBTK_BBS_REL_PATH)
84 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
85 // The path to the bbs folder (=mInstall_path+"/"+mBbs_rel_path)
86 //EED mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
87 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
88 // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
89 // mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
90 // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
91 // mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
92 // The path to the bbtk data folder
93 // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
94 // But can be overriden by value read from bbtk_config.xml
95 //EED mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
96 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
98 Utilities::replace( mBin_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
99 Utilities::replace( mInstall_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
100 Utilities::replace( mDoc_rel_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
101 Utilities::replace( mDoc_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
102 Utilities::replace( mBbs_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
103 Utilities::replace( mData_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
105 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
106 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
107 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
108 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
109 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
115 // always add "." (current working directory) at the begining
116 mBbs_paths.push_back( "." );
117 // add system bbs path
118 mBbs_paths.push_back(mBbs_path);
119 // add toolsbbtk/appli
120 //EED std::string toolsappli_rel_path(mFile_separator);
121 std::string toolsappli_rel_path("/");
122 //EED toolsappli_rel_path += "toolsbbtk" + mFile_separator + "appli";
123 toolsappli_rel_path += "toolsbbtk/appli";
125 //-----------------------------------------------------------
126 // LG : REMOVE BUGGY PATH WITH include *:
128 // mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
129 //-----------------------------------------------------------
130 int iStrVec,sizeStrVec;
132 sizeStrVec = mBbs_paths.size();
133 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
134 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
138 // always add "." (current working directory) at the begining
139 mPackage_paths.push_back(".");
140 // add system bin path (for build tree / standalone folder install)
141 mPackage_paths.push_back(mBin_path);
142 // add system lib path (for install tree)
143 //EED mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
144 mPackage_paths.push_back(mInstall_path + "/lib");
146 // add bin/Debug bin/Release paths (for build/install tree)
147 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
148 mPackage_paths.push_back(mBin_path + "/Debug");
149 //EED mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
150 mPackage_paths.push_back(mBin_path + "/Release");
153 sizeStrVec = mPackage_paths.size();
154 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
155 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
162 // ==> First we look for bbtk_config.xml in "."
164 const char *currentDir = getcwd(buf, 2048);
168 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
169 // \todo : what else?
170 // How abort a constructor and warn the caller function?
171 // LG : throw an exception
174 // std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
175 std::string configXmlFullPathName = currentDir ;
176 configXmlFullPathName += "/bbtk_config.xml";
177 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
179 if ( Utilities::FileExists( configXmlFullPathName ))
181 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
182 "] found in current directory" << std::endl);
183 //Read(configXmlFullPathName.c_str());
184 // traiter le fichier local
187 // ==> Then we look for bbtk_config.xml in ".bbtk"
190 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
191 if (!Utilities::FileExists( configXmlFullPathName ))
193 // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
198 // In any case, deal with bbtk_config.xml!
199 Read(configXmlFullPathName.c_str());
201 //=========================================================================
203 //=========================================================================
205 ConfigurationFile::~ConfigurationFile()
208 //=========================================================================
213 //=========================================================================
214 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
218 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
219 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
220 fp = fopen (configXml, "w");
221 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
222 fprintf(fp, "<config>\n");
223 fprintf(fp, " <bbs_path> </bbs_path>\n");
224 fprintf(fp, " <package_path> </package_path>\n");
225 fprintf(fp, " <default_temp_dir> $ </default_temp_dir>\n");
226 fprintf(fp, "</config>\n");
229 //=========================================================================
233 //=========================================================================
234 // From http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
236 int get_app_path (char *pname, size_t pathsize)
239 /* Oddly, the readlink(2) man page says no NULL is appended. */
240 /* So you have to do it yourself, based on the return value: */
241 pathsize --; /* Preserve a space to add the trailing NULL */
242 long result = readlink("/proc/self/exe", pname, pathsize);
245 pname[result] = 0; /* add the #@!%ing NULL */
247 if ((access(pname, 0) == 0))
248 return 0; /* file exists, return OK */
249 /*else name doesn't seem to exist, return FAIL (falls
255 long result = GetModuleFileName(NULL, pname, pathsize);
258 /* fix up the dir slashes... */
259 int len = strlen(pname);
261 for (idx = 0; idx < len; idx++)
263 if (pname[idx] == '\\') pname[idx] = '/';
266 for (idx = len-1; idx >=0 ; idx--)
268 if (pname[idx] == '/')
275 if ((access(pname, 0) == 0))
276 return 0; /* file exists, return OK */
277 /*else name doesn't seem to exist, return FAIL (falls
283 char *p = getexecname();
286 /* According to the Sun manpages, getexecname will
287 "normally" return an */
288 /* absolute path - BUT might not... AND that IF it is not,
290 /* getcwd() will "usually" be the correct thing... Urgh!
293 /* check pathname is absolute (begins with a / ???) */
294 if (p[0] == '/') /* assume this means we have an
297 strncpy(pname, p, pathsize);
298 if ((access(pname, 0) == 0))
299 return 0; /* file exists, return OK */
301 else /* if not, prepend getcwd() then check if file
304 getcwd(pname, pathsize);
305 long result = strlen(pname);
306 strncat(pname, "/", (pathsize - result));
308 strncat(pname, p, (pathsize - result));
310 if ((access(pname, 0) == 0))
311 return 0; /* file exists, return OK */
312 /*else name doesn't seem to exist, return FAIL
318 #ifdef MACOSX /* assume this is OSX */
320 from http://www.hmug.org/man/3/NSModule.html
322 extern int _NSGetExecutablePath(char *buf, unsigned long
325 _NSGetExecutablePath copies the path of the executable
326 into the buffer and returns 0 if the path was successfully
327 copied in the provided buffer. If the buffer is not large
328 enough, -1 is returned and the expected buffer size is
329 copied in *bufsize. Note that _NSGetExecutablePath will
330 return "a path" to the executable not a "real path" to the
331 executable. That is the path may be a symbolic link and
332 not the real file. And with deep directories the total
333 bufsize needed could be more than MAXPATHLEN.
336 char *given_path = (char*)malloc(MAXPATHLEN * 2);
337 if (!given_path) return status;
339 uint32_t npathsize = MAXPATHLEN * 2;
340 long result = _NSGetExecutablePath(given_path, &npathsize);
342 { /* OK, we got something - now try and resolve the real path...
344 if (realpath(given_path, pname) != NULL)
346 if ((access(pname, 0) == 0))
347 status = 0; /* file exists, return OK */
354 return -1; /* Path Lookup Failed */
359 //=========================================================================
360 std::string ConfigurationFile::GetExecutablePath()
363 int err = get_app_path(name, PATH_MAX);
366 bbtkError("Could not determine current executable path ?");
369 // remove the exe name
371 slash = strrchr(name, VALID_FILE_SEPARATOR_CHAR);
376 printf("EED ConfigurationFile::GetExecutablePath %s\n",name);
381 /// \todo : Think to delete it!
382 char *buf = (char *)malloc(512);
385 bbtkError("Could not allocate 512 bytes !");
390 GetModuleFileName(NULL, buf, 511);
391 slash = strrchr(buf, '\\');
399 #if defined(__GNUC__)
401 res = readlink("/proc/self/exe", buf, 512);
408 slash = strrchr(buf, '/');
414 std::string ret(buf);
418 //=========================================================================
421 //=========================================================================
422 void ConfigurationFile::InstallPath ()
425 /*--------------------------------------------------
426 New policy for bbtk_config.xml :
428 if bbtk_config.xml found in current directory (user is an aware user!)
431 else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
434 else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
435 copy it as .bbtk/bbtk_config.xml
437 else (nothing installed)
438 create a minimum version in HOME/.bbtk
439 ----------------------------------------------------*/
442 // -----------------------------------------------------------------
443 #if defined(__GNUC__)
445 // ------------------ create some usefull strings ----------------
446 // installed bbtk_path
448 strcpy(bbtk_path, "/usr/local/bin");
451 char rootDirectory[200];
452 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
455 char configPath[200];
456 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
460 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
463 char configXmlTmp[250];
464 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
469 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
471 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
473 system(makeDir); // create .bbtk
476 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
477 CreateConfigXML( rootDirectory );// create .bbtk
481 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
482 if (!Utilities::FileExists(rootDirectory))
484 //std::cout << "makeDir[" << makeDir << "]" << std::endl;
488 if (!Utilities::FileExists(configPath))
495 // ------------------------------------------------------------------
499 // installed bbtk_path
501 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
502 char bbtk_path2[100];
503 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
506 char rootDirectory[200];
507 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
508 // std::cout << "[" << rootDirectory << "]" << std::endl;
511 char configPath[200];
512 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
516 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
519 char configXmlTmp[250];
520 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
525 if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
527 if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
529 system(makeDir); // create .bbtk
532 // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
533 CreateConfigXML( rootDirectory );// create .bbtk
537 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
539 int attribs = GetFileAttributes (rootDirectory);
540 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
541 if ( attribs != 0xFFFFFFFF)
543 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
545 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
557 // ------------------------------------------------------------------
559 /// \todo ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
565 //=========================================================================
569 //=========================================================================
570 // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
573 void ConfigurationFile::Read(const std::string& filename)
576 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
578 mConfig_xml_full_path = filename;
579 XMLResults* res = new XMLResults;
580 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
582 if ( res->error != eXMLErrorNone )
584 std::string mess = GetErrorMessage(res,filename);
586 bbtkDebugMessage("Config",1,mess<< std::endl);
591 bbtkDebugMessage("Config",1,"OK" << std::endl);
596 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
599 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
604 if( BB.nChildNode((XMLCSTR)"url") )
605 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
608 if( BB.nChildNode((XMLCSTR)"data_path") )
609 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
612 // if( BB.nChildNode((XMLCSTR)"install_path") )
613 // GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
615 // add user bbs paths
616 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
619 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
620 mBbs_paths.push_back(val);
625 // add user package path
626 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
629 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
630 mPackage_paths.push_back(val);
634 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
635 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
637 if ( mDefault_temp_dir == "$") // no value found in config_xml
639 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
640 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
645 //=========================================================================
647 //=========================================================================
648 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
650 bbtkDebugMessageInc("Config",9,
651 "ConfigurationFile::AddPackagePathsAndWrite("
652 <<path<<")"<<std::endl);
654 XMLResults* res = new XMLResults;
656 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
657 (XMLCSTR)"config",res);
659 if ( res->error != eXMLErrorNone )
661 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
663 bbtkDebugMessage("Config",1,mess<< std::endl);
669 std::string bbs_path = path + "/bbs";
671 std::string bbs_path = path + "/share/bbtk/bbs" ;
673 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
674 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
675 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
676 mBbs_paths.push_back(bbs_path);
679 std::string pack_path = path + "/bin";
681 std::string pack_path = path ;
683 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
684 PACKPATH.addText((XMLCSTR)pack_path.c_str());
685 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
686 mPackage_paths.push_back(pack_path);
689 pack_path = path + "/Debug";
690 PACKPATH = BB.addChild((XMLCSTR)"package_path");
691 PACKPATH.addText((XMLCSTR)pack_path.c_str());
692 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
693 mPackage_paths.push_back(pack_path);
694 pack_path = path + "/Release";
695 PACKPATH = BB.addChild((XMLCSTR)"package_path");
696 PACKPATH.addText((XMLCSTR)pack_path.c_str());
697 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
698 mPackage_paths.push_back(pack_path);
702 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
703 if ( err != eXMLErrorNone )
705 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
706 bbtkDebugMessage("Config",1,mess<< std::endl);
712 //=========================================================================
714 //=========================================================================
715 void ConfigurationFile::GetHelp(int level) const
717 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
720 const std::string config_xml_full_path = Get_config_xml_full_path();
721 const std::string description = Get_description();
722 const std::string url = Get_doc_path();
723 const std::string data_path = Get_data_path();
724 const std::string default_temp_dir = Get_default_temp_dir();
725 const std::string file_separator = Get_file_separator();
726 const std::vector<std::string>bbs_paths = Get_bbs_paths();
727 const std::vector<std::string>package_paths = Get_package_paths();
729 bbtkMessage("Help",level, "=============" << std::endl);
730 bbtkMessage("Help",level, "Configuration" << std::endl);
731 bbtkMessage("Help",level, "=============" << std::endl);
732 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
733 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
734 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
735 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
736 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
738 std::vector<std::string>::const_iterator i;
740 bbtkMessage("Help",level, "BBS Paths " << std::endl);
741 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
743 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
746 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
747 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
749 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
752 bbtkDebugDecTab("Config",9);
754 //=========================================================================