]> Creatis software - bbtk.git/blob - kernel/src/bbtkUtilities.cxx
4bf1689be6aed0a62dee70aa01cc4db48b078df3
[bbtk.git] / kernel / src / bbtkUtilities.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkUtilities.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.15 $
34 =========================================================================*/
35
36
37
38
39 #include "bbtkUtilities.h"
40 #include "bbtkMessageManager.h"
41
42 //EED 2018-07-16
43 #if defined(_WIN32) 
44         #include <shlwapi.h>
45 #endif
46
47 #if defined(MACOSX) // assume this is OSX 
48 # include <sys/param.h>
49 # include <mach-o/dyld.h> // _NSGetExecutablePath : must add -framework CoreFoundation to link line 
50 # include <string.h>
51 # ifndef PATH_MAX
52 #  define PATH_MAX MAXPATHLEN
53 # endif
54 #endif // MACOSX
55
56 #ifndef PATH_MAX // If not defined yet : do it 
57 #  define PATH_MAX 2048
58 #endif 
59
60 namespace bbtk
61 {
62
63
64
65             // ======================================================================
66     // See : http://www.techbytes.ca/techbyte103.html for more O.S.
67     bool Utilities::FileExists(std::string strFilename) 
68     {
69       struct stat stFileInfo;
70      bool blnReturn;
71      int intStat;
72      
73      // Attempt to get the file attributes
74      intStat = stat(strFilename.c_str(),&stFileInfo);
75      if(intStat == 0) 
76        {
77          // We were able to get the file attributes
78          // so the file obviously exists.
79          blnReturn = true;
80        } 
81      else 
82        {
83          // We were not able to get the file attributes.
84          // This may mean that we don't have permission to
85          // access the folder which contains this file. If you
86          // need to do that level of checking, lookup the
87          // return values of stat which will give you
88          // more details on why stat failed.
89          blnReturn = false;
90        }
91      
92      return(blnReturn);
93     }
94     
95     
96     // =====================================================================
97     
98     std::string Utilities::ExtractPackageName(const std::string  &name, 
99                                           std::string& path)
100     {
101       std::string pkgname;
102       path = "";
103       
104       std::string::size_type slash_position = name.find_last_of("/\\");
105       if (slash_position != std::string::npos) 
106         {
107           pkgname = name.substr(slash_position+1,std::string::npos);
108           path = name.substr(0,slash_position);
109           //    std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
110         }
111       else 
112         {
113           pkgname = name;
114         }
115       
116       // remove {.so | dll} if any
117       std::string::size_type dot_position = pkgname.find_last_of('.');      
118       if (dot_position != std::string::npos){
119         pkgname = pkgname.substr(0,dot_position);
120       }      
121 #if defined(__GNUC__)
122       
123       // GCC mechanism
124       // shared lib name = libbb<name>.so
125       
126       // remove {libbb} if any
127       if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
128         pkgname =  pkgname.substr(5, pkgname.length());
129       }
130       /*
131      /// \ \todo     what would happen if (stupid) user names his package 'libbb' ?!?
132       /// \ --> Should be forbidden!
133       */
134 #elif defined(_WIN32)
135       
136       // WIN 32 mechanism
137       // shared lib name = <name>.dll
138       
139       // EED Problem loading package call bbtkTools
140       //     // remove {bb} if any
141       if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
142         pkgname =  pkgname.substr(2, pkgname.length());  
143       }
144       
145       /*
146      /// \ \todo     what would happen if (stupid) user names his package 'bb' ?!?
147      /// \ --> Should be forbidden!
148      */
149 #else
150       bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
151 #endif      
152       return pkgname;
153     }
154     
155     //=====================================================================
156     std::string Utilities::ExtractScriptName(const std::string &name,
157                                          std::string& path)
158     {
159       std::string pkgname;
160       
161       std::string::size_type slash_position = name.find_last_of("/\\");
162       if (slash_position != std::string::npos) {
163         pkgname =name.substr(slash_position+1,std::string::npos);
164         path = name.substr(0,slash_position);      
165       } else {
166         pkgname = name;
167       }
168       // remove {.bbs } if any
169       std::string::size_type dot_position = pkgname.find_last_of('.');
170       if (dot_position != std::string::npos){
171         pkgname = pkgname.substr(0,dot_position);
172       }
173       return pkgname;
174     }
175     
176     // ========================================================================
177
178     std::string Utilities::ExpandLibName(const std::string &name, bool verbose)
179     {
180       // -----   Think of expanding path name ( ./ ../ ../../ )
181       
182       char buf[2048]; // for getcwd
183       char * currentDir = getcwd(buf, 2048);
184       std::string cwd(currentDir);
185       std::string libname(name);
186       std::string fileSeparator;
187       fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
188       // tooHigh : true is user supplies a library pathname with too many "../"
189       bool tooHigh = false;
190       
191       //std::cout << "------------------cwd ["  << cwd << "] name [" << name << "]" << std::endl;
192       
193       if ( name[0] == '/' ||  name[1] == ':' ) // Linux or Windows absolute name
194         {
195           return(libname);
196         }
197       else if  ( name =="." )
198         {
199           libname = cwd  + fileSeparator;
200           return(libname);
201         }
202       else if  (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
203         {
204           libname = cwd  + fileSeparator + name.substr(2, name.length());
205           return(libname);
206         }
207       else if ( name[0] == '.' &&  name[1] == '.' /*  && (name[2] == '/' || name[2] == '\\') */ ) 
208         {
209           if ( IsAtRoot(cwd) )  // hope it gets / (for Linux),  C: D: (for Windows)
210       {  
211      // if we are already at / or c: --> hopeless  
212          if (verbose)
213            std::cout << "   File path [" <<  name << "] doesn't exist" << std::endl;
214          tooHigh = true;
215       }
216       else
217       {
218          // iterate on ../ and go up from the current working dir!
219          std::string a(name); 
220          bool alreadyProcessRoot = false;
221
222           //if (a[a.size()-1] != fileSeparator[0])
223           //   a.append(fileSeparator);
224 //std::cout << "------------------a ["  << a << "]" << std::endl;
225
226          for(;;)  // wild loop !
227          {
228             std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
229             if (slash_position != std::string::npos) {
230              if (slash_position == 0)
231                 slash_position = 1;
232               cwd = cwd.substr(0,slash_position/*+1*/);
233 //std::cout << "------------------cwd ["  << cwd << "]" << std::endl;
234             //  if (a == "..") {
235             //    a = "";
236             //    break;
237             //   }
238             //   else
239                  a = a.substr(3, /*name.length()*/ a.length());  // remove ../
240 //std::cout << "------------------a ["  << a << "]" << std::endl;  
241               if (a == "" || alreadyProcessRoot)
242               {
243                 if (verbose)
244                   std::cout << "   File path : [" <<  name << "] doesn't exist" << std::endl;
245                 tooHigh = true;
246                 break;
247               }
248              // std::string b = cwd + a;
249               libname =  cwd;
250               char c = cwd[cwd.size()-1];
251               if (c != '/' && c != '\\' )
252                 libname += fileSeparator;
253               libname += a;
254
255               if ( a[0] != '.' ) // if . (probabely ../), loop again
256                 break;
257
258               if (IsAtRoot(cwd))
259                 alreadyProcessRoot = true;
260             }
261          } // end iterating on ../
262       }
263 //std::cout << "------------------out of loop]" << std::endl;        
264       if (tooHigh)
265          libname="";
266       return (libname);
267
268     }  // -----   End of expanding path name   ( ./ ../ ../../ )
269
270     std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
271     // To avoid warning
272     return(""); // Will never get here!
273   }
274
275 // ===================================================================================
276
277   std::string Utilities::MakeLibnameFromPath(std::string path, std::string pkgname)
278   {
279     std::string libname = path;
280         if(path.size()>0){
281                 char c = path[path.size()-1];    
282 #if defined(__GNUC__)
283        if (c != '/')
284           libname += "/libbb";
285        else
286           libname += "libbb";
287        libname += pkgname;
288 #if defined(MACOSX)
289           libname += ".dylib";
290 #else
291           libname += ".so";
292 #endif  
293           
294 #elif defined(_WIN32)
295        if (c != '\\') 
296           libname += "\\bb";
297        else
298           libname += "bb";
299        libname += pkgname;
300        libname += ".dll";
301 #endif
302         }
303     
304     return libname;    
305   }
306
307 // ===================================================================================
308
309   std::string Utilities::MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
310   {
311     std::string libname = path;
312         if(path.size()>0){
313                 char c = path[path.size()-1];
314                 if (c != '/' && c != '\\')
315                 {
316                    libname +=  ConfigurationFile::GetInstance().Get_file_separator ();
317                 }
318                 libname += pkgname;
319                 if (addExt)
320                 {
321                    int l = libname.size();
322                    if (l>4)
323                    {
324                           if (libname.substr(l-4, 4) != ".bbs")
325                           {
326                                    libname = libname + ".bbs";
327                           }
328                    }
329                 }
330         }
331     
332     return libname;
333   }
334   // =======================================================================
335
336   // =======================================================================
337   /// Returns the user settings dir, e.g. /home/username/.bbtk
338   std::string Utilities::GetUserSettingsDir()
339   {
340     std::string str_Home=Utilities::GetEnvHome();
341     std::string fullname = str_Home + "/.bbtk";
342     MakeValidFileName(fullname);
343     return fullname;
344   }
345   
346
347   // =======================================================================
348   /// Builds the complete path to the file 'name' located 
349   /// in user settings dir, e.g. /home/username/.bbtk/
350   std::string Utilities::MakeUserSettingsFullFileName(const std::string& name)
351   {
352     std::string str_home=Utilities::GetEnvHome();
353     std::string fullname = str_home + "/.bbtk/" + name;
354     MakeValidFileName(fullname);
355     return fullname;
356   }
357   // =======================================================================
358   
359
360   // =======================================================================
361   void Utilities::CreateDirectoryIfNeeded( std::string const &dirName)
362   {
363     if (FileExists(dirName)) return;
364     std::string cmd("mkdir \"");
365     cmd += dirName;
366     cmd += "\"";
367     system(cmd.c_str());
368   }  
369   // =======================================================================
370   
371   
372   //========================================================================
373   bool Utilities::IsAtRoot(std::string cwd)
374   {
375     if ( cwd == "/"              // hope it gets /     (for Linux)
376          || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
377       return (true);
378     else
379       return(false);
380   }
381   // ======================================================================
382
383   // ======================================================================
384   bool Utilities::IsDirectory(std::string const &dirName)
385   {
386     struct stat fs;
387     
388     if ( stat(dirName.c_str(), &fs) == 0 )
389       {
390 #if _WIN32
391         return ((fs.st_mode & _S_IFDIR) != 0);
392 #else
393         return S_ISDIR(fs.st_mode);
394 #endif
395       }
396     else
397       {
398         return false;
399       }
400   }
401   // =======================================================================
402     
403   // =======================================================================
404   void Utilities::SplitAroundFirstDot( const std::string& in,
405                                        std::string& left,
406                                        std::string& right)
407   {
408     std::string delimiter = ".";
409     std::string::size_type pos = in.find_first_of(delimiter);
410     if (std::string::npos != pos) 
411       {
412         left = in.substr(0,pos);
413         right = in.substr(pos+1,in.size());
414         
415       }
416     else
417       {
418         left ="";
419         right = "";
420         bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
421       }
422   }
423   //=======================================================================
424
425   //=======================================================================
426   void Utilities::SplitString ( const std::string& str, 
427                                 const std::string& delimiters, 
428                                 std::vector<std::string>& tokens)
429   {
430     // Skip delimiters at beginning.
431     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
432     // Find first delimiter.
433     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
434     
435     while (std::string::npos != pos || std::string::npos != lastPos)
436       {
437         // Found a token, add it to the vector.
438         tokens.push_back(str.substr(lastPos, pos - lastPos));
439         // Skip delimiters.  Note the "not_of"
440         lastPos = str.find_first_not_of(delimiters, pos);
441         // Find next delimiter
442         pos = str.find_first_of(delimiters, lastPos);
443       }
444     
445   }
446   //=======================================================================
447   
448   
449   // ======================================================================
450   std::string Utilities::get_file_name(const std::string& s) 
451   { 
452     std::string::size_type slash_position = s.find_last_of("/\\");
453     if (slash_position != std::string::npos) 
454       {
455         return  s.substr(slash_position+1,std::string::npos);   
456       }
457     else 
458       {
459         return s;
460       }
461   }
462   //=======================================================================
463  
464
465   // ========================================================================
466   /**
467    * \brief   Explore a directory with possibility of recursion
468    *          return number of files read
469    * @param  dirpath   directory to explore
470    * @param  recursive whether we want recursion or not
471    */
472   int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
473   {
474     int numberOfFiles = 0;
475     std::string fileName;
476       
477       std::string dirName = dirpath;
478       
479 #ifdef _MSC_VER
480       WIN32_FIND_DATA fileData;
481    HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData);
482
483    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
484        b = FindNextFile(hFile, &fileData))
485    {
486       fileName = fileData.cFileName;
487       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
488       {
489          // Need to check for . and .. to avoid infinite loop
490          if ( fileName != "." && fileName != ".." && recursive )
491          {
492             numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
493          }
494       }
495       else
496       {
497          Filenames.push_back(dirName+"\\"+fileName);
498          numberOfFiles++;
499       }
500    }
501    DWORD dwError = GetLastError();
502    if (hFile != INVALID_HANDLE_VALUE) 
503       FindClose(hFile);
504    if (dwError != ERROR_NO_MORE_FILES) 
505    {
506       LPVOID lpMsgBuf;
507       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
508                     FORMAT_MESSAGE_FROM_SYSTEM|
509                     FORMAT_MESSAGE_IGNORE_INSERTS,
510                     NULL,GetLastError(),
511                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
512                     (LPTSTR) &lpMsgBuf,0,NULL);
513
514      // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
515      //             <<" for the directory : "<<dirName);
516       
517       return 0;
518    }
519
520 #else
521   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
522   // work on debian for example
523 //std::cout <<"in Explor dirname[" << dirName << "]" << std::endl; 
524    DIR* dir = opendir(dirName.c_str());
525    if (!dir)
526    {
527       return 0;
528    }
529 //std::cout <<"Open OK" << std::endl; 
530    // According to POSIX, the dirent structure contains a field char d_name[]
531    // of unspecified size, with at most NAME_MAX characters preceeding the
532    // terminating null character. Use of other fields will harm the  porta-
533    // bility of your programs.
534
535    struct stat buf;
536    dirent *d;
537    for (d = readdir(dir); d; d = readdir(dir))
538    {
539       fileName = dirName + "/" + d->d_name;
540 //std::cout <<"in Explor filename[" << fileName << "]" << std::endl;      
541       if( stat(fileName.c_str(), &buf) != 0 )
542       {
543          //ErrorMacro( strerror(errno) );
544       }
545       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
546       {
547          Filenames.push_back( fileName );
548          numberOfFiles++;
549       }
550       else if ( S_ISDIR(buf.st_mode) ) //directory?
551       {
552          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
553          {
554             numberOfFiles += Explore( fileName, recursive, Filenames);
555          }
556       }
557       else
558       {
559          //ErrorMacro( "Unexpected error" );
560          return -1;
561       }
562    }
563    if( closedir(dir) != 0 )
564    {
565      // ErrorMacro( strerror(errno) );
566    }
567 #endif
568
569   return numberOfFiles;
570
571 }
572   //=======================================================================
573  
574
575     //=======================================================================
576     // Replaces substrings "\\n" by a real carriage return "\n"
577     void Utilities::SubsBackslashN ( std::string& s )
578     {
579       std::string ss("\\n");
580       std::string::size_type pos = 0;
581       pos = s.find(ss,0);
582       const char* cr = "\n";
583       while ( pos != std::string::npos )
584         {
585           s.replace(pos,2,cr,1);
586           pos = s.find(ss, pos-1);
587         }
588     }
589     //=======================================================================
590
591
592    //=======================================================================
593
594   bool Utilities::loosematch(std::string stdLine,std::string stdOptions) 
595   {
596     bool result=false;
597     std::vector<std::string> tokens;
598     SplitString ( stdOptions,"|", tokens);
599     int i,size=tokens.size();  
600     for (i=0; i<size; i++)
601       {           
602 #ifdef WIN32
603         if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0) 
604           { 
605             result=true; 
606           }               
607 #else
608         if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0) 
609           { 
610             result=true; 
611           }               
612 #endif
613         
614       }
615     return result;
616   }
617   //=========================================================================  
618   
619   //=========================================================================  
620   // From http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
621   //
622   int get_app_path (char *pname, size_t pathsize)
623   {
624
625 #ifdef LINUX
626     /* Oddly, the readlink(2) man page says no NULL is appended. */
627     /* So you have to do it yourself, based on the return value: */
628     pathsize --; /* Preserve a space to add the trailing NULL */
629     long result = readlink("/proc/self/exe", pname, pathsize);
630     if (result > 0)
631       {
632         pname[result] = 0; /* add the #@!%ing NULL */
633         
634         if ((access(pname, 0) == 0))
635           return 0; /* file exists, return OK */
636         /*else name doesn't seem to exist, return FAIL (falls
637           through) */
638       }
639 #endif /* LINUX */
640     
641 #ifdef WIN32
642
643 //2018-07-06 mingw64
644     wchar_t pname2[512];
645     long result = GetModuleFileName(NULL, pname2, pathsize);
646 //      int ret = wcstombs ( pname, pname2, sizeof(pname) );    
647 //    long result = GetModuleFileName(NULL, pname, pathsize);
648     if (result > 0)
649       {
650         /* fix up the dir slashes... */
651         int len = strlen(pname);
652         int idx;
653         for (idx = 0; idx < len; idx++)
654           {
655             if (pname[idx] == '\\') pname[idx] = '/';
656           }
657         
658         for (idx = len-1; idx >=0 ; idx--)
659           {
660             if (pname[idx] == '/')
661               { 
662                 pname[idx+1] = '\0';
663                 idx = -1;
664               }
665           }
666         
667         if ((access(pname, 0) == 0))
668           return 0; /* file exists, return OK */
669         /*else name doesn't seem to exist, return FAIL (falls
670           through) */
671       }
672 #endif /* WIN32 */
673     
674 #ifdef SOLARIS
675     char *p = getexecname();
676     if (p)
677       {
678         /* According to the Sun manpages, getexecname will
679            "normally" return an */
680         /* absolute path - BUT might not... AND that IF it is not,
681            pre-pending */
682         /* getcwd() will "usually" be the correct thing... Urgh!
683          */
684         
685         /* check pathname is absolute (begins with a / ???) */
686         if (p[0] == '/') /* assume this means we have an
687                             absolute path */
688           {
689             strncpy(pname, p, pathsize);
690             if ((access(pname, 0) == 0))
691               return 0; /* file exists, return OK */
692           }
693         else /* if not, prepend getcwd() then check if file
694                 exists */
695           {
696             getcwd(pname, pathsize);
697             long result = strlen(pname);
698             strncat(pname, "/", (pathsize - result));
699             result ++;
700             strncat(pname, p, (pathsize - result));
701             
702             if ((access(pname, 0) == 0))
703               return 0; /* file exists, return OK */
704             /*else name doesn't seem to exist, return FAIL
705               (falls through) */
706           }
707       }
708 #endif /* SOLARIS */
709     
710 #ifdef MACOSX /* assume this is OSX */
711     /*
712       from http://www.hmug.org/man/3/NSModule.html
713       
714       extern int _NSGetExecutablePath(char *buf, unsigned long
715       *bufsize);
716       
717       _NSGetExecutablePath  copies  the  path  of the executable
718       into the buffer and returns 0 if the path was successfully
719       copied  in the provided buffer. If the buffer is not large
720       enough, -1 is returned and the  expected  buffer  size  is
721       copied  in  *bufsize.  Note that _NSGetExecutablePath will
722       return "a path" to the executable not a "real path" to the
723       executable.  That  is  the path may be a symbolic link and
724       not the real file. And with  deep  directories  the  total
725       bufsize needed could be more than MAXPATHLEN.
726     */
727     int status = -1;
728     char *given_path = (char*)malloc(MAXPATHLEN * 2);
729     if (!given_path) return status;
730     
731     uint32_t npathsize = MAXPATHLEN * 2;
732     long result = _NSGetExecutablePath(given_path, &npathsize);
733     if (result == 0)
734       { /* OK, we got something - now try and resolve the real path...
735          */
736         if (realpath(given_path, pname) != NULL)
737           {
738             if ((access(pname, 0) == 0))
739               status = 0; /* file exists, return OK */
740           }
741       }
742     free (given_path);
743     return status;
744 #endif /* MACOSX */
745
746 printf("EED Utilities   get_app_path END\n");
747     
748     return -1; /* Path Lookup Failed */
749   } 
750   //=========================================================================
751         
752
753         
754   //=========================================================================
755   std::string Utilities::GetExecutablePath()
756   {
757 printf("EED Utilities::GetExecutablePath Start\n");
758     char name[PATH_MAX];
759 printf("EED Utilities::GetExecutablePath 1 \n");
760     int err = get_app_path(name, PATH_MAX);
761 printf("EED Utilities::GetExecutablePath 2 \n");
762     if (err) 
763       {
764 printf("EED Utilities::GetExecutablePath 2.1 \n");
765 //      bbtkGlobalError("Could not determine current executable path ?");  
766
767 /*
768
769 printf("EED Utilities  bbtkGlobalError  XX Start\n");   
770   do                                                    
771     {                                                   
772 printf("EED Utilities  bbtkGlobalError XX A.1\n");      
773       std::ostringstream s;                             
774 printf("EED Utilities  bbtkGlobalError XX A.2\n");      
775       s << "Could not determine current executable path ?";                                     
776 printf("EED Utilities  bbtkGlobalError XX A.3\n");      
777       std::ostringstream f;                             
778 printf("EED Utilities  bbtkGlobalError XX A.4\n");      
779       f << __FILE__ << " (l."<<__LINE__<<")";           
780 printf("EED Utilities  bbtkGlobalError XX A.5\n");      
781 std::string aa=f.str();
782 printf("EED Utilities  bbtkGlobalError XX A.6\n");      
783 std::string bb=f.str();
784 printf("EED Utilities  bbtkGlobalError XX A.7\n");      
785       bbtk::Exception e( "global scope",                
786                         f.str(),                        
787                         s.str());                       
788       throw e;                                          
789 printf("EED Utilities  bbtkGlobalError XX B\n");        
790     }                                                   
791   while (0); 
792 printf("EED Utilities  bbtkGlobalError XX End\n");      
793
794 */
795
796
797 printf("EED Utilities::GetExecutablePath 2.2 \n");
798       }
799     
800     // remove the exe name
801     char *slash;                
802 printf("EED Utilities::GetExecutablePath 3 >%s<\n",name);
803     slash = strrchr(name, VALID_FILE_SEPARATOR_CHAR);
804 printf("EED Utilities::GetExecutablePath 4 \n"); 
805     if (slash)
806       {
807         *slash = 0;
808       }
809 printf("EED Utilities::GetExecutablePath END \n");
810     return name;
811   }
812   //=========================================================================
813
814
815 std::string Utilities::GetEnvHome()
816 {
817 #if defined(__GNUC__)
818         std::string strHome;
819     char *envHome=getenv("HOME");
820     if (envHome!=NULL)  
821         { 
822                 strHome=envHome; 
823         } else {
824                 strHome = "/var/www/testwtdbg/docroot";
825         } // if
826 #elif defined(_WIN32)
827         std::string strHome( getenv("USERPROFILE") );
828 #endif
829         return strHome;
830 }   
831
832
833
834 //TAD Arbol CFT
835
836
837 //---------NodeTree---------------
838
839 NodeTreeC::NodeTreeC() 
840 {
841         
842 }
843
844 NodeTreeC::NodeTreeC(std::string _data)
845 {
846         data = _data;
847 }
848
849 NodeTreeC::~NodeTreeC()
850 {
851
852 }
853 void NodeTreeC::deleteTree()
854 {
855         data = "";
856         std::cout<<"NodeTreeC::deleteTree 1"<<std::endl;
857         childs.erase(childs.begin(),childs.begin()+childs.size());
858         std::cout<<"NodeTreeC::deleteTree 2"<<std::endl;
859 }
860
861 void NodeTreeC::insertChild(std::string _data)
862 {
863         NodeTreeC temp = NodeTreeC(_data);
864         childs.push_back(temp);
865         //std::cout<<"NodeTreeC::insertChild"<<std::endl;
866 }
867
868 void NodeTreeC::treeTour(int lvl)
869 {
870         std::string space = "";
871         for (int j=0; j<lvl ; j++){
872                                 space += "   ";
873         }
874         lvl++;
875         std::cout <<space<<"data: "<< data << "  size: "<< childs.size() << std::endl;
876         for(int i = 0 ; i < childs.size(); i++)
877         {
878                 childs[i].treeTour(lvl);                
879         }
880 }
881
882 void NodeTreeC::setData(std::string _data)
883 {
884         data = _data;
885         //std::cout<<"NodeTreeC::setData"<<std::endl;
886 }
887
888 }