49namespace fileUtilsDetail
55void noOperationFailure( operation )
59void noErrorCodeFailure( operation, std::error_code & )
65operationHookT &operationHook()
67 static operationHookT hook = noOperationFailure;
71errorCodeHookT &errorCodeHook()
73 static errorCodeHookT hook = noErrorCodeFailure;
87 std::filesystem::create_directories( path, ec );
88 fileUtilsDetail::errorCodeHook()( fileUtilsDetail::operation::createDirectories, ec );
89 if( ec.value() != 0 && ec.value() != EEXIST )
104 std::filesystem::path p( fname );
105 return p.stem().string();
110 std::filesystem::path p( fname );
111 return p.filename().string();
116 std::filesystem::path p( fname );
117 return p.parent_path().string();
120template error_t getFileNames<verbose::d>( std::vector<std::string> &fileNames,
121 const std::string &directory,
122 const std::string &prefix,
123 const std::string &substr,
124 const std::string &extension );
128 std::string dir, base, ext;
130 std::filesystem::path p = fname;
131 dir = p.parent_path().string();
132 base = p.stem().string();
133 ext = p.extension().string();
135 return dir +
'/' + prepend + base + append + ext;
149getSequentialFilename(
const std::string &basename,
const std::string &extension,
const int startat,
const int ndigit )
153 int maxdig = pow( 10, ndigit );
156 snprintf( formstr,
sizeof( formstr ),
"%%0%dd", ndigit );
161 std::stringstream outn;
163 snprintf( digstr,
sizeof( digstr ), formstr, i );
169 while( std::filesystem::exists( outn.str() ) && i < maxdig )
174 snprintf( digstr,
sizeof( digstr ), formstr, i );
194 if( ( fstat( fd, &stbuf ) != 0 ) || ( !S_ISREG( stbuf.st_mode ) ) )
199 return stbuf.st_size;
Declarations of utilities for working with files.
error_t
The mxlib error codes.
static constexpr error_t errno2error_t(const int &err)
Convert an errno code to error_t.
@ noerror
No error has occurred.
@ filesystem
A general filesystem error occurred.
@ error
A general error has occurred.
std::string fileNamePrepend(const std::string &fname, const std::string &prepend)
Prepend strings to a file name, leaving the directory and extension unaltered.
std::string getSequentialFilename(const std::string &basename, const std::string &extension="", const int startat=0, int ndigit=4)
Get the next file in a numbered sequence.
std::string fileNamePrependAppend(const std::string &fname, const std::string &prepend, const std::string &append)
Prepend and/or append strings to a file name, leaving the directory and extension unaltered.
std::string fileNameAppend(const std::string &fname, const std::string &append)
Append a string to a file name, leaving the directory and extension unaltered.
bool exists(const std::string &path, mx::error_t &errc)
Check if a path exists.
error_t createDirectories(const std::string &path)
Create a directory or directories.
bool dir_exists_is(const std::string &dir, mx::error_t &errc)
Check if a path exists and is a directory.
std::string parentPath(const std::string &fname)
Get the parent path from a filename.
std::string pathStem(const std::string &fname)
Get the stem of the filename.
off_t fileSize(int fd)
Get the size in bytes of a file.
std::string pathFilename(const std::string &fname)
Get the base filename.