1 #include "bbtkUtilities.h"
9 // ======================================================================
10 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
11 bool Utilities::FileExists(std::string strFilename)
13 struct stat stFileInfo;
17 // Attempt to get the file attributes
18 intStat = stat(strFilename.c_str(),&stFileInfo);
21 // We were able to get the file attributes
22 // so the file obviously exists.
27 // We were not able to get the file attributes.
28 // This may mean that we don't have permission to
29 // access the folder which contains this file. If you
30 // need to do that level of checking, lookup the
31 // return values of stat which will give you
32 // more details on why stat failed.
40 // =====================================================================
42 std::string Utilities::ExtractPackageName(const std::string &name,
48 std::string::size_type slash_position = name.find_last_of("/\\");
49 if (slash_position != std::string::npos)
51 pkgname = name.substr(slash_position+1,std::string::npos);
52 path = name.substr(0,slash_position);
53 // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
60 // remove {.so | dll} if any
61 std::string::size_type dot_position = pkgname.find_last_of('.');
62 if (dot_position != std::string::npos){
63 pkgname = pkgname.substr(0,dot_position);
68 // shared lib name = libbb<name>.so
70 // remove {libbb} if any
71 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
72 pkgname = pkgname.substr(5, pkgname.length());
75 /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!?
76 /// \ --> Should be forbidden!
81 // shared lib name = <name>.dll
83 // EED Problem loading package call bbtkTools
84 // // remove {bb} if any
85 if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
86 pkgname = pkgname.substr(2, pkgname.length());
90 /// \ \todo what would happen if (stupid) user names his package 'bb' ?!?
91 /// \ --> Should be forbidden!
94 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
99 //=====================================================================
100 std::string Utilities::ExtractScriptName(const std::string &name,
105 std::string::size_type slash_position = name.find_last_of("/\\");
106 if (slash_position != std::string::npos) {
107 pkgname =name.substr(slash_position+1,std::string::npos);
108 path = name.substr(0,slash_position);
112 // remove {.bbs } if any
113 std::string::size_type dot_position = pkgname.find_last_of('.');
114 if (dot_position != std::string::npos){
115 pkgname = pkgname.substr(0,dot_position);
120 // ========================================================================
122 std::string Utilities::ExpandLibName(const std::string &name, bool verbose)
124 // ----- Think of expanding path name ( ./ ../ ../../ )
126 char buf[2048]; // for getcwd
127 char * currentDir = getcwd(buf, 2048);
128 std::string cwd(currentDir);
129 std::string libname(name);
130 std::string fileSeparator;
131 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
132 // tooHigh : true is user supplies a library pathname with too many "../"
133 bool tooHigh = false;
135 //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl;
137 if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name
141 else if ( name =="." )
143 libname = cwd + fileSeparator;
146 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
148 libname = cwd + fileSeparator + name.substr(2, name.length());
151 else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ )
153 if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows)
155 // if we are already at / or c: --> hopeless
157 std::cout << " File path [" << name << "] doesn't exist" << std::endl;
162 // iterate on ../ and go up from the current working dir!
164 bool alreadyProcessRoot = false;
166 //if (a[a.size()-1] != fileSeparator[0])
167 // a.append(fileSeparator);
168 //std::cout << "------------------a [" << a << "]" << std::endl;
170 for(;;) // wild loop !
172 std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
173 if (slash_position != std::string::npos) {
174 if (slash_position == 0)
176 cwd = cwd.substr(0,slash_position/*+1*/);
177 //std::cout << "------------------cwd [" << cwd << "]" << std::endl;
183 a = a.substr(3, /*name.length()*/ a.length()); // remove ../
184 //std::cout << "------------------a [" << a << "]" << std::endl;
185 if (a == "" || alreadyProcessRoot)
188 std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
192 // std::string b = cwd + a;
194 char c = cwd[cwd.size()-1];
195 if (c != '/' && c != '\\' )
196 libname += fileSeparator;
199 if ( a[0] != '.' ) // if . (probabely ../), loop again
203 alreadyProcessRoot = true;
205 } // end iterating on ../
207 //std::cout << "------------------out of loop]" << std::endl;
212 } // ----- End of expanding path name ( ./ ../ ../../ )
214 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
216 return(""); // Will never get here!
219 // ===================================================================================
221 std::string Utilities::MakeLibnameFromPath(std::string path, std::string pkgname)
223 std::string libname = path;
224 char c = path[path.size()-1];
225 #if defined(__GNUC__)
233 #elif defined(_WIN32)
235 libname = path+"\\bb";
242 // ===================================================================================
244 std::string Utilities::MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
246 std::string libname = path;
247 char c = path[path.size()-1];
248 if (c != '/' && c != '\\')
250 libname += ConfigurationFile::GetInstance().Get_file_separator ();
255 int l = libname.size();
258 if (libname.substr(l-4, 4) != ".bbs")
260 libname = libname + ".bbs";
267 //========================================================================
269 bool Utilities::IsAtRoot(std::string cwd)
271 if ( cwd == "/" // hope it gets / (for Linux)
272 || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
278 // ======================================================================
280 bool Utilities::IsDirectory(std::string const &dirName)
284 if ( stat(dirName.c_str(), &fs) == 0 )
287 return ((fs.st_mode & _S_IFDIR) != 0);
289 return S_ISDIR(fs.st_mode);
298 // ===================================================================================
300 void Utilities::SplitAroundFirstDot( const std::string& in,
304 std::string delimiter = ".";
305 std::string::size_type pos = in.find_first_of(delimiter);
306 if (std::string::npos != pos)
308 left = in.substr(0,pos);
309 right = in.substr(pos+1,in.size());
314 // bbtkError(in<<" : expected 'a.b' format but no dot found");
319 //=======================================================================
320 void Utilities::SplitString ( const std::string& str,
321 const std::string& delimiters,
322 std::vector<std::string>& tokens)
324 // Skip delimiters at beginning.
325 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
326 // Find first delimiter.
327 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
329 while (std::string::npos != pos || std::string::npos != lastPos)
331 // Found a token, add it to the vector.
332 tokens.push_back(str.substr(lastPos, pos - lastPos));
333 // Skip delimiters. Note the "not_of"
334 lastPos = str.find_first_not_of(delimiters, pos);
335 // Find next delimiter
336 pos = str.find_first_of(delimiters, lastPos);
340 //=======================================================================
343 // ===================================================================================
345 std::string Utilities::get_file_name(const std::string& s)
347 std::string::size_type slash_position = s.find_last_of("/\\");
348 if (slash_position != std::string::npos)
350 return s.substr(slash_position+1,std::string::npos);
358 // ===================================================================================
360 * \brief Explore a directory with possibility of recursion
361 * return number of files read
362 * @param dirpath directory to explore
363 * @param recursive whether we want recursion or not
365 int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
367 int numberOfFiles = 0;
368 std::string fileName;
370 std::string dirName = dirpath;
373 WIN32_FIND_DATA fileData;
374 HANDLE hFile = FindFirstFile((dirName+"\\*").c_str(), &fileData);
376 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
377 b = FindNextFile(hFile, &fileData))
379 fileName = fileData.cFileName;
380 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
382 // Need to check for . and .. to avoid infinite loop
383 if ( fileName != "." && fileName != ".." && recursive )
385 numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
390 Filenames.push_back(dirName+"\\"+fileName);
394 DWORD dwError = GetLastError();
395 if (hFile != INVALID_HANDLE_VALUE)
397 if (dwError != ERROR_NO_MORE_FILES)
400 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
401 FORMAT_MESSAGE_FROM_SYSTEM|
402 FORMAT_MESSAGE_IGNORE_INSERTS,
404 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
405 (LPTSTR) &lpMsgBuf,0,NULL);
407 // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
408 // <<" for the directory : "<<dirName);
414 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
415 // work on debian for example
416 //std::cout <<"in Explor dirname[" << dirName << "]" << std::endl;
417 DIR* dir = opendir(dirName.c_str());
422 //std::cout <<"Open OK" << std::endl;
423 // According to POSIX, the dirent structure contains a field char d_name[]
424 // of unspecified size, with at most NAME_MAX characters preceeding the
425 // terminating null character. Use of other fields will harm the porta-
426 // bility of your programs.
430 for (d = readdir(dir); d; d = readdir(dir))
432 fileName = dirName + "/" + d->d_name;
433 //std::cout <<"in Explor filename[" << fileName << "]" << std::endl;
434 if( stat(fileName.c_str(), &buf) != 0 )
436 //ErrorMacro( strerror(errno) );
438 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
440 Filenames.push_back( fileName );
443 else if ( S_ISDIR(buf.st_mode) ) //directory?
445 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
447 numberOfFiles += Explore( fileName, recursive, Filenames);
452 //ErrorMacro( "Unexpected error" );
456 if( closedir(dir) != 0 )
458 // ErrorMacro( strerror(errno) );
462 return numberOfFiles;
467 //=======================================================================
468 // Replaces substrings "\\n" by a real carriage return "\n"
469 void Utilities::SubsBackslashN ( std::string& s )
471 std::string ss("\\n");
472 std::string::size_type pos = 0;
475 while ( pos != std::string::npos )
477 s.replace(pos,2,cr,1);
478 pos = s.find(ss, pos-1);
481 //=======================================================================
485 bool Utilities::loosematch(std::string stdLine,std::string stdOptions)
488 std::vector<std::string> tokens;
489 SplitString ( stdOptions,"|", tokens);
490 int i,size=tokens.size();
491 for (i=0; i<size; i++)
494 if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0)
499 if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0)