yadro-task/include/filetape.h
erius 5f9f6d2e92 Added tape namespace to all tape-related declarations
Added tapeutil header and source file for sorting methods
Added FileTapeSettings struct to store delay settings for a file tape
2024-10-22 03:24:13 +03:00

82 lines
2 KiB
C++

#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 readDelayMs;
unsigned int writeDelayMs;
unsigned int seekForwardDelayMs;
unsigned int seekBackwardsDelayMs;
};
/**
* 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.
*/
class FileTape : public Tape {
private:
std::fstream file_;
FileTapeSettings settings_;
public:
FileTape &operator=(const FileTape &) = delete;
FileTape(const FileTape &) = delete;
FileTape(FileTape &&) = delete;
FileTape &operator=(FileTape &&) = delete;
~FileTape() override;
/**
* 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 &fileName, FileTapeSettings settings);
/**
* Advances the underlying fstream to a new line.
*
* @return false if EOF is reached, otherwise true.
*/
bool seekForward() override;
/**
* Rewinds the underlying fstream to a previous line.
*
* @return false if called from the first line of a file, otherwise true.
*/
bool seekBackwards() override;
/**
* 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;
/**
* Writes data to the current line.
*
* @param data Number, that should be written to the current line.
*/
void write(int32_t data) override;
};
} // namespace tape
#endif