]> Creatis software - bbtk.git/blob - kernel/src/bbtkConfigurationFile.cxx
BUG slash
[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/10 06:24:12 $
7   Version:   $Revision: 1.13 $
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
33
34 namespace bbtk
35 {
36
37   //====================================================================
38   /// Constructor
39   ConfigurationFile::ConfigurationFile()
40   {
41
42         mFile_separator = VALID_FILE_SEPARATOR;
43     
44     // ==> Set system paths 
45     mBin_path = GetExecutablePath();
46 //EED    mInstall_path = mBin_path + mFile_separator + "..";
47     mInstall_path = mBin_path + "/..";
48     // The relative path to the doc folder (=BBTK_DOC_REL_PATH)
49     mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
50     // The path to the doc folder (=mInstall_path+"/"+mDoc_rel_path)
51 //EED    mDoc_path = mInstall_path + mFile_separator + mDoc_rel_path;
52     mDoc_path = mInstall_path + "/" + 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 //EED    mBbs_path = mInstall_path + mFile_separator + mBbs_rel_path;
57     mBbs_path = mInstall_path + "/" + mBbs_rel_path;
58     // The relative path to the rsc folder (=BBTK_RSC_REL_PATH)
59     //   mRsc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_RSC_REL_PATH);
60     // The path to the rsc folder (=mInstall_path+"/"+mRsc_rel_path)
61     //   mRsc_path = mInstall_path + mFile_separator + mRsc_rel_path;
62     // The path to the bbtk data folder 
63     // Initialized to mInstall_path+"/"+BBTK_DATA_REL_PATH
64     // But can be overriden by value read from bbtk_config.xml
65 //EED    mData_path = mInstall_path + mFile_separator + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
66     mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
67
68         Utilities::replace( mBin_path           , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
69         Utilities::replace( mInstall_path       , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
70         Utilities::replace( mDoc_rel_path       , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
71         Utilities::replace( mDoc_path           , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
72         Utilities::replace( mBbs_path           , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
73         Utilities::replace( mData_path          , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
74
75     bbtkMessage("Config",1," ==> bin    : '"<<mBin_path<<"'"<<std::endl);
76     bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
77     bbtkMessage("Config",1," ==> doc    : '"<<mDoc_path<<"'"<<std::endl);
78     bbtkMessage("Config",1," ==> bbs    : '"<<mBbs_path<<"'"<<std::endl);
79     bbtkMessage("Config",1," ==> data   : '"<<mData_path<<"'"<<std::endl);
80
81
82
83     
84     // bbs_paths
85     // always add "." (current working directory) at the begining
86         mBbs_paths.push_back( "." ); 
87     // add system bbs path 
88     mBbs_paths.push_back(mBbs_path);
89     // add toolsbbtk/appli 
90 //EED    std::string toolsappli_rel_path(mFile_separator);
91     std::string toolsappli_rel_path("/");
92 //EED    toolsappli_rel_path +=  "toolsbbtk" + mFile_separator + "appli";
93     toolsappli_rel_path +=  "toolsbbtk/appli";
94     mBbs_paths.push_back(mBbs_path + toolsappli_rel_path);
95
96         int iStrVec,sizeStrVec;
97
98         sizeStrVec = mBbs_paths.size();
99         for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
100                 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
101         }
102
103     
104     // always add "." (current working directory) at the begining
105     mPackage_paths.push_back(".");   
106     // add system bin path (for build tree / standalone folder install)
107     mPackage_paths.push_back(mBin_path);
108     // add system lib path (for install tree)
109 //EED    mPackage_paths.push_back(mInstall_path + mFile_separator + "lib");
110     mPackage_paths.push_back(mInstall_path + "/lib");
111 #ifdef WIN32
112     // add bin/Debug bin/Release paths (for build/install tree)
113 //EED    mPackage_paths.push_back(mBin_path + mFile_separator + "Debug");
114     mPackage_paths.push_back(mBin_path + "/Debug");
115 //EED    mPackage_paths.push_back(mBin_path + mFile_separator + "Release");
116     mPackage_paths.push_back(mBin_path + "/Release");
117 #endif
118
119         sizeStrVec = mPackage_paths.size();
120         for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
121                 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
122         }
123
124
125
126     GetHelp(2);
127    
128     // ==> First we look for bbtk_config.xml in "."
129     char buf[2048];
130     const char *currentDir = getcwd(buf, 2048);
131     
132     if( !currentDir )
133       {
134         std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
135         // \todo : what else?
136         // How abort a constructor and warn the caller function?
137         // LG : throw an exception 
138       }
139     
140 //    std::string configXmlFullPathName = currentDir + mFile_separator + "bbtk_config.xml";
141     std::string configXmlFullPathName = currentDir ;
142                 configXmlFullPathName += "/bbtk_config.xml";
143         Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
144
145     if ( Utilities::FileExists( configXmlFullPathName ))
146       {
147         bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName << 
148                     "] found in current directory" << std::endl); 
149         //Read(configXmlFullPathName.c_str());
150         // traiter le fichier local     
151       }
152     
153     // ==> Then we look for bbtk_config.xml in ".bbtk"
154     else 
155       {
156 #if defined(__GNUC__)
157         std::string str_home(getenv("HOME"));
158 #elif defined(_WIN32)
159         std::string str_home(getenv("USERPROFILE"));
160 #endif
161 //EED   configXmlFullPathName = str_home + mFile_separator + ".bbtk/bbtk_config.xml";
162         configXmlFullPathName = str_home + "/.bbtk/bbtk_config.xml";
163         Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
164
165         if (!Utilities::FileExists( configXmlFullPathName ))
166           {         
167             // ==> Nothing found, we create bbtk_config.xml in ".bbtk"
168             InstallPath ();
169           }
170       }
171     
172     // In any case, deal with bbtk_config.xml!
173     Read(configXmlFullPathName.c_str());
174   }
175   //=========================================================================
176
177   //=========================================================================
178   /// Destructor
179   ConfigurationFile::~ConfigurationFile()
180   {
181   }
182   //=========================================================================
183   
184
185
186
187   //=========================================================================
188   void ConfigurationFile::CreateConfigXML( char *rootDirectory )
189   {
190     FILE *fp;
191     char configXml[250];
192     sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
193     bbtkMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
194     fp = fopen (configXml, "w");
195     fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
196     //  fprintf(fp, "<!DOCTYPE config SYSTEM \"/mnt/windows/bbtk/bbtk_config.xml>\n");
197     fprintf(fp, "<config>\n");
198     fprintf(fp, "   <description>  </description>\n");
199     //   fprintf(fp, "   <url>  http://www.creatis.insa-lyon.fr/software/bbtk  </url>\n");
200     //  fprintf(fp, "   <install_path> </install_path>\n");
201     fprintf(fp, "   <bbs_path>     </bbs_path>\n");
202     // fprintf(fp, "   <ext_dll_path> </ext_dll_path>\n");
203     fprintf(fp, "   <package_path> </package_path>\n");
204     fprintf(fp, "   <data_path> </data_path>\n");//, BBTK_DATA_PATH);
205     /// \todo find a decent default value !
206       ///fprintf(fp, "   <default_temp_dir>  %s </default_temp_dir>\n", " ");
207       // when $ will be found, default_temp_dir value will be replaced
208       fprintf(fp, "   <default_temp_dir>$</default_temp_dir>\n");
209       fprintf(fp, "</config>\n");
210       fclose(fp);
211   }
212   //=========================================================================  
213
214
215   //=========================================================================
216   std::string ConfigurationFile::GetExecutablePath()
217   {
218     /// \todo : Think to delete it!
219     char *buf = (char *)malloc(512);
220     char *slash;
221     
222 #if defined(WIN32)
223     GetModuleFileName(NULL, buf, 511);
224     slash = strrchr(buf, '\\');
225     if (slash)
226       {
227         *slash = 0;
228       }
229 #elif defined(__GNUC__)
230     int res;
231     res = readlink("/proc/self/exe", buf, 512);
232     if (res == -1)
233       return "";
234     buf[res] = 0;
235     slash = strrchr(buf, '/');
236     if (slash)
237       {
238         *slash = 0;
239       }
240 #else
241     return "";
242 #endif
243     std::string ret(buf);
244     free(buf);
245     return ret;
246   }
247   //=========================================================================
248
249
250   //=========================================================================
251   void ConfigurationFile::InstallPath ()
252   {
253     
254     /*--------------------------------------------------
255       New policy for bbtk_config.xml :
256       
257       if bbtk_config.xml found in current directory (user is an aware user!)
258       use it!
259       
260       else if bbtk_config.xml found in HOME/.bbtk (user already worked with it)
261       use it!
262       
263       else if bbtk_config.xml.tmp found in /usr/local/bin or c:\\Program Files\\BBTK\\bin
264       copy it as .bbtk/bbtk_config.xml
265       
266       else (nothing installed)
267       create a minimum version in HOME/.bbtk
268       ----------------------------------------------------*/
269     
270     
271     // -----------------------------------------------------------------
272 #if defined(__GNUC__)
273     
274     // ------------------ create some usefull strings ----------------
275     // installed bbtk_path
276     char bbtk_path[100];
277     strcpy(bbtk_path, "/usr/local/bin");
278     
279     // rootDirectory
280     char rootDirectory[200];
281     sprintf( rootDirectory,  "%s/.bbtk", getenv("HOME"));
282     
283     // configPath
284     char configPath[200];
285     sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
286     
287     // makeDir
288     char makeDir[250];
289     sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
290     
291     // configXmlTmp
292     char configXmlTmp[250]; 
293     sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
294     
295     // copyFile
296     char copyFile[250];
297     
298     if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found (not installed)
299       {  
300         if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
301           {
302             system(makeDir);  // create .bbtk
303           }
304         
305         // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
306         CreateConfigXML( rootDirectory );// create .bbtk
307       }
308     else
309       {
310         sprintf(copyFile,"cp %s  %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
311         if (!Utilities::FileExists(rootDirectory))
312           {
313             //std::cout << "makeDir[" << makeDir << "]" << std::endl;
314             system(makeDir);
315           }
316         
317         if (!Utilities::FileExists(configPath))
318           {
319             system(copyFile);
320           }
321       }
322     return;
323     
324     // ------------------------------------------------------------------
325 #elif defined(WIN32)
326     
327     
328     // installed bbtk_path
329     char bbtk_path[100];
330     strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
331     char bbtk_path2[100];
332     strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
333     
334     // rootDirectory
335     char rootDirectory[200];  
336     sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
337     //  std::cout << "[" << rootDirectory << "]" << std::endl;
338     
339     // configPath
340     char configPath[200];
341     sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
342     
343     // makeDir
344     char makeDir[250];
345     sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
346     
347     // configXmlTmp
348     char configXmlTmp[250]; 
349     sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
350     
351     // copyFile
352     char copyFile[250];
353     
354     if (!Utilities::FileExists(configXmlTmp)) // bbtk_config.xml.tmp not found
355       {
356         if (!Utilities::FileExists(rootDirectory)) // .bbtk not found
357           {
358             system(makeDir);  // create .bbtk
359           }
360         
361         // if "bbtk_path/bbtk_config.xml.tmp" doesn't exist, hard-create a minimum version in .bbtk
362         CreateConfigXML( rootDirectory );// create .bbtk
363         return;
364       }  
365     
366     sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
367     
368     int attribs = GetFileAttributes (rootDirectory);
369     bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
370     if ( attribs != 0xFFFFFFFF)
371       {
372         if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) /// \TODO : check !
373           {
374             if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
375               {
376                 system(copyFile);  
377               } 
378           }
379       } 
380     else 
381       {
382         system(makeDir);
383         system(copyFile); 
384       }    
385     return;  
386     // ------------------------------------------------------------------    
387 #else
388 /// \todo  ConfigurationFile::InstallPath() : exit when for not WIN32 and not__GNUC__
389
390   return;    
391 #endif
392
393 }
394   //=========================================================================
395
396
397
398   //=========================================================================
399   // Gets the list of directories holding bb scripts, packages, dll, ... from the xml file
400   //      bbtk_config.xml
401   
402   void ConfigurationFile::Read(const std::string& filename)
403   {
404     
405     bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
406     
407     mConfig_xml_full_path = filename;
408     XMLResults* res = new XMLResults;
409     XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
410     
411     if ( res->error != eXMLErrorNone ) 
412       {
413         std::string mess = GetErrorMessage(res,filename);
414         delete res;
415         bbtkDebugMessage("Config",1,mess<< std::endl);
416         bbtkError(mess);
417       }
418     delete res;
419     
420     bbtkDebugMessage("Config",1,"OK" << std::endl);
421   
422     int i,j;
423     
424     // Description
425     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++) 
426       {
427         std::string val;
428         GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
429         mDescription += val;
430       }    
431     
432     // Url
433     if( BB.nChildNode((XMLCSTR)"url") ) 
434       GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
435     
436     // Data_Path
437     if( BB.nChildNode((XMLCSTR)"data_path") ) 
438       GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
439     
440     // install_path
441     //  if( BB.nChildNode((XMLCSTR)"install_path") )
442     //   GetTextOrClear(BB.getChildNode((XMLCSTR)"install_path"),mInstall_path);
443     
444     // add user bbs paths
445     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++) 
446       {
447         std::string val;
448         GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
449         mBbs_paths.push_back(val);
450       }
451     
452     // package_paths
453     
454     // add user package path
455     for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++) 
456       {
457         std::string val;
458         GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
459         mPackage_paths.push_back(val);
460       }
461     
462     // default_temp_dir
463     if( BB.nChildNode((XMLCSTR)"default_temp_dir") ) 
464       GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
465     
466     if ( mDefault_temp_dir == "$") // no value found in config_xml
467       {
468         size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
469         mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos); 
470       }    
471
472     GetHelp(2);
473   }
474   //=========================================================================
475   
476
477   //=========================================================================
478   void ConfigurationFile::GetHelp(int level) const
479   {
480     bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
481                         <<")"<<std::endl);
482     
483     const std::string config_xml_full_path      = Get_config_xml_full_path();    
484     const std::string description               = Get_description();
485     const std::string url                       = Get_doc_path();
486     const std::string data_path                 = Get_data_path();
487     const std::string default_temp_dir          = Get_default_temp_dir();    
488     const std::string file_separator            = Get_file_separator();    
489     const std::vector<std::string>bbs_paths     = Get_bbs_paths();
490     const std::vector<std::string>package_paths = Get_package_paths();
491     
492     bbtkMessage("Help",level, "============="   << std::endl);           
493     bbtkMessage("Help",level, "Configuration"   << std::endl);
494     bbtkMessage("Help",level, "============="   << std::endl);
495     bbtkMessage("Help",level, "bbtk_config.xml    : [" << config_xml_full_path  << "]" << std::endl); 
496     bbtkMessage("Help",level, "Documentation Path : [" << url             << "]" << std::endl);
497     bbtkMessage("Help",level, "Data Path          : [" << data_path       << "]" << std::endl);
498     bbtkMessage("Help",level, "Temp Directory     : [" << default_temp_dir << "]" << std::endl);
499     bbtkMessage("Help",level, "File Separator     : [" << file_separator  << "]" << std::endl);
500
501     std::vector<std::string>::const_iterator i;
502            
503     bbtkMessage("Help",level, "BBS Paths   " << std::endl);     
504     for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
505     {
506       bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
507     }    
508     
509     bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);     
510     for (i = package_paths.begin(); i!=package_paths.end(); ++i )
511     {
512       bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
513     }
514
515     bbtkDebugDecTab("Config",9);
516   }
517   //=========================================================================
518
519
520 } // namespace bbtk