42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpIoException.h>
50 #include <sys/types.h>
55 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
65 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
66 # include <TargetConditionals.h>
80 static std::string build_info =
81 #include "version_string.inc"
132 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
134 char *_username = NULL;
136 if (_username == NULL) {
137 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
139 "Cannot get the username")) ;
141 username = _username;
142 #elif defined(_WIN32)
143 # if ( ! defined(WINRT) )
144 unsigned int info_buffer_size = 1024;
145 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
146 DWORD bufCharCount = (DWORD) info_buffer_size;
148 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
179 std::string username;
180 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
182 char *_username = NULL;
184 if (_username == NULL) {
185 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
187 "Cannot get the username")) ;
189 username = _username;
190 #elif defined(_WIN32)
191 # if ( ! defined(WINRT) )
192 unsigned int info_buffer_size = 1024;
193 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
194 DWORD bufCharCount = (DWORD) info_buffer_size;
196 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
200 "Cannot get the username")) ;
246 #if defined(_WIN32) && defined(WINRT)
253 if (_value == NULL) {
255 "Cannot get the environment variable value")) ;
313 if(version.size() == 0){
319 size_t major_pos = version.find(
'.');
320 std::string major_str = version.substr(0, major_pos);
321 major = (unsigned)atoi(major_str.c_str());
323 if(major_pos != std::string::npos){
324 size_t minor_pos = version.find(
'.', major_pos+1);
325 std::string minor_str = version.substr(major_pos+1, (minor_pos - (major_pos+1)));
326 minor = (unsigned)atoi(minor_str.c_str());
328 if(minor_pos != std::string::npos){
329 std::string patch_str = version.substr(minor_pos+1);
330 patch = (unsigned)atoi(patch_str.c_str());
360 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
362 #elif defined(_WIN32)
366 if ( dirname == NULL || dirname[0] ==
'\0' ) {
370 std::string _dirname =
path(dirname);
372 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
373 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
374 #elif defined(_WIN32)
375 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
380 if ( (stbuf.st_mode & S_IFDIR) == 0 ) {
383 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
384 if ( (stbuf.st_mode & S_IWUSR) == 0 )
385 #elif defined(_WIN32)
386 if ( (stbuf.st_mode & S_IWRITE) == 0 )
429 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
431 #elif defined(_WIN32)
435 if ( dirname == NULL || dirname[0] ==
'\0' ) {
438 "invalid directory name")) ;
441 std::string _dirname =
path(dirname);
443 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
444 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
445 #elif defined(_WIN32)
446 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
449 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
450 if ( mkdir( _dirname.c_str(), (mode_t)0755 ) != 0 )
451 #elif defined(_WIN32)
452 if ( _mkdir( _dirname.c_str()) != 0 )
455 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
457 "unable to create directory")) ;
463 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
465 "unable to create directory")) ;
488 vpERROR_TRACE(
"unable to create directory '%s'\n",dirname.c_str());
490 "unable to create directory")) ;
510 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
512 #elif defined(_WIN32)
516 if ( filename == NULL || filename[0] ==
'\0' ) {
520 std::string _filename =
path(filename);
521 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
522 if ( stat( _filename.c_str(), &stbuf ) != 0 )
523 #elif defined(_WIN32)
524 if ( _stat( _filename.c_str(), &stbuf ) != 0 )
529 if ( (stbuf.st_mode & S_IFREG) == 0 ) {
532 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
533 if ( (stbuf.st_mode & S_IRUSR) == 0 )
534 #elif defined(_WIN32)
535 if ( (stbuf.st_mode & S_IREAD) == 0 )
579 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
580 char cmd[FILENAME_MAX];
582 sprintf(cmd,
"cp -p %s %s", src, dst);
587 #elif defined(_WIN32)
588 # if ( ! defined(WINRT) )
589 char cmd[FILENAME_MAX];
593 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
605 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
606 char cmd[FILENAME_MAX];
608 sprintf(cmd,
"cp -p -r %s %s", src, dst);
613 #elif defined(_WIN32)
614 # if ( ! defined(WINRT) )
615 char cmd[FILENAME_MAX];
619 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
630 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
668 if (::
remove(file_or_dir) != 0)
675 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
676 char cmd[FILENAME_MAX];
677 sprintf(cmd,
"rm -rf %s", file_or_dir);
678 int ret = system(cmd);
682 #elif defined(_WIN32)
683 # if ( ! defined(WINRT) )
684 char cmd[FILENAME_MAX];
686 sprintf(cmd,
"rmdir /S /Q %s", file_or_dir_.c_str());
687 int ret = system(cmd);
697 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
732 if (::
rename(oldfilename, newfilename) != 0)
772 std::string
path(pathname);
775 for(
unsigned int i=0 ; i<path.length() ; i++)
776 if( path[i] ==
'/') path[i] =
'\\';
777 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
778 for(
unsigned int i=0 ; i<path.length() ; i++)
779 if( path[i] ==
'\\') path[i] =
'/';
780 # if TARGET_OS_IOS == 0 // The following code is not working on iOS since wordexp() is not available
781 wordexp_t exp_result;
783 wordexp(path.c_str(), &exp_result, 0);
787 for(
size_t i = 0; i < exp_result.we_wordc; i++) {
788 path += exp_result.we_wordv[i];
789 if(i < exp_result.we_wordc-1) {
793 wordfree(&exp_result);
815 return path(pathname.c_str());
830 std::ifstream confContent(
configFile.c_str(), std::ios::in);
832 if(confContent.is_open())
834 std::string line,var,val;
837 std::string stop[3] = {
" ",
"\t",
"#"};
838 while(std::getline(confContent, line))
840 if((line.compare(0,1,
"#") != 0) && (line.size() > 2))
845 k = (
unsigned long)line.find(
" ");
846 var = line.substr(0,k);
849 for(
unsigned i=0;i<3;++i)
852 c = (int)line.size();
853 long unsigned int c_ = (
long unsigned int) c;
854 val = line.substr(k+1,c_-k-1);
880 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
885 value = (
float) M_PI;
887 value = (
float) (M_PI/2.0);
889 value = (
float) (-M_PI/2.0);
896 std::cout << var <<
" not found in config file" << std::endl;
910 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
926 std::cout << var <<
" not found in config file" << std::endl;
941 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
950 std::cout << var <<
" not found in config file" << std::endl;
966 value = (
unsigned int) v;
1013 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
1022 std::cout << var <<
" not found in config file" << std::endl;
1042 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
1048 if(nCols != 0 && nRows != 0)
1049 value.
resize(nRows, nCols);
1051 for(
unsigned int i=0;i<value.
getRows();++i)
1052 for(
unsigned int j=0;j<value.
getCols();++j)
1056 if(nb.compare(
"PI") == 0)
1058 else if(nb.compare(
"PI/2") == 0)
1059 value[i][j] = M_PI/2;
1060 else if(nb.compare(
"-PI/2") == 0)
1061 value[i][j] = -M_PI/2;
1063 value[i][j] = atof(nb.c_str());
1069 std::cout << var <<
" not found in config file" << std::endl;
1086 else if(strFalse !=
"")
1100 if(std::fabs(val) < std::numeric_limits<double>::epsilon())
1103 sprintf(valC,
"%.3f", val);
1104 std::string valS(valC);
1159 std::string data_path;
1160 std::string file_to_test(
"ViSP-images/mbt/cube.cao");
1161 std::string filename;
1162 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1164 data_path =
"/usr/share/visp-images-data";
1165 filename = data_path +
"/" + file_to_test;
1172 filename = data_path +
"/" + file_to_test;
1198 std::string sep =
"\\";
1199 std::string altsep =
"/";
1200 std::string extsep =
".";
1203 std::string sep =
"/";
1204 std::string altsep =
"";
1205 std::string extsep =
".";
1238 int sepIndex = (int)pathname.rfind(sep);
1239 if(!altsep.empty()) {
1240 int altsepIndex = (int)pathname.rfind(altsep);
1241 sepIndex = (std::max)(sepIndex, altsepIndex);
1244 size_t dotIndex = pathname.rfind(extsep);
1245 if(dotIndex != std::string::npos) {
1247 if((sepIndex != (
int)std::string::npos && (
int)dotIndex > sepIndex) || sepIndex == (
int)std::string::npos) {
1248 if(sepIndex == (
int)std::string::npos) {
1251 size_t filenameIndex = (size_t)(sepIndex + 1);
1253 while(filenameIndex < dotIndex) {
1254 if(pathname.compare(filenameIndex, 1, extsep) != 0) {
1255 return pathname.substr(dotIndex);
1273 if(pathname.size() > 0)
1278 if(index != std::string::npos) {
1279 return convertedPathname.substr(index + 1);
1282 return convertedPathname;
1296 size_t found = name.find_last_of(
".");
1297 std::string name_we = name.substr(0, found);
1308 if(pathname.size() > 0)
1313 if(index != std::string::npos) {
1314 return convertedPathname.substr(0, index);
1330 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1331 std::string real_path_str = pathname;
1332 char *real_path = realpath(pathname.c_str(), NULL);
1334 if (real_path != NULL) {
1335 real_path_str = real_path;
1338 return real_path_str;
1339 #elif defined(_WIN32)
1340 # if ( ! defined(WINRT) )
1341 std::string real_path_str = pathname;
1343 TCHAR buffer[4096] = TEXT(
"");
1345 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1347 real_path_str = buffer;
1349 return real_path_str;
1368 if(child.size() == 0 && parent.size() == 0)
1373 if(child.size() == 0)
1378 if(parent.size() == 0)
1386 std::stringstream ss;
1388 std::string stringSeparator;
1389 ss >> stringSeparator;
1391 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1392 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1394 if(lastConvertedParentChar == stringSeparator)
1396 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1399 if(firstConvertedChildChar == stringSeparator)
1401 convertedChild = convertedChild.substr(1);
1404 return std::string(convertedParent + vpIoTools::separator + convertedChild);
1426 return path.size() > 0 && (path.substr(0, 1) ==
"/" || path.substr(0, 1) ==
"\\");
1444 return (path1_normalize == path2_normalize);
1504 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1505 return std::pair<std::string, std::string>(
"", pathname);
1507 const std::string sep =
"\\";
1508 const std::string sepsep =
"\\\\";
1509 const std::string altsep =
"/";
1511 if(pathname.size() > 1) {
1512 std::string normPathname = pathname;
1513 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1515 if(normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1520 size_t index = normPathname.find(sep, 2);
1521 if(index == std::string::npos) {
1522 return std::pair<std::string, std::string>(
"", pathname);
1525 size_t index2 = normPathname.find(sep, index + 1);
1528 if(index2 == index + 1) {
1529 return std::pair<std::string, std::string>(
"", pathname);
1532 if(index2 == std::string::npos) {
1533 index2 = pathname.size();
1536 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1539 if(normPathname[1] ==
':') {
1540 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1544 return std::pair<std::string, std::string>(
"", pathname);
1598 size_t startIndex = 0;
1600 std::string chainToSplit = chain;
1601 std::vector<std::string> subChain;
1602 size_t sepIndex = chainToSplit.find(sep);
1604 while(sepIndex != std::string::npos) {
1605 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1607 subChain.push_back( sub );
1608 chainToSplit = chainToSplit.substr(sepIndex+1, chain.size()-1);
1610 sepIndex = chainToSplit.find(sep);
1612 if (!chainToSplit.empty())
1613 subChain.push_back(chainToSplit);
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Class to define colors available for display functionnalities.
Error that can be emited by the vpIoTools class and its derivates.
unsigned int getCols() const
Return the number of columns of the 2D array.
static Type minimum(const Type &a, const Type &b)
unsigned int getRows() const
Return the number of rows of the 2D array.
static vpColor getColor(const unsigned int &i)