64 #include <sys/types.h>
65 #include <visp3/core/vpDebug.h>
66 #include <visp3/core/vpEndian.h>
67 #include <visp3/core/vpIoException.h>
68 #include <visp3/core/vpIoTools.h>
69 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
84 #if defined(__APPLE__) && defined(__MACH__)
85 #include <TargetConditionals.h>
90 #define PATH_MAX _MAX_PATH
97 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
99 #elif defined(_WIN32) && defined(__MINGW32__)
101 #elif defined(_WIN32)
102 #define VP_STAT _stat
107 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
108 #define USE_ZLIB_API 0
113 #include "basisu_miniz.h"
121 #define UNUSED(x) ((void)(x))
132 return (((
reinterpret_cast<char *
>(&x))[0]) ?
'<' :
'>');
137 if (t ==
typeid(
float)) {
return 'f'; }
138 if (t ==
typeid(
double)) {
return 'f'; }
139 if (t ==
typeid(
long double)) {
return 'f'; }
141 if (t ==
typeid(
int)) {
return 'i'; }
142 if (t ==
typeid(
char)) {
return 'i'; }
143 if (t ==
typeid(
short)) {
return 'i'; }
144 if (t ==
typeid(
long)) {
return 'i'; }
145 if (t ==
typeid(
long long)) {
return 'i'; }
147 if (t ==
typeid(
unsigned char)) {
return 'u'; }
148 if (t ==
typeid(
unsigned short)) {
return 'u'; }
149 if (t ==
typeid(
unsigned long)) {
return 'u'; }
150 if (t ==
typeid(
unsigned long long)) {
return 'u'; }
151 if (t ==
typeid(
unsigned int)) {
return 'u'; }
153 if (t ==
typeid(
bool)) {
return 'b'; }
155 if (t ==
typeid(std::complex<float>)) {
return 'c'; }
156 if (t ==
typeid(std::complex<double>)) {
return 'c'; }
157 if (t ==
typeid(std::complex<long double>)) {
return 'c'; }
164 uint16_t header_len = *
reinterpret_cast<uint16_t *
>(buffer+8);
165 std::string header(
reinterpret_cast<char *
>(buffer+9), header_len);
168 size_t loc1 = header.find(
"fortran_order")+16;
169 fortran_order = (header.substr(loc1, 4) ==
"True" ? true :
false);
172 loc1 = header.find(
"(");
173 size_t loc2 = header.find(
")");
175 std::regex num_regex(
"[0-9][0-9]*");
179 std::string str_shape = header.substr(loc1+1, loc2-loc1-1);
180 while (std::regex_search(str_shape, sm, num_regex)) {
181 shape.push_back(std::stoi(sm[0].str()));
182 str_shape = sm.suffix().str();
188 loc1 = header.find(
"descr")+9;
189 bool littleEndian = (((header[loc1] ==
'<') || (header[loc1] ==
'|')) ?
true :
false);
190 UNUSED(littleEndian); assert(littleEndian);
192 std::string str_ws = header.substr(loc1+2);
193 loc2 = str_ws.find(
"'");
194 word_size = atoi(str_ws.substr(0, loc2).c_str());
200 size_t res = fread(buffer,
sizeof(
char), 11, fp);
202 throw std::runtime_error(
"parse_npy_header: failed fread");
204 std::string header = fgets(buffer, 256, fp);
205 assert(header[header.size()-1] ==
'\n');
210 loc1 = header.find(
"fortran_order");
211 if (loc1 == std::string::npos) {
212 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'fortran_order'");
215 fortran_order = (header.substr(loc1, 4) ==
"True" ? true :
false);
218 loc1 = header.find(
"(");
219 loc2 = header.find(
")");
220 if ((loc1 == std::string::npos) || (loc2 == std::string::npos)) {
221 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: '(' or ')'");
224 std::regex num_regex(
"[0-9][0-9]*");
228 std::string str_shape = header.substr(loc1+1, loc2-loc1-1);
229 while (std::regex_search(str_shape, sm, num_regex)) {
230 shape.push_back(std::stoi(sm[0].str()));
231 str_shape = sm.suffix().str();
237 loc1 = header.find(
"descr");
238 if (loc1 == std::string::npos) {
239 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'descr'");
242 bool littleEndian = ((header[loc1] ==
'<') || (header[loc1] ==
'|') ? true :
false);
243 UNUSED(littleEndian); assert(littleEndian);
248 std::string str_ws = header.substr(loc1+2);
249 loc2 = str_ws.find(
"'");
250 word_size = atoi(str_ws.substr(0, loc2).c_str());
255 std::vector<char> footer(22);
256 fseek(fp, -22, SEEK_END);
257 size_t res = fread(&footer[0],
sizeof(
char), 22, fp);
259 throw std::runtime_error(
"parse_zip_footer: failed fread");
262 uint16_t disk_no, disk_start, nrecs_on_disk, comment_len;
263 disk_no = *(uint16_t *)&footer[4];
264 disk_start = *(uint16_t *)&footer[6];
265 nrecs_on_disk = *(uint16_t *)&footer[8];
266 nrecs = *(uint16_t *)&footer[10];
267 global_header_size = *(uint32_t *)&footer[12];
268 global_header_offset = *(uint32_t *)&footer[16];
269 comment_len = *(uint16_t *)&footer[20];
271 UNUSED(disk_no); assert(disk_no == 0);
272 UNUSED(disk_start); assert(disk_start == 0);
273 UNUSED(nrecs_on_disk); assert(nrecs_on_disk == nrecs);
274 UNUSED(comment_len); assert(comment_len == 0);
279 std::vector<size_t> shape;
285 size_t nread = fread(arr.data<
char>(), 1, arr.num_bytes(), fp);
286 if (nread != arr.num_bytes()) {
287 throw std::runtime_error(
"load_the_npy_file: failed fread");
294 std::vector<unsigned char> buffer_compr(compr_bytes);
295 std::vector<unsigned char> buffer_uncompr(uncompr_bytes);
296 size_t nread = fread(&buffer_compr[0], 1, compr_bytes, fp);
297 if (nread != compr_bytes) {
298 throw std::runtime_error(
"load_the_npy_file: failed fread");
303 d_stream.zalloc = Z_NULL;
304 d_stream.zfree = Z_NULL;
305 d_stream.opaque = Z_NULL;
306 d_stream.avail_in = 0;
307 d_stream.next_in = Z_NULL;
308 int err = inflateInit2(&d_stream, -MAX_WBITS);
309 UNUSED(err); assert(err == 0);
311 d_stream.avail_in = compr_bytes;
312 d_stream.next_in = &buffer_compr[0];
313 d_stream.avail_out = uncompr_bytes;
314 d_stream.next_out = &buffer_uncompr[0];
316 err = inflate(&d_stream, Z_FINISH);
317 UNUSED(err); assert(err == 0);
318 err = inflateEnd(&d_stream);
319 UNUSED(err); assert(err == 0);
321 std::vector<size_t> shape;
328 size_t offset = uncompr_bytes - array.num_bytes();
329 memcpy(array.data<
unsigned char>(), &buffer_uncompr[0]+offset, array.num_bytes());
344 FILE *fp = fopen(fname.c_str(),
"rb");
347 throw std::runtime_error(
"npz_load: Error! Unable to open file "+fname+
"!");
353 std::vector<char> local_header(30);
354 size_t headerres = fread(&local_header[0],
sizeof(
char), 30, fp);
355 if (headerres != 30) {
356 throw std::runtime_error(
"npz_load: failed fread");
360 if ((local_header[2] != 0x03) || (local_header[3] != 0x04)) {
365 uint16_t name_len = *(uint16_t *)&local_header[26];
366 std::string varname(name_len,
' ');
367 size_t vname_res = fread(&varname[0],
sizeof(
char), name_len, fp);
368 if (vname_res != name_len) {
369 throw std::runtime_error(
"npz_load: failed fread");
373 varname.erase(varname.end()-4, varname.end());
376 uint16_t extra_field_len = *(uint16_t *)&local_header[28];
377 if (extra_field_len > 0) {
378 std::vector<char> buff(extra_field_len);
379 size_t efield_res = fread(&buff[0],
sizeof(
char), extra_field_len, fp);
380 if (efield_res != extra_field_len) {
381 throw std::runtime_error(
"npz_load: failed fread");
385 uint16_t compr_method = *
reinterpret_cast<uint16_t *
>(&local_header[0]+8);
386 uint32_t compr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0]+18);
387 uint32_t uncompr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0]+22);
389 if (compr_method == 0) { arrays[varname] = load_the_npy_file(fp); }
390 else { arrays[varname] = load_the_npz_array(fp, compr_bytes, uncompr_bytes); }
408 FILE *fp = fopen(fname.c_str(),
"rb");
411 throw std::runtime_error(
"npz_load: Unable to open file "+fname);
416 std::vector<char> local_header(30);
417 size_t header_res = fread(&local_header[0],
sizeof(
char), 30, fp);
418 if (header_res != 30) {
419 throw std::runtime_error(
"npz_load: failed fread");
423 if ((local_header[2] != 0x03) || (local_header[3] != 0x04)) {
428 uint16_t name_len = *(uint16_t *)&local_header[26];
429 std::string vname(name_len,
' ');
430 size_t vname_res = fread(&vname[0],
sizeof(
char), name_len, fp);
431 if (vname_res != name_len) {
432 throw std::runtime_error(
"npz_load: failed fread");
434 vname.erase(vname.end()-4, vname.end());
437 uint16_t extra_field_len = *(uint16_t *)&local_header[28];
438 fseek(fp, extra_field_len, SEEK_CUR);
440 uint16_t compr_method = *
reinterpret_cast<uint16_t *
>(&local_header[0]+8);
441 uint32_t compr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0]+18);
442 uint32_t uncompr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0]+22);
444 if (vname == varname) {
445 NpyArray array = (compr_method == 0) ? load_the_npy_file(fp) : load_the_npz_array(fp, compr_bytes, uncompr_bytes);
451 uint32_t size = *(uint32_t *)&local_header[22];
452 fseek(fp, size, SEEK_CUR);
459 throw std::runtime_error(
"npz_load: Variable name "+varname+
" not found in "+fname);
473 FILE *fp = fopen(fname.c_str(),
"rb");
476 throw std::runtime_error(
"npy_load: Unable to open file "+fname);
479 NpyArray arr = load_the_npy_file(fp);
504 #if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
505 #if (TARGET_OS_IOS == 0) && !defined(__ANDROID__)
506 void replaceAll(std::string &str,
const std::string &search,
const std::string &replace)
508 size_t start_pos = 0;
509 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
510 str.replace(start_pos, search.length(), replace);
511 start_pos += replace.length();
518 std::string <rim(std::string &s)
520 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
521 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int c) { return !std::isspace(c); }));
523 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
528 std::string &rtrim(std::string &s)
530 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
531 s.erase(std::find_if(s.rbegin(), s.rend(), [](
int c) { return !std::isspace(c); }).base(), s.end());
533 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
544 static std::string build_info =
545 #include "version_string.inc"
603 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
604 std::string username;
606 return "/tmp/" + username;
607 #elif defined(_WIN32) && !defined(WINRT)
610 TCHAR lpTempPathBuffer[MAX_PATH];
611 DWORD dwRetVal = GetTempPath(MAX_PATH , lpTempPathBuffer );
612 if (dwRetVal > MAX_PATH || (dwRetVal == 0)) {
615 std::string temp_path(lpTempPathBuffer);
616 if (!temp_path.empty()) {
617 if (temp_path.back() ==
'\\') {
618 temp_path.resize(temp_path.size() - 1);
622 temp_path =
"C:\temp";
680 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
682 char *logname = ::getenv(
"LOGNAME");
684 username =
"unknown";
689 #elif defined(_WIN32)
690 #if (!defined(WINRT))
691 unsigned int info_buffer_size = 1024;
692 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
693 DWORD bufCharCount = (DWORD)info_buffer_size;
695 if (!GetUserName(infoBuf, &bufCharCount)) {
696 username =
"unknown";
704 username =
"unknown";
707 username =
"unknown";
727 std::string username;
728 getUserName(username);
764 #if defined(_WIN32) && defined(WINRT)
766 "implemented on Universal Windows Platform"));
770 char *v_value = ::getenv(env.c_str());
789 void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
791 if (version.size() == 0) {
797 size_t major_pos = version.find(
'.');
798 std::string major_str = version.substr(0, major_pos);
799 major =
static_cast<unsigned>(atoi(major_str.c_str()));
801 if (major_pos != std::string::npos) {
802 size_t minor_pos = version.find(
'.', major_pos + 1);
803 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
804 minor =
static_cast<unsigned>(atoi(minor_str.c_str()));
806 if (minor_pos != std::string::npos) {
807 std::string patch_str = version.substr(minor_pos + 1);
808 patch =
static_cast<unsigned>(atoi(patch_str.c_str()));
834 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
836 #elif defined(_WIN32) && defined(__MINGW32__)
838 #elif defined(_WIN32)
842 if (dirname.empty()) {
846 std::string path_dirname = path(dirname);
848 if (VP_STAT(path_dirname.c_str(), &stbuf) != 0) {
850 if (path_dirname.at(path_dirname.size() - 1) != separator) {
851 if (VP_STAT((path_dirname + separator).c_str(), &stbuf) != 0) {
856 if (path_dirname.at(path_dirname.size() - 1) == separator) {
857 if (VP_STAT((path_dirname.substr(0, path_dirname.size() - 1)).c_str(), &stbuf) != 0) {
863 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
864 if ((stbuf.st_mode & S_IFDIR) == 0)
869 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
870 if ((stbuf.st_mode & S_IWUSR) == 0)
871 #elif defined(_WIN32)
872 if ((stbuf.st_mode & S_IWRITE) == 0)
894 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
897 std::string v_filename = path(fifofilename);
898 if (stat(v_filename.c_str(), &stbuf) != 0) {
901 if ((stbuf.st_mode & S_IFIFO) == 0) {
904 if ((stbuf.st_mode & S_IRUSR) == 0)
910 #elif defined(_WIN32)
916 #ifndef DOXYGEN_SHOULD_SKIP_THIS
917 int vpIoTools::mkdir_p(
const std::string &path,
int mode)
920 if (path.size() > PATH_MAX) {
921 errno = ENAMETOOLONG;
926 std::string cpy_path = path;
927 std::string sub_path;
929 sub_path += cpy_path.substr(0, pos + 1);
932 cpy_path.erase(0, pos + 1);
935 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
936 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
937 #elif defined(_WIN32)
939 if (!checkDirectory(sub_path) && _mkdir(sub_path.c_str()) != 0)
942 if (errno != EEXIST) {
946 cpy_path.erase(0, pos + 1);
949 if (!cpy_path.empty()) {
950 sub_path += cpy_path;
951 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
952 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
953 #elif defined(_WIN32)
955 if (_mkdir(sub_path.c_str()) != 0)
958 if (errno != EEXIST) {
983 #if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32)
984 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
988 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
990 #elif defined(_WIN32) && defined(__MINGW32__)
992 #elif defined(_WIN32)
996 if (dirname.empty()) {
1000 std::string v_dirname = path(dirname);
1002 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1003 if (stat(v_dirname.c_str(), &stbuf) != 0)
1004 #elif defined(_WIN32) && defined(__MINGW32__)
1005 if (stat(v_dirname.c_str(), &stbuf) != 0)
1006 #elif defined(_WIN32)
1007 if (_stat(v_dirname.c_str(), &stbuf) != 0)
1010 if (vpIoTools::mkdir_p(v_dirname, 0755) != 0) {
1015 if (checkDirectory(dirname) ==
false) {
1034 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1039 "Unable to create fifo file. '%s' is an existing directory.", fifoname.c_str()));
1053 else if (mkfifo(fifoname.c_str(), 0666) < 0) {
1056 #elif defined(_WIN32)
1062 #if defined(_WIN32) && !defined(WINRT)
1063 std::string getUuid()
1066 if (UuidCreate(&uuid) != RPC_S_OK) {
1070 RPC_CSTR stringUuid;
1071 if (UuidToString(&uuid, &stringUuid) != RPC_S_OK) {
1075 return reinterpret_cast<char *
>(stringUuid);
1137 #if defined(WINRT) || !defined(_WIN32) && !(defined(__unix__) || defined(__unix) || \
1138 (defined(__APPLE__) && defined(__MACH__)))
1142 std::string dirname_cpy = std::string(dirname);
1143 std::string correctEnding =
"XXXXXX";
1147 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1149 if (dirname_cpy.rfind(correctEnding) == std::string::npos) {
1150 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
1151 dirname_cpy = dirname_cpy +
"/";
1160 dirname_cpy = dirname_cpy +
"XXXXXX";
1163 #elif defined(_WIN32) && !defined(WINRT)
1165 dirname_cpy = dirname_cpy.substr(0, dirname_cpy.rfind(correctEnding));
1167 dirname_cpy = dirname_cpy + getUuid();
1173 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1174 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
1175 dirname_cpy = dirname_cpy +
"/";
1177 dirname_cpy = dirname_cpy +
"XXXXXX";
1178 #elif defined(_WIN32) && !defined(WINRT)
1179 dirname_cpy = createFilePath(dirname_cpy, getUuid());
1183 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1184 char *dirname_char =
new char[dirname_cpy.length() + 1];
1185 strcpy(dirname_char, dirname_cpy.c_str());
1187 char *computedDirname = mkdtemp(dirname_char);
1189 if (!computedDirname) {
1190 delete[] dirname_char;
1194 std::string res(computedDirname);
1195 delete[] dirname_char;
1197 #elif defined(_WIN32) && !defined(WINRT)
1198 makeDirectory(dirname_cpy);
1215 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1217 #elif defined(_WIN32)
1221 if (filename.empty()) {
1225 std::string v_filename = path(filename);
1226 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1227 if (stat(v_filename.c_str(), &stbuf) != 0)
1228 #elif defined(_WIN32)
1229 if (_stat(v_filename.c_str(), &stbuf) != 0)
1234 if ((stbuf.st_mode & S_IFREG) == 0) {
1237 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1238 if ((stbuf.st_mode & S_IRUSR) == 0)
1239 #elif defined(_WIN32)
1240 if ((stbuf.st_mode & S_IREAD) == 0)
1260 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1261 #if TARGET_OS_IOS == 0
1263 std::stringstream cmd;
1268 int ret = system(cmd.str().c_str());
1277 #elif defined(_WIN32)
1278 #if (!defined(WINRT))
1279 std::stringstream cmd;
1284 int ret = system(cmd.str().c_str());
1291 src.c_str(), dst.c_str()));
1297 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1298 #if TARGET_OS_IOS == 0
1300 std::stringstream cmd;
1305 int ret = system(cmd.str().c_str());
1314 #elif defined(_WIN32)
1315 #if (!defined(WINRT))
1316 std::stringstream cmd;
1321 int ret = system(cmd.str().c_str());
1328 src.c_str(), dst.c_str()));
1333 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
1352 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1356 if (::remove(file_or_dir.c_str()) != 0) {
1364 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1365 #if TARGET_OS_IOS == 0
1367 std::stringstream cmd;
1371 int ret = system(cmd.str().c_str());
1378 file_or_dir.c_str()));
1380 #elif defined(_WIN32)
1381 #if (!defined(WINRT))
1382 std::stringstream cmd;
1383 cmd <<
"rmdir /S /Q ";
1386 int ret = system(cmd.str().c_str());
1393 file_or_dir.c_str()));
1398 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
1414 if (::rename(oldfilename.c_str(), newfilename.c_str()) != 0) {
1434 std::string path(pathname);
1437 for (
unsigned int i = 0; i < path.length(); ++i)
1440 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
1441 unsigned int path_length = path.length();
1442 for (
unsigned int i = 0; i < path_length; ++i) {
1443 if (path[i] ==
'\\') {
1447 #if TARGET_OS_IOS == 0
1452 wordexp_t exp_result;
1455 replaceAll(path,
"'",
"'\\''");
1457 wordexp(std::string(
"'" + path +
"'").c_str(), &exp_result, 0);
1458 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
1459 wordfree(&exp_result);
1477 configFile = path(confFile);
1479 configValues.clear();
1480 std::ifstream confContent(configFile.c_str(), std::ios::in);
1482 if (confContent.is_open()) {
1483 std::string line, var, val;
1484 unsigned long int k;
1486 std::string stop[3] = {
" ",
"\t",
"#" };
1487 while (std::getline(confContent, line)) {
1488 if ((line.compare(0, 1,
"#") != 0) && (line.size() > 2)) {
1490 k =
static_cast<unsigned long>(line.find(
" "));
1491 var = line.substr(0, k);
1494 for (
unsigned i = 0; i < 3; ++i) {
1496 static_cast<int>(line.find(stop[i],
static_cast<size_t>(k) +
static_cast<size_t>(1))));
1499 c =
static_cast<int>(line.size());
1501 unsigned long int c_ =
static_cast<unsigned long int>(c);
1502 val = line.substr(
static_cast<size_t>(k) +
static_cast<size_t>(1),
1503 static_cast<size_t>(c_) -
static_cast<size_t>(k) -
static_cast<size_t>(1));
1504 configVars.push_back(var);
1505 configValues.push_back(val);
1508 confContent.close();
1527 unsigned int configvars_size = configVars.size();
1528 for (
unsigned int k = 0; (k < configvars_size) && (found ==
false); ++k) {
1529 if (configVars[k] == var) {
1530 if (configValues[k].compare(
"PI") == 0) {
1531 value =
static_cast<float>(M_PI);
1533 else if (configValues[k].compare(
"PI/2") == 0) {
1534 value =
static_cast<float>(M_PI / 2.0);
1536 else if (configValues[k].compare(
"-PI/2") == 0) {
1537 value =
static_cast<float>(-M_PI / 2.0);
1540 value =
static_cast<float>(atof(configValues[k].c_str()));
1545 if (found ==
false) {
1546 std::cout << var <<
" not found in config file" << std::endl;
1561 unsigned int configvars_size = configVars.size();
1562 for (
unsigned int k = 0; (k < configvars_size) && (found ==
false); ++k) {
1563 if (configVars[k] == var) {
1564 if (configValues[k].compare(
"PI") == 0) {
1567 else if (configValues[k].compare(
"PI/2") == 0) {
1570 else if (configValues[k].compare(
"-PI/2") == 0) {
1574 value = atof(configValues[k].c_str());
1579 if (found ==
false) {
1580 std::cout << var <<
" not found in config file" << std::endl;
1596 unsigned int configvars_size = configVars.size();
1597 for (
unsigned int k = 0; (k < configvars_size) && (found ==
false); ++k) {
1598 if (configVars[k] == var) {
1599 value = atoi(configValues[k].c_str());
1603 if (found ==
false) {
1604 std::cout << var <<
" not found in config file" << std::endl;
1620 bool found = readConfigVar(var, v);
1621 value =
static_cast<unsigned int>(v);
1636 bool found = readConfigVar(var, v);
1652 bool found = readConfigVar(var, v);
1668 unsigned int configvars_size = configVars.size();
1669 for (
unsigned int k = 0; (k < configvars_size) && (found ==
false); ++k) {
1670 if (configVars[k] == var) {
1671 value = configValues[k];
1675 if (found ==
false) {
1676 std::cout << var <<
" not found in config file" << std::endl;
1695 const unsigned int &nRows)
1699 unsigned int configvars_size = configVars.size();
1700 for (
unsigned int k = 0; (k < configvars_size) && (found ==
false); ++k) {
1701 if (configVars[k] == var) {
1704 if ((nCols != 0) && (nRows != 0)) {
1705 value.
resize(nRows, nCols);
1707 size_t ind = 0, ind2;
1708 unsigned int value_rows = value.
getRows();
1709 unsigned int value_cols = value.
getCols();
1710 for (
unsigned int i = 0; i < value_rows; ++i) {
1711 for (
unsigned int j = 0; j < value_cols; ++j) {
1712 ind2 = configValues[k].find(
",", ind);
1713 nb = configValues[k].substr(ind, ind2 - ind);
1714 if (nb.compare(
"PI") == 0) {
1717 else if (nb.compare(
"PI/2") == 0) {
1718 value[i][j] = M_PI / 2;
1720 else if (nb.compare(
"-PI/2") == 0) {
1721 value[i][j] = -M_PI / 2;
1724 value[i][j] = atof(nb.c_str());
1731 if (found ==
false) {
1732 std::cout << var <<
" not found in config file" << std::endl;
1750 baseName +=
"_" + strTrue;
1752 else if (strFalse !=
"") {
1753 baseName +=
"_" + strFalse;
1768 if (std::fabs(val) < std::numeric_limits<double>::epsilon()) {
1769 std::stringstream valS;
1772 baseName +=
"_" + strTrue + valS.str();
1786 std::string path = baseDir + baseName;
1790 std::cout <<
"Creating directory " << path << std::endl;
1798 std::cout <<
"Emptying directory " << path << std::endl;
1813 std::string dest = baseDir +
"/" + baseName +
"_config.txt";
1834 std::string data_path;
1835 std::string file_to_test(
"mbt/cube.cao");
1836 std::string filename;
1839 filename = data_path +
"/" + file_to_test;
1844 filename = data_path +
"/" + file_to_test;
1849 filename = data_path +
"/" + file_to_test;
1854 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1856 data_path =
"/usr/share/visp-images-data/ViSP-images";
1857 filename = data_path +
"/" + file_to_test;
1861 data_path =
"/usr/share/visp-images-data/visp-images";
1862 filename = data_path +
"/" + file_to_test;
1907 std::string sep =
"\\";
1908 std::string altsep =
"/";
1909 std::string extsep =
".";
1912 std::string sep =
"/";
1913 std::string altsep =
"";
1914 std::string extsep =
".";
1947 int sepIndex =
static_cast<int>(pathname.rfind(sep));
1948 if (!altsep.empty()) {
1949 int altsepIndex =
static_cast<int>(pathname.rfind(altsep));
1950 sepIndex = std::max<int>(sepIndex, altsepIndex);
1953 size_t dotIndex = pathname.rfind(extsep);
1954 if (dotIndex != std::string::npos) {
1956 size_t npos = std::string::npos;
1957 if (((sepIndex !=
static_cast<int>(npos)) && (
static_cast<int>(dotIndex) > sepIndex)) ||
1958 (sepIndex ==
static_cast<int>(npos))) {
1959 if (sepIndex ==
static_cast<int>(npos)) {
1962 size_t filenameIndex =
static_cast<size_t>(sepIndex) +
static_cast<size_t>(1);
1964 while (filenameIndex < dotIndex) {
1965 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1966 return pathname.substr(dotIndex);
1983 if (pathname.size() > 0) {
1987 if (index != std::string::npos) {
1988 return convertedPathname.substr(index + 1);
1991 return convertedPathname;
2006 size_t found = name.find_last_of(
".");
2007 std::string name_we = name.substr(0, found);
2046 size_t indexBegin = format.find_last_of(
'%');
2047 size_t indexEnd = format.find_first_of(
'd', indexBegin);
2048 size_t suffixLength = format.length() - indexEnd - 1;
2050 if (filename.length() <= (suffixLength + indexBegin)) {
2053 size_t indexLength = filename.length() - suffixLength - indexBegin;
2054 std::string indexSubstr = filename.substr(indexBegin, indexLength);
2055 std::istringstream ss(indexSubstr);
2058 if (ss.fail() || (index < 0) || (!ss.eof())) {
2063 char nameByFormat[FILENAME_MAX];
2064 snprintf(nameByFormat, FILENAME_MAX, format.c_str(), index);
2065 if (std::string(nameByFormat) != filename) {
2088 if (pathname.size() > 0) {
2092 if (index != std::string::npos) {
2093 return convertedPathname.substr(0, index);
2114 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
2115 for (std::string::const_iterator it = input.cbegin(); it != input.cend(); ++it) {
2117 for (std::string::const_iterator it = input.begin(); it != input.end(); ++it) {
2119 out += std::tolower(*it);
2135 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
2136 for (std::string::const_iterator it = input.cbegin(); it != input.cend(); ++it) {
2138 for (std::string::const_iterator it = input.begin(); it != input.end(); ++it) {
2140 out += std::toupper(*it);
2156 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
2157 std::string real_path_str = pathname;
2158 char *real_path = realpath(pathname.c_str(),
nullptr);
2161 real_path_str = real_path;
2164 return real_path_str;
2165 #elif defined(_WIN32)
2166 #if (!defined(WINRT))
2167 std::string real_path_str = pathname;
2169 TCHAR buffer[4096] = TEXT(
"");
2171 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
2173 real_path_str = buffer;
2175 return real_path_str;
2178 "Cannot get absolute path of %s: not implemented on "
2179 "Universal Windows Platform",
2197 if ((child.size() == 0) && (parent.size() == 0)) {
2201 if (child.size() == 0) {
2205 if (parent.size() == 0) {
2212 std::stringstream ss;
2214 std::string stringSeparator;
2215 ss >> stringSeparator;
2217 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
2218 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
2220 if (lastConvertedParentChar == stringSeparator) {
2221 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
2224 if (firstConvertedChildChar == stringSeparator) {
2225 convertedChild = convertedChild.substr(1);
2249 std::string path = splitDrive(pathname).second;
2250 return (path.size() > 0) && ((path.substr(0, 1) ==
"/") || (path.substr(0, 1) ==
"\\"));
2270 return (path1_normalize == path2_normalize);
2333 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
2334 return std::pair<std::string, std::string>(
"", pathname);
2336 const std::string sep =
"\\";
2337 const std::string sepsep =
"\\\\";
2338 const std::string altsep =
"/";
2340 if (pathname.size() > 1) {
2341 std::string normPathname = pathname;
2342 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
2344 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
2349 size_t index = normPathname.find(sep, 2);
2350 if (index == std::string::npos) {
2351 return std::pair<std::string, std::string>(
"", pathname);
2354 size_t index2 = normPathname.find(sep, index + 1);
2357 if (index2 == index + 1) {
2358 return std::pair<std::string, std::string>(
"", pathname);
2361 if (index2 == std::string::npos) {
2362 index2 = pathname.size();
2365 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
2368 if (normPathname[1] ==
':') {
2369 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
2373 return std::pair<std::string, std::string>(
"", pathname);
2427 size_t startIndex = 0;
2429 std::string chainToSplit = chain;
2430 std::vector<std::string> subChain;
2431 size_t sepIndex = chainToSplit.find(sep);
2433 while (sepIndex != std::string::npos) {
2434 std::string sub = chainToSplit.substr(startIndex, sepIndex);
2436 subChain.push_back(sub);
2438 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
2440 sepIndex = chainToSplit.find(sep);
2442 if (!chainToSplit.empty()) {
2443 subChain.push_back(chainToSplit);
2459 if (!checkDirectory(pathname)) {
2462 std::string dirName = path(pathname);
2464 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
2466 std::vector<std::string> files;
2467 struct dirent **list =
nullptr;
2468 int filesCount = scandir(dirName.c_str(), &list,
nullptr,
nullptr);
2469 if (filesCount == -1) {
2472 for (
int i = 0; i < filesCount; ++i) {
2473 std::string fileName = list[i]->d_name;
2474 if ((fileName !=
".") && (fileName !=
"..")) {
2475 files.push_back(fileName);
2480 std::sort(files.begin(), files.end());
2483 #elif defined(_WIN32)
2484 #if (!defined(WINRT))
2486 std::vector<std::string> files;
2487 std::string fileMask = dirName;
2488 fileMask.append(
"\\*");
2489 WIN32_FIND_DATA FindFileData;
2490 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
2492 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
2495 if (hFind == INVALID_HANDLE_VALUE) {
2499 std::string fileName = FindFileData.cFileName;
2500 if (fileName !=
"." && fileName !=
"..") {
2501 files.push_back(fileName);
2503 }
while (FindNextFile(hFind, &FindFileData));
2505 std::sort(files.begin(), files.end());
2510 "Cannot read files of directory %s: not implemented on "
2511 "Universal Windows Platform",
2522 file.read((
char *)(&short_value),
sizeof(short_value));
2524 #ifdef VISP_BIG_ENDIAN
2535 file.read((
char *)(&ushort_value),
sizeof(ushort_value));
2537 #ifdef VISP_BIG_ENDIAN
2548 file.read((
char *)(&int_value),
sizeof(int_value));
2550 #ifdef VISP_BIG_ENDIAN
2561 file.read((
char *)(&uint_value),
sizeof(uint_value));
2563 #ifdef VISP_BIG_ENDIAN
2574 file.read((
char *)(&float_value),
sizeof(float_value));
2576 #ifdef VISP_BIG_ENDIAN
2587 file.read((
char *)(&double_value),
sizeof(double_value));
2589 #ifdef VISP_BIG_ENDIAN
2600 #ifdef VISP_BIG_ENDIAN
2603 file.write((
char *)(&swap_short),
sizeof(swap_short));
2605 file.write((
char *)(&short_value),
sizeof(short_value));
2614 #ifdef VISP_BIG_ENDIAN
2617 file.write((
char *)(&swap_ushort),
sizeof(swap_ushort));
2619 file.write((
char *)(&ushort_value),
sizeof(ushort_value));
2628 #ifdef VISP_BIG_ENDIAN
2631 file.write((
char *)(&swap_int),
sizeof(swap_int));
2633 file.write((
char *)(&int_value),
sizeof(int_value));
2642 #ifdef VISP_BIG_ENDIAN
2645 file.write((
char *)(&swap_int),
sizeof(swap_int));
2647 file.write((
char *)(&uint_value),
sizeof(uint_value));
2656 #ifdef VISP_BIG_ENDIAN
2659 file.write((
char *)(&swap_float),
sizeof(swap_float));
2661 file.write((
char *)(&float_value),
sizeof(float_value));
2670 #ifdef VISP_BIG_ENDIAN
2673 file.write((
char *)(&swap_double),
sizeof(swap_double));
2675 file.write((
char *)(&double_value),
sizeof(double_value));
2681 std::transform(input.begin(), input.end(), input.begin(), ::tolower);
2682 std::istringstream is(input);
2686 is >> (input.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
unsigned int getCols() const
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int getRows() const
Class to define RGB colors available for display functionalities.
static vpColor getColor(const unsigned int &i)
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.
static Type minimum(const Type &a, const Type &b)
VISP_EXPORT char map_type(const std::type_info &t)
VISP_EXPORT npz_t npz_load(std::string fname)
VISP_EXPORT void parse_zip_footer(FILE *fp, uint16_t &nrecs, size_t &global_header_size, size_t &global_header_offset)
VISP_EXPORT void parse_npy_header(FILE *fp, size_t &word_size, std::vector< size_t > &shape, bool &fortran_order)
std::map< std::string, NpyArray > npz_t
VISP_EXPORT NpyArray npy_load(std::string fname)
VISP_EXPORT char BigEndianTest()
VISP_EXPORT float swapFloat(float f)
VISP_EXPORT uint32_t swap32bits(uint32_t val)
VISP_EXPORT double swapDouble(double d)
VISP_EXPORT uint16_t swap16bits(uint16_t val)