erius
f3aaa26df8
Changed file format for a FileTape - added and example in the class comment Removed prev_line_pos and at_first_line fields from FieldTape Changed Tape data type from int32_t to uint32_t Cleand up includes Implemented FileTape methods Unit tests are in working state
123 lines
3.6 KiB
C++
123 lines
3.6 KiB
C++
#ifndef FILE_TAPE_H
|
|
#define FILE_TAPE_H
|
|
|
|
#include "tape.h"
|
|
#include <chrono>
|
|
#include <fstream>
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
namespace tape {
|
|
|
|
/**
|
|
* Strcuture for storing all possible settings for a file tape,
|
|
* such as read dealy, write delay etc.
|
|
*/
|
|
struct FileTapeSettings {
|
|
milliseconds read_delay;
|
|
milliseconds write_delay;
|
|
milliseconds seek_forward_delay;
|
|
milliseconds seek_backwards_delay;
|
|
};
|
|
|
|
const static struct FileTapeSettings FT_DEFAULT_SETTINGS =
|
|
FileTapeSettings{.read_delay = milliseconds(0),
|
|
.write_delay = milliseconds(0),
|
|
.seek_forward_delay = milliseconds(0),
|
|
.seek_backwards_delay = milliseconds(0)};
|
|
|
|
const static char FT_DELIMETER = '\n';
|
|
|
|
// uint32_t digits max count is 10
|
|
const static int FT_CELL_SIZE = 10;
|
|
const static int FT_SEEK_OFFSET = FT_CELL_SIZE + 1;
|
|
|
|
/**
|
|
* Mock tape implementation that uses text files as data source.
|
|
*
|
|
* Simulates tape operations by artificially pausing the execution of some
|
|
* methods using time intervals, specified in command line arguments or a
|
|
* configuration file. Each cell is represented as a plain text 32-bit unsigned
|
|
* int, padded to FT_CELL_SIZE with leading zeroes, separated by FT_DELIMETER.
|
|
* Each cell MUST be FT_CELL_SIZE long to correctly implement write operation
|
|
* without re-writing the entire file. FT_DELIMETER
|
|
*
|
|
* Example of a valid file for a FileTape
|
|
* - example.txt
|
|
* 000000001
|
|
* 000000034
|
|
* 000001234
|
|
* 123456789
|
|
* 000000000
|
|
*/
|
|
class FileTape : public Tape {
|
|
private:
|
|
std::fstream file;
|
|
FileTapeSettings settings;
|
|
|
|
public:
|
|
/**
|
|
* Initializes a new instance of FileTape that will open an existing file
|
|
* with the specified name, already filled with data. File must be read and
|
|
* write accessible.
|
|
*
|
|
* @param file_name Name of a file to be opened.
|
|
* @param settings FileTape settings
|
|
*/
|
|
explicit FileTape(const std::string &file_name,
|
|
FileTapeSettings settings = FT_DEFAULT_SETTINGS);
|
|
|
|
/**
|
|
* Initializes a new instance of FileTape that will create and open a file
|
|
* with the specified name, and write *size* zeroes into it.
|
|
* File must not exist and directory must be write accessible.
|
|
*
|
|
* @param size Size of a newly created FileTpae
|
|
* @param file_name Name of a file to be created and opened
|
|
* @param settings FileTape settings
|
|
*/
|
|
explicit FileTape(size_t size, const std::string &file_name,
|
|
FileTapeSettings settings = FT_DEFAULT_SETTINGS);
|
|
|
|
/**
|
|
* Initializes a new instance of FileTape that will create and open a
|
|
* temporary file, and write *size* zeroes into it.
|
|
*
|
|
* @param size Size of a newly created FileTape
|
|
* @param settings FileTape settings
|
|
*/
|
|
explicit FileTape(size_t size,
|
|
FileTapeSettings settings = FT_DEFAULT_SETTINGS);
|
|
|
|
/**
|
|
* Advances the underlying fstream to a new line.
|
|
*
|
|
* @return false if empty line is reached, otherwise true.
|
|
*/
|
|
bool seek_forward() override;
|
|
|
|
/**
|
|
* Rewinds the underlying fstream to a previous line.
|
|
*
|
|
* @return false if called from the first line of a file, otherwise true.
|
|
*/
|
|
bool seek_backwards() override;
|
|
|
|
/**
|
|
* Reads and parses data from the current line.
|
|
*
|
|
* @return number from the line, or 0 if data can not be parsed.
|
|
*/
|
|
uint32_t read() override;
|
|
|
|
/**
|
|
* Writes data to the current line.
|
|
*
|
|
* @param data Number, that should be written to the current line.
|
|
*/
|
|
void write(uint32_t data) override;
|
|
};
|
|
|
|
} // namespace tape
|
|
|
|
#endif
|