23 #ifdef MRPT_OS_WINDOWS 28 #include <sys/utime.h> 40 #include <sys/types.h> 43 #if !defined(_MSC_VER) 44 # define _access access 46 # ifndef _stat // It seems MinGW already defines this. 63 int i,dotPos = int(filePath.size());
64 if (filePath.size()<2)
return string(
"");
66 for (i=(
int)filePath.size()-1;i>=0 && !(filePath[i]==
'\\' || filePath[i]==
'/');i--)
67 if (dotPos==
int(filePath.size()) && filePath[i]==
'.')
69 return filePath.substr(i+1,dotPos-i-1);
79 if (filePath.size()<2)
return filePath;
83 for (i=(
int)filePath.size()-1;i>0;i--)
84 if (filePath[i]==
'\\' || filePath[i]==
'/')
88 else return filePath.substr(0,i+1);
98 if (filePath.size()<2)
return string(
"");
100 size_t i_end = filePath.size()-1;
105 if (filePath[i]==
'.')
107 string the_ext = filePath.substr(i+1,i_end-i);
108 if (!ignore_gz || the_ext!=
"gz")
126 return 0 ==
_access(path.c_str(), 0x00 );
137 if (!path.empty() && (*path.rbegin()==
'/' || *path.rbegin()==
'\\'))
138 path = path.substr(0,path.size()-1);
142 if (0!=
_stat(path.c_str(),&buf))
return false;
144 #ifdef MRPT_OS_WINDOWS 145 return 0!=(buf.st_mode &_S_IFDIR);
147 return S_ISDIR(buf.st_mode);
156 #ifdef MRPT_OS_WINDOWS 157 bool rc = 0!=CreateDirectoryA( dirName.c_str(), NULL);
158 return (rc || GetLastError()==ERROR_ALREADY_EXISTS);
160 int ret = mkdir( dirName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
161 if (ret && errno!=EEXIST)
163 string str =
format(
"[createDirectory %s]",dirName.c_str());
177 return 0==
remove( fileName.c_str() );
186 size_t len =
s.size()+20;
187 char *aux=
new char[
len];
188 #ifdef MRPT_OS_WINDOWS 190 for (
char *
c=aux;*
c;
c++)
191 if (*
c==
'/') *
c =
'\\';
197 int res = ::system(aux);
200 cerr <<
"[mrpt::system::deleteFiles] Warning: error invoking: " << aux << endl;
220 if (i->name!=
"." && i->name!=
"..")
234 if (deleteDirectoryAsWell)
235 return 0==
_rmdir(path.c_str());
243 #ifdef MRPT_OS_WINDOWS 244 char auxBuf[MAX_PATH] =
"";
245 if(!::GetCurrentDirectoryA(
sizeof(auxBuf) - 1, auxBuf))
275 #ifdef MRPT_OS_WINDOWS 277 GetSystemTimeAsFileTime(&tt);
278 const UINT uniq =
static_cast<UINT
>(tt.dwLowDateTime);
279 char TMP_PATH[ MAX_PATH ];
280 char tmpPath[ MAX_PATH ];
281 GetTempPathA(MAX_PATH,tmpPath);
282 GetTempFileNameA(tmpPath,
"mrpt", uniq, TMP_PATH);
285 char tmp[] =
"/tmp/mrpt_tempXXXXXX";
299 bool ret_err = 0==rename( oldFileName.c_str(), newFileName.c_str() );
304 *error_msg= strerror(errno);
317 const char forbid[] = {
'<',
'>',
':',
'"',
'/',
'\\',
'|',
'?',
'*'};
318 const unsigned int nForbid =
sizeof(forbid) /
sizeof(forbid[0]);
320 string ret(filename );
323 bool invalid = (*
c < 32);
325 for (
unsigned int i = 0; !invalid && i < nForbid; i++)
329 if (invalid) *
c =
'_';
340 #if defined(_MSC_VER) 342 struct __stat64 filStat;
343 if ( _stat64( fileName.c_str(), &filStat ) )
345 else return uint64_t(filStat.st_size);
349 if ( stat( fileName.c_str(), &filStat ) )
351 else return uint64_t(filStat.st_size);
358 if (filePath.size()<2)
return filePath;
360 const size_t i_end = filePath.size()-1;
362 for (
int i=
int(i_end);i>0;i--)
363 if (filePath[i]==
'.')
364 return filePath.substr(0,i+1) + newExtension;
367 return filePath +
string(
".") + newExtension;
389 if (outErrStr) *outErrStr =
string(
"Source does not exist or permission denied!: ")+org;
394 if (outErrStr) *outErrStr =
string(
"Is source a directory?: ")+org;
399 FILE *f_src=
fopen(org.c_str(),
"rb");
402 if (outErrStr) *outErrStr =
string(
"Source file exists but cannot open it... is file being used?: ")+org;
409 if (outErrStr) *outErrStr =
string(
"Target cannot be a directory: ")+trg;
415 FILE *f_trg=
fopen(trg.c_str(),
"wb");
421 if (outErrStr) *outErrStr =
string(
"Cannot create target file: ")+trg;
429 #ifdef MRPT_OS_WINDOWS 431 DWORD dwProp = GetFileAttributesA( trg.c_str() );
432 if (dwProp==INVALID_FILE_ATTRIBUTES)
434 if (outErrStr) *outErrStr =
string(
"Cannot get file attributes for target file, trying to remove a possible read-only attribute after first attempt of copy failed, for: ")+trg;
439 dwProp &= ~FILE_ATTRIBUTE_HIDDEN;
440 dwProp &= ~FILE_ATTRIBUTE_READONLY;
441 dwProp &= ~FILE_ATTRIBUTE_SYSTEM;
443 if (!SetFileAttributesA( trg.c_str(), dwProp ))
445 if (outErrStr) *outErrStr =
string(
"Cannot get file attributes for target file, trying to remove a possible read-only attribute after first attempt of copy failed, for: ")+trg;
451 f_trg=
fopen(trg.c_str(),
"wb");
454 if (outErrStr) *outErrStr =
string(
"Cannot overwrite target file, even after changing file attributes! : ")+trg;
460 if (chmod( trg.c_str(), S_IRWXU | S_IRGRP | S_IROTH ) )
462 if (outErrStr) *outErrStr =
string(
"Cannot set file permissions for target file, trying to remove a possible read-only attribute after first attempt of copy failed, for: ")+trg;
468 f_trg=
fopen(trg.c_str(),
"wb");
471 if (outErrStr) *outErrStr =
string(
"Cannot overwrite target file, even after changing file permissions! : ")+trg;
482 while (0!= (nBytes=fread(buf,1,64*1024,f_src)) )
484 if (nBytes!=fwrite(buf,1,nBytes,f_trg))
486 if (outErrStr) *outErrStr =
string(
"Error writing the contents of the target file (disk full?): ")+trg;
500 #ifdef MRPT_OS_WINDOWS 501 DWORD dwPropSrc = GetFileAttributesA( org.c_str() );
502 if (dwPropSrc==INVALID_FILE_ATTRIBUTES)
504 if (outErrStr) *outErrStr =
string(
"Cannot get the file attributes for source file: ")+org;
507 DWORD dwPropTrg = GetFileAttributesA( trg.c_str() );
508 if (dwPropTrg==INVALID_FILE_ATTRIBUTES)
510 if (outErrStr) *outErrStr =
string(
"Cannot get the file attributes for target file: ")+trg;
516 dwPropSrc &= FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL | \
517 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_OFFLINE | \
518 FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY;
521 dwPropTrg &= ~(FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL | \
522 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_OFFLINE | \
523 FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY);
524 dwPropTrg |= dwPropSrc;
527 if (!SetFileAttributesA( trg.c_str(), dwPropTrg ))
529 if (outErrStr) *outErrStr =
string(
"Cannot set file attributes for target file: ")+trg;
546 const size_t N = ret.size();
547 for (
size_t i=0;i<N;i++)
549 #ifdef MRPT_OS_WINDOWS 550 if (ret[i]==
'/') ret[i]=
'\\';
552 if (ret[i]==
'\\') ret[i]=
'/';
561 if (0!=stat( filename.c_str(), &fS))
return 0;
562 else return fS.st_mtime;
565 #include <mrpt/version.h> 572 static vector<string> sPaths;
573 static string sDetectedPath;
574 static bool is_first =
true;
580 sPaths.push_back(
string(MRPT_CMAKE_SOURCE_DIR) +
string(
"/share/mrpt/"));
582 sPaths.push_back(
string(MRPT_CMAKE_INSTALL_PREFIX) +
string(
"/share/mrpt/"));
587 #ifdef MRPT_OS_WINDOWS 588 sBufOk = (0 != GetModuleFileNameA(NULL, buf,
sizeof(buf)));
591 sBufOk = (-1 != readlink(
"/proc/self/exe", buf,
sizeof(buf)));
595 string sBuf =
string(buf);
596 std::replace(sBuf.begin(), sBuf.end(),
'\\',
'/');
598 sPaths.push_back(sBuf +
string(
"share/mrpt/"));
599 sPaths.push_back(sBuf +
string(
"../share/mrpt/"));
600 sPaths.push_back(sBuf +
string(
"../../share/mrpt/"));
603 for (
const auto &e : sPaths)
609 return sDetectedPath;
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
bool BASE_IMPEXP createDirectory(const std::string &dirName)
Creates a directory.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::string BASE_IMPEXP getShareMRPTDir()
Attempts to find the directory [PREFIX/]share/mrpt/ and returns its absolute path, or empty string if not found.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
#define THROW_EXCEPTION(msg)
bool BASE_IMPEXP fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
std::string BASE_IMPEXP fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores ('_') or any other user-given char. ...
#define FILE_ATTRIB_ARCHIVE
std::string BASE_IMPEXP getTempFileName()
Returns the name of a proposed temporary file name.
std::deque< TFileInfo > TFileInfoList
The list type used in "explore".
std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/'.
std::string BASE_IMPEXP fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
void BASE_IMPEXP deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including '...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
GLsizei const GLchar ** string
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define FILE_ATTRIB_DIRECTORY
std::string BASE_IMPEXP getcwd()
Returns the current working directory.
char BASE_IMPEXP * strcat(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS
An OS-independent version of strcat.
bool BASE_IMPEXP deleteFile(const std::string &fileName)
Deletes a single file.
bool BASE_IMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell=false)
Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).
int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
bool BASE_IMPEXP directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file)...
uint64_t BASE_IMPEXP getFileSize(const std::string &fileName)
Return the size of the given file, or size_t(-1) if some error is found accessing that file...
std::string BASE_IMPEXP extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
bool BASE_IMPEXP copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=NULL, bool copyAttribs=true)
Copies file sourceFile to targetFile.
bool BASE_IMPEXP renameFile(const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL)
Renames a file - If the target path is different and the filesystem allows it, it will be moved to th...
std::string BASE_IMPEXP extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension...
time_t BASE_IMPEXP getFileModificationTime(const std::string &filename)
Return the time of the file last modification, or "0" if the file doesn't exist.