Skip to content
This repository has been archived by the owner on Jun 22, 2019. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/Part-2-FileSystem'
Browse files Browse the repository at this point in the history
  • Loading branch information
Moridi committed Apr 25, 2019
2 parents f24ee04 + 95d8d86 commit aaa175e
Show file tree
Hide file tree
Showing 19 changed files with 602 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
*.exe
*.out
*.app

.vscode
75 changes: 75 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
CC = g++
BUILD_DIR = build
SRC_DIR = src
INCLUDE_DIR = include
CFLAGS = -std=c++11 -Wall -Werror -I$(INCLUDE_DIR)

EXECUTABLE_FILE = FileSystem.out

OBJECTS = \
$(BUILD_DIR)/File.o \
$(BUILD_DIR)/Link.o \
$(BUILD_DIR)/Directory.o \
$(BUILD_DIR)/FileSystem.o \
$(BUILD_DIR)/Main.o \

FileSensitivityList = \
$(SRC_DIR)/File.cpp \
$(INCLUDE_DIR)/File.h \
$(INCLUDE_DIR)/File-inl.h \
$(INCLUDE_DIR)/Element.h \
$(INCLUDE_DIR)/Element-inl.h \

DirectorySensitivityList = \
$(SRC_DIR)/Directory.cpp \
$(INCLUDE_DIR)/Directory.h \
$(INCLUDE_DIR)/Directory-inl.h \
$(INCLUDE_DIR)/Element.h \
$(INCLUDE_DIR)/Element-inl.h \

LinkSensitivityList = \
$(SRC_DIR)/Link.cpp \
$(INCLUDE_DIR)/Link.h \
$(INCLUDE_DIR)/Link-inl.h \
$(INCLUDE_DIR)/Element.h \
$(INCLUDE_DIR)/Element-inl.h \

FileSystemSensitivityList = \
$(SRC_DIR)/FileSystem.cpp \
$(INCLUDE_DIR)/FileSystem.h \
$(INCLUDE_DIR)/FileSystem-inl.h \
$(INCLUDE_DIR)/Exception.h \
$(INCLUDE_DIR)/Exception-inl.h\
$(INCLUDE_DIR)/Element.h \
$(INCLUDE_DIR)/Element-inl.h \

MainSensitivityList = \
$(SRC_DIR)/Main.cpp \
$(INCLUDE_DIR)/FileSystem.h \

all: $(BUILD_DIR) $(EXECUTABLE_FILE)

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

$(BUILD_DIR)/File.o: $(FileSensitivityList)
$(CC) $(CFLAGS) -c $(SRC_DIR)/File.cpp -o $(BUILD_DIR)/File.o

$(BUILD_DIR)/Directory.o: $(DirectorySensitivityList)
$(CC) $(CFLAGS) -c $(SRC_DIR)/Directory.cpp -o $(BUILD_DIR)/Directory.o

$(BUILD_DIR)/Link.o: $(LinkSensitivityList)
$(CC) $(CFLAGS) -c $(SRC_DIR)/Link.cpp -o $(BUILD_DIR)/Link.o

$(BUILD_DIR)/FileSystem.o: $(FileSystemSensitivityList)
$(CC) $(CFLAGS) -c $(SRC_DIR)/FileSystem.cpp -o $(BUILD_DIR)/FileSystem.o

$(BUILD_DIR)/Main.o: $(MainSensitivityList)
$(CC) $(CFLAGS) -c $(SRC_DIR)/Main.cpp -o $(BUILD_DIR)/Main.o

$(EXECUTABLE_FILE): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) -o $(EXECUTABLE_FILE)

.PHONY: clean
clean:
rm -rf $(BUILD_DIR) *.o *.out
31 changes: 31 additions & 0 deletions include/Directory-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef DIRECTORY_INL_H_
#define DIRECTORY_INL_H_

#ifndef DIRECTORY_H_
#error "Directory-inl.h" should be included only in "Directory.h" file.
#endif

#include "Directory.h"

#include "Exception.h"

Directory::Directory(int _id, std::string _title, int _parent_id) noexcept
: Element(_id, _title, _parent_id)
{
}

std::string Directory::get_type() const
{
return "Directory";
}

void Directory::check_new_element_validity(std::string title) throw()
{
constexpr int FIRST_ELEMENT = 0;

for (uint i = FIRST_ELEMENT; i < elements.size(); ++i)
if (elements[i]->has_same_title(title))
throw BadTitle();
}

#endif
26 changes: 26 additions & 0 deletions include/Directory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef DIRECTORY_H_
#define DIRECTORY_H_

#include <memory>
#include <vector>

#include "Element.h"

class Directory : public Element
{
public:
typedef std::shared_ptr<Element> ElementSharedPointer;

inline Directory(int _id, std::string _title, int _parent_id) noexcept;
virtual void view() override;
inline virtual std::string get_type() const override;
inline void check_new_element_validity(std::string title) throw();
void add_element(ElementSharedPointer new_element) throw();

private:
std::vector<ElementSharedPointer> elements;
};

#include "Directory-inl.h"

#endif
34 changes: 34 additions & 0 deletions include/Element-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef ELEMENT_INL_H_
#define ELEMENT_INL_H_

#ifndef ELEMENT_H_
#error "Element-inl.h" should be included only in "Element.h" file.
#endif

#include "Element.h"

#include <string>

Element::Element(int _id, std::string _title, int _parent_id) noexcept
: id(_id),
title(_title),
parent_id(_parent_id)
{
}

bool Element::has_same_id(int _id) const
{
return id == _id;
}

std::string Element::get_title() const
{
return title;
}

bool Element::has_same_title(std::string _title)
{
return title == _title;
}

#endif
26 changes: 26 additions & 0 deletions include/Element.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ELEMENT_H_
#define ELEMENT_H_

#include <string>

class Element
{
public:
inline Element(int _id, std::string _title, int _parent_id) noexcept;

virtual void view() = 0;
virtual std::string get_type() const = 0;

inline std::string get_title() const;
inline bool has_same_id(int _id) const;
inline bool has_same_title(std::string _title);

protected:
int id;
std::string title;
int parent_id;
};

#include "Element-inl.h"

#endif
30 changes: 30 additions & 0 deletions include/Exception-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef EXCEPTION_INL_H_
#define EXCEPTION_INL_H_

#ifndef EXCEPTION_H_
#error "Exception-inl.h" should be included only in "Exception.h" file.
#endif

#include "Exception.h"

inline const char* BadElementId::what() const throw()
{
return "Invalid element ID requested!";
}

inline const char* IdAlreadyExists::what() const throw()
{
return "Requested ID already exists!";
}

inline const char* BadParentId::what() const throw()
{
return "Parent ID is not referring to a directory!";
}

inline const char* BadTitle::what() const throw()
{
return "Parent directory has the same child!";
}

#endif
32 changes: 32 additions & 0 deletions include/Exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef EXCEPTION_H_
#define EXCEPTION_H_

#include <exception>

class BadElementId : public std::exception
{
public:
inline const char* what() const throw();
};

class IdAlreadyExists : public std::exception
{
public:
inline const char* what() const throw();
};

class BadParentId : public std::exception
{
public:
inline const char* what() const throw();
};

class BadTitle : public std::exception
{
public:
inline const char* what() const throw();
};

#include "Exception-inl.h"

#endif
24 changes: 24 additions & 0 deletions include/File-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef FILE_INL_H_
#define FILE_INL_H_

#ifndef FILE_H_
#error "File-inl.h" should be included only in "File.h" file.
#endif

#include "File.h"

#include "Exception.h"

File::File(int _id, std::string _title,
int _parent_id, std::string _content) noexcept
: Element(_id, _title, _parent_id),
content(_content)
{
}

std::string File::get_type() const
{
return "File";
}

#endif
23 changes: 23 additions & 0 deletions include/File.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef FILE_H_
#define FILE_H_

#include <memory>
#include <vector>

#include "Element.h"

class File : public Element
{
public:
inline File(int _id, std::string _title, int _parent_id,
std::string _content) noexcept;
virtual void view() override;
inline virtual std::string get_type() const override;

private:
std::string content;
};

#include "File-inl.h"

#endif
61 changes: 61 additions & 0 deletions include/FileSystem-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef FILE_SYSTEM_INL_H_
#define FILE_SYSTEM_INL_H_

#ifndef FILE_SYSTEM_H_
#error "FileSystem-inl.h" should be included only in "FileSystem.h" file.
#endif

#include "FileSystem.h"

#include <memory>

#include "File.h"
#include "Directory.h"
#include "Link.h"
#include "Exception.h"

FileSystem::FileSystem()
{
constexpr int ROOT_ID = 0;
constexpr char ROOT_NAME[] = "root";

elements.push_back(std::make_shared<Directory>(
Directory(ROOT_ID, ROOT_NAME, ROOT_ID)));
}

FileSystem::ElementSharedPointer FileSystem::get_element(int id) const throw()
{
constexpr int FIRST_ELEMENT = 0;

for (uint i = FIRST_ELEMENT; i < elements.size(); ++i)
if (elements[i]->has_same_id(id))
return elements[i];

throw BadElementId();
}

void FileSystem::check_id_validity(int id) const throw()
{
constexpr int FIRST_ELEMENT = 0;

for (uint i = FIRST_ELEMENT; i < elements.size(); ++i)
if (elements[i]->has_same_id(id))
throw IdAlreadyExists();
}

void FileSystem::check_parent_id_validity(int id) const throw()
{
constexpr int FIRST_ELEMENT = 0;
constexpr char DIRECTORY[] = "Directory";

for (uint i = FIRST_ELEMENT; i < elements.size(); ++i)
if (elements[i]->has_same_id(id))
{
if (elements[i]->get_type() != DIRECTORY)
throw BadParentId();
else
break;
}
}

#endif
Loading

0 comments on commit aaa175e

Please sign in to comment.