39 #include <sys/types.h>
43 #include <boost/filesystem.hpp>
45 using namespace boost::filesystem;
55 boost::system::error_code ec;
56 boost::filesystem::create_directories(path, ec);
57 if(ec.value() != boost::system::errc::success && ec.value() != boost::system::errc::file_exists)
65 std::string
pathStem(
const std::string & fname)
67 boost::filesystem::path p(fname);
68 return p.stem().string();
73 boost::filesystem::path p(fname);
74 return p.filename().string();
79 boost::filesystem::path p(fname);
80 return p.parent_path().string();
85 std::vector<std::string>
getFileNames(
const std::string & directory,
86 const std::string & prefix,
87 const std::string & substr,
88 const std::string & extension
91 typedef std::vector<path> vec;
93 std::vector<std::string> vect;
94 if( exists(directory) )
96 if(is_directory(directory) )
100 copy(directory_iterator(directory), directory_iterator(), back_inserter(v));
102 std::sort(v.begin(), v.end());
106 auto it_end = v.end();
114 if(it->extension() != extension)
120 if(prefix !=
"" && inc)
122 std::string p = it->filename().generic_string();
124 if( p.size() < prefix.size() )
130 if(p.compare(0, prefix.size(), prefix) != 0)
137 if(substr !=
"" && inc)
139 std::string p = it->filename().generic_string();
140 if(p.find(substr) == std::string::npos)
148 vect.push_back(it->native());
156 std::cerr << directory <<
" is not a directory\n";
162 std::cerr <<
"directory " << directory <<
" does not exist\n";
169 const std::string & extension
182 const std::string & prepend,
183 const std::string & append
186 std::string dir, base, ext;
189 dir = p.parent_path().string();
190 base = p.stem().string();
191 ext = p.extension().string();
194 return dir +
'/' + prepend + base + append + ext;
200 const std::string & append
207 const std::string & prepend
214 const std::string & extension,
221 int maxdig = pow(10, ndigit);
224 snprintf(formstr,
sizeof(formstr),
"%%0%dd", ndigit);
229 std::stringstream outn;
231 snprintf(digstr,
sizeof(digstr),formstr, i);
237 while(boost::filesystem::exists(outn.str()) && i < maxdig)
242 snprintf(digstr,
sizeof(digstr), formstr, i);
262 if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode)))
267 return stbuf.st_size;
Declarations of utilities for working with files.
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.
std::vector< std::string > getFileNames(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,...
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.
off_t fileSize(int fd)
Get the size in bytes of a file.
std::string pathFilename(const std::string &fname)
Get the base filename.