]> Creatis software - bbtk.git/blob - kernel/src/bbtkConfigurationFile.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkConfigurationFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkConfigurationFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/03 08:06:36 $
7   Version:   $Revision: 1.12 $
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.
12                                                                                 
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.
16
17 =========================================================================*/
18 /**
19  *\file
20  *\brief Class bbtk::ConfigurationFile
21  */
22
23 #include "bbtkConfigurationFile.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkXML.h"
26 #include "bbtkUtilities.h"
27
28 #if defined(WIN32)
29 #include <direct.h> // for getcwd
30 #endif
31
32 namespace bbtk
33 {
34
35   //====================================================================
36   /// Constructor
37   ConfigurationFile::ConfigurationFile()
38   {
39
40     // file separator
41 #if defined(_WIN32)
42     mFile_separator = "\\";
43 #else
44     mFile_separator = "/";
45 #endif
46     
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);
67
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);
73     
74     
75     // bbs_paths
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);
84 #ifdef WIN32
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);
90 #endif
91
92    
93     
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");
100 #ifdef WIN32
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");
104 #endif
105     
106     GetHelp(2);
107    
108     // ==> First we look for bbtk_config.xml in "."
109     char buf[2048];
110     const char *currentDir = getcwd(buf, 2048);
111     
112     if( !currentDir )
113       {
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 
118       }
119     
120     std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
121     
122     if ( Utilities::FileExists( configXmlFullPathName ))
123       {
124         bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName << 
125                     "] found in current directory" << std::endl); 
126         //Read(configXmlFullPathName.c_str());
127         // traiter le fichier local     
128       }
129     
130     // ==> Then we look for bbtk_config.xml in ".bbtk"
131     else 
132       {
133 #if defined(__GNUC__)
134         std::string str_home(getenv("HOME"));
135 #elif defined(_WIN32)
136         std::string str_home(getenv("USERPROFILE"));
137 #endif
138         configXmlFullPathName = str_home + mFile_separator + ".bbtk/bbtk_config.xml";
139         if (!Utilities::FileExists( configXmlFullPathName ))
140           {         
141             // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
142             InstallPath ();
143           }
144       }
145     
146     // In any case, deal with bbtk_config.xml!
147     Read(configXmlFullPathName.c_str());
148   }
149   //=========================================================================
150
151   //=========================================================================
152   /// Destructor
153   ConfigurationFile::~ConfigurationFile()
154   {
155   }
156   //=========================================================================
157   
158
159
160
161   //=========================================================================
162   void ConfigurationFile::CreateConfigXML( char *rootDirectory )
163   {
164     FILE *fp;
165     char configXml[250];
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");
184       fclose(fp);
185   }
186   //=========================================================================  
187
188
189   //=========================================================================
190   std::string ConfigurationFile::GetExecutablePath()
191   {
192     /// \todo : Think to delete it!
193     char *buf = (char *)malloc(512);
194     char *slash;
195     
196 #if defined(WIN32)
197     GetModuleFileName(NULL, buf, 511);
198     slash = strrchr(buf, '\\');
199     if (slash)
200       {
201         *slash = 0;
202       }
203 #elif defined(__GNUC__)
204     int res;
205     res = readlink("/proc/self/exe", buf, 512);
206     if (res == -1)
207       return "";
208     buf[res] = 0;
209     slash = strrchr(buf, '/');
210     if (slash)
211       {
212         *slash = 0;
213       }
214 #else
215     return "";
216 #endif
217     std::string ret(buf);
218     free(buf);
219     return ret;
220   }
221   //=========================================================================
222
223
224   //=========================================================================
225   void ConfigurationFile::InstallPath ()
226   {
227     
228     /*--------------------------------------------------
229       New policy for bbtk_config.xml :
230       
231       if bbtk_config.xml found in current directory (user is an aware user!)
232       use it!
233       
234       else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
235       use it!
236       
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
239       
240       else (nothing installed)
241       create a minimum version in HOME/.bbtk
242       ----------------------------------------------------*/
243     
244     
245     // -----------------------------------------------------------------
246 #if defined(__GNUC__)
247     
248     // ------------------ create some usefull strings ----------------
249     // installed bbtk_path
250     char bbtk_path[100];
251     strcpy(bbtk_path, "/usr/local/bin");
252     
253     // rootDirectory
254     char rootDirectory[200];
255     sprintf( rootDirectory,  "%s/.bbtk", getenv("HOME"));
256     
257     // configPath
258     char configPath[200];
259     sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
260     
261     // makeDir
262     char makeDir[250];
263     sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
264     
265     // configXmlTmp
266     char configXmlTmp[250]; 
267     sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
268     
269     // copyFile
270     char copyFile[250];
271     
272     if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
273       {  
274         if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
275           {
276             system(makeDir);  // create .bbtk
277           }
278         
279         // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
280         CreateConfigXML( rootDirectory );// create .bbtk
281       }
282     else
283       {
284         sprintf(copyFile,"cp %s  %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
285         if (!Utilities::FileExists(rootDirectory))
286           {
287             //std::cout << "makeDir[" << makeDir << "]" << std::endl;
288             system(makeDir);
289           }
290         
291         if (!Utilities::FileExists(configPath))
292           {
293             system(copyFile);
294           }
295       }
296     return;
297     
298     // ------------------------------------------------------------------
299 #elif defined(WIN32)
300     
301     
302     // installed bbtk_path
303     char bbtk_path[100];
304     strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
305     char bbtk_path2[100];
306     strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
307     
308     // rootDirectory
309     char rootDirectory[200];  
310     sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
311     //  std::cout << "[" << rootDirectory << "]" << std::endl;
312     
313     // configPath
314     char configPath[200];
315     sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
316     
317     // makeDir
318     char makeDir[250];
319     sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
320     
321     // configXmlTmp
322     char configXmlTmp[250]; 
323     sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
324     
325     // copyFile
326     char copyFile[250];
327     
328     if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
329       {
330         if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
331           {
332             system(makeDir);  // create .bbtk
333           }
334         
335         // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
336         CreateConfigXML( rootDirectory );// create .bbtk
337         return;
338       }  
339     
340     sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
341     
342     int attribs = GetFileAttributes (rootDirectory);
343     bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
344     if ( attribs != 0xFFFFFFFF)
345       {
346         if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
347           {
348             if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
349               {
350                 system(copyFile);  
351               } 
352           }
353       } 
354     else 
355       {
356         system(makeDir);
357         system(copyFile); 
358       }    
359     return;  
360     // ------------------------------------------------------------------    
361 #else
362 /// \todo  ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
363
364   return;    
365 #endif
366
367 }
368   //=========================================================================
369
370
371
372   //=========================================================================
373   // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
374   //      bbtk_config.xml
375   
376   void ConfigurationFile::Read(const std::string& filename)
377   {
378     
379     bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
380     
381     mConfig_xml_full_path = filename;
382     XMLResults* res = new XMLResults;
383     XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
384     
385     if ( res->error != eXMLErrorNone ) 
386       {
387         std::string mess = GetErrorMessage(res,filename);
388         delete res;
389         bbtkDebugMessage("Config",1,mess<< std::endl);
390         bbtkError(mess);
391       }
392     delete res;
393     
394     bbtkDebugMessage("Config",1,"OK" << std::endl);
395   
396     int i,j;
397     
398     // Description
399     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++) 
400       {
401         std::string val;
402         GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
403         mDescription += val;
404       }    
405     
406     // Url
407     if( BB.nChildNode((XMLCSTR)"url") ) 
408       GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
409     
410     // Data_Path
411     if( BB.nChildNode((XMLCSTR)"data_path") ) 
412       GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
413     
414     // install_path
415     //  if( BB.nChildNode((XMLCSTR)"install_path") )
416     //   GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
417     
418     // add user bbs paths
419     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++) 
420       {
421         std::string val;
422         GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
423         mBbs_paths.push_back(val);
424       }
425     
426     // package_paths
427     
428     // add user package path
429     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++) 
430       {
431         std::string val;
432         GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
433         mPackage_paths.push_back(val);
434       }
435     
436     // default_temp_dir
437     if( BB.nChildNode((XMLCSTR)"default_temp_dir") ) 
438       GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
439     
440     if ( mDefault_temp_dir == "$") // no value found in config_xml
441       {
442         size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
443         mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos); 
444       }    
445
446     GetHelp(2);
447   }
448   //=========================================================================
449   
450
451   //=========================================================================
452   void ConfigurationFile::GetHelp(int level) const
453   {
454     bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
455                         <<")"<<std::endl);
456     
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();
465     
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);
474
475     std::vector<std::string>::const_iterator i;
476            
477     bbtkMessage("Help",level, "BBS Paths   " << std::endl);     
478     for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
479     {
480       bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
481     }    
482     
483     bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);     
484     for (i = package_paths.begin(); i!=package_paths.end(); ++i )
485     {
486       bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
487     }
488
489     bbtkDebugDecTab("Config",9);
490   }
491   //=========================================================================
492
493
494 } // namespace bbtk