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;
67 i >= 0 && !(filePath[i] ==
'\\' || filePath[i] ==
'/'); i--)
68 if (dotPos ==
int(filePath.size()) && filePath[i] ==
'.') dotPos = 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] ==
'/')
break;
89 return filePath.substr(0, i + 1);
98 const string& filePath,
bool ignore_gz)
100 if (filePath.size() < 2)
return string(
"");
102 size_t i_end = filePath.size() - 1;
104 int i = (int)(i_end);
107 if (filePath[i] ==
'.')
109 string the_ext = filePath.substr(i + 1, i_end - i);
110 if (!ignore_gz || the_ext !=
"gz")
141 if (!path.empty() && (*path.rbegin() ==
'/' || *path.rbegin() ==
'\\'))
142 path = path.substr(0, path.size() - 1);
146 if (0 !=
_stat(path.c_str(), &buf))
return false;
148 #ifdef MRPT_OS_WINDOWS 149 return 0 != (buf.st_mode & _S_IFDIR);
151 return S_ISDIR(buf.st_mode);
160 #ifdef MRPT_OS_WINDOWS 161 bool rc = 0 != CreateDirectoryA(dirName.c_str(),
nullptr);
162 return (rc || GetLastError() == ERROR_ALREADY_EXISTS);
164 int ret = mkdir(dirName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
165 if (ret && errno != EEXIST)
167 string str =
format(
"[createDirectory %s]", dirName.c_str());
181 return 0 ==
remove(fileName.c_str());
190 size_t len =
s.size() + 20;
191 char* aux =
new char[
len];
192 #ifdef MRPT_OS_WINDOWS 194 for (
char*
c = aux; *
c;
c++)
195 if (*
c ==
'/') *
c =
'\\';
201 int res = ::system(aux);
204 cerr <<
"[mrpt::system::deleteFiles] Warning: error invoking: " << aux
215 const string& path,
bool deleteDirectoryAsWell)
220 CDirectoryExplorer::explore(
224 i != lstFiles.end(); ++i)
228 if (i->name !=
"." && i->name !=
"..")
241 if (deleteDirectoryAsWell)
242 return 0 ==
_rmdir(path.c_str());
251 #ifdef MRPT_OS_WINDOWS 252 char auxBuf[MAX_PATH] =
"";
253 if (!::GetCurrentDirectoryA(
sizeof(auxBuf) - 1, auxBuf))
283 #ifdef MRPT_OS_WINDOWS 285 GetSystemTimeAsFileTime(&tt);
286 const UINT uniq =
static_cast<UINT
>(tt.dwLowDateTime);
287 char TMP_PATH[MAX_PATH];
288 char tmpPath[MAX_PATH];
289 GetTempPathA(MAX_PATH, tmpPath);
290 GetTempFileNameA(tmpPath,
"mrpt", uniq, TMP_PATH);
293 char tmp[] =
"/tmp/mrpt_tempXXXXXX";
308 const string& oldFileName,
const string& newFileName,
311 bool ret_err = 0 == rename(oldFileName.c_str(), newFileName.c_str());
316 *error_msg = strerror(errno);
328 const std::string& filename,
const char replacement_to_invalid_chars)
330 const char forbid[] = {
'<',
'>',
':',
'"',
'/',
'\\',
'|',
'?',
'*'};
331 const unsigned int nForbid =
sizeof(forbid) /
sizeof(forbid[0]);
333 string ret(filename);
336 bool invalid = (*
c < 32);
338 for (
unsigned int i = 0; !invalid && i < nForbid; i++)
339 if (*
c == forbid[i]) invalid =
true;
341 if (invalid) *
c =
'_';
351 #if defined(_MSC_VER) 353 struct __stat64 filStat;
354 if (_stat64(fileName.c_str(), &filStat))
361 if (stat(fileName.c_str(), &filStat))
372 if (filePath.size() < 2)
return filePath;
374 const size_t i_end = filePath.size() - 1;
376 for (
int i =
int(i_end); i > 0; i--)
377 if (filePath[i] ==
'.')
return filePath.substr(0, i + 1) + newExtension;
380 return filePath +
string(
".") + newExtension;
403 string(
"Source does not exist or permission denied!: ") + org;
408 if (outErrStr) *outErrStr =
string(
"Is source a directory?: ") + org;
413 FILE* f_src =
fopen(org.c_str(),
"rb");
418 "Source file exists but cannot open it... is file " 428 *outErrStr =
string(
"Target cannot be a directory: ") + trg;
434 FILE* f_trg =
fopen(trg.c_str(),
"wb");
441 *outErrStr =
string(
"Cannot create target file: ") + trg;
449 #ifdef MRPT_OS_WINDOWS 451 DWORD dwProp = GetFileAttributesA(trg.c_str());
452 if (dwProp == INVALID_FILE_ATTRIBUTES)
457 "Cannot get file attributes for target file, " 458 "trying to remove a possible read-only attribute " 459 "after first attempt of copy failed, for: ") +
465 dwProp &= ~FILE_ATTRIBUTE_HIDDEN;
466 dwProp &= ~FILE_ATTRIBUTE_READONLY;
467 dwProp &= ~FILE_ATTRIBUTE_SYSTEM;
469 if (!SetFileAttributesA(trg.c_str(), dwProp))
474 "Cannot get file attributes for target file, " 475 "trying to remove a possible read-only attribute " 476 "after first attempt of copy failed, for: ") +
483 f_trg =
fopen(trg.c_str(),
"wb");
488 "Cannot overwrite target file, even after " 489 "changing file attributes! : ") +
496 if (chmod(trg.c_str(), S_IRWXU | S_IRGRP | S_IROTH))
501 "Cannot set file permissions for target file, " 502 "trying to remove a possible read-only attribute " 503 "after first attempt of copy failed, for: ") +
510 f_trg =
fopen(trg.c_str(),
"wb");
515 "Cannot overwrite target file, even after " 516 "changing file permissions! : ") +
528 while (0 != (nBytes = fread(buf, 1, 64 * 1024, f_src)))
530 if (nBytes != fwrite(buf, 1, nBytes, f_trg))
534 "Error writing the contents of the target " 535 "file (disk full?): ") +
550 #ifdef MRPT_OS_WINDOWS 551 DWORD dwPropSrc = GetFileAttributesA(org.c_str());
552 if (dwPropSrc == INVALID_FILE_ATTRIBUTES)
557 "Cannot get the file attributes for source file: ") +
561 DWORD dwPropTrg = GetFileAttributesA(trg.c_str());
562 if (dwPropTrg == INVALID_FILE_ATTRIBUTES)
567 "Cannot get the file attributes for target file: ") +
574 dwPropSrc &= FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN |
575 FILE_ATTRIBUTE_NORMAL |
576 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
577 FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_READONLY |
578 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY;
582 ~(FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN |
583 FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
584 FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_READONLY |
585 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY);
586 dwPropTrg |= dwPropSrc;
589 if (!SetFileAttributesA(trg.c_str(), dwPropTrg))
593 string(
"Cannot set file attributes for target file: ") +
612 const size_t N = ret.size();
613 for (
size_t i = 0; i < N; i++)
615 #ifdef MRPT_OS_WINDOWS 616 if (ret[i] ==
'/') ret[i] =
'\\';
618 if (ret[i] ==
'\\') ret[i] =
'/';
627 if (0 != stat(filename.c_str(), &fS))
633 #include <mrpt/version.h> 640 static vector<string> sPaths;
641 static string sDetectedPath;
642 static bool is_first =
true;
649 string(MRPT_CMAKE_SOURCE_DIR) +
string(
"/share/mrpt/"));
652 string(MRPT_CMAKE_INSTALL_PREFIX) +
string(
"/share/mrpt/"));
657 #ifdef MRPT_OS_WINDOWS 658 sBufOk = (0 != GetModuleFileNameA(NULL, buf,
sizeof(buf)));
661 sBufOk = (-1 != readlink(
"/proc/self/exe", buf,
sizeof(buf)));
666 string sBuf =
string(buf);
667 std::replace(sBuf.begin(), sBuf.end(),
'\\',
'/');
669 sPaths.push_back(sBuf +
string(
"share/mrpt/"));
670 sPaths.push_back(sBuf +
string(
"../share/mrpt/"));
671 sPaths.push_back(sBuf +
string(
"../../share/mrpt/"));
674 for (
const auto& e : sPaths)
681 return sDetectedPath;
bool 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 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 void fclose(FILE *f)
An OS-independent version of fclose.
#define THROW_EXCEPTION(msg)
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
bool renameFile(const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=nullptr)
Renames a file - If the target path is different and the filesystem allows it, it will be moved to th...
std::string 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 getTempFileName()
Returns the name of a proposed temporary file name.
std::deque< TFileInfo > TFileInfoList
The list type used in "explore".
std::string filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/'.
std::string fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
void deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including '...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::string 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
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
std::string getcwd()
Returns the current working directory.
bool deleteFile(const std::string &fileName)
Deletes a single file.
bool copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=nullptr, bool copyAttribs=true)
Copies file sourceFile to targetFile.
bool 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).
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file)...
uint64_t 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 extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
std::string extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension...
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
time_t getFileModificationTime(const std::string &filename)
Return the time of the file last modification, or "0" if the file doesn't exist.