Threading safe library to simplify managing files for some specific database engines. This library provides relaxing files between the folders out of the box.
- Title
- Build
- Docs
- Usage
- Public methods of DBFS namespace
- void DBFS::set_root(string path)
- void DBFS::set_prefix(string prefix)
- void DBFS::set_suffix(string suffix)
- void DBFS::set_filename_length(int length)
- void DBFS::use_suffix_minutes(bool use)
- std::string DBFS::random_filename()
- DBFS::File* DBFS::create()
- DBFS::File* DBFS::create(std::string name)
- DBFS::File* DBFS::create(DBFS::file_hook_fn on_open, DBFS::file_hook_fn of_close)
- bool DBFS::move(std::string name, std::string new_name)
- bool DBFS::remove(std::string name, bool remove_path)
- bool DBFS::exists(std::string name)
- public methods of
DBFS::Fileclass- DBFS::File()
- DBFS::File(string name)
- DBFS::File(DBFS::file_hook_fn on_open, DBFS::file_hook_fn on_close)
- std::string DBFS::File::name()
- size_t DBFS::File::size()
- bool DBFS::File::open()
- bool DBFS::File::open(std::string name)
- void DBFS::File::close()
- bool DBFS::File::is_open()
- bool DBFS::File::fail()
- bool DBFS::File::move(std::string new_name)
- bool DBFS::File::remove()
- template<typename T> void DBFS::File::read(T& val)
- void DBFS::File::read(char* pos, size_t size)
- template<typename T> void DBFS::File::write(T val)
- void DBFS::File::write(char* pos, size_t size)
- void DBFS::File::seekg(size_t pos)
- void DBFS::File::seekp(size_t pos)
- size_t DBFS::File::tellg()
- size_t DBFS::File::tellp()
- void DBFS::File::on_open(DBFS::file_hook_fn on_open)
- void DBFS::File::on_close(DBFS::file_hook_fn on_close)
- std::fstream& DBFS::File::stream()
- std::mutex& DBFS::File::get_mutex()
- std::lock_guard<std::mutex> get_lock()
- License
Library was tested using GNU G++ compiler with flag -std=c++17. So it is recommended to use C++ 17 or higher version of compiler. Compiling with another compilers might need code corrections.
First of all you have to define your files root folder. You can do it by calling the DBFS::set_root(string) method with path to the root folder as single argument. You can also set up preffix, suffix and other options. You can find all available options below.
Then you can start using methods to create and manipulate files.
Example: Create file, write the data, and delete this file after it is closed.
DBFS::set_root("./tmp");
DBFS::File* file = DBFS::create_file();
file->on_close([](DBFS::File* f){
f->remove();
});
file->write("Hello World!");Methods for setting up the root folder for all files. By default "."
Example:
DBFS::set_root("./tmp");Method for setting up the prefix for your files. This prefix used exclusively for storing files on OS filesystem, and not included to the filename returned by string DBFS::File::name() method. By default ""
Example:
DBFS::set_prefix("f_");
auto f = new DBFS::File("somefilename"); // created as ${root_folder}/so/me/f_somefilename
std::cout << f->name() << std::endl; // somefilenameMethod for setting up the suffix for your files. The rules the same as for prefix. You can use it for setting up file extension. By default ""
Example:
DBFS::set_suffix(".txt");
auto f = new DBFS::File("somefilename"); // created as ${root_folder}/so/me/somefilename.txt
std::cout << f->name() << std::endl; // somefilenameMethod for setting up filename lengths for automatically created files (not including prefix or suffix). By default 10
Example:
DBFS::set_filename_length(8);
auto f = DBFS::create();
std::cout << f->name() << std::endl; // something like "a5ftc5g4"true by default. If active it will add ~ 5 symbols at the end of filename corresponding to the current time in minutes.
Note: We strongly recommend leaving this feature on to avoid any filename collisions
Accepts one string parameter - name of the file and returns full path to the file including prefix, and suffix.
Example:
auto f = DBFS::create();
std::cout << DBFS::get_file_path(f->name()) << std::endl; // something like ${root}/a7/bc/a7bcfsa7wfhgawq8asmReturns filename you can use to create new file with DBFS::create(name) construction.
Example:
DBFS::create(DBFS::random_filename());Creates and opens new file and returns its pointer. Uses DBFS::random_filename() to generate filename.
Creates or opens file with filename name.
Creates and opens new file and set on_open and on_close hooks.
Note: DBFS::file_hook_fn is alias for std::function<void(DBFS::File*)>
Moves file to new direction.
Note: File associated with name you want to move should be closed before moving
Deletes file with name name. if remove_path is set to true (true by default) then if folders are empty, it will remove the folders as well.
Checks whenever file with name exists or not
Default constructor. Creates instance of DBFS::File with no associated filename.
You can use DBFS::File::open(std::string) method to open specific file.
Example:
auto f = new DBFS::File();
f->open("jrhg31hj5h12bf");Creates or opens file with specific name.
Note: It is recommended to use new keyword when creating or opening files with DBFS::File constructors.
Constructor. Match DBFS::create(DBFS::file_hook_fn on_open, DBFS::file_hook_fn on_close). For more details, see corresponding method.
Returns name of associated with current instance file.
Returns size of the associated associated with current instance file.
Opens associated with current instance file. Returns true if file was opened successfully.
Opens file associated with name. Returns true if file was opened successfully.
Closes associated with current instance file.
returns true if file is opened
Returns true if corresponding to current instant std::fstream is in error stated.
Moves associated with current instance file to move location. If current file is opened, it will be closed, moved and then reopened again. If file is closed, it will be just moved.
Deletes associated with current instance file. If file is opened, it will be closed and then deleted.
Reads file content to corresponding variable val. Methods works similar to stringstream operator>>
Example:
auto f = DBFS::create();
f->write("18 24\n");
int a1,a2;
f->read(a1);
f->read(a2);
std::cout << a1 + a2 << std::endl; // 42Reads size bytes from file and put them to char buffer starting at pos
Example:
auto f = DBFS::create();
f->write("123abc");
char* buf = new char[4];
f->read(buf,3);
buf[3] = '\0';
std::cout << buf << std::endl; // 123Writes content of corresponding variable to file. Method works similar to stringstream operator<<.
Writes size bytes to the file from buffer starting at position pos
Moves read pointer to corresponding position.
Example:
auto f = DBFS::create();
f->write("abc42\n");
f->seekg(3);
int a;
f->read(a);
std::cout << a << std::endl; // 42Moves write pointer to corresponding position.
Example:
auto f = DBFS::create();
f->write("F**k this shit I'm out!");
f->seekp(1);
f->write("uc");Returns position of write pointer.
Returns position of read pointer.
Associate function with current instant. Every time file opens this function will run.
Note: There is no way to remove function from hook list yet
Associate function with current file. Every time file closes this function will run.
Example:
auto f = DBFS::create();
f->on_close([](DBFS::File* f){
f->remove();
});Returns reference to associated with current file std::fstream
Returns reference to associated with current file std::mutex so you can block file when reading it from 2 different threads.
Returns lock_guard that uses mutex of current file.
Example:
auto f = DBFS::create("somefilename");
char* buf = new char[1024];
size_t sz = f->size();
auto lock = f->get_lock();
f->seekg(0)
while(sz){
int read_size = std::min(sz,1024);
f->read(buf,read_size);
// Do something with buffer
sz -= read_size;
}MIT
Have fun :)