libmrbesen/inc/files.h

47 lines
1.9 KiB
C
Raw Permalink Normal View History

2020-09-28 23:49:14 +02:00
#pragma once
2020-10-02 15:54:12 +02:00
#include <functional>
#include <iterator>
2020-09-28 23:49:14 +02:00
#include <string>
2020-10-07 13:03:07 +02:00
namespace mrbesen::files {
2020-09-28 23:49:14 +02:00
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
2020-10-02 15:54:12 +02:00
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
};
2021-02-07 21:25:58 +01:00
//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
2020-10-02 15:54:12 +02:00
typedef std::function<bool(const std::string&, FileType type)> fileNameFilter;
2020-10-02 17:35:48 +02:00
typedef std::function<void(const std::string&, FileType type)> fileCallback;
2020-10-02 15:54:12 +02:00
template<class Container>
bool scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
2020-10-02 17:35:48 +02:00
bool scan(const std::string& path, fileCallback clb, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
2020-10-02 15:54:12 +02:00
2021-02-07 21:25:58 +01:00
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
2020-10-08 10:34:12 +02:00
bool readFile(int fd, std::ostream& output, unsigned int maxsize = 0);
2020-11-03 21:02:16 +01:00
bool copy(int fd_from, int fd_to);
bool copy(const std::string& from, const std::string& to);
2020-12-10 21:09:20 +01:00
std::string realPath(const std::string& str);
2020-09-28 23:49:14 +02:00
}