From cb3b7ff3d9bf41f73ee1506c8a59d4784fd29b7e Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 19 Oct 2024 05:36:22 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + CMakeLists.txt | 16 ++++++++++++ include/filetape.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++ include/tape.h | 50 +++++++++++++++++++++++++++++++++++++ src/filetape.cpp | 28 +++++++++++++++++++++ src/main.cpp | 8 ++++++ 6 files changed, 165 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/filetape.h create mode 100644 include/tape.h create mode 100644 src/filetape.cpp create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c0e634b --- /dev/null +++ b/CMakeLists.txt @@ -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 +) + diff --git a/include/filetape.h b/include/filetape.h new file mode 100644 index 0000000..3c7bd00 --- /dev/null +++ b/include/filetape.h @@ -0,0 +1,62 @@ +#ifndef FILE_TAPE_H +#define FILE_TAPE_H + +#include "tape.h" +#include +#include +#include + +/** +* 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 diff --git a/include/tape.h b/include/tape.h new file mode 100644 index 0000000..05aee3d --- /dev/null +++ b/include/tape.h @@ -0,0 +1,50 @@ +#ifndef TAPE_H +#define TAPE_H + +#include + +/** +* 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 diff --git a/src/filetape.cpp b/src/filetape.cpp new file mode 100644 index 0000000..de478d7 --- /dev/null +++ b/src/filetape.cpp @@ -0,0 +1,28 @@ +#include "filetape.h" +#include +#include +#include + +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(); +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0409909 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,8 @@ +#include "filetape.h" +#include + +int main(int argc, char *argv[]) { + std::unique_ptr tape(new FileTape("test")); + return 0; +} +