yadro-task/include/tapelib/filetape.h

83 lines
2.1 KiB
C
Raw Normal View History

2024-10-19 02:36:22 +00:00
#ifndef FILE_TAPE_H
#define FILE_TAPE_H
#include "tape.h"
#include <cstdint>
#include <fstream>
#include <string>
namespace tape {
/**
* Strcuture for storing all possible settings for a file tape,
* such as read dealy, write delay etc.
*/
struct FileTapeSettings {
unsigned int read_delay_ms;
unsigned int write_delay_ms;
unsigned int seek_forward_delay_ms;
unsigned int seek_backwards_delay_ms;
};
2024-10-19 02:36:22 +00:00
/**
2024-10-21 08:03:12 +00:00
* 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 seperate line in a file,
* cell data is stored as plain text.
*/
2024-10-19 02:36:22 +00:00
class FileTape : public Tape {
private:
std::fstream file;
FileTapeSettings settings;
2024-10-21 08:03:12 +00:00
public:
FileTape &operator=(const FileTape &) = delete;
FileTape(const FileTape &) = delete;
FileTape(FileTape &&) = delete;
FileTape &operator=(FileTape &&) = delete;
~FileTape() override;
2024-10-21 08:03:12 +00:00
/**
* Initialize a new instance of FileTape that will open a file with the
* specified name. File must be read and write accessible.
*
* @param file_name Name of a file to be opened.
* @param settings File tape settings
*/
explicit FileTape(const std::string &file_name, FileTapeSettings settings);
2024-10-21 08:03:12 +00:00
/**
* Advances the underlying fstream to a new line.
*
* @return false if EOF is reached, otherwise true.
*/
bool seek_forward() override;
2024-10-21 08:03:12 +00:00
/**
* 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;
2024-10-21 08:03:12 +00:00
/**
* Reads and parses data from the current line.
*
* @return number from the line, or 0 if data can not be parsed.
*/
int32_t read() override;
2024-10-21 08:03:12 +00:00
/**
* Writes data to the current line.
*
* @param data Number, that should be written to the current line.
*/
void write(int32_t data) override;
2024-10-21 08:03:12 +00:00
};
} // namespace tape
2024-10-19 02:36:22 +00:00
#endif