45#include "../../include/mxException.hpp"
52bool exists(
const std::string &path )
54 return std::filesystem::exists( std::filesystem::path( path ) );
61 std::filesystem::create_directories( path, ec );
62 if( ec.value() != 0 && ec.value() != EEXIST )
70std::string
pathStem(
const std::string &fname )
72 std::filesystem::path p( fname );
73 return p.stem().string();
78 std::filesystem::path p( fname );
79 return p.filename().string();
84 std::filesystem::path p( fname );
85 return p.parent_path().string();
90template <
class verboseT>
91error_t getFileNames( std::vector<std::string> &fileNames,
92 const std::string &directory,
93 const std::string &prefix,
94 const std::string &substr,
95 const std::string &extension )
101 if( std::filesystem::exists( directory ) )
103 if( std::filesystem::is_directory( directory ) )
107 if( extension.size() > 0 )
109 if( extension[0] !=
'.' )
119 bool hasprefix = ( prefix.size() > 0 );
121 bool hassub = ( substr.size() > 0 );
123 std::filesystem::directory_iterator it{ directory };
124 auto it_end = std::filesystem::directory_iterator{};
125 for( it; it != it_end; ++it )
129 if( it->path().extension() != _ext )
135 std::string p = it->path().filename().generic_string();
139 if( p.size() < prefix.size() )
150 if( p.compare( 0, prefix.size(), prefix ) != 0 )
164 size_t sspos = p.find( substr, 1 );
166 if( sspos == std::string::npos )
174 fileNames.push_back( it->path().native() );
177 sort( fileNames.begin(), fileNames.end() );
181 return internal::mxlib_error_report<verboseT>(
error_t::invalidarg, directory +
" is not a directory" );
186 return internal::mxlib_error_report<verboseT>(
error_t::dirnotfound, directory +
" was not found" );
191 catch(
const std::exception &e )
203error_t getFileNames<verbose::o>( std::vector<std::string> &fileNames,
204 const std::string &directory,
205 const std::string &prefix,
206 const std::string &substr,
207 const std::string &extension )
209 return impl::getFileNames<verbose::o>( fileNames, directory, prefix, substr, extension );
213error_t getFileNames<verbose::v>( std::vector<std::string> &fileNames,
214 const std::string &directory,
215 const std::string &prefix,
216 const std::string &substr,
217 const std::string &extension )
219 return impl::getFileNames<verbose::v>( fileNames, directory, prefix, substr, extension );
223error_t getFileNames<verbose::vv>( std::vector<std::string> &fileNames,
224 const std::string &directory,
225 const std::string &prefix,
226 const std::string &substr,
227 const std::string &extension )
229 return impl::getFileNames<verbose::vv>( fileNames, directory, prefix, substr, extension );
233error_t getFileNames<verbose::vvv>( std::vector<std::string> &fileNames,
234 const std::string &directory,
235 const std::string &prefix,
236 const std::string &substr,
237 const std::string &extension )
239 return impl::getFileNames<verbose::vvv>( fileNames, directory, prefix, substr, extension );
244 std::string dir, base, ext;
246 std::filesystem::path p = fname;
247 dir = p.parent_path().string();
248 base = p.stem().string();
249 ext = p.extension().string();
251 return dir +
'/' + prepend + base + append + ext;
265getSequentialFilename(
const std::string &basename,
const std::string &extension,
const int startat,
const int ndigit )
269 int maxdig = pow( 10, ndigit );
272 snprintf( formstr,
sizeof( formstr ),
"%%0%dd", ndigit );
277 std::stringstream outn;
279 snprintf( digstr,
sizeof( digstr ), formstr, i );
285 while( std::filesystem::exists( outn.str() ) && i < maxdig )
290 snprintf( digstr,
sizeof( digstr ), formstr, i );
310 if( ( fstat( fd, &stbuf ) != 0 ) || ( !S_ISREG( stbuf.st_mode ) ) )
315 return stbuf.st_size;
Declarations of utilities for working with files.
error_t
The mxlib error codes.
@ noerror
No error has occurred.
@ dirnotfound
The directory was not found.
@ exception
An exception was thrown.
@ invalidarg
An argument was invalid.
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.
int createDirectories(const std::string &path)
Create a directory or directories.
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.
bool exists(const std::string &path)
Check if a path exists.
off_t fileSize(int fd)
Get the size in bytes of a file.
std::string pathFilename(const std::string &fname)
Get the base filename.