Skip to content

Simple C++ class which manages the interaction with system files, e.g. txt files.

License

Notifications You must be signed in to change notification settings

RomanHein/FileManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Preamble

FileManager is a line-based and crash-safe file manipulation tool for C++17. I began the project mainly for the reason of attempting to learn how to handle files. This repository was created to share the knowledge I've gathered in one simple to use class. The class aims to stay user-friendly while possessing impressive features. It is not a replacement for SQLite or designed for heavy workloads.

Selling points

  • Very beginner/user friendly
  • Line-based (deleting a line shifts all later lines down)
  • Crash resilient, able to recover most data after a crash
  • Prevents file corruption by atomic rewrites
  • Very memory efficient
  • Extremely efficient line deletes
  • File will stay humanly readable
  • Standalone library

When should I use this?

Use it when:

  • You need simple, persistent storage
  • Data loss must be avoided
  • File are small (KBs - MBs, not GBs)
  • Human readable files are desired
  • You don't want gaps inside your file (empty lines)

Do not use it when:

  • Files are very large
  • You need complex queries
  • You need concurrent access

Installation

  1. Download the latest package
  2. Make sure to use C++17 and a 64-bit system
  3. Extract the header file (include/filemanager.h) into your project folder
  4. Include it: #include "filemanager.h"

FAQ

How much RAM does it use?

Roughly 16 bytes per line plus 80-120 bytes per unsaved change. Memory usage is predictable and scales linearly with file size.

Is it thread safe?

No. It was designed for single-threaded usage.

What happens on crash?

All flushed changes will be recovered automatically on the next startup. Unflushed changes are lost.

Why is there a _journal.log file in the same directory as the file?

DO NOT TOUCH OR DELETE THE JOURNAL FILE! That file is responsible for recovering lost data in the case of a crash. Modifying its contents can lead to issues in restoring lost data.

What is kept inside the journal?

The journal's purpose is to record method calls during runtime. In order to do that, it'll have to serialize method calls in a way that is able to be verified later when replaying. The journal's entries all follow the same structure, namely: (instruction)(arguments) The arguments are always saved in this way: (argument length)(argument) in order to be able to verify whether the extracted argument is invalid or not. Unless you know what you're doing, it's highly advised to keep your hands off of the journal!

Documentation

Method Description Note
FileManager(file path) Creates a new FileManager instance that manages the specified file
read(index) Returns the text at the specified row
front() Returns a copy of the text at the first row
back() Returns a copy of the text at the last row
all() Returns a copy of the text at every row
append(args) Converts the given arguments into text and appends it to the file
overwrite(index, args) Converts the given arguments into text and overwrites the specified line
erase(index) Deletes the specified line Deleting a line shifts all later lines down
clear() Deletes entire text in the file
flush() Records all changes, making recovery possible Does not apply changes to the file
commit() Applies all changes to the file Very expensive if file is big, use wisely
empty() Returns true if there are no present lines
size() Returns the number of present lines

About

Simple C++ class which manages the interaction with system files, e.g. txt files.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages