30#ifndef ioutils_fileUtils_hpp
31#define ioutils_fileUtils_hpp
45#ifndef ioutils_fileUtils_detail_hpp
46#define ioutils_fileUtils_detail_hpp
47namespace fileUtilsDetail
60using operationHookT = void ( * )( operation );
63operationHookT &operationHook();
66using errorCodeHookT = void ( * )( operation, std::error_code & );
69errorCodeHookT &errorCodeHook();
75#ifdef MXLIBTEST_NAMESPACE
76namespace MXLIBTEST_NAMESPACE
95template <
class verboseT>
103template <
class verboseT = verbose::d>
104bool exists(
const std::string &path,
113template <
class verboseT = verbose::d>
130std::string
pathStem(
const std::string &fname );
142std::string
parentPath(
const std::string &fname );
155template <
class verboseT = verbose::d>
157 const std::string &directory,
159 const std::string &prefix,
162 const std::string &substr,
165 const std::string &extension
176 const std::string &prepend,
178 const std::string &append
188 const std::string &append
198 const std::string &prepend
215 const std::string &extension =
"",
217 const int startat = 0,
248template <
class verboseT>
253 fileUtilsDetail::operationHook()( fileUtilsDetail::operation::stringToPath );
258 catch(
const std::bad_alloc &e )
261 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS )
268 catch(
const std::filesystem::filesystem_error &e )
271 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined(MXLIB_CATCH_NONALLOC_EXCEPTIONS)
278 catch(
const std::exception &e )
281 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined(MXLIB_CATCH_NONALLOC_EXCEPTIONS)
291 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined(MXLIB_CATCH_NONALLOC_EXCEPTIONS)
300template <
class verboseT>
305 std::filesystem::path path;
322 bool ex = std::filesystem::exists( path, ec );
324 if( ec.value() != 0 )
341template <
class verboseT>
346 std::filesystem::path path;
363 bool exists = std::filesystem::exists( path, ec );
366 #ifdef MXLIBTEST_DIREXISTSIS_ISEXISTSERR
367 ec = std::error_code( EEXIST, std::system_category() );
371 if( ec.value() != 0 )
389 bool isdir = std::filesystem::is_directory( path, ec );
392 #ifdef MXLIBTEST_DIREXISTSIS_ISDIRERR
393 ec = std::error_code( EACCES, std::system_category() );
397 if( ec.value() != 0 )
414template <
class verboseT>
416 const std::string &directory,
417 const std::string &prefix,
418 const std::string &substr,
419 const std::string &extension )
423 fileUtilsDetail::operationHook()( fileUtilsDetail::operation::getFileNames );
426 if( std::filesystem::exists( directory ) )
428 if( std::filesystem::is_directory( directory ) )
432 if( extension.size() > 0 )
434 if( extension[0] !=
'.' )
444 bool hasprefix = ( prefix.size() > 0 );
446 bool hassub = ( substr.size() > 0 );
448 std::filesystem::directory_iterator it{ directory };
449 auto it_end = std::filesystem::directory_iterator{};
450 for( it; it != it_end; ++it )
454 if( it->path().extension() != _ext )
460 std::string p = it->path().filename().generic_string();
464 if( p.size() < prefix.size() )
475 if( p.compare( 0, prefix.size(), prefix ) != 0 )
489 size_t sspos = p.find( substr, 1 );
491 if( sspos == std::string::npos )
499 fileNames.push_back( it->path().native() );
502 std::sort( fileNames.begin(), fileNames.end() );
516 catch(
const std::bad_alloc &e )
519 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS )
526 catch(
const std::filesystem::filesystem_error &e )
529 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined( MXLIB_CATCH_NONALLOC_EXCEPTIONS )
536 catch(
const std::exception &e )
539 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined( MXLIB_CATCH_NONALLOC_EXCEPTIONS )
549 #if defined( MXLIB_CATCH_ALL_EXCEPTIONS ) || defined( MXLIB_CATCH_NONALLOC_EXCEPTIONS )
558#ifdef MXLIBTEST_NAMESPACE
Augments an exception with the source file and line.
error_t code() const
Get the error code.
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.
@ std_exception
An exception was thrown.
@ dirnotfound
The directory was not found.
@ exception
An exception was thrown.
@ std_bad_alloc
A bad allocation exception was thrown.
@ filesystem
A general filesystem error occurred.
@ invalidarg
An argument was invalid.
@ std_filesystem_error
A filesystem error exception was thrown.
@ error
A general error has occurred.
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
error_t getFileNames(std::vector< std::string > &fileNames, const std::string &directory, const std::string &prefix, const std::string &substr, const std::string &extension)
Get a list of file names from the specified directory, specifying a prefix, a substring to match,...
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.
error_t string2path(std::filesystem::path &path, const std::string &str)
Convert a string to a path, handling exceptions.
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.
Declarations of some libarary wide utilities.