This repository has been archived by the owner on Jun 22, 2019. It is now read-only.
forked from Moridi/AP-Spring-98-CA-6-Inheritance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/Part-2-FileSystem'
- Loading branch information
Showing
19 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.