+
+
+
+ // ======================================================================
+ // See : http://www.techbytes.ca/techbyte103.html for more O.S.
+ bool Utilities::FileExists(std::string strFilename)
+ {
+ struct stat stFileInfo;
+ bool blnReturn;
+ int intStat;
+
+ // Attempt to get the file attributes
+ intStat = stat(strFilename.c_str(),&stFileInfo);
+ if(intStat == 0)
+ {
+ // We were able to get the file attributes
+ // so the file obviously exists.
+ blnReturn = true;
+ }
+ else
+ {
+ // We were not able to get the file attributes.
+ // This may mean that we don't have permission to
+ // access the folder which contains this file. If you
+ // need to do that level of checking, lookup the
+ // return values of stat which will give you
+ // more details on why stat failed.
+ blnReturn = false;
+ }
+
+ return(blnReturn);
+ }
+
+
+ // =====================================================================
+
+ std::string Utilities::ExtractPackageName(const std::string &name,
+ std::string& path)
+ {
+ std::string pkgname;
+ path = "";
+
+ std::string::size_type slash_position = name.find_last_of("/\\");
+ if (slash_position != std::string::npos)
+ {
+ pkgname = name.substr(slash_position+1,std::string::npos);
+ path = name.substr(0,slash_position);
+ // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
+ }
+ else
+ {
+ pkgname = name;
+ }
+
+ // remove {.so | dll} if any
+ std::string::size_type dot_position = pkgname.find_last_of('.');
+ if (dot_position != std::string::npos){
+ pkgname = pkgname.substr(0,dot_position);
+ }
+#if defined(__GNUC__)
+
+ // GCC mechanism
+ // shared lib name = libbb<name>.so
+
+ // remove {libbb} if any
+ if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
+ pkgname = pkgname.substr(5, pkgname.length());
+ }
+ /*
+ /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!?
+ /// \ --> Should be forbidden!
+ */
+#elif defined(_WIN32)
+
+ // WIN 32 mechanism
+ // shared lib name = <name>.dll
+
+ // EED Problem loading package call bbtkTools
+ // // remove {bb} if any
+ if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
+ pkgname = pkgname.substr(2, pkgname.length());
+ }
+
+ /*
+ /// \ \todo what would happen if (stupid) user names his package 'bb' ?!?
+ /// \ --> Should be forbidden!
+ */
+#else
+ bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
+#endif
+ return pkgname;
+ }
+
+ //=====================================================================
+ std::string Utilities::ExtractScriptName(const std::string &name,
+ std::string& path)
+ {
+ std::string pkgname;
+
+ std::string::size_type slash_position = name.find_last_of("/\\");
+ if (slash_position != std::string::npos) {
+ pkgname =name.substr(slash_position+1,std::string::npos);
+ path = name.substr(0,slash_position);
+ } else {
+ pkgname = name;
+ }
+ // remove {.bbs } if any
+ std::string::size_type dot_position = pkgname.find_last_of('.');
+ if (dot_position != std::string::npos){
+ pkgname = pkgname.substr(0,dot_position);
+ }
+ return pkgname;
+ }
+
+ // ========================================================================
+
+ std::string Utilities::ExpandLibName(const std::string &name, bool verbose)
+ {
+ // ----- Think of expanding path name ( ./ ../ ../../ )
+
+ char buf[2048]; // for getcwd
+ char * currentDir = getcwd(buf, 2048);
+ std::string cwd(currentDir);
+ std::string libname(name);
+ std::string fileSeparator;
+ fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
+ // tooHigh : true is user supplies a library pathname with too many "../"
+ bool tooHigh = false;
+
+ //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl;
+
+ if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name
+ {
+ return(libname);
+ }
+ else if ( name =="." )
+ {
+ libname = cwd + fileSeparator;
+ return(libname);
+ }
+ else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
+ {
+ libname = cwd + fileSeparator + name.substr(2, name.length());
+ return(libname);
+ }
+ else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ )
+ {
+ if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows)
+ {
+ // if we are already at / or c: --> hopeless
+ if (verbose)
+ std::cout << " File path [" << name << "] doesn't exist" << std::endl;
+ tooHigh = true;
+ }
+ else
+ {
+ // iterate on ../ and go up from the current working dir!
+ std::string a(name);
+ bool alreadyProcessRoot = false;
+
+ //if (a[a.size()-1] != fileSeparator[0])
+ // a.append(fileSeparator);
+//std::cout << "------------------a [" << a << "]" << std::endl;
+
+ for(;;) // wild loop !
+ {
+ std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
+ if (slash_position != std::string::npos) {
+ if (slash_position == 0)
+ slash_position = 1;
+ cwd = cwd.substr(0,slash_position/*+1*/);
+//std::cout << "------------------cwd [" << cwd << "]" << std::endl;
+ // if (a == "..") {
+ // a = "";
+ // break;
+ // }
+ // else
+ a = a.substr(3, /*name.length()*/ a.length()); // remove ../
+//std::cout << "------------------a [" << a << "]" << std::endl;
+ if (a == "" || alreadyProcessRoot)
+ {
+ if (verbose)
+ std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
+ tooHigh = true;
+ break;
+ }
+ // std::string b = cwd + a;
+ libname = cwd;
+ char c = cwd[cwd.size()-1];
+ if (c != '/' && c != '\\' )
+ libname += fileSeparator;
+ libname += a;
+
+ if ( a[0] != '.' ) // if . (probabely ../), loop again
+ break;
+
+ if (IsAtRoot(cwd))
+ alreadyProcessRoot = true;
+ }
+ } // end iterating on ../
+ }
+//std::cout << "------------------out of loop]" << std::endl;
+ if (tooHigh)
+ libname="";
+ return (libname);
+
+ } // ----- End of expanding path name ( ./ ../ ../../ )
+
+ std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
+ // To avoid warning
+ return(""); // Will never get here!
+ }
+
+// ===================================================================================
+
+ std::string Utilities::MakeLibnameFromPath(std::string path, std::string pkgname)
+ {
+ std::string libname = path;
+ if(path.size()>0){
+ char c = path[path.size()-1];
+#if defined(__GNUC__)
+ if (c != '/')
+ libname += "/libbb";
+ else
+ libname += "libbb";
+ libname += pkgname;
+#if defined(MACOSX)
+ libname += ".dylib";
+#else
+ libname += ".so";
+#endif
+
+#elif defined(_WIN32)
+ if (c != '\\')
+ libname += "\\bb";
+ else
+ libname += "bb";
+ libname += pkgname;
+ libname += ".dll";
+#endif
+ }
+
+ 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());
+ }
+ // =======================================================================
+
+