63 #include <sys/types.h>
64 #include <visp3/core/vpEndian.h>
65 #include <visp3/core/vpIoException.h>
66 #include <visp3/core/vpIoTools.h>
67 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
82 #if defined(__APPLE__) && defined(__MACH__)
83 #include <TargetConditionals.h>
88 #define PATH_MAX _MAX_PATH
94 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
96 #elif defined(_WIN32) && defined(__MINGW32__)
107 #if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
108 #if (TARGET_OS_IOS == 0) && !defined(__ANDROID__)
109 void replaceAll(std::string &str,
const std::string &search,
const std::string &replace)
111 size_t start_pos = 0;
112 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
113 str.replace(start_pos, search.length(), replace);
114 start_pos += replace.length();
128 static std::string build_info =
129 #include "version_string.inc"
191 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
192 std::string username;
194 return "/tmp/" + username;
195 #elif defined(_WIN32) && !defined(WINRT)
198 TCHAR lpTempPathBuffer[MAX_PATH];
199 DWORD dwRetVal = GetTempPath(MAX_PATH , lpTempPathBuffer );
200 if (dwRetVal > MAX_PATH || (dwRetVal == 0)) {
203 std::string temp_path(lpTempPathBuffer);
204 if (!temp_path.empty()) {
205 if (temp_path.back() ==
'\\') {
206 temp_path.resize(temp_path.size() - 1);
210 temp_path =
"C:\temp";
240 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
242 char *logname =
::getenv(
"LOGNAME");
244 username =
"unknown";
249 #elif defined(_WIN32)
250 #if (!defined(WINRT))
251 unsigned int info_buffer_size = 1024;
252 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
253 DWORD bufCharCount = (DWORD)info_buffer_size;
255 if (!GetUserName(infoBuf, &bufCharCount)) {
256 username =
"unknown";
264 username =
"unknown";
267 username =
"unknown";
287 std::string username;
328 #if defined(_WIN32) && defined(WINRT)
330 "implemented on Universal Windows Platform"));
334 char *v_value =
::getenv(env.c_str());
353 void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
355 if (version.size() == 0) {
361 size_t major_pos = version.find(
'.');
362 std::string major_str = version.substr(0, major_pos);
363 major =
static_cast<unsigned>(atoi(major_str.c_str()));
365 if (major_pos != std::string::npos) {
366 size_t minor_pos = version.find(
'.', major_pos + 1);
367 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
368 minor =
static_cast<unsigned>(atoi(minor_str.c_str()));
370 if (minor_pos != std::string::npos) {
371 std::string patch_str = version.substr(minor_pos + 1);
372 patch =
static_cast<unsigned>(atoi(patch_str.c_str()));
398 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
400 #elif defined(_WIN32) && defined(__MINGW32__)
402 #elif defined(_WIN32)
406 if (dirname.empty()) {
410 std::string path_dirname =
path(dirname);
412 if (VP_STAT(path_dirname.c_str(), &stbuf) != 0) {
414 if (path_dirname.at(path_dirname.size() - 1) !=
separator) {
415 if (VP_STAT((path_dirname +
separator).c_str(), &stbuf) != 0) {
420 if (path_dirname.at(path_dirname.size() - 1) ==
separator) {
421 if (VP_STAT((path_dirname.substr(0, path_dirname.size() - 1)).c_str(), &stbuf) != 0) {
427 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
428 if ((stbuf.st_mode & S_IFDIR) == 0)
433 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
434 if ((stbuf.st_mode & S_IWUSR) == 0)
435 #elif defined(_WIN32)
436 if ((stbuf.st_mode & S_IWRITE) == 0)
458 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
461 std::string v_filename =
path(fifofilename);
462 if (stat(v_filename.c_str(), &stbuf) != 0) {
465 if ((stbuf.st_mode & S_IFIFO) == 0) {
468 if ((stbuf.st_mode & S_IRUSR) == 0)
474 #elif defined(_WIN32)
480 #ifndef DOXYGEN_SHOULD_SKIP_THIS
481 int vpIoTools::mkdir_p(
const std::string &path,
int mode)
484 if (
path.size() > PATH_MAX) {
485 errno = ENAMETOOLONG;
490 std::string cpy_path =
path;
491 std::string sub_path;
493 while (pos != std::string::npos) {
494 sub_path += cpy_path.substr(0, pos + 1);
496 bool stop_for_loop =
false;
498 cpy_path.erase(0, pos + 1);
499 stop_for_loop =
true;
501 if (!stop_for_loop) {
502 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
503 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
504 #elif defined(_WIN32)
509 if (errno != EEXIST) {
513 cpy_path.erase(0, pos + 1);
518 if (!cpy_path.empty()) {
519 sub_path += cpy_path;
520 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
521 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
522 #elif defined(_WIN32)
524 if (_mkdir(sub_path.c_str()) != 0)
527 if (errno != EEXIST) {
552 #if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32)
553 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
557 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
559 #elif defined(_WIN32) && defined(__MINGW32__)
561 #elif defined(_WIN32)
565 if (dirname.empty()) {
569 std::string v_dirname =
path(dirname);
571 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
572 if (stat(v_dirname.c_str(), &stbuf) != 0)
573 #elif defined(_WIN32) && defined(__MINGW32__)
574 if (stat(v_dirname.c_str(), &stbuf) != 0)
575 #elif defined(_WIN32)
576 if (_stat(v_dirname.c_str(), &stbuf) != 0)
579 if (vpIoTools::mkdir_p(v_dirname, 0755) != 0) {
603 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
608 "Unable to create fifo file. '%s' is an existing directory.", fifoname.c_str()));
622 else if (mkfifo(fifoname.c_str(), 0666) < 0) {
625 #elif defined(_WIN32)
631 #if defined(_WIN32) && !defined(WINRT)
632 std::string getUuid()
635 if (UuidCreate(&uuid) != RPC_S_OK) {
640 if (UuidToString(&uuid, &stringUuid) != RPC_S_OK) {
644 return reinterpret_cast<char *
>(stringUuid);
710 #if defined(WINRT) || !defined(_WIN32) && !(defined(__unix__) || defined(__unix) || \
711 (defined(__APPLE__) && defined(__MACH__)))
715 std::string dirname_cpy = std::string(dirname);
716 std::string correctEnding =
"XXXXXX";
720 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
722 if (dirname_cpy.rfind(correctEnding) == std::string::npos) {
723 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
724 dirname_cpy = dirname_cpy +
"/";
733 dirname_cpy = dirname_cpy +
"XXXXXX";
736 #elif defined(_WIN32) && !defined(WINRT)
738 dirname_cpy = dirname_cpy.substr(0, dirname_cpy.rfind(correctEnding));
740 dirname_cpy = dirname_cpy + getUuid();
746 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
747 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
748 dirname_cpy = dirname_cpy +
"/";
750 dirname_cpy = dirname_cpy +
"XXXXXX";
751 #elif defined(_WIN32) && !defined(WINRT)
756 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
757 char *dirname_char =
new char[dirname_cpy.length() + 1];
758 strcpy(dirname_char, dirname_cpy.c_str());
760 char *computedDirname = mkdtemp(dirname_char);
762 if (!computedDirname) {
763 delete[] dirname_char;
767 std::string res(computedDirname);
768 delete[] dirname_char;
770 #elif defined(_WIN32) && !defined(WINRT)
788 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
790 #elif defined(_WIN32)
794 if (filename.empty()) {
798 std::string v_filename =
path(filename);
799 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
800 if (stat(v_filename.c_str(), &stbuf) != 0)
801 #elif defined(_WIN32)
802 if (_stat(v_filename.c_str(), &stbuf) != 0)
807 if ((stbuf.st_mode & S_IFREG) == 0) {
810 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
811 if ((stbuf.st_mode & S_IRUSR) == 0)
812 #elif defined(_WIN32)
813 if ((stbuf.st_mode & S_IREAD) == 0)
833 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
834 #if TARGET_OS_IOS == 0
836 std::stringstream cmd;
841 int ret = system(cmd.str().c_str());
850 #elif defined(_WIN32)
851 #if (!defined(WINRT))
852 std::stringstream cmd;
857 int ret = system(cmd.str().c_str());
864 src.c_str(), dst.c_str()));
870 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
871 #if TARGET_OS_IOS == 0
873 std::stringstream cmd;
878 int ret = system(cmd.str().c_str());
887 #elif defined(_WIN32)
888 #if (!defined(WINRT))
889 std::stringstream cmd;
894 int ret = system(cmd.str().c_str());
901 src.c_str(), dst.c_str()));
906 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
925 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
929 if (::
remove(file_or_dir.c_str()) != 0) {
937 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
938 #if TARGET_OS_IOS == 0
940 std::stringstream cmd;
944 int ret = system(cmd.str().c_str());
951 file_or_dir.c_str()));
953 #elif defined(_WIN32)
954 #if (!defined(WINRT))
955 std::stringstream cmd;
956 cmd <<
"rmdir /S /Q ";
959 int ret = system(cmd.str().c_str());
966 file_or_dir.c_str()));
971 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
987 if (::
rename(oldfilename.c_str(), newfilename.c_str()) != 0) {
1007 std::string
path(pathname);
1010 for (
unsigned int i = 0; i <
path.length(); ++i)
1013 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
1014 unsigned int path_length =
path.length();
1015 for (
unsigned int i = 0; i < path_length; ++i) {
1016 if (
path[i] ==
'\\') {
1020 #if TARGET_OS_IOS == 0
1025 wordexp_t exp_result;
1028 replaceAll(
path,
"'",
"'\\''");
1030 wordexp(std::string(
"'" +
path +
"'").c_str(), &exp_result, 0);
1031 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
1032 wordfree(&exp_result);
1055 std::string data_path;
1056 std::string file_to_test(
"mbt/cube.cao");
1057 std::string filename;
1061 filename = data_path +
"/" + file_to_test;
1070 filename = data_path +
"/" + file_to_test;
1079 filename = data_path +
"/" + file_to_test;
1087 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1089 data_path =
"/usr/share/visp-images-data/ViSP-images";
1090 filename = data_path +
"/" + file_to_test;
1094 data_path =
"/usr/share/visp-images-data/visp-images";
1095 filename = data_path +
"/" + file_to_test;
1143 std::string sep =
"\\";
1144 std::string altsep =
"/";
1145 std::string extsep =
".";
1148 std::string sep =
"/";
1149 std::string altsep =
"";
1150 std::string extsep =
".";
1183 int sepIndex =
static_cast<int>(pathname.rfind(sep));
1184 if (!altsep.empty()) {
1185 int altsepIndex =
static_cast<int>(pathname.rfind(altsep));
1186 sepIndex = std::max<int>(sepIndex, altsepIndex);
1189 size_t dotIndex = pathname.rfind(extsep);
1190 if (dotIndex != std::string::npos) {
1192 size_t npos = std::string::npos;
1193 if (((sepIndex !=
static_cast<int>(npos)) && (
static_cast<int>(dotIndex) > sepIndex)) ||
1194 (sepIndex ==
static_cast<int>(npos))) {
1195 if (sepIndex ==
static_cast<int>(npos)) {
1198 size_t filenameIndex =
static_cast<size_t>(sepIndex) +
static_cast<size_t>(1);
1200 while (filenameIndex < dotIndex) {
1201 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1202 return pathname.substr(dotIndex);
1219 if (pathname.size() > 0) {
1223 if (index != std::string::npos) {
1224 return convertedPathname.substr(index + 1);
1227 return convertedPathname;
1242 size_t found = name.find_last_of(
".");
1243 std::string name_we = name.substr(0, found);
1286 size_t indexBegin = format.find_last_of(
'%');
1287 size_t indexEnd = format.find_first_of(
'd', indexBegin);
1288 size_t suffixLength = format.length() - indexEnd - 1;
1290 if (filename.length() <= (suffixLength + indexBegin)) {
1293 size_t indexLength = filename.length() - suffixLength - indexBegin;
1294 std::string indexSubstr = filename.substr(indexBegin, indexLength);
1295 std::istringstream ss(indexSubstr);
1298 if (ss.fail() || (index < 0) || (!ss.eof())) {
1303 char nameByFormat[FILENAME_MAX];
1304 snprintf(nameByFormat, FILENAME_MAX, format.c_str(), index);
1305 if (std::string(nameByFormat) != filename) {
1328 if (pathname.size() > 0) {
1332 if (index != std::string::npos) {
1333 return convertedPathname.substr(0, index);
1354 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1355 const std::string::const_iterator it_end = input.cend();
1356 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1358 const std::string::const_iterator it_end = input.end();
1359 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1361 out += std::tolower(*it);
1377 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1378 const std::string::const_iterator it_end = input.cend();
1379 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1381 const std::string::const_iterator it_end = input.end();
1382 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1384 out += std::toupper(*it);
1400 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1401 std::string real_path_str = pathname;
1402 char *real_path = realpath(pathname.c_str(),
nullptr);
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);
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);
1675 size_t startIndex = 0;
1677 std::string chainToSplit = chain;
1678 std::vector<std::string> subChain;
1679 size_t sepIndex = chainToSplit.find(sep);
1681 while (sepIndex != std::string::npos) {
1682 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1684 subChain.push_back(sub);
1686 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1688 sepIndex = chainToSplit.find(sep);
1690 if (!chainToSplit.empty()) {
1691 subChain.push_back(chainToSplit);
1710 std::string dirName =
path(pathname);
1712 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1714 std::vector<std::string> files;
1715 struct dirent **list =
nullptr;
1716 int filesCount = scandir(dirName.c_str(), &list,
nullptr,
nullptr);
1717 if (filesCount == -1) {
1720 for (
int i = 0; i < filesCount; ++i) {
1721 std::string fileName = list[i]->d_name;
1722 if ((fileName !=
".") && (fileName !=
"..")) {
1723 files.push_back(fileName);
1728 std::sort(files.begin(), files.end());
1731 #elif defined(_WIN32)
1732 #if (!defined(WINRT))
1734 std::vector<std::string> files;
1735 std::string fileMask = dirName;
1736 fileMask.append(
"\\*");
1737 WIN32_FIND_DATA FindFileData;
1738 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1740 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1743 if (hFind == INVALID_HANDLE_VALUE) {
1747 std::string fileName = FindFileData.cFileName;
1748 if (fileName !=
"." && fileName !=
"..") {
1749 files.push_back(fileName);
1751 }
while (FindNextFile(hFind, &FindFileData));
1753 std::sort(files.begin(), files.end());
1758 "Cannot read files of directory %s: not implemented on "
1759 "Universal Windows Platform",
error that can be emitted by ViSP classes.
@ notImplementedError
Not implemented.
Error that can be emitted by the vpIoTools class and its derivatives.
@ invalidDirectoryName
Directory name is invalid.
@ cantCreateDirectory
Unable to create a directory.
@ cantGetenv
Cannot get environment variable value.