53 #include <sys/types.h> 54 #include <visp3/core/vpDebug.h> 55 #include <visp3/core/vpIoException.h> 56 #include <visp3/core/vpIoTools.h> 57 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 68 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 69 #include <TargetConditionals.h> 73 #define PATH_MAX _MAX_PATH 82 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 86 void replaceAll(std::string &str,
const std::string &search,
const std::string &replace)
89 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
90 str.replace(start_pos, search.length(), replace);
91 start_pos += replace.length();
103 static std::string build_info =
104 #include "version_string.inc" 154 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 156 char *_username = NULL;
158 if (_username == NULL) {
159 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable");
162 username = _username;
163 #elif defined(_WIN32) 164 #if (!defined(WINRT)) 165 unsigned int info_buffer_size = 1024;
166 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
167 DWORD bufCharCount = (DWORD)info_buffer_size;
169 if (!GetUserName(infoBuf, &bufCharCount)) {
177 "Windows Platform"));
200 std::string username;
201 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 203 char *_username = NULL;
205 if (_username == NULL) {
206 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable");
209 username = _username;
210 #elif defined(_WIN32) 211 #if (!defined(WINRT)) 212 unsigned int info_buffer_size = 1024;
213 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
214 DWORD bufCharCount = (DWORD)info_buffer_size;
216 if (!GetUserName(infoBuf, &bufCharCount)) {
225 "Windows Platform"));
264 #if defined(_WIN32) && defined(WINRT) 266 "implemented on Universal Windows Platform"));
272 if (_value == NULL) {
324 void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
326 if (version.size() == 0) {
331 size_t major_pos = version.find(
'.');
332 std::string major_str = version.substr(0, major_pos);
333 major = (unsigned)atoi(major_str.c_str());
335 if (major_pos != std::string::npos) {
336 size_t minor_pos = version.find(
'.', major_pos + 1);
337 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
338 minor = (unsigned)atoi(minor_str.c_str());
340 if (minor_pos != std::string::npos) {
341 std::string patch_str = version.substr(minor_pos + 1);
342 patch = (unsigned)atoi(patch_str.c_str());
369 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 371 #elif defined(_WIN32) && defined(__MINGW32__) 373 #elif defined(_WIN32) 377 if (dirname == NULL || dirname[0] ==
'\0') {
381 std::string _dirname =
path(dirname);
383 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 384 if (stat(_dirname.c_str(), &stbuf) != 0)
385 #elif defined(_WIN32) && defined(__MINGW32__) 390 _dirname = _dirname.substr(0, _dirname.size() - 1);
391 if (stat(_dirname.c_str(), &stbuf) != 0)
392 #elif defined(_WIN32) 393 if (_stat(_dirname.c_str(), &stbuf) != 0)
398 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 399 if ((stbuf.st_mode & S_IFDIR) == 0)
404 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 405 if ((stbuf.st_mode & S_IWUSR) == 0)
406 #elif defined(_WIN32)
407 if ((stbuf.st_mode & S_IWRITE) == 0)
435 const size_t len = strlen(path);
436 char _path[PATH_MAX];
441 if (len >
sizeof(_path) - 1) {
442 errno = ENAMETOOLONG;
449 for (p = _path + 1; *p; p++) {
454 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 455 if (mkdir(_path, (mode_t)mode) != 0)
456 #elif defined(_WIN32) 468 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 469 if (mkdir(_path, (mode_t)mode) != 0)
470 #elif defined(_WIN32)
471 if (_mkdir(_path) != 0)
497 #if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32) 498 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
502 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 504 #elif defined(_WIN32) && defined(__MINGW32__) 506 #elif defined(_WIN32) 510 if (dirname == NULL || dirname[0] ==
'\0') {
515 std::string _dirname =
path(dirname);
517 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 518 if (stat(_dirname.c_str(), &stbuf) != 0)
519 #elif defined(_WIN32) && defined(__MINGW32__) 520 if (stat(_dirname.c_str(), &stbuf) != 0)
521 #elif defined(_WIN32) 522 if (_stat(_dirname.c_str(), &stbuf) != 0)
556 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname.c_str());
575 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 577 #elif defined(_WIN32) 581 if (filename == NULL || filename[0] ==
'\0') {
585 std::string _filename =
path(filename);
586 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 587 if (stat(_filename.c_str(), &stbuf) != 0)
588 #elif defined(_WIN32) 589 if (_stat(_filename.c_str(), &stbuf) != 0)
594 if ((stbuf.st_mode & S_IFREG) == 0) {
597 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 598 if ((stbuf.st_mode & S_IRUSR) == 0)
599 #elif defined(_WIN32)
600 if ((stbuf.st_mode & S_IREAD) == 0)
639 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 640 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 642 char cmd[FILENAME_MAX];
644 sprintf(cmd,
"cp -p %s %s", src, dst);
653 #elif defined(_WIN32) 654 #if (!defined(WINRT)) 655 char cmd[FILENAME_MAX];
659 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
672 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 673 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 675 char cmd[FILENAME_MAX];
677 sprintf(cmd,
"cp -p -r %s %s", src, dst);
686 #elif defined(_WIN32) 687 #if (!defined(WINRT)) 688 char cmd[FILENAME_MAX];
692 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
704 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
740 if (::
remove(file_or_dir) != 0)
746 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 747 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 749 char cmd[FILENAME_MAX];
750 sprintf(cmd,
"rm -rf %s", file_or_dir);
751 int ret = system(cmd);
759 #elif defined(_WIN32) 760 #if (!defined(WINRT)) 761 char cmd[FILENAME_MAX];
763 sprintf(cmd,
"rmdir /S /Q %s", file_or_dir_.c_str());
764 int ret = system(cmd);
775 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
805 if (::
rename(oldfilename, newfilename) != 0)
841 std::string
path(pathname);
844 for (
unsigned int i = 0; i < path.length(); i++)
847 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) 848 for (
unsigned int i = 0; i < path.length(); i++)
851 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 853 wordexp_t exp_result;
856 replaceAll(path,
"'",
"'\\''");
858 wordexp(std::string(
"'" + path +
"'").c_str(), &exp_result, 0);
859 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
860 wordfree(&exp_result);
894 std::ifstream confContent(
configFile.c_str(), std::ios::in);
896 if (confContent.is_open()) {
897 std::string line, var, val;
900 std::string stop[3] = {
" ",
"\t",
"#"};
901 while (std::getline(confContent, line)) {
902 if ((line.compare(0, 1,
"#") != 0) && (line.size() > 2)) {
905 k = (
unsigned long)line.find(
" ");
906 var = line.substr(0, k);
909 for (
unsigned i = 0; i < 3; ++i)
912 c = (int)line.size();
913 long unsigned int c_ = (
long unsigned int)c;
914 val = line.substr(k + 1, c_ - k - 1);
939 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
944 value = (
float)(M_PI / 2.0);
946 value = (
float)(-M_PI / 2.0);
953 std::cout << var <<
" not found in config file" << std::endl;
967 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
981 std::cout << var <<
" not found in config file" << std::endl;
996 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1003 std::cout << var <<
" not found in config file" << std::endl;
1019 value = (
unsigned int)v;
1066 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1073 std::cout << var <<
" not found in config file" << std::endl;
1091 const unsigned int &nRows)
1095 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1099 if (nCols != 0 && nRows != 0)
1100 value.
resize(nRows, nCols);
1101 size_t ind = 0, ind2;
1102 for (
unsigned int i = 0; i < value.
getRows(); ++i)
1103 for (
unsigned int j = 0; j < value.
getCols(); ++j) {
1106 if (nb.compare(
"PI") == 0)
1108 else if (nb.compare(
"PI/2") == 0)
1109 value[i][j] = M_PI / 2;
1110 else if (nb.compare(
"-PI/2") == 0)
1111 value[i][j] = -M_PI / 2;
1113 value[i][j] = atof(nb.c_str());
1119 std::cout << var <<
" not found in config file" << std::endl;
1137 else if (strFalse !=
"")
1152 if (std::fabs(val) < std::numeric_limits<double>::epsilon()) {
1154 sprintf(valC,
"%.3f", val);
1155 std::string valS(valC);
1212 std::string data_path;
1213 std::string file_to_test(
"mbt/cube.cao");
1214 std::string filename;
1215 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1217 data_path =
"/usr/share/visp-images-data/ViSP-images";
1218 filename = data_path +
"/" + file_to_test;
1221 data_path =
"/usr/share/visp-images-data/visp-images";
1222 filename = data_path +
"/" + file_to_test;
1229 filename = data_path +
"/" + file_to_test;
1233 filename = data_path +
"/" + file_to_test;
1237 filename = data_path +
"/" + file_to_test;
1264 std::string sep =
"\\";
1265 std::string altsep =
"/";
1266 std::string extsep =
".";
1269 std::string sep =
"/";
1270 std::string altsep =
"";
1271 std::string extsep =
".";
1304 int sepIndex = (int)pathname.rfind(sep);
1305 if (!altsep.empty()) {
1306 int altsepIndex = (int)pathname.rfind(altsep);
1307 sepIndex = ((std::max))(sepIndex, altsepIndex);
1310 size_t dotIndex = pathname.rfind(extsep);
1311 if (dotIndex != std::string::npos) {
1313 if ((sepIndex != (
int)std::string::npos && (
int)dotIndex > sepIndex) || sepIndex == (
int)std::string::npos) {
1314 if (sepIndex == (
int)std::string::npos) {
1317 size_t filenameIndex = (size_t)(sepIndex + 1);
1319 while (filenameIndex < dotIndex) {
1320 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1321 return pathname.substr(dotIndex);
1338 if (pathname.size() > 0) {
1342 if (index != std::string::npos) {
1343 return convertedPathname.substr(index + 1);
1346 return convertedPathname;
1361 size_t found = name.find_last_of(
".");
1362 std::string name_we = name.substr(0, found);
1373 if (pathname.size() > 0) {
1377 if (index != std::string::npos) {
1378 return convertedPathname.substr(0, index);
1396 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1397 std::string real_path_str = pathname;
1398 char *real_path = realpath(pathname.c_str(), NULL);
1400 if (real_path != NULL) {
1401 real_path_str = real_path;
1404 return real_path_str;
1405 #elif defined(_WIN32) 1406 #if (!defined(WINRT)) 1407 std::string real_path_str = pathname;
1409 TCHAR buffer[4096] = TEXT(
"");
1411 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1413 real_path_str = buffer;
1415 return real_path_str;
1418 "Cannot get absolute path of %s: not implemented on " 1419 "Universal Windows Platform",
1437 if (child.size() == 0 && parent.size() == 0) {
1441 if (child.size() == 0) {
1445 if (parent.size() == 0) {
1452 std::stringstream ss;
1454 std::string stringSeparator;
1455 ss >> stringSeparator;
1457 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1458 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1460 if (lastConvertedParentChar == stringSeparator) {
1461 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1464 if (firstConvertedChildChar == stringSeparator) {
1465 convertedChild = convertedChild.substr(1);
1468 return std::string(convertedParent + vpIoTools::separator + convertedChild);
1490 return path.size() > 0 && (path.substr(0, 1) ==
"/" || path.substr(0, 1) ==
"\\");
1510 return (path1_normalize == path2_normalize);
1573 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 1574 return std::pair<std::string, std::string>(
"", pathname);
1576 const std::string sep =
"\\";
1577 const std::string sepsep =
"\\\\";
1578 const std::string altsep =
"/";
1580 if (pathname.size() > 1) {
1581 std::string normPathname = pathname;
1582 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1584 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1589 size_t index = normPathname.find(sep, 2);
1590 if (index == std::string::npos) {
1591 return std::pair<std::string, std::string>(
"", pathname);
1594 size_t index2 = normPathname.find(sep, index + 1);
1597 if (index2 == index + 1) {
1598 return std::pair<std::string, std::string>(
"", pathname);
1601 if (index2 == std::string::npos) {
1602 index2 = pathname.size();
1605 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1608 if (normPathname[1] ==
':') {
1609 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1613 return std::pair<std::string, std::string>(
"", pathname);
1667 size_t startIndex = 0;
1669 std::string chainToSplit = chain;
1670 std::vector<std::string> subChain;
1671 size_t sepIndex = chainToSplit.find(sep);
1673 while (sepIndex != std::string::npos) {
1674 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1676 subChain.push_back(sub);
1677 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1679 sepIndex = chainToSplit.find(sep);
1681 if (!chainToSplit.empty())
1682 subChain.push_back(chainToSplit);
1700 std::string dirName =
path(pathname);
1702 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1704 std::vector<std::string> files;
1705 struct dirent **list = NULL;
1706 int filesCount = scandir(dirName.c_str(), &list, NULL, NULL);
1707 if (filesCount == -1) {
1710 for (
int i = 0; i < filesCount; i++) {
1711 std::string fileName = list[i]->d_name;
1712 if (fileName !=
"." && fileName !=
"..") {
1713 files.push_back(fileName);
1718 std::sort(files.begin(), files.end());
1721 #elif defined(_WIN32) 1722 #if (!defined(WINRT)) 1724 std::vector<std::string> files;
1725 std::string fileMask = dirName;
1726 fileMask.append(
"\\*");
1727 WIN32_FIND_DATA FindFileData;
1728 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1730 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1733 if (hFind == INVALID_HANDLE_VALUE) {
1737 std::string fileName = FindFileData.cFileName;
1738 if (fileName !=
"." && fileName !=
"..") {
1739 files.push_back(fileName);
1741 }
while (FindNextFile(hFind, &FindFileData));
1743 std::sort(files.begin(), files.end());
1748 "Cannot read files of directory %s: not implemented on " 1749 "Universal Windows Platform",
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Class to define colors available for display functionnalities.
unsigned int getRows() const
Error that can be emited by the vpIoTools class and its derivates.
unsigned int getCols() const
static Type minimum(const Type &a, const Type &b)
static vpColor getColor(const unsigned int &i)