Initial commit

This commit is contained in:
Egor 2024-10-19 05:36:22 +03:00
commit cb3b7ff3d9
6 changed files with 165 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
build

16
CMakeLists.txt Normal file
View file

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.30)
project(yadro-task VERSION 0.1)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-Wall")
file(GLOB SOURCES "src/*.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
)

62
include/filetape.h Normal file
View file

@ -0,0 +1,62 @@
#ifndef FILE_TAPE_H
#define FILE_TAPE_H
#include "tape.h"
#include <cstdint>
#include <fstream>
#include <string>
/**
* 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;
public:
/**
* 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.
*/
FileTape(std::string file_name);
/**
* 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;
/**
* Closes the underlying fstream
*/
~FileTape() override;
};
#endif

50
include/tape.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef TAPE_H
#define TAPE_H
#include <cstdint>
/**
* Abstract class representing a magnetic tape.
*
* Tape is a storage medium consisting of N cells, where each cell contains a 32-bit signed integer.
* Data can be read or written into one cell at a time. Tape can be moved
* one cell forward or backwards in order to change the targeted cell.
* Tape performs every operation sequentially. The amount of data stored on a single tape
* could exceed the amount of available RAM. Moving tape forward or backwards
* is a very time-consuming operation.
*/
class Tape {
public:
/**
* Advances the tape one cell forward.
*
* @return false if the next cell doesn't exist (reached end of tape), otherwise true.
*/
virtual bool seekForward() = 0;
/**
* Rewinds the tape one cell backwards.
*
* @return false if the previous cell doesn't exist (reached beginning of tape), otherwise true.
*/
virtual bool seekBackwards() = 0;
/**
* Reads data from the current cell.
*
* @return number, that is stored on the current cell.
*/
virtual int32_t read() = 0;
/**
* Writes data to the current cell.
*
* @param data Number, that should be written to the current cell.
*/
virtual void write(int32_t data) = 0;
// default virtual destructor to prevent memory leaks in implementing classes
virtual ~Tape() = default;
};
#endif

28
src/filetape.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "filetape.h"
#include <cstdint>
#include <fstream>
#include <string>
FileTape::FileTape(std::string file_name) {
this->file = std::fstream(file_name);
}
bool FileTape::seekForward() {
return true;
}
bool FileTape::seekBackwards() {
return true;
}
int32_t FileTape::read() {
return 0;
}
void FileTape::write(int32_t data) {
}
FileTape::~FileTape() {
this->file.close();
}

8
src/main.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "filetape.h"
#include <memory>
int main(int argc, char *argv[]) {
std::unique_ptr<Tape> tape(new FileTape("test"));
return 0;
}