+ }
+
+ return libname;
+ }
+
+// ===================================================================================
+
+ std::string Utilities::MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
+ {
+ std::string libname = path;
+ if(path.size()>0){
+ char c = path[path.size()-1];
+ if (c != '/' && c != '\\')
+ {
+ libname += ConfigurationFile::GetInstance().Get_file_separator ();
+ }
+ libname += pkgname;
+ if (addExt)
+ {
+ int l = libname.size();
+ if (l>4)
+ {
+ if (libname.substr(l-4, 4) != ".bbs")
+ {
+ libname = libname + ".bbs";
+ }
+ }
+ }
+ }
+
+ return libname;
+ }
+ // =======================================================================
+
+ // =======================================================================
+ /// Returns the user settings dir, e.g. /home/username/.bbtk
+ std::string Utilities::GetUserSettingsDir()
+ {
+#if defined(__GNUC__)
+ std::string str_home(getenv("HOME"));
+#elif defined(_WIN32)
+ std::string str_home(getenv("USERPROFILE"));
+#endif
+ std::string fullname = str_home + "/.bbtk";
+ MakeValidFileName(fullname);
+ return fullname;
+ }
+
+
+ // =======================================================================
+ /// Builds the complete path to the file 'name' located
+ /// in user settings dir, e.g. /home/username/.bbtk/
+ std::string Utilities::MakeUserSettingsFullFileName(const std::string& name)
+ {
+#if defined(__GNUC__)
+ std::string str_home(getenv("HOME"));
+#elif defined(_WIN32)
+ std::string str_home(getenv("USERPROFILE"));
+#endif
+ std::string fullname = str_home + "/.bbtk/" + name;
+ MakeValidFileName(fullname);
+ return fullname;
+ }
+ // =======================================================================
+
+
+ // =======================================================================
+ void Utilities::CreateDirectoryIfNeeded( std::string const &dirName)
+ {
+ if (FileExists(dirName)) return;
+ std::string cmd("mkdir \"");
+ cmd += dirName;
+ cmd += "\"";
+ system(cmd.c_str());
+ }
+ // =======================================================================
+
+
+ //========================================================================
+ bool Utilities::IsAtRoot(std::string cwd)
+ {
+ if ( cwd == "/" // hope it gets / (for Linux)
+ || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
+ return (true);
+ else
+ return(false);
+ }
+ // ======================================================================
+
+ // ======================================================================
+ bool Utilities::IsDirectory(std::string const &dirName)
+ {
+ struct stat fs;
+
+ if ( stat(dirName.c_str(), &fs) == 0 )
+ {
+#if _WIN32
+ return ((fs.st_mode & _S_IFDIR) != 0);
+#else
+ return S_ISDIR(fs.st_mode);
+#endif
+ }
+ else
+ {
+ return false;
+ }
+ }
+ // =======================================================================
+
+ // =======================================================================
+ void Utilities::SplitAroundFirstDot( const std::string& in,
+ std::string& left,
+ std::string& right)
+ {
+ std::string delimiter = ".";
+ std::string::size_type pos = in.find_first_of(delimiter);
+ if (std::string::npos != pos)
+ {
+ left = in.substr(0,pos);
+ right = in.substr(pos+1,in.size());
+
+ }
+ else
+ {
+ left ="";
+ right = "";
+ bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
+ }
+ }
+ //=======================================================================
+
+ //=======================================================================
+ void Utilities::SplitString ( const std::string& str,
+ const std::string& delimiters,
+ std::vector<std::string>& tokens)
+ {
+ // Skip delimiters at beginning.
+ std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+ // Find first delimiter.
+ std::string::size_type pos = str.find_first_of(delimiters, lastPos);
+
+ while (std::string::npos != pos || std::string::npos != lastPos)
+ {
+ // Found a token, add it to the vector.
+ tokens.push_back(str.substr(lastPos, pos - lastPos));
+ // Skip delimiters. Note the "not_of"
+ lastPos = str.find_first_not_of(delimiters, pos);
+ // Find next delimiter
+ pos = str.find_first_of(delimiters, lastPos);
+ }
+
+ }
+ //=======================================================================
+
+
+ // ======================================================================
+ std::string Utilities::get_file_name(const std::string& s)
+ {
+ std::string::size_type slash_position = s.find_last_of("/\\");
+ if (slash_position != std::string::npos)
+ {
+ return s.substr(slash_position+1,std::string::npos);
+ }
+ else
+ {
+ return s;
+ }
+ }
+ //=======================================================================
+
+
+ // ========================================================================
+ /**
+ * \brief Explore a directory with possibility of recursion
+ * return number of files read
+ * @param dirpath directory to explore
+ * @param recursive whether we want recursion or not
+ */
+ int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
+ {
+ int numberOfFiles = 0;
+ std::string fileName;
+
+ std::string dirName = dirpath;
+
+#ifdef _MSC_VER
+ WIN32_FIND_DATA fileData;
+ HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData);
+
+ for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
+ b = FindNextFile(hFile, &fileData))
+ {
+ fileName = fileData.cFileName;
+ if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
+ {
+ // Need to check for . and .. to avoid infinite loop
+ if ( fileName != "." && fileName != ".." && recursive )
+ {
+ numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
+ }
+ }
+ else
+ {
+ Filenames.push_back(dirName+"\\"+fileName);
+ numberOfFiles++;
+ }
+ }
+ DWORD dwError = GetLastError();
+ if (hFile != INVALID_HANDLE_VALUE)
+ FindClose(hFile);
+ if (dwError != ERROR_NO_MORE_FILES)
+ {
+ LPVOID lpMsgBuf;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
+ FORMAT_MESSAGE_FROM_SYSTEM|
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf,0,NULL);
+
+ // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
+ // <<" for the directory : "<<dirName);
+
+ return 0;
+ }
+
+#else
+ // Real POSIX implementation: scandir is a BSD extension only, and doesn't
+ // work on debian for example
+//std::cout <<"in Explor dirname[" << dirName << "]" << std::endl;
+ DIR* dir = opendir(dirName.c_str());
+ if (!dir)
+ {
+ return 0;
+ }
+//std::cout <<"Open OK" << std::endl;
+ // According to POSIX, the dirent structure contains a field char d_name[]
+ // of unspecified size, with at most NAME_MAX characters preceeding the
+ // terminating null character. Use of other fields will harm the porta-
+ // bility of your programs.
+
+ struct stat buf;
+ dirent *d;
+ for (d = readdir(dir); d; d = readdir(dir))
+ {
+ fileName = dirName + "/" + d->d_name;
+//std::cout <<"in Explor filename[" << fileName << "]" << std::endl;
+ if( stat(fileName.c_str(), &buf) != 0 )
+ {
+ //ErrorMacro( strerror(errno) );
+ }
+ if ( S_ISREG(buf.st_mode) ) //is it a regular file?
+ {
+ Filenames.push_back( fileName );
+ numberOfFiles++;
+ }
+ else if ( S_ISDIR(buf.st_mode) ) //directory?
+ {
+ if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
+ {
+ numberOfFiles += Explore( fileName, recursive, Filenames);
+ }
+ }
+ else
+ {
+ //ErrorMacro( "Unexpected error" );
+ return -1;
+ }
+ }
+ if( closedir(dir) != 0 )
+ {
+ // ErrorMacro( strerror(errno) );
+ }
+#endif
+
+ return numberOfFiles;