#include <bbtkUtilities.h>
Static Public Member Functions | |
static bool | FileExists (std::string strFilename) |
static std::string | ExtractPackageName (const std::string &name, std::string &path) |
static std::string | ExtractScriptName (const std::string &name, std::string &path) |
static std::string | ExpandLibName (const std::string &name, bool verbose) |
static std::string | MakeLibnameFromPath (std::string path, std::string pkgname) |
static std::string | MakePkgnameFromPath (std::string path, std::string pkgname, bool addExt) |
static std::string | MakeUserSettingsFullFileName (const std::string &name) |
in user settings dir, e.g. /home/username/.bbtk/ | |
static bool | IsAtRoot (std::string cwd) |
static bool | IsDirectory (std::string const &dirName) |
static void | SplitAroundFirstDot (const std::string &in, std::string &left, std::string &right) |
static void | SplitString (const std::string &str, const std::string &delimiters, std::vector< std::string > &tokens) |
static std::string | get_file_name (const std::string &s) |
static int | Explore (std::string const &dirpath, bool recursive, std::vector< std::string > &Filenames) |
Explore a directory with possibility of recursion return number of files read. | |
static void | SubsBackslashN (std::string &s) |
static void | replace (std::string &str, const std::string &what, const std::string &with) |
static void | html_format (std::string &str) |
static bool | loosematch (std::string stdLine, std::string stdOptions) |
various usefull methods
Definition at line 73 of file bbtkUtilities.h.
bool bbtk::Utilities::FileExists | ( | std::string | strFilename | ) | [static] |
Definition at line 43 of file bbtkUtilities.cxx.
00044 { 00045 struct stat stFileInfo; 00046 bool blnReturn; 00047 int intStat; 00048 00049 // Attempt to get the file attributes 00050 intStat = stat(strFilename.c_str(),&stFileInfo); 00051 if(intStat == 0) 00052 { 00053 // We were able to get the file attributes 00054 // so the file obviously exists. 00055 blnReturn = true; 00056 } 00057 else 00058 { 00059 // We were not able to get the file attributes. 00060 // This may mean that we don't have permission to 00061 // access the folder which contains this file. If you 00062 // need to do that level of checking, lookup the 00063 // return values of stat which will give you 00064 // more details on why stat failed. 00065 blnReturn = false; 00066 } 00067 00068 return(blnReturn); 00069 }
std::string bbtk::Utilities::ExtractPackageName | ( | const std::string & | name, | |
std::string & | path | |||
) | [static] |
Definition at line 74 of file bbtkUtilities.cxx.
References bbtkError.
00076 { 00077 std::string pkgname; 00078 path = ""; 00079 00080 std::string::size_type slash_position = name.find_last_of("/\\"); 00081 if (slash_position != std::string::npos) 00082 { 00083 pkgname = name.substr(slash_position+1,std::string::npos); 00084 path = name.substr(0,slash_position); 00085 // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos); 00086 } 00087 else 00088 { 00089 pkgname = name; 00090 } 00091 00092 // remove {.so | dll} if any 00093 std::string::size_type dot_position = pkgname.find_last_of('.'); 00094 if (dot_position != std::string::npos){ 00095 pkgname = pkgname.substr(0,dot_position); 00096 } 00097 #if defined(__GNUC__) 00098 00099 // GCC mechanism 00100 // shared lib name = libbb<name>.so 00101 00102 // remove {libbb} if any 00103 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) { 00104 pkgname = pkgname.substr(5, pkgname.length()); 00105 } 00106 /* 00109 */ 00110 #elif defined(_WIN32) 00111 00112 // WIN 32 mechanism 00113 // shared lib name = <name>.dll 00114 00115 // EED Problem loading package call bbtkTools 00116 // // remove {bb} if any 00117 if (memcmp (pkgname.c_str(), "bb", 2) == 0) { 00118 pkgname = pkgname.substr(2, pkgname.length()); 00119 } 00120 00121 /* 00124 */ 00125 #else 00126 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?"); 00127 #endif 00128 return pkgname; 00129 }
std::string bbtk::Utilities::ExtractScriptName | ( | const std::string & | name, | |
std::string & | path | |||
) | [static] |
Definition at line 132 of file bbtkUtilities.cxx.
00134 { 00135 std::string pkgname; 00136 00137 std::string::size_type slash_position = name.find_last_of("/\\"); 00138 if (slash_position != std::string::npos) { 00139 pkgname =name.substr(slash_position+1,std::string::npos); 00140 path = name.substr(0,slash_position); 00141 } else { 00142 pkgname = name; 00143 } 00144 // remove {.bbs } if any 00145 std::string::size_type dot_position = pkgname.find_last_of('.'); 00146 if (dot_position != std::string::npos){ 00147 pkgname = pkgname.substr(0,dot_position); 00148 } 00149 return pkgname; 00150 }
std::string bbtk::Utilities::ExpandLibName | ( | const std::string & | name, | |
bool | verbose | |||
) | [static] |
Definition at line 154 of file bbtkUtilities.cxx.
References IsAtRoot().
00155 { 00156 // ----- Think of expanding path name ( ./ ../ ../../ ) 00157 00158 char buf[2048]; // for getcwd 00159 char * currentDir = getcwd(buf, 2048); 00160 std::string cwd(currentDir); 00161 std::string libname(name); 00162 std::string fileSeparator; 00163 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator(); 00164 // tooHigh : true is user supplies a library pathname with too many "../" 00165 bool tooHigh = false; 00166 00167 //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl; 00168 00169 if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name 00170 { 00171 return(libname); 00172 } 00173 else if ( name =="." ) 00174 { 00175 libname = cwd + fileSeparator; 00176 return(libname); 00177 } 00178 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') ) 00179 { 00180 libname = cwd + fileSeparator + name.substr(2, name.length()); 00181 return(libname); 00182 } 00183 else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ ) 00184 { 00185 if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows) 00186 { 00187 // if we are already at / or c: --> hopeless 00188 if (verbose) 00189 std::cout << " File path [" << name << "] doesn't exist" << std::endl; 00190 tooHigh = true; 00191 } 00192 else 00193 { 00194 // iterate on ../ and go up from the current working dir! 00195 std::string a(name); 00196 bool alreadyProcessRoot = false; 00197 00198 //if (a[a.size()-1] != fileSeparator[0]) 00199 // a.append(fileSeparator); 00200 //std::cout << "------------------a [" << a << "]" << std::endl; 00201 00202 for(;;) // wild loop ! 00203 { 00204 std::string::size_type slash_position = cwd.find_last_of(fileSeparator); 00205 if (slash_position != std::string::npos) { 00206 if (slash_position == 0) 00207 slash_position = 1; 00208 cwd = cwd.substr(0,slash_position/*+1*/); 00209 //std::cout << "------------------cwd [" << cwd << "]" << std::endl; 00210 // if (a == "..") { 00211 // a = ""; 00212 // break; 00213 // } 00214 // else 00215 a = a.substr(3, /*name.length()*/ a.length()); // remove ../ 00216 //std::cout << "------------------a [" << a << "]" << std::endl; 00217 if (a == "" || alreadyProcessRoot) 00218 { 00219 if (verbose) 00220 std::cout << " File path : [" << name << "] doesn't exist" << std::endl; 00221 tooHigh = true; 00222 break; 00223 } 00224 // std::string b = cwd + a; 00225 libname = cwd; 00226 char c = cwd[cwd.size()-1]; 00227 if (c != '/' && c != '\\' ) 00228 libname += fileSeparator; 00229 libname += a; 00230 00231 if ( a[0] != '.' ) // if . (probabely ../), loop again 00232 break; 00233 00234 if (IsAtRoot(cwd)) 00235 alreadyProcessRoot = true; 00236 } 00237 } // end iterating on ../ 00238 } 00239 //std::cout << "------------------out of loop]" << std::endl; 00240 if (tooHigh) 00241 libname=""; 00242 return (libname); 00243 00244 } // ----- End of expanding path name ( ./ ../ ../../ ) 00245 00246 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl; 00247 // To avoid warning 00248 return(""); // Will never get here! 00249 }
std::string bbtk::Utilities::MakeLibnameFromPath | ( | std::string | path, | |
std::string | pkgname | |||
) | [static] |
Definition at line 253 of file bbtkUtilities.cxx.
00254 { 00255 std::string libname = path; 00256 char c = path[path.size()-1]; 00257 #if defined(__GNUC__) 00258 if (c != '/') 00259 libname += "/libbb"; 00260 else 00261 libname += "libbb"; 00262 libname += pkgname; 00263 libname += ".so"; 00264 00265 #elif defined(_WIN32) 00266 if (c != '\\') 00267 libname = path+"\\bb"; 00268 libname += pkgname; 00269 libname += ".dll"; 00270 #endif 00271 return libname; 00272 }
std::string bbtk::Utilities::MakePkgnameFromPath | ( | std::string | path, | |
std::string | pkgname, | |||
bool | addExt | |||
) | [static] |
Definition at line 276 of file bbtkUtilities.cxx.
00277 { 00278 std::string libname = path; 00279 char c = path[path.size()-1]; 00280 if (c != '/' && c != '\\') 00281 { 00282 libname += ConfigurationFile::GetInstance().Get_file_separator (); 00283 } 00284 libname += pkgname; 00285 if (addExt) 00286 { 00287 int l = libname.size(); 00288 if (l>4) 00289 { 00290 if (libname.substr(l-4, 4) != ".bbs") 00291 { 00292 libname = libname + ".bbs"; 00293 } 00294 } 00295 } 00296 return libname; 00297 }
std::string bbtk::Utilities::MakeUserSettingsFullFileName | ( | const std::string & | name | ) | [static] |
in user settings dir, e.g. /home/username/.bbtk/
Builds the complete path to the file 'name' located
Builds the complete path to the file 'name' located in user settings dir, e.g. /home/username/.bbtk/
Definition at line 302 of file bbtkUtilities.cxx.
References INVALID_FILE_SEPARATOR, and VALID_FILE_SEPARATOR.
00303 { 00304 #if defined(__GNUC__) 00305 std::string str_home(getenv("HOME")); 00306 #elif defined(_WIN32) 00307 std::string str_home(getenv("USERPROFILE")); 00308 #endif 00309 std::string fullname = str_home + "/.bbtk/" + name; 00310 Utilities::replace( fullname, 00311 INVALID_FILE_SEPARATOR , 00312 VALID_FILE_SEPARATOR); 00313 return fullname; 00314 }
bool bbtk::Utilities::IsAtRoot | ( | std::string | cwd | ) | [static] |
Definition at line 320 of file bbtkUtilities.cxx.
Referenced by ExpandLibName().
00321 { 00322 if ( cwd == "/" // hope it gets / (for Linux) 00323 || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows) 00324 return (true); 00325 else 00326 return(false); 00327 }
bool bbtk::Utilities::IsDirectory | ( | std::string const & | dirName | ) | [static] |
Definition at line 331 of file bbtkUtilities.cxx.
00332 { 00333 struct stat fs; 00334 00335 if ( stat(dirName.c_str(), &fs) == 0 ) 00336 { 00337 #if _WIN32 00338 return ((fs.st_mode & _S_IFDIR) != 0); 00339 #else 00340 return S_ISDIR(fs.st_mode); 00341 #endif 00342 } 00343 else 00344 { 00345 return false; 00346 } 00347 }
void bbtk::Utilities::SplitAroundFirstDot | ( | const std::string & | in, | |
std::string & | left, | |||
std::string & | right | |||
) | [static] |
Definition at line 351 of file bbtkUtilities.cxx.
00354 { 00355 std::string delimiter = "."; 00356 std::string::size_type pos = in.find_first_of(delimiter); 00357 if (std::string::npos != pos) 00358 { 00359 left = in.substr(0,pos); 00360 right = in.substr(pos+1,in.size()); 00361 00362 } 00363 else 00364 { 00365 // bbtkError(in<<" : expected 'a.b' format but no dot found"); 00366 left =""; 00367 right = ""; 00368 } 00369 }
void bbtk::Utilities::SplitString | ( | const std::string & | str, | |
const std::string & | delimiters, | |||
std::vector< std::string > & | tokens | |||
) | [static] |
Definition at line 371 of file bbtkUtilities.cxx.
Referenced by loosematch().
00374 { 00375 // Skip delimiters at beginning. 00376 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); 00377 // Find first delimiter. 00378 std::string::size_type pos = str.find_first_of(delimiters, lastPos); 00379 00380 while (std::string::npos != pos || std::string::npos != lastPos) 00381 { 00382 // Found a token, add it to the vector. 00383 tokens.push_back(str.substr(lastPos, pos - lastPos)); 00384 // Skip delimiters. Note the "not_of" 00385 lastPos = str.find_first_not_of(delimiters, pos); 00386 // Find next delimiter 00387 pos = str.find_first_of(delimiters, lastPos); 00388 } 00389 00390 }
std::string bbtk::Utilities::get_file_name | ( | const std::string & | s | ) | [static] |
Definition at line 396 of file bbtkUtilities.cxx.
00397 { 00398 std::string::size_type slash_position = s.find_last_of("/\\"); 00399 if (slash_position != std::string::npos) 00400 { 00401 return s.substr(slash_position+1,std::string::npos); 00402 } 00403 else 00404 { 00405 return s; 00406 } 00407 }
int bbtk::Utilities::Explore | ( | std::string const & | dirpath, | |
bool | recursive, | |||
std::vector< std::string > & | Filenames | |||
) | [static] |
Explore a directory with possibility of recursion return number of files read.
dirpath | directory to explore | |
recursive | whether we want recursion or not |
Definition at line 416 of file bbtkUtilities.cxx.
References Explore().
Referenced by Explore().
00417 { 00418 int numberOfFiles = 0; 00419 std::string fileName; 00420 00421 std::string dirName = dirpath; 00422 00423 #ifdef _MSC_VER 00424 WIN32_FIND_DATA fileData; 00425 HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData); 00426 00427 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b; 00428 b = FindNextFile(hFile, &fileData)) 00429 { 00430 fileName = fileData.cFileName; 00431 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) 00432 { 00433 // Need to check for . and .. to avoid infinite loop 00434 if ( fileName != "." && fileName != ".." && recursive ) 00435 { 00436 numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames); 00437 } 00438 } 00439 else 00440 { 00441 Filenames.push_back(dirName+"\\"+fileName); 00442 numberOfFiles++; 00443 } 00444 } 00445 DWORD dwError = GetLastError(); 00446 if (hFile != INVALID_HANDLE_VALUE) 00447 FindClose(hFile); 00448 if (dwError != ERROR_NO_MORE_FILES) 00449 { 00450 LPVOID lpMsgBuf; 00451 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| 00452 FORMAT_MESSAGE_FROM_SYSTEM| 00453 FORMAT_MESSAGE_IGNORE_INSERTS, 00454 NULL,GetLastError(), 00455 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 00456 (LPTSTR) &lpMsgBuf,0,NULL); 00457 00458 // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf 00459 // <<" for the directory : "<<dirName); 00460 00461 return 0; 00462 } 00463 00464 #else 00465 // Real POSIX implementation: scandir is a BSD extension only, and doesn't 00466 // work on debian for example 00467 //std::cout <<"in Explor dirname[" << dirName << "]" << std::endl; 00468 DIR* dir = opendir(dirName.c_str()); 00469 if (!dir) 00470 { 00471 return 0; 00472 } 00473 //std::cout <<"Open OK" << std::endl; 00474 // According to POSIX, the dirent structure contains a field char d_name[] 00475 // of unspecified size, with at most NAME_MAX characters preceeding the 00476 // terminating null character. Use of other fields will harm the porta- 00477 // bility of your programs. 00478 00479 struct stat buf; 00480 dirent *d; 00481 for (d = readdir(dir); d; d = readdir(dir)) 00482 { 00483 fileName = dirName + "/" + d->d_name; 00484 //std::cout <<"in Explor filename[" << fileName << "]" << std::endl; 00485 if( stat(fileName.c_str(), &buf) != 0 ) 00486 { 00487 //ErrorMacro( strerror(errno) ); 00488 } 00489 if ( S_ISREG(buf.st_mode) ) //is it a regular file? 00490 { 00491 Filenames.push_back( fileName ); 00492 numberOfFiles++; 00493 } 00494 else if ( S_ISDIR(buf.st_mode) ) //directory? 00495 { 00496 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files 00497 { 00498 numberOfFiles += Explore( fileName, recursive, Filenames); 00499 } 00500 } 00501 else 00502 { 00503 //ErrorMacro( "Unexpected error" ); 00504 return -1; 00505 } 00506 } 00507 if( closedir(dir) != 0 ) 00508 { 00509 // ErrorMacro( strerror(errno) ); 00510 } 00511 #endif 00512 00513 return numberOfFiles; 00514 00515 }
void bbtk::Utilities::SubsBackslashN | ( | std::string & | s | ) | [static] |
Definition at line 520 of file bbtkUtilities.cxx.
00521 { 00522 std::string ss("\\n"); 00523 std::string::size_type pos = 0; 00524 pos = s.find(ss,0); 00525 char* cr = "\n"; 00526 while ( pos != std::string::npos ) 00527 { 00528 s.replace(pos,2,cr,1); 00529 pos = s.find(ss, pos-1); 00530 } 00531 }
static void bbtk::Utilities::replace | ( | std::string & | str, | |
const std::string & | what, | |||
const std::string & | with | |||
) | [inline, static] |
Definition at line 149 of file bbtkUtilities.h.
00152 { 00153 std::string::size_type pos = str.find( what ); 00154 while ( pos != std::string::npos ) 00155 { 00156 str.replace( pos, what.size(), with ); 00157 pos = str.find( what, pos+what.size()-1 ); 00158 } 00159 }
static void bbtk::Utilities::html_format | ( | std::string & | str | ) | [inline, static] |
Definition at line 162 of file bbtkUtilities.h.
00163 { 00164 replace( str, "&", "&" ); 00165 replace( str, "<", "<" ); 00166 replace( str, ">", ">" ); 00167 }
bool bbtk::Utilities::loosematch | ( | std::string | stdLine, | |
std::string | stdOptions | |||
) | [static] |
Definition at line 536 of file bbtkUtilities.cxx.
References bbtk::i, and SplitString().
00537 { 00538 bool result=false; 00539 std::vector<std::string> tokens; 00540 SplitString ( stdOptions,"|", tokens); 00541 int i,size=tokens.size(); 00542 for (i=0; i<size; i++) 00543 { 00544 #ifdef WIN32 00545 if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0) 00546 { 00547 result=true; 00548 } 00549 #else 00550 if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0) 00551 { 00552 result=true; 00553 } 00554 #endif 00555 00556 } 00557 return result; 00558 }