00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "bbtkUtilities.h"
00033 #include "bbtkMessageManager.h"
00034
00035 #if defined(MACOSX) // assume this is OSX
00036 # include <sys/param.h>
00037 # include <mach-o/dyld.h>
00038 # include <string.h>
00039 # ifndef PATH_MAX
00040 # define PATH_MAX MAXPATHLEN
00041 # endif
00042 #endif // MACOSX
00043
00044 #ifndef PATH_MAX // If not defined yet : do it
00045 # define PATH_MAX 2048
00046 #endif
00047
00048 namespace bbtk
00049 {
00050
00051
00052
00053
00054
00055 bool Utilities::FileExists(std::string strFilename)
00056 {
00057 struct stat stFileInfo;
00058 bool blnReturn;
00059 int intStat;
00060
00061
00062 intStat = stat(strFilename.c_str(),&stFileInfo);
00063 if(intStat == 0)
00064 {
00065
00066
00067 blnReturn = true;
00068 }
00069 else
00070 {
00071
00072
00073
00074
00075
00076
00077 blnReturn = false;
00078 }
00079
00080 return(blnReturn);
00081 }
00082
00083
00084
00085
00086 std::string Utilities::ExtractPackageName(const std::string &name,
00087 std::string& path)
00088 {
00089 std::string pkgname;
00090 path = "";
00091
00092 std::string::size_type slash_position = name.find_last_of("/\\");
00093 if (slash_position != std::string::npos)
00094 {
00095 pkgname = name.substr(slash_position+1,std::string::npos);
00096 path = name.substr(0,slash_position);
00097
00098 }
00099 else
00100 {
00101 pkgname = name;
00102 }
00103
00104
00105 std::string::size_type dot_position = pkgname.find_last_of('.');
00106 if (dot_position != std::string::npos){
00107 pkgname = pkgname.substr(0,dot_position);
00108 }
00109 #if defined(__GNUC__)
00110
00111
00112
00113
00114
00115 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
00116 pkgname = pkgname.substr(5, pkgname.length());
00117 }
00118
00121
00122 #elif defined(_WIN32)
00123
00124
00125
00126
00127
00128
00129 if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
00130 pkgname = pkgname.substr(2, pkgname.length());
00131 }
00132
00133
00136
00137 #else
00138 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
00139 #endif
00140 return pkgname;
00141 }
00142
00143
00144 std::string Utilities::ExtractScriptName(const std::string &name,
00145 std::string& path)
00146 {
00147 std::string pkgname;
00148
00149 std::string::size_type slash_position = name.find_last_of("/\\");
00150 if (slash_position != std::string::npos) {
00151 pkgname =name.substr(slash_position+1,std::string::npos);
00152 path = name.substr(0,slash_position);
00153 } else {
00154 pkgname = name;
00155 }
00156
00157 std::string::size_type dot_position = pkgname.find_last_of('.');
00158 if (dot_position != std::string::npos){
00159 pkgname = pkgname.substr(0,dot_position);
00160 }
00161 return pkgname;
00162 }
00163
00164
00165
00166 std::string Utilities::ExpandLibName(const std::string &name, bool verbose)
00167 {
00168
00169
00170 char buf[2048];
00171 char * currentDir = getcwd(buf, 2048);
00172 std::string cwd(currentDir);
00173 std::string libname(name);
00174 std::string fileSeparator;
00175 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
00176
00177 bool tooHigh = false;
00178
00179
00180
00181 if ( name[0] == '/' || name[1] == ':' )
00182 {
00183 return(libname);
00184 }
00185 else if ( name =="." )
00186 {
00187 libname = cwd + fileSeparator;
00188 return(libname);
00189 }
00190 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
00191 {
00192 libname = cwd + fileSeparator + name.substr(2, name.length());
00193 return(libname);
00194 }
00195 else if ( name[0] == '.' && name[1] == '.' )
00196 {
00197 if ( IsAtRoot(cwd) )
00198 {
00199
00200 if (verbose)
00201 std::cout << " File path [" << name << "] doesn't exist" << std::endl;
00202 tooHigh = true;
00203 }
00204 else
00205 {
00206
00207 std::string a(name);
00208 bool alreadyProcessRoot = false;
00209
00210
00211
00212
00213
00214 for(;;)
00215 {
00216 std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
00217 if (slash_position != std::string::npos) {
00218 if (slash_position == 0)
00219 slash_position = 1;
00220 cwd = cwd.substr(0,slash_position);
00221
00222
00223
00224
00225
00226
00227 a = a.substr(3, a.length());
00228
00229 if (a == "" || alreadyProcessRoot)
00230 {
00231 if (verbose)
00232 std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
00233 tooHigh = true;
00234 break;
00235 }
00236
00237 libname = cwd;
00238 char c = cwd[cwd.size()-1];
00239 if (c != '/' && c != '\\' )
00240 libname += fileSeparator;
00241 libname += a;
00242
00243 if ( a[0] != '.' )
00244 break;
00245
00246 if (IsAtRoot(cwd))
00247 alreadyProcessRoot = true;
00248 }
00249 }
00250 }
00251
00252 if (tooHigh)
00253 libname="";
00254 return (libname);
00255
00256 }
00257
00258 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
00259
00260 return("");
00261 }
00262
00263
00264
00265 std::string Utilities::MakeLibnameFromPath(std::string path, std::string pkgname)
00266 {
00267 std::string libname = path;
00268 if(path.size()>0){
00269 char c = path[path.size()-1];
00270 #if defined(__GNUC__)
00271 if (c != '/')
00272 libname += "/libbb";
00273 else
00274 libname += "libbb";
00275 libname += pkgname;
00276 #if defined(MACOSX)
00277 libname += ".dylib";
00278 #else
00279 libname += ".so";
00280 #endif
00281
00282 #elif defined(_WIN32)
00283 if (c != '\\')
00284 libname += "\\bb";
00285 else
00286 libname += "bb";
00287 libname += pkgname;
00288 libname += ".dll";
00289 #endif
00290 }
00291
00292 return libname;
00293 }
00294
00295
00296
00297 std::string Utilities::MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
00298 {
00299 std::string libname = path;
00300 if(path.size()>0){
00301 char c = path[path.size()-1];
00302 if (c != '/' && c != '\\')
00303 {
00304 libname += ConfigurationFile::GetInstance().Get_file_separator ();
00305 }
00306 libname += pkgname;
00307 if (addExt)
00308 {
00309 int l = libname.size();
00310 if (l>4)
00311 {
00312 if (libname.substr(l-4, 4) != ".bbs")
00313 {
00314 libname = libname + ".bbs";
00315 }
00316 }
00317 }
00318 }
00319
00320 return libname;
00321 }
00322
00323
00324
00326 std::string Utilities::GetUserSettingsDir()
00327 {
00328 #if defined(__GNUC__)
00329 std::string str_home(getenv("HOME"));
00330 #elif defined(_WIN32)
00331 std::string str_home(getenv("USERPROFILE"));
00332 #endif
00333 std::string fullname = str_home + "/.bbtk";
00334 MakeValidFileName(fullname);
00335 return fullname;
00336 }
00337
00338
00339
00342 std::string Utilities::MakeUserSettingsFullFileName(const std::string& name)
00343 {
00344 #if defined(__GNUC__)
00345 std::string str_home(getenv("HOME"));
00346 #elif defined(_WIN32)
00347 std::string str_home(getenv("USERPROFILE"));
00348 #endif
00349 std::string fullname = str_home + "/.bbtk/" + name;
00350 MakeValidFileName(fullname);
00351 return fullname;
00352 }
00353
00354
00355
00356
00357 void Utilities::CreateDirectoryIfNeeded( std::string const &dirName)
00358 {
00359 if (FileExists(dirName)) return;
00360 std::string cmd("mkdir \"");
00361 cmd += dirName;
00362 cmd += "\"";
00363 system(cmd.c_str());
00364 }
00365
00366
00367
00368
00369 bool Utilities::IsAtRoot(std::string cwd)
00370 {
00371 if ( cwd == "/"
00372 || (cwd.size() <= 3 && cwd[1] == ':') )
00373 return (true);
00374 else
00375 return(false);
00376 }
00377
00378
00379
00380 bool Utilities::IsDirectory(std::string const &dirName)
00381 {
00382 struct stat fs;
00383
00384 if ( stat(dirName.c_str(), &fs) == 0 )
00385 {
00386 #if _WIN32
00387 return ((fs.st_mode & _S_IFDIR) != 0);
00388 #else
00389 return S_ISDIR(fs.st_mode);
00390 #endif
00391 }
00392 else
00393 {
00394 return false;
00395 }
00396 }
00397
00398
00399
00400 void Utilities::SplitAroundFirstDot( const std::string& in,
00401 std::string& left,
00402 std::string& right)
00403 {
00404 std::string delimiter = ".";
00405 std::string::size_type pos = in.find_first_of(delimiter);
00406 if (std::string::npos != pos)
00407 {
00408 left = in.substr(0,pos);
00409 right = in.substr(pos+1,in.size());
00410
00411 }
00412 else
00413 {
00414 left ="";
00415 right = "";
00416 bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
00417 }
00418 }
00419
00420
00421
00422 void Utilities::SplitString ( const std::string& str,
00423 const std::string& delimiters,
00424 std::vector<std::string>& tokens)
00425 {
00426
00427 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00428
00429 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00430
00431 while (std::string::npos != pos || std::string::npos != lastPos)
00432 {
00433
00434 tokens.push_back(str.substr(lastPos, pos - lastPos));
00435
00436 lastPos = str.find_first_not_of(delimiters, pos);
00437
00438 pos = str.find_first_of(delimiters, lastPos);
00439 }
00440
00441 }
00442
00443
00444
00445
00446 std::string Utilities::get_file_name(const std::string& s)
00447 {
00448 std::string::size_type slash_position = s.find_last_of("/\\");
00449 if (slash_position != std::string::npos)
00450 {
00451 return s.substr(slash_position+1,std::string::npos);
00452 }
00453 else
00454 {
00455 return s;
00456 }
00457 }
00458
00459
00460
00461
00468 int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
00469 {
00470 int numberOfFiles = 0;
00471 std::string fileName;
00472
00473 std::string dirName = dirpath;
00474
00475 #ifdef _MSC_VER
00476 WIN32_FIND_DATA fileData;
00477 HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData);
00478
00479 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
00480 b = FindNextFile(hFile, &fileData))
00481 {
00482 fileName = fileData.cFileName;
00483 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00484 {
00485
00486 if ( fileName != "." && fileName != ".." && recursive )
00487 {
00488 numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
00489 }
00490 }
00491 else
00492 {
00493 Filenames.push_back(dirName+"\\"+fileName);
00494 numberOfFiles++;
00495 }
00496 }
00497 DWORD dwError = GetLastError();
00498 if (hFile != INVALID_HANDLE_VALUE)
00499 FindClose(hFile);
00500 if (dwError != ERROR_NO_MORE_FILES)
00501 {
00502 LPVOID lpMsgBuf;
00503 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
00504 FORMAT_MESSAGE_FROM_SYSTEM|
00505 FORMAT_MESSAGE_IGNORE_INSERTS,
00506 NULL,GetLastError(),
00507 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00508 (LPTSTR) &lpMsgBuf,0,NULL);
00509
00510
00511
00512
00513 return 0;
00514 }
00515
00516 #else
00517
00518
00519
00520 DIR* dir = opendir(dirName.c_str());
00521 if (!dir)
00522 {
00523 return 0;
00524 }
00525
00526
00527
00528
00529
00530
00531 struct stat buf;
00532 dirent *d;
00533 for (d = readdir(dir); d; d = readdir(dir))
00534 {
00535 fileName = dirName + "/" + d->d_name;
00536
00537 if( stat(fileName.c_str(), &buf) != 0 )
00538 {
00539
00540 }
00541 if ( S_ISREG(buf.st_mode) )
00542 {
00543 Filenames.push_back( fileName );
00544 numberOfFiles++;
00545 }
00546 else if ( S_ISDIR(buf.st_mode) )
00547 {
00548 if ( d->d_name[0] != '.' && recursive )
00549 {
00550 numberOfFiles += Explore( fileName, recursive, Filenames);
00551 }
00552 }
00553 else
00554 {
00555
00556 return -1;
00557 }
00558 }
00559 if( closedir(dir) != 0 )
00560 {
00561
00562 }
00563 #endif
00564
00565 return numberOfFiles;
00566
00567 }
00568
00569
00570
00571
00572
00573 void Utilities::SubsBackslashN ( std::string& s )
00574 {
00575 std::string ss("\\n");
00576 std::string::size_type pos = 0;
00577 pos = s.find(ss,0);
00578 const char* cr = "\n";
00579 while ( pos != std::string::npos )
00580 {
00581 s.replace(pos,2,cr,1);
00582 pos = s.find(ss, pos-1);
00583 }
00584 }
00585
00586
00587
00588
00589
00590 bool Utilities::loosematch(std::string stdLine,std::string stdOptions)
00591 {
00592 bool result=false;
00593 std::vector<std::string> tokens;
00594 SplitString ( stdOptions,"|", tokens);
00595 int i,size=tokens.size();
00596 for (i=0; i<size; i++)
00597 {
00598 #ifdef WIN32
00599 if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0)
00600 {
00601 result=true;
00602 }
00603 #else
00604 if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0)
00605 {
00606 result=true;
00607 }
00608 #endif
00609
00610 }
00611 return result;
00612 }
00613
00614
00615
00616
00617
00618 int get_app_path (char *pname, size_t pathsize)
00619 {
00620 #ifdef LINUX
00621
00622
00623 pathsize --;
00624 long result = readlink("/proc/self/exe", pname, pathsize);
00625 if (result > 0)
00626 {
00627 pname[result] = 0;
00628
00629 if ((access(pname, 0) == 0))
00630 return 0;
00631
00632
00633 }
00634 #endif
00635
00636 #ifdef WIN32
00637 long result = GetModuleFileName(NULL, pname, pathsize);
00638 if (result > 0)
00639 {
00640
00641 int len = strlen(pname);
00642 int idx;
00643 for (idx = 0; idx < len; idx++)
00644 {
00645 if (pname[idx] == '\\') pname[idx] = '/';
00646 }
00647
00648 for (idx = len-1; idx >=0 ; idx--)
00649 {
00650 if (pname[idx] == '/')
00651 {
00652 pname[idx+1] = '\0';
00653 idx = -1;
00654 }
00655 }
00656
00657 if ((access(pname, 0) == 0))
00658 return 0;
00659
00660
00661 }
00662 #endif
00663
00664 #ifdef SOLARIS
00665 char *p = getexecname();
00666 if (p)
00667 {
00668
00669
00670
00671
00672
00673
00674
00675
00676 if (p[0] == '/')
00677
00678 {
00679 strncpy(pname, p, pathsize);
00680 if ((access(pname, 0) == 0))
00681 return 0;
00682 }
00683 else
00684
00685 {
00686 getcwd(pname, pathsize);
00687 long result = strlen(pname);
00688 strncat(pname, "/", (pathsize - result));
00689 result ++;
00690 strncat(pname, p, (pathsize - result));
00691
00692 if ((access(pname, 0) == 0))
00693 return 0;
00694
00695
00696 }
00697 }
00698 #endif
00699
00700 #ifdef MACOSX
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 int status = -1;
00718 char *given_path = (char*)malloc(MAXPATHLEN * 2);
00719 if (!given_path) return status;
00720
00721 uint32_t npathsize = MAXPATHLEN * 2;
00722 long result = _NSGetExecutablePath(given_path, &npathsize);
00723 if (result == 0)
00724 {
00725
00726 if (realpath(given_path, pname) != NULL)
00727 {
00728 if ((access(pname, 0) == 0))
00729 status = 0;
00730 }
00731 }
00732 free (given_path);
00733 return status;
00734 #endif
00735
00736 return -1;
00737 }
00738
00739
00740
00741
00742
00743 std::string Utilities::GetExecutablePath()
00744 {
00745 char name[PATH_MAX];
00746 int err = get_app_path(name, PATH_MAX);
00747 if (err)
00748 {
00749 bbtkGlobalError("Could not determine current executable path ?");
00750 }
00751
00752
00753 char *slash;
00754 slash = strrchr(name, VALID_FILE_SEPARATOR_CHAR);
00755 if (slash)
00756 {
00757 *slash = 0;
00758 }
00759 return name;
00760 }
00761
00762
00763
00764
00765 }