Go to the documentation of this file.
29 #include <sys/utime.h>
41 #include <sys/types.h>
44 #if !defined(_MSC_VER)
45 #define _access access
47 #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;
149 return 0 != (buf.st_mode & _S_IFDIR);
151 return S_ISDIR(buf.st_mode);
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];
194 for (
char*
c = aux; *
c;
c++)
195 if (*
c ==
'/') *
c =
'\\';
201 int res = ::system(aux);
206 "[mrpt::system::deleteFiles] Warning: error invoking: `%s`\n", aux);
216 const string& path,
bool deleteDirectoryAsWell)
225 i != lstFiles.end(); ++i)
229 if (i->name !=
"." && i->name !=
"..")
242 if (deleteDirectoryAsWell)
243 return 0 ==
_rmdir(path.c_str());
253 char auxBuf[MAX_PATH] =
"";
254 if (!::GetCurrentDirectoryA(
sizeof(auxBuf) - 1, auxBuf))
286 GetSystemTimeAsFileTime(&tt);
287 const UINT uniq =
static_cast<UINT
>(tt.dwLowDateTime);
288 char TMP_PATH[MAX_PATH];
289 char tmpPath[MAX_PATH];
290 GetTempPathA(MAX_PATH, tmpPath);
291 GetTempFileNameA(tmpPath,
"mrpt", uniq, TMP_PATH);
294 char tmp[] =
"/tmp/mrpt_tempXXXXXX";
309 const string& oldFileName,
const string& newFileName,
312 bool ret_err = 0 == rename(oldFileName.c_str(), newFileName.c_str());
317 *error_msg = strerror(errno);
329 const std::string& filename,
const char replacement_to_invalid_chars)
331 const char forbid[] = {
'<',
'>',
':',
'"',
'/',
'\\',
'|',
'?',
'*'};
332 const unsigned int nForbid =
sizeof(forbid) /
sizeof(forbid[0]);
334 string ret(filename);
337 bool invalid = (*
c < 32);
339 for (
unsigned int i = 0; !invalid && i < nForbid; i++)
340 if (*
c == forbid[i]) invalid =
true;
342 if (invalid) *
c =
'_';
352 #if defined(_MSC_VER)
354 struct __stat64 filStat;
355 if (_stat64(fileName.c_str(), &filStat))
362 if (stat(fileName.c_str(), &filStat))
373 if (filePath.size() < 2)
return filePath;
375 const size_t i_end = filePath.size() - 1;
377 for (
int i =
int(i_end); i > 0; i--)
378 if (filePath[i] ==
'.')
return filePath.substr(0, i + 1) + newExtension;
381 return filePath +
string(
".") + newExtension;
404 string(
"Source does not exist or permission denied!: ") + org;
409 if (outErrStr) *outErrStr =
string(
"Is source a directory?: ") + org;
414 FILE* f_src =
fopen(org.c_str(),
"rb");
419 "Source file exists but cannot open it... is file "
429 *outErrStr =
string(
"Target cannot be a directory: ") + trg;
435 FILE* f_trg =
fopen(trg.c_str(),
"wb");
442 *outErrStr =
string(
"Cannot create target file: ") + trg;
452 DWORD dwProp = GetFileAttributesA(trg.c_str());
453 if (dwProp == INVALID_FILE_ATTRIBUTES)
458 "Cannot get file attributes for target file, "
459 "trying to remove a possible read-only attribute "
460 "after first attempt of copy failed, for: ") +
466 dwProp &= ~FILE_ATTRIBUTE_HIDDEN;
467 dwProp &= ~FILE_ATTRIBUTE_READONLY;
468 dwProp &= ~FILE_ATTRIBUTE_SYSTEM;
470 if (!SetFileAttributesA(trg.c_str(), dwProp))
475 "Cannot get file attributes for target file, "
476 "trying to remove a possible read-only attribute "
477 "after first attempt of copy failed, for: ") +
484 f_trg =
fopen(trg.c_str(),
"wb");
489 "Cannot overwrite target file, even after "
490 "changing file attributes! : ") +
497 if (chmod(trg.c_str(), S_IRWXU | S_IRGRP | S_IROTH))
502 "Cannot set file permissions for target file, "
503 "trying to remove a possible read-only attribute "
504 "after first attempt of copy failed, for: ") +
511 f_trg =
fopen(trg.c_str(),
"wb");
516 "Cannot overwrite target file, even after "
517 "changing file permissions! : ") +
529 while (0 != (nBytes = fread(buf, 1, 64 * 1024, f_src)))
531 if (nBytes != fwrite(buf, 1, nBytes, f_trg))
535 "Error writing the contents of the target "
536 "file (disk full?): ") +
552 DWORD dwPropSrc = GetFileAttributesA(org.c_str());
553 if (dwPropSrc == INVALID_FILE_ATTRIBUTES)
558 "Cannot get the file attributes for source file: ") +
562 DWORD dwPropTrg = GetFileAttributesA(trg.c_str());
563 if (dwPropTrg == INVALID_FILE_ATTRIBUTES)
568 "Cannot get the file attributes for target file: ") +
575 dwPropSrc &= FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN |
576 FILE_ATTRIBUTE_NORMAL |
577 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
578 FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_READONLY |
579 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY;
583 ~(FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN |
584 FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
585 FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_READONLY |
586 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY);
587 dwPropTrg |= dwPropSrc;
590 if (!SetFileAttributesA(trg.c_str(), dwPropTrg))
594 string(
"Cannot set file attributes for target file: ") +
613 const size_t N = ret.size();
614 for (
size_t i = 0; i < N; i++)
617 if (ret[i] ==
'/') ret[i] =
'\\';
619 if (ret[i] ==
'\\') ret[i] =
'/';
628 if (0 != stat(filename.c_str(), &fS))
634 #include <mrpt/version.h>
641 static vector<string> sPaths;
642 static string sDetectedPath;
643 static bool is_first =
true;
650 string(MRPT_CMAKE_SOURCE_DIR) +
string(
"/share/mrpt/"));
653 string(MRPT_CMAKE_INSTALL_PREFIX) +
string(
"/share/mrpt/"));
659 sBufOk = (0 != GetModuleFileNameA(NULL, buf,
sizeof(buf)));
662 sBufOk = (-1 != readlink(
"/proc/self/exe", buf,
sizeof(buf)));
667 string sBuf =
string(buf);
668 std::replace(sBuf.begin(), sBuf.end(),
'\\',
'/');
670 sPaths.push_back(sBuf +
string(
"share/mrpt/"));
671 sPaths.push_back(sBuf +
string(
"../share/mrpt/"));
672 sPaths.push_back(sBuf +
string(
"../../share/mrpt/"));
675 for (
const auto& e : sPaths)
682 return sDetectedPath;
int void fclose(FILE *f)
An OS-independent version of fclose.
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 extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
bool copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=nullptr, bool copyAttribs=true)
Copies file sourceFile to targetFile.
time_t getFileModificationTime(const std::string &filename)
Return the time of the file last modification, or "0" if the file doesn't exist.
std::string getShareMRPTDir()
Attempts to find the directory [PREFIX/]share/mrpt/ and returns its absolute path,...
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define THROW_EXCEPTION(msg)
#define ASSERT_(f)
Defines an assertion mechanism.
std::string getTempFileName()
Returns the name of a proposed temporary file name.
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
#define FILE_ATTRIB_ARCHIVE
#define FILE_ATTRIB_DIRECTORY
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.
std::string fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
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...
std::deque< TFileInfo > TFileInfoList
The list type used in "explore".
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::string getcwd()
Returns the current working directory
std::string filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/'.
unsigned __int64 uint64_t
bool deleteFile(const std::string &fileName)
Deletes a single file.
void deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including '?...
std::string extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
bool deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell=false)
Delete all the files in a given directory (nothing done if directory does not exists,...
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
std::string extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
GLsizei const GLchar ** string
bool createDirectory(const std::string &dirName)
Creates a directory.
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
static void explore(const std::string &path, const unsigned long mask, TFileInfoList &outList)
The path of the directory to examine must be passed to this constructor, among the According to the f...
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
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.
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST | |