46 #include <visp/vpIoTools.h>
47 #include <visp/vpDebug.h>
48 #include <visp/vpIoException.h>
54 #include <sys/types.h>
59 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
97 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
99 char *_username = NULL;
101 if (_username == NULL) {
102 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
104 "Cannot get the username")) ;
106 username = _username;
107 #elif defined(_WIN32)
108 unsigned int info_buffer_size = 1024;
109 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
110 DWORD bufCharCount = (DWORD) info_buffer_size;
112 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
116 "Cannot get the username")) ;
143 std::string username;
144 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
146 char *_username = NULL;
148 if (_username == NULL) {
149 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
151 "Cannot get the username")) ;
153 username = _username;
154 #elif defined(_WIN32)
155 unsigned int info_buffer_size = 1024;
156 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
157 DWORD bufCharCount = (DWORD) info_buffer_size;
159 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
163 "Cannot get the username")) ;
210 if (_value == NULL) {
211 vpERROR_TRACE(
"Cannot get the environment variable value" );
213 "Cannot get the environment variable value")) ;
270 if(version.size() == 0){
276 size_t major_pos = version.find(
'.');
277 std::string major_str = version.substr(0, major_pos);
278 major = (unsigned)atoi(major_str.c_str());
280 if(major_pos != std::string::npos){
281 size_t minor_pos = version.find(
'.', major_pos+1);
282 std::string minor_str = version.substr(major_pos+1, (minor_pos - (major_pos+1)));
283 minor = (unsigned)atoi(minor_str.c_str());
285 if(minor_pos != std::string::npos){
286 std::string patch_str = version.substr(minor_pos+1);
287 patch = (unsigned)atoi(patch_str.c_str());
317 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
319 #elif defined(_WIN32)
323 if ( dirname == NULL || dirname[0] ==
'\0' ) {
327 std::string _dirname =
path(dirname);
329 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
330 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
331 #elif defined(_WIN32)
332 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
337 if ( (stbuf.st_mode & S_IFDIR) == 0 ) {
340 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
341 if ( (stbuf.st_mode & S_IWUSR) == 0 )
342 #elif defined(_WIN32)
343 if ( (stbuf.st_mode & S_IWRITE) == 0 )
386 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
388 #elif defined(_WIN32)
392 if ( dirname == NULL || dirname[0] ==
'\0' ) {
395 "invalid directory name")) ;
398 std::string _dirname =
path(dirname);
400 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
401 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
402 #elif defined(_WIN32)
403 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
406 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
407 if ( mkdir( _dirname.c_str(), (mode_t)0755 ) != 0 )
408 #elif defined(_WIN32)
409 if ( _mkdir( _dirname.c_str()) != 0 )
412 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
414 "unable to create directory")) ;
420 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
422 "unable to create directory")) ;
445 vpERROR_TRACE(
"unable to create directory '%s'\n",dirname.c_str());
447 "unable to create directory")) ;
467 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
469 #elif defined(_WIN32)
473 if ( filename == NULL || filename[0] ==
'\0' ) {
477 std::string _filename =
path(filename);
478 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
479 if ( stat( _filename.c_str(), &stbuf ) != 0 )
480 #elif defined(_WIN32)
481 if ( _stat( _filename.c_str(), &stbuf ) != 0 )
486 if ( (stbuf.st_mode & S_IFREG) == 0 ) {
489 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
490 if ( (stbuf.st_mode & S_IRUSR) == 0 )
491 #elif defined(_WIN32)
492 if ( (stbuf.st_mode & S_IREAD) == 0 )
533 char cmd[FILENAME_MAX];
538 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
539 sprintf(cmd,
"cp -p %s %s", src, dst);
540 #elif defined(_WIN32)
543 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
551 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
552 sprintf(cmd,
"cp -p -r %s %s", src, dst);
553 #elif defined(_WIN32)
556 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
564 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
602 if (::
remove(file_or_dir) != 0)
609 char cmd[FILENAME_MAX];
610 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
611 sprintf(cmd,
"rm -rf %s", file_or_dir);
612 #elif defined(_WIN32)
614 sprintf(cmd,
"rmdir /S /Q %s", file_or_dir_.c_str());
616 int ret = system( cmd );
622 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
657 if (::
rename(oldfilename, newfilename) != 0)
697 std::string
path(pathname);
700 for(
unsigned int i=0 ; i<path.length() ; i++)
701 if( path[i] ==
'/') path[i] =
'\\';
703 for(
unsigned int i=0 ; i<path.length() ; i++)
704 if( path[i] ==
'\\') path[i] =
'/';
705 wordexp_t exp_result;
706 wordexp(path.c_str(), &exp_result, 0);
707 path = std::string(exp_result.we_wordv[0]);
708 wordfree(&exp_result);
729 return path(pathname.c_str());
744 std::ifstream confContent(
configFile.c_str(), std::ios::in);
746 if(confContent.is_open())
748 std::string line,var,val;
751 std::string stop[3] = {
" ",
"\t",
"#"};
752 while(std::getline(confContent, line))
754 if((line.find(
"#",0,1) != 0) && (line.size() > 2))
759 k = (
unsigned long)line.find(
" ");
760 var = line.substr(0,k);
763 for(
unsigned i=0;i<3;++i)
766 c = (int)line.size();
767 long unsigned int c_ = (
long unsigned int) c;
768 val = line.substr(k+1,c_-k-1);
794 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
810 std::cout << var <<
" not found in config file" << std::endl;
824 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
840 std::cout << var <<
" not found in config file" << std::endl;
855 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
864 std::cout << var <<
" not found in config file" << std::endl;
880 value = (
unsigned int) v;
927 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
936 std::cout << var <<
" not found in config file" << std::endl;
956 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
962 if(nCols != 0 && nRows != 0)
963 value.
resize(nRows, nCols);
965 for(
unsigned int i=0;i<value.
getRows();++i)
966 for(
unsigned int j=0;j<value.
getCols();++j)
970 if(nb.compare(
"PI") == 0)
972 else if(nb.compare(
"PI/2") == 0)
973 value[i][j] = M_PI/2;
974 else if(nb.compare(
"-PI/2") == 0)
975 value[i][j] = -M_PI/2;
977 value[i][j] = atof(nb.c_str());
983 std::cout << var <<
" not found in config file" << std::endl;
1000 else if(strFalse !=
"")
1014 if(std::fabs(val) < std::numeric_limits<double>::epsilon())
1017 sprintf(valC,
"%.3f", val);
1018 std::string valS(valC);
1073 std::string data_path;
1074 std::string file_to_test(
"ViSP-images/mbt/cube.cao");
1075 std::string filename;
1078 data_path =
"/usr/share/visp-images-data";
1079 filename = data_path +
"/" + file_to_test;
1086 filename = data_path +
"/" + file_to_test;
1112 std::string sep =
"/";
1113 std::string altsep =
"";
1114 std::string extsep =
".";
1152 int sepIndex = (int)pathname.rfind(sep);
1153 if(!altsep.empty()) {
1154 int altsepIndex = (int)pathname.rfind(altsep);
1155 sepIndex = (std::max)(sepIndex, altsepIndex);
1158 size_t dotIndex = pathname.rfind(extsep);
1159 if(dotIndex != std::string::npos) {
1161 if((sepIndex != (
int)std::string::npos && (
int)dotIndex > sepIndex) || sepIndex == (
int)std::string::npos) {
1162 if(sepIndex == (
int)std::string::npos) {
1165 size_t filenameIndex = (size_t)(sepIndex + 1);
1167 while(filenameIndex < dotIndex) {
1168 if(pathname.compare(filenameIndex, 1, extsep) != 0) {
1169 return pathname.substr(dotIndex);
1187 if(pathname.size() > 0)
1192 if(index != std::string::npos) {
1193 return convertedPathname.substr(index + 1);
1196 return convertedPathname;
1210 size_t found = name.find_last_of(
".");
1211 std::string name_we = name.substr(0, found);
1222 if(pathname.size() > 0)
1227 if(index != std::string::npos) {
1228 return convertedPathname.substr(0, index);
1247 if(child.size() == 0 && parent.size() == 0)
1252 if(child.size() == 0)
1257 if(parent.size() == 0)
1265 std::stringstream ss;
1267 std::string stringSeparator;
1268 ss >> stringSeparator;
1270 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1271 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1273 if(lastConvertedParentChar == stringSeparator)
1275 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1278 if(firstConvertedChildChar == stringSeparator)
1280 convertedChild = convertedChild.substr(1);
1283 return std::string(convertedParent + vpIoTools::separator + convertedChild);
1305 return path.size() > 0 && (path.substr(0, 1) ==
"/" || path.substr(0, 1) ==
"\\");
1365 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1366 return std::pair<std::string, std::string>(
"", pathname);
1368 const std::string sep =
"\\";
1369 const std::string sepsep =
"\\\\";
1370 const std::string altsep =
"/";
1372 if(pathname.size() > 1) {
1373 std::string normPathname = pathname;
1374 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1376 if(normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1381 size_t index = normPathname.find(sep, 2);
1382 if(index == std::string::npos) {
1383 return std::pair<std::string, std::string>(
"", pathname);
1386 size_t index2 = normPathname.find(sep, index + 1);
1389 if(index2 == index + 1) {
1390 return std::pair<std::string, std::string>(
"", pathname);
1393 if(index2 == std::string::npos) {
1394 index2 = pathname.size();
1397 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1400 if(normPathname[1] ==
':') {
1401 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1405 return std::pair<std::string, std::string>(
"", pathname);
Definition of the vpMatrix class.
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Class to define colors available for display functionnalities.
Error that can be emited by the vpIoTools class and its derivates.
static Type minimum(const Type &a, const Type &b)
unsigned int getCols() const
Return the number of columns of the matrix.
static vpColor getColor(const unsigned int &i)
unsigned int getRows() const
Return the number of rows of the matrix.