libmrbesen/inc/files.h

47 lines
1.9 KiB
C++

#pragma once
#include <functional>
#include <iterator>
#include <string>
namespace mrbesen::files {
void parent(const std::string& child, std::string& out); //get the parent directory of a file or directory path
void file(const std::string& path, std::string& out); //get the filename without the path
void extention(const std::string& path, std::string& ext); //get the files extenetion
enum class FileType : unsigned char {
UNKNOWN = 0,
FIFO = 1,
CHARACTER = 2,
DIRECTORY = 4,
BLOCK = 6,
REGULAR = 8,
LINK = 10,
SOCKET = 12,
WHT = 14 ///????
//copied from dirent.h -> see man readdir
};
//read through a file line by line call the callback with every line (without \n or \r) return true on sccess or false on error
typedef std::function<void(const std::string&)> fileLineCallback;
bool iterateFile(const std::string& filename, fileLineCallback clb, bool ignoreBlanks = true);
bool iterateFile(std::istream& file, fileLineCallback clb, bool ignoreBlanks = true);
//iterate over a folder
typedef std::function<bool(const std::string&, FileType type)> fileNameFilter;
typedef std::function<void(const std::string&, FileType type)> fileCallback;
template<class Container>
bool scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
bool scan(const std::string& path, fileCallback clb, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
bool readFile(const std::string& name, std::ostream& output, unsigned int maxsize = 0); //reads file name and writes it line for line into output, it stops at maxsize (it might be a bit more, its not a hard limit - see it more as a hint), if maxsize is 0 it reads unlimited
bool readFile(int fd, std::ostream& output, unsigned int maxsize = 0);
bool copy(int fd_from, int fd_to);
bool copy(const std::string& from, const std::string& to);
std::string realPath(const std::string& str);
}