1 /*=========================================================================
3 Module: $RCSfile: bbtkUtilities.cxx,v $
5 Date: $Date: 2009/01/27 14:22:57 $
6 Version: $Revision: 1.11 $
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 * ------------------------------------------------------------------------ */
32 #include "bbtkUtilities.h"
33 #include "bbtkMessageManager.h"
42 // ======================================================================
43 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
44 bool Utilities::FileExists(std::string strFilename)
46 struct stat stFileInfo;
50 // Attempt to get the file attributes
51 intStat = stat(strFilename.c_str(),&stFileInfo);
54 // We were able to get the file attributes
55 // so the file obviously exists.
60 // We were not able to get the file attributes.
61 // This may mean that we don't have permission to
62 // access the folder which contains this file. If you
63 // need to do that level of checking, lookup the
64 // return values of stat which will give you
65 // more details on why stat failed.
73 // =====================================================================
75 std::string Utilities::ExtractPackageName(const std::string &name,
81 std::string::size_type slash_position = name.find_last_of("/\\");
82 if (slash_position != std::string::npos)
84 pkgname = name.substr(slash_position+1,std::string::npos);
85 path = name.substr(0,slash_position);
86 // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
93 // remove {.so | dll} if any
94 std::string::size_type dot_position = pkgname.find_last_of('.');
95 if (dot_position != std::string::npos){
96 pkgname = pkgname.substr(0,dot_position);
101 // shared lib name = libbb<name>.so
103 // remove {libbb} if any
104 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
105 pkgname = pkgname.substr(5, pkgname.length());
108 /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!?
109 /// \ --> Should be forbidden!
111 #elif defined(_WIN32)
114 // shared lib name = <name>.dll
116 // EED Problem loading package call bbtkTools
117 // // remove {bb} if any
118 if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
119 pkgname = pkgname.substr(2, pkgname.length());
123 /// \ \todo what would happen if (stupid) user names his package 'bb' ?!?
124 /// \ --> Should be forbidden!
127 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
132 //=====================================================================
133 std::string Utilities::ExtractScriptName(const std::string &name,
138 std::string::size_type slash_position = name.find_last_of("/\\");
139 if (slash_position != std::string::npos) {
140 pkgname =name.substr(slash_position+1,std::string::npos);
141 path = name.substr(0,slash_position);
145 // remove {.bbs } if any
146 std::string::size_type dot_position = pkgname.find_last_of('.');
147 if (dot_position != std::string::npos){
148 pkgname = pkgname.substr(0,dot_position);
153 // ========================================================================
155 std::string Utilities::ExpandLibName(const std::string &name, bool verbose)
157 // ----- Think of expanding path name ( ./ ../ ../../ )
159 char buf[2048]; // for getcwd
160 char * currentDir = getcwd(buf, 2048);
161 std::string cwd(currentDir);
162 std::string libname(name);
163 std::string fileSeparator;
164 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
165 // tooHigh : true is user supplies a library pathname with too many "../"
166 bool tooHigh = false;
168 //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl;
170 if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name
174 else if ( name =="." )
176 libname = cwd + fileSeparator;
179 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
181 libname = cwd + fileSeparator + name.substr(2, name.length());
184 else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ )
186 if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows)
188 // if we are already at / or c: --> hopeless
190 std::cout << " File path [" << name << "] doesn't exist" << std::endl;
195 // iterate on ../ and go up from the current working dir!
197 bool alreadyProcessRoot = false;
199 //if (a[a.size()-1] != fileSeparator[0])
200 // a.append(fileSeparator);
201 //std::cout << "------------------a [" << a << "]" << std::endl;
203 for(;;) // wild loop !
205 std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
206 if (slash_position != std::string::npos) {
207 if (slash_position == 0)
209 cwd = cwd.substr(0,slash_position/*+1*/);
210 //std::cout << "------------------cwd [" << cwd << "]" << std::endl;
216 a = a.substr(3, /*name.length()*/ a.length()); // remove ../
217 //std::cout << "------------------a [" << a << "]" << std::endl;
218 if (a == "" || alreadyProcessRoot)
221 std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
225 // std::string b = cwd + a;
227 char c = cwd[cwd.size()-1];
228 if (c != '/' && c != '\\' )
229 libname += fileSeparator;
232 if ( a[0] != '.' ) // if . (probabely ../), loop again
236 alreadyProcessRoot = true;
238 } // end iterating on ../
240 //std::cout << "------------------out of loop]" << std::endl;
245 } // ----- End of expanding path name ( ./ ../ ../../ )
247 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
249 return(""); // Will never get here!
252 // ===================================================================================
254 std::string Utilities::MakeLibnameFromPath(std::string path, std::string pkgname)
256 std::string libname = path;
257 char c = path[path.size()-1];
258 #if defined(__GNUC__)
270 #elif defined(_WIN32)
272 libname = path+"\\bb";
279 // ===================================================================================
281 std::string Utilities::MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
283 std::string libname = path;
284 char c = path[path.size()-1];
285 if (c != '/' && c != '\\')
287 libname += ConfigurationFile::GetInstance().Get_file_separator ();
292 int l = libname.size();
295 if (libname.substr(l-4, 4) != ".bbs")
297 libname = libname + ".bbs";
303 // =======================================================================
305 // =======================================================================
306 /// Returns the user settings dir, e.g. /home/username/.bbtk
307 std::string Utilities::GetUserSettingsDir()
309 #if defined(__GNUC__)
310 std::string str_home(getenv("HOME"));
311 #elif defined(_WIN32)
312 std::string str_home(getenv("USERPROFILE"));
314 std::string fullname = str_home + "/.bbtk";
315 MakeValidFileName(fullname);
320 // =======================================================================
321 /// Builds the complete path to the file 'name' located
322 /// in user settings dir, e.g. /home/username/.bbtk/
323 std::string Utilities::MakeUserSettingsFullFileName(const std::string& name)
325 #if defined(__GNUC__)
326 std::string str_home(getenv("HOME"));
327 #elif defined(_WIN32)
328 std::string str_home(getenv("USERPROFILE"));
330 std::string fullname = str_home + "/.bbtk/" + name;
331 MakeValidFileName(fullname);
334 // =======================================================================
337 // =======================================================================
338 void Utilities::CreateDirectoryIfNeeded( std::string const &dirName)
340 if (FileExists(dirName)) return;
341 std::string cmd("mkdir \"");
346 // =======================================================================
349 //========================================================================
350 bool Utilities::IsAtRoot(std::string cwd)
352 if ( cwd == "/" // hope it gets / (for Linux)
353 || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
358 // ======================================================================
360 // ======================================================================
361 bool Utilities::IsDirectory(std::string const &dirName)
365 if ( stat(dirName.c_str(), &fs) == 0 )
368 return ((fs.st_mode & _S_IFDIR) != 0);
370 return S_ISDIR(fs.st_mode);
378 // =======================================================================
380 // =======================================================================
381 void Utilities::SplitAroundFirstDot( const std::string& in,
385 std::string delimiter = ".";
386 std::string::size_type pos = in.find_first_of(delimiter);
387 if (std::string::npos != pos)
389 left = in.substr(0,pos);
390 right = in.substr(pos+1,in.size());
397 bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
400 //=======================================================================
402 //=======================================================================
403 void Utilities::SplitString ( const std::string& str,
404 const std::string& delimiters,
405 std::vector<std::string>& tokens)
407 // Skip delimiters at beginning.
408 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
409 // Find first delimiter.
410 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
412 while (std::string::npos != pos || std::string::npos != lastPos)
414 // Found a token, add it to the vector.
415 tokens.push_back(str.substr(lastPos, pos - lastPos));
416 // Skip delimiters. Note the "not_of"
417 lastPos = str.find_first_not_of(delimiters, pos);
418 // Find next delimiter
419 pos = str.find_first_of(delimiters, lastPos);
423 //=======================================================================
426 // ======================================================================
427 std::string Utilities::get_file_name(const std::string& s)
429 std::string::size_type slash_position = s.find_last_of("/\\");
430 if (slash_position != std::string::npos)
432 return s.substr(slash_position+1,std::string::npos);
439 //=======================================================================
442 // ========================================================================
444 * \brief Explore a directory with possibility of recursion
445 * return number of files read
446 * @param dirpath directory to explore
447 * @param recursive whether we want recursion or not
449 int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
451 int numberOfFiles = 0;
452 std::string fileName;
454 std::string dirName = dirpath;
457 WIN32_FIND_DATA fileData;
458 HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData);
460 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
461 b = FindNextFile(hFile, &fileData))
463 fileName = fileData.cFileName;
464 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
466 // Need to check for . and .. to avoid infinite loop
467 if ( fileName != "." && fileName != ".." && recursive )
469 numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
474 Filenames.push_back(dirName+"\\"+fileName);
478 DWORD dwError = GetLastError();
479 if (hFile != INVALID_HANDLE_VALUE)
481 if (dwError != ERROR_NO_MORE_FILES)
484 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
485 FORMAT_MESSAGE_FROM_SYSTEM|
486 FORMAT_MESSAGE_IGNORE_INSERTS,
488 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
489 (LPTSTR) &lpMsgBuf,0,NULL);
491 // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
492 // <<" for the directory : "<<dirName);
498 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
499 // work on debian for example
500 //std::cout <<"in Explor dirname[" << dirName << "]" << std::endl;
501 DIR* dir = opendir(dirName.c_str());
506 //std::cout <<"Open OK" << std::endl;
507 // According to POSIX, the dirent structure contains a field char d_name[]
508 // of unspecified size, with at most NAME_MAX characters preceeding the
509 // terminating null character. Use of other fields will harm the porta-
510 // bility of your programs.
514 for (d = readdir(dir); d; d = readdir(dir))
516 fileName = dirName + "/" + d->d_name;
517 //std::cout <<"in Explor filename[" << fileName << "]" << std::endl;
518 if( stat(fileName.c_str(), &buf) != 0 )
520 //ErrorMacro( strerror(errno) );
522 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
524 Filenames.push_back( fileName );
527 else if ( S_ISDIR(buf.st_mode) ) //directory?
529 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
531 numberOfFiles += Explore( fileName, recursive, Filenames);
536 //ErrorMacro( "Unexpected error" );
540 if( closedir(dir) != 0 )
542 // ErrorMacro( strerror(errno) );
546 return numberOfFiles;
549 //=======================================================================
552 //=======================================================================
553 // Replaces substrings "\\n" by a real carriage return "\n"
554 void Utilities::SubsBackslashN ( std::string& s )
556 std::string ss("\\n");
557 std::string::size_type pos = 0;
559 const char* cr = "\n";
560 while ( pos != std::string::npos )
562 s.replace(pos,2,cr,1);
563 pos = s.find(ss, pos-1);
566 //=======================================================================
569 //=======================================================================
571 bool Utilities::loosematch(std::string stdLine,std::string stdOptions)
574 std::vector<std::string> tokens;
575 SplitString ( stdOptions,"|", tokens);
576 int i,size=tokens.size();
577 for (i=0; i<size; i++)
580 if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0)
585 if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0)
594 //=========================================================================
596 //=========================================================================
597 // From http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
599 int get_app_path (char *pname, size_t pathsize)
602 /* Oddly, the readlink(2) man page says no NULL is appended. */
603 /* So you have to do it yourself, based on the return value: */
604 pathsize --; /* Preserve a space to add the trailing NULL */
605 long result = readlink("/proc/self/exe", pname, pathsize);
608 pname[result] = 0; /* add the #@!%ing NULL */
610 if ((access(pname, 0) == 0))
611 return 0; /* file exists, return OK */
612 /*else name doesn't seem to exist, return FAIL (falls
618 long result = GetModuleFileName(NULL, pname, pathsize);
621 /* fix up the dir slashes... */
622 int len = strlen(pname);
624 for (idx = 0; idx < len; idx++)
626 if (pname[idx] == '\\') pname[idx] = '/';
629 for (idx = len-1; idx >=0 ; idx--)
631 if (pname[idx] == '/')
638 if ((access(pname, 0) == 0))
639 return 0; /* file exists, return OK */
640 /*else name doesn't seem to exist, return FAIL (falls
646 char *p = getexecname();
649 /* According to the Sun manpages, getexecname will
650 "normally" return an */
651 /* absolute path - BUT might not... AND that IF it is not,
653 /* getcwd() will "usually" be the correct thing... Urgh!
656 /* check pathname is absolute (begins with a / ???) */
657 if (p[0] == '/') /* assume this means we have an
660 strncpy(pname, p, pathsize);
661 if ((access(pname, 0) == 0))
662 return 0; /* file exists, return OK */
664 else /* if not, prepend getcwd() then check if file
667 getcwd(pname, pathsize);
668 long result = strlen(pname);
669 strncat(pname, "/", (pathsize - result));
671 strncat(pname, p, (pathsize - result));
673 if ((access(pname, 0) == 0))
674 return 0; /* file exists, return OK */
675 /*else name doesn't seem to exist, return FAIL
681 #ifdef MACOSX /* assume this is OSX */
683 from http://www.hmug.org/man/3/NSModule.html
685 extern int _NSGetExecutablePath(char *buf, unsigned long
688 _NSGetExecutablePath copies the path of the executable
689 into the buffer and returns 0 if the path was successfully
690 copied in the provided buffer. If the buffer is not large
691 enough, -1 is returned and the expected buffer size is
692 copied in *bufsize. Note that _NSGetExecutablePath will
693 return "a path" to the executable not a "real path" to the
694 executable. That is the path may be a symbolic link and
695 not the real file. And with deep directories the total
696 bufsize needed could be more than MAXPATHLEN.
699 char *given_path = (char*)malloc(MAXPATHLEN * 2);
700 if (!given_path) return status;
702 uint32_t npathsize = MAXPATHLEN * 2;
703 long result = _NSGetExecutablePath(given_path, &npathsize);
705 { /* OK, we got something - now try and resolve the real path...
707 if (realpath(given_path, pname) != NULL)
709 if ((access(pname, 0) == 0))
710 status = 0; /* file exists, return OK */
717 return -1; /* Path Lookup Failed */
719 //=========================================================================
723 //=========================================================================
724 std::string Utilities::GetExecutablePath()
727 int err = get_app_path(name, PATH_MAX);
730 bbtkGlobalError("Could not determine current executable path ?");
733 // remove the exe name
735 slash = strrchr(name, VALID_FILE_SEPARATOR_CHAR);
742 //=========================================================================