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> 74 # define PATH_MAX _MAX_PATH 76 # define PATH_MAX 1024 86 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 90 void replaceAll(std::string &str,
const std::string &search,
const std::string &replace)
93 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
94 str.replace(start_pos, search.length(), replace);
95 start_pos += replace.length();
107 static std::string build_info =
108 #include "version_string.inc" 158 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 160 char *_username = NULL;
162 if (_username == NULL) {
163 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable");
166 username = _username;
167 #elif defined(_WIN32) 168 #if (!defined(WINRT)) 169 unsigned int info_buffer_size = 1024;
170 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
171 DWORD bufCharCount = (DWORD)info_buffer_size;
173 if (!GetUserName(infoBuf, &bufCharCount)) {
181 "Windows Platform"));
204 std::string username;
205 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 207 char *_username = NULL;
209 if (_username == NULL) {
210 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable");
213 username = _username;
214 #elif defined(_WIN32) 215 #if (!defined(WINRT)) 216 unsigned int info_buffer_size = 1024;
217 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
218 DWORD bufCharCount = (DWORD)info_buffer_size;
220 if (!GetUserName(infoBuf, &bufCharCount)) {
229 "Windows Platform"));
268 #if defined(_WIN32) && defined(WINRT) 270 "implemented on Universal Windows Platform"));
276 if (_value == NULL) {
328 void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
330 if (version.size() == 0) {
335 size_t major_pos = version.find(
'.');
336 std::string major_str = version.substr(0, major_pos);
337 major = (unsigned)atoi(major_str.c_str());
339 if (major_pos != std::string::npos) {
340 size_t minor_pos = version.find(
'.', major_pos + 1);
341 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
342 minor = (unsigned)atoi(minor_str.c_str());
344 if (minor_pos != std::string::npos) {
345 std::string patch_str = version.substr(minor_pos + 1);
346 patch = (unsigned)atoi(patch_str.c_str());
373 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 375 #elif defined(_WIN32) && defined(__MINGW32__) 377 #elif defined(_WIN32) 381 if (dirname == NULL || dirname[0] ==
'\0') {
385 std::string _dirname =
path(dirname);
387 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 388 if (stat(_dirname.c_str(), &stbuf) != 0)
389 #elif defined(_WIN32) && defined(__MINGW32__) 394 _dirname = _dirname.substr(0, _dirname.size() - 1);
395 if (stat(_dirname.c_str(), &stbuf) != 0)
396 #elif defined(_WIN32) 397 if (_stat(_dirname.c_str(), &stbuf) != 0)
402 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 403 if ((stbuf.st_mode & S_IFDIR) == 0)
408 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 409 if ((stbuf.st_mode & S_IWUSR) == 0)
410 #elif defined(_WIN32)
411 if ((stbuf.st_mode & S_IWRITE) == 0)
439 const size_t len = strlen(path);
440 char _path[PATH_MAX];
445 if (len >
sizeof(_path) - 1) {
446 errno = ENAMETOOLONG;
453 for (p = _path + 1; *p; p++) {
458 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 459 if (mkdir(_path, (mode_t)mode) != 0)
460 #elif defined(_WIN32) 472 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 473 if (mkdir(_path, (mode_t)mode) != 0)
474 #elif defined(_WIN32)
475 if (_mkdir(_path) != 0)
501 #if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32) 502 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
506 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 508 #elif defined(_WIN32) && defined(__MINGW32__) 510 #elif defined(_WIN32) 514 if (dirname == NULL || dirname[0] ==
'\0') {
519 std::string _dirname =
path(dirname);
521 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 522 if (stat(_dirname.c_str(), &stbuf) != 0)
523 #elif defined(_WIN32) && defined(__MINGW32__) 524 if (stat(_dirname.c_str(), &stbuf) != 0)
525 #elif defined(_WIN32) 526 if (_stat(_dirname.c_str(), &stbuf) != 0)
560 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname.c_str());
579 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 581 #elif defined(_WIN32) 585 if (filename == NULL || filename[0] ==
'\0') {
589 std::string _filename =
path(filename);
590 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 591 if (stat(_filename.c_str(), &stbuf) != 0)
592 #elif defined(_WIN32) 593 if (_stat(_filename.c_str(), &stbuf) != 0)
598 if ((stbuf.st_mode & S_IFREG) == 0) {
601 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 602 if ((stbuf.st_mode & S_IRUSR) == 0)
603 #elif defined(_WIN32)
604 if ((stbuf.st_mode & S_IREAD) == 0)
643 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 644 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 646 char cmd[FILENAME_MAX];
648 sprintf(cmd,
"cp -p %s %s", src, dst);
657 #elif defined(_WIN32) 658 #if (!defined(WINRT)) 659 char cmd[FILENAME_MAX];
663 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
676 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 677 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 679 char cmd[FILENAME_MAX];
681 sprintf(cmd,
"cp -p -r %s %s", src, dst);
690 #elif defined(_WIN32) 691 #if (!defined(WINRT)) 692 char cmd[FILENAME_MAX];
696 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
708 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
744 if (::
remove(file_or_dir) != 0)
750 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 751 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 753 char cmd[FILENAME_MAX];
754 sprintf(cmd,
"rm -rf %s", file_or_dir);
755 int ret = system(cmd);
763 #elif defined(_WIN32) 764 #if (!defined(WINRT)) 765 char cmd[FILENAME_MAX];
767 sprintf(cmd,
"rmdir /S /Q %s", file_or_dir_.c_str());
768 int ret = system(cmd);
779 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
809 if (::
rename(oldfilename, newfilename) != 0)
845 std::string
path(pathname);
848 for (
unsigned int i = 0; i < path.length(); i++)
851 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) 852 for (
unsigned int i = 0; i < path.length(); i++)
855 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since 857 wordexp_t exp_result;
860 replaceAll(path,
"'",
"'\\''");
862 wordexp(std::string(
"'" + path +
"'").c_str(), &exp_result, 0);
863 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
864 wordfree(&exp_result);
898 std::ifstream confContent(
configFile.c_str(), std::ios::in);
900 if (confContent.is_open()) {
901 std::string line, var, val;
904 std::string stop[3] = {
" ",
"\t",
"#"};
905 while (std::getline(confContent, line)) {
906 if ((line.compare(0, 1,
"#") != 0) && (line.size() > 2)) {
909 k = (
unsigned long)line.find(
" ");
910 var = line.substr(0, k);
913 for (
unsigned i = 0; i < 3; ++i)
916 c = (int)line.size();
917 long unsigned int c_ = (
long unsigned int)c;
918 val = line.substr(k + 1, c_ - k - 1);
943 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
948 value = (
float)(M_PI / 2.0);
950 value = (
float)(-M_PI / 2.0);
957 std::cout << var <<
" not found in config file" << std::endl;
971 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
985 std::cout << var <<
" not found in config file" << std::endl;
1000 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1007 std::cout << var <<
" not found in config file" << std::endl;
1023 value = (
unsigned int)v;
1070 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1077 std::cout << var <<
" not found in config file" << std::endl;
1095 const unsigned int &nRows)
1099 for (
unsigned int k = 0; k <
configVars.size() && found ==
false; ++k) {
1103 if (nCols != 0 && nRows != 0)
1104 value.
resize(nRows, nCols);
1105 size_t ind = 0, ind2;
1106 for (
unsigned int i = 0; i < value.
getRows(); ++i)
1107 for (
unsigned int j = 0; j < value.
getCols(); ++j) {
1110 if (nb.compare(
"PI") == 0)
1112 else if (nb.compare(
"PI/2") == 0)
1113 value[i][j] = M_PI / 2;
1114 else if (nb.compare(
"-PI/2") == 0)
1115 value[i][j] = -M_PI / 2;
1117 value[i][j] = atof(nb.c_str());
1123 std::cout << var <<
" not found in config file" << std::endl;
1141 else if (strFalse !=
"")
1156 if (std::fabs(val) < std::numeric_limits<double>::epsilon()) {
1158 sprintf(valC,
"%.3f", val);
1159 std::string valS(valC);
1216 std::string data_path;
1217 std::string file_to_test(
"mbt/cube.cao");
1218 std::string filename;
1219 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1221 data_path =
"/usr/share/visp-images-data/ViSP-images";
1222 filename = data_path +
"/" + file_to_test;
1225 data_path =
"/usr/share/visp-images-data/visp-images";
1226 filename = data_path +
"/" + file_to_test;
1233 filename = data_path +
"/" + file_to_test;
1237 filename = data_path +
"/" + file_to_test;
1241 filename = data_path +
"/" + file_to_test;
1268 std::string sep =
"\\";
1269 std::string altsep =
"/";
1270 std::string extsep =
".";
1273 std::string sep =
"/";
1274 std::string altsep =
"";
1275 std::string extsep =
".";
1308 int sepIndex = (int)pathname.rfind(sep);
1309 if (!altsep.empty()) {
1310 int altsepIndex = (int)pathname.rfind(altsep);
1311 sepIndex = ((std::max))(sepIndex, altsepIndex);
1314 size_t dotIndex = pathname.rfind(extsep);
1315 if (dotIndex != std::string::npos) {
1317 if ((sepIndex != (
int)std::string::npos && (
int)dotIndex > sepIndex) || sepIndex == (
int)std::string::npos) {
1318 if (sepIndex == (
int)std::string::npos) {
1321 size_t filenameIndex = (size_t)(sepIndex + 1);
1323 while (filenameIndex < dotIndex) {
1324 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1325 return pathname.substr(dotIndex);
1342 if (pathname.size() > 0) {
1346 if (index != std::string::npos) {
1347 return convertedPathname.substr(index + 1);
1350 return convertedPathname;
1365 size_t found = name.find_last_of(
".");
1366 std::string name_we = name.substr(0, found);
1377 if (pathname.size() > 0) {
1381 if (index != std::string::npos) {
1382 return convertedPathname.substr(0, index);
1400 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1401 std::string real_path_str = pathname;
1402 char *real_path = realpath(pathname.c_str(), NULL);
1404 if (real_path != NULL) {
1405 real_path_str = real_path;
1408 return real_path_str;
1409 #elif defined(_WIN32) 1410 #if (!defined(WINRT)) 1411 std::string real_path_str = pathname;
1413 TCHAR buffer[4096] = TEXT(
"");
1415 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1417 real_path_str = buffer;
1419 return real_path_str;
1422 "Cannot get absolute path of %s: not implemented on " 1423 "Universal Windows Platform",
1441 if (child.size() == 0 && parent.size() == 0) {
1445 if (child.size() == 0) {
1449 if (parent.size() == 0) {
1456 std::stringstream ss;
1458 std::string stringSeparator;
1459 ss >> stringSeparator;
1461 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1462 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1464 if (lastConvertedParentChar == stringSeparator) {
1465 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1468 if (firstConvertedChildChar == stringSeparator) {
1469 convertedChild = convertedChild.substr(1);
1472 return std::string(convertedParent + vpIoTools::separator + convertedChild);
1494 return path.size() > 0 && (path.substr(0, 1) ==
"/" || path.substr(0, 1) ==
"\\");
1514 return (path1_normalize == path2_normalize);
1577 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 1578 return std::pair<std::string, std::string>(
"", pathname);
1580 const std::string sep =
"\\";
1581 const std::string sepsep =
"\\\\";
1582 const std::string altsep =
"/";
1584 if (pathname.size() > 1) {
1585 std::string normPathname = pathname;
1586 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1588 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1593 size_t index = normPathname.find(sep, 2);
1594 if (index == std::string::npos) {
1595 return std::pair<std::string, std::string>(
"", pathname);
1598 size_t index2 = normPathname.find(sep, index + 1);
1601 if (index2 == index + 1) {
1602 return std::pair<std::string, std::string>(
"", pathname);
1605 if (index2 == std::string::npos) {
1606 index2 = pathname.size();
1609 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1612 if (normPathname[1] ==
':') {
1613 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1617 return std::pair<std::string, std::string>(
"", pathname);
1671 size_t startIndex = 0;
1673 std::string chainToSplit = chain;
1674 std::vector<std::string> subChain;
1675 size_t sepIndex = chainToSplit.find(sep);
1677 while (sepIndex != std::string::npos) {
1678 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1680 subChain.push_back(sub);
1681 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1683 sepIndex = chainToSplit.find(sep);
1685 if (!chainToSplit.empty())
1686 subChain.push_back(chainToSplit);
1704 std::string dirName =
path(pathname);
1706 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 1708 std::vector<std::string> files;
1709 struct dirent **list = NULL;
1710 int filesCount = scandir(dirName.c_str(), &list, NULL, NULL);
1711 if (filesCount == -1) {
1714 for (
int i = 0; i < filesCount; i++) {
1715 std::string fileName = list[i]->d_name;
1716 if (fileName !=
"." && fileName !=
"..") {
1717 files.push_back(fileName);
1722 std::sort(files.begin(), files.end());
1725 #elif defined(_WIN32) 1726 #if (!defined(WINRT)) 1728 std::vector<std::string> files;
1729 std::string fileMask = dirName;
1730 fileMask.append(
"\\*");
1731 WIN32_FIND_DATA FindFileData;
1732 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1734 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1737 if (hFind == INVALID_HANDLE_VALUE) {
1741 std::string fileName = FindFileData.cFileName;
1742 if (fileName !=
"." && fileName !=
"..") {
1743 files.push_back(fileName);
1745 }
while (FindNextFile(hFind, &FindFileData));
1747 std::sort(files.begin(), files.end());
1752 "Cannot read files of directory %s: not implemented on " 1753 "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)