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;
1060 filename = data_path +
"/" + file_to_test;
1065 filename = data_path +
"/" + file_to_test;
1070 filename = data_path +
"/" + file_to_test;
1075 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1077 data_path =
"/usr/share/visp-images-data/ViSP-images";
1078 filename = data_path +
"/" + file_to_test;
1082 data_path =
"/usr/share/visp-images-data/visp-images";
1083 filename = data_path +
"/" + file_to_test;
1131 std::string sep =
"\\";
1132 std::string altsep =
"/";
1133 std::string extsep =
".";
1136 std::string sep =
"/";
1137 std::string altsep =
"";
1138 std::string extsep =
".";
1171 int sepIndex =
static_cast<int>(pathname.rfind(sep));
1172 if (!altsep.empty()) {
1173 int altsepIndex =
static_cast<int>(pathname.rfind(altsep));
1174 sepIndex = std::max<int>(sepIndex, altsepIndex);
1177 size_t dotIndex = pathname.rfind(extsep);
1178 if (dotIndex != std::string::npos) {
1180 size_t npos = std::string::npos;
1181 if (((sepIndex !=
static_cast<int>(npos)) && (
static_cast<int>(dotIndex) > sepIndex)) ||
1182 (sepIndex ==
static_cast<int>(npos))) {
1183 if (sepIndex ==
static_cast<int>(npos)) {
1186 size_t filenameIndex =
static_cast<size_t>(sepIndex) +
static_cast<size_t>(1);
1188 while (filenameIndex < dotIndex) {
1189 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1190 return pathname.substr(dotIndex);
1207 if (pathname.size() > 0) {
1211 if (index != std::string::npos) {
1212 return convertedPathname.substr(index + 1);
1215 return convertedPathname;
1230 size_t found = name.find_last_of(
".");
1231 std::string name_we = name.substr(0, found);
1274 size_t indexBegin = format.find_last_of(
'%');
1275 size_t indexEnd = format.find_first_of(
'd', indexBegin);
1276 size_t suffixLength = format.length() - indexEnd - 1;
1278 if (filename.length() <= (suffixLength + indexBegin)) {
1281 size_t indexLength = filename.length() - suffixLength - indexBegin;
1282 std::string indexSubstr = filename.substr(indexBegin, indexLength);
1283 std::istringstream ss(indexSubstr);
1286 if (ss.fail() || (index < 0) || (!ss.eof())) {
1291 char nameByFormat[FILENAME_MAX];
1292 snprintf(nameByFormat, FILENAME_MAX, format.c_str(), index);
1293 if (std::string(nameByFormat) != filename) {
1316 if (pathname.size() > 0) {
1320 if (index != std::string::npos) {
1321 return convertedPathname.substr(0, index);
1342 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1343 const std::string::const_iterator it_end = input.cend();
1344 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1346 const std::string::const_iterator it_end = input.end();
1347 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1349 out += std::tolower(*it);
1365 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1366 const std::string::const_iterator it_end = input.cend();
1367 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1369 const std::string::const_iterator it_end = input.end();
1370 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1372 out += std::toupper(*it);
1388 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1389 std::string real_path_str = pathname;
1390 char *real_path = realpath(pathname.c_str(),
nullptr);
1393 real_path_str = real_path;
1396 return real_path_str;
1397 #elif defined(_WIN32)
1398 #if (!defined(WINRT))
1399 std::string real_path_str = pathname;
1401 TCHAR buffer[4096] = TEXT(
"");
1403 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1405 real_path_str = buffer;
1407 return real_path_str;
1410 "Cannot get absolute path of %s: not implemented on "
1411 "Universal Windows Platform",
1429 if ((child.size() == 0) && (parent.size() == 0)) {
1433 if (child.size() == 0) {
1437 if (parent.size() == 0) {
1444 std::stringstream ss;
1446 std::string stringSeparator;
1447 ss >> stringSeparator;
1449 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1450 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1452 if (lastConvertedParentChar == stringSeparator) {
1453 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1456 if (firstConvertedChildChar == stringSeparator) {
1457 convertedChild = convertedChild.substr(1);
1482 return (
path.size() > 0) && ((
path.substr(0, 1) ==
"/") || (
path.substr(0, 1) ==
"\\"));
1502 return (path1_normalize == path2_normalize);
1565 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1566 return std::pair<std::string, std::string>(
"", pathname);
1568 const std::string sep =
"\\";
1569 const std::string sepsep =
"\\\\";
1570 const std::string altsep =
"/";
1572 if (pathname.size() > 1) {
1573 std::string normPathname = pathname;
1574 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1576 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1581 size_t index = normPathname.find(sep, 2);
1582 if (index == std::string::npos) {
1583 return std::pair<std::string, std::string>(
"", pathname);
1586 size_t index2 = normPathname.find(sep, index + 1);
1589 if (index2 == index + 1) {
1590 return std::pair<std::string, std::string>(
"", pathname);
1593 if (index2 == std::string::npos) {
1594 index2 = pathname.size();
1597 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1600 if (normPathname[1] ==
':') {
1601 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1605 return std::pair<std::string, std::string>(
"", pathname);
1663 size_t startIndex = 0;
1665 std::string chainToSplit = chain;
1666 std::vector<std::string> subChain;
1667 size_t sepIndex = chainToSplit.find(sep);
1669 while (sepIndex != std::string::npos) {
1670 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1672 subChain.push_back(sub);
1674 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1676 sepIndex = chainToSplit.find(sep);
1678 if (!chainToSplit.empty()) {
1679 subChain.push_back(chainToSplit);
1698 std::string dirName =
path(pathname);
1700 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1702 std::vector<std::string> files;
1703 struct dirent **list =
nullptr;
1704 int filesCount = scandir(dirName.c_str(), &list,
nullptr,
nullptr);
1705 if (filesCount == -1) {
1708 for (
int i = 0; i < filesCount; ++i) {
1709 std::string fileName = list[i]->d_name;
1710 if ((fileName !=
".") && (fileName !=
"..")) {
1711 files.push_back(fileName);
1716 std::sort(files.begin(), files.end());
1719 #elif defined(_WIN32)
1720 #if (!defined(WINRT))
1722 std::vector<std::string> files;
1723 std::string fileMask = dirName;
1724 fileMask.append(
"\\*");
1725 WIN32_FIND_DATA FindFileData;
1726 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1728 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1731 if (hFind == INVALID_HANDLE_VALUE) {
1735 std::string fileName = FindFileData.cFileName;
1736 if (fileName !=
"." && fileName !=
"..") {
1737 files.push_back(fileName);
1739 }
while (FindNextFile(hFind, &FindFileData));
1741 std::sort(files.begin(), files.end());
1746 "Cannot read files of directory %s: not implemented on "
1747 "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.