From 7520a1da93847ae2f4d12f395a866d1e354c02ee Mon Sep 17 00:00:00 2001 From: Isak Date: Mon, 12 Feb 2018 22:06:21 +0100 Subject: [PATCH] Add files via upload Added project files --- src/Directory.cpp | 25 +++++++++++ src/Directory.h | 30 +++++++++++++ src/Main.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++ src/compile.sh | 4 ++ 4 files changed, 164 insertions(+) create mode 100644 src/Directory.cpp create mode 100644 src/Directory.h create mode 100644 src/Main.cpp create mode 100644 src/compile.sh diff --git a/src/Directory.cpp b/src/Directory.cpp new file mode 100644 index 0000000..b6d57f6 --- /dev/null +++ b/src/Directory.cpp @@ -0,0 +1,25 @@ +#include "Directory.h" + +Directory::Directory(std::string pathname) + : pathname_m(pathname) +{ + dir = opendir(pathname_m.c_str()); +} +Directory::~Directory() +{ + closedir(dir); +} +dirent* Directory::read() +{ + ent = readdir(dir); return ent; +} +std::string Directory::name() +{ + return ent->d_name; +} +Types Directory::type() +{ + return ((ent->d_type == 4) ? Types::DIR : Types::FILE); +} + + diff --git a/src/Directory.h b/src/Directory.h new file mode 100644 index 0000000..651930c --- /dev/null +++ b/src/Directory.h @@ -0,0 +1,30 @@ +#ifndef DIRECTORY_H +#define DIRECTORY_H + +#include +#include + +enum class Types +{ + DIR, + FILE, +}; + +class Directory +{ +private: + DIR *dir; + const std::string pathname_m; + dirent *ent; +public: + Directory(std::string pathname); + ~Directory(); + + /* Class functions */ + dirent* read(); + std::string name(); + Types type(); +}; + +#endif + diff --git a/src/Main.cpp b/src/Main.cpp new file mode 100644 index 0000000..56ddbc8 --- /dev/null +++ b/src/Main.cpp @@ -0,0 +1,105 @@ +/* Program to navigate directories */ +#include +#include //file I/O +#include +#include +#include //for get_current_dir() and chdir() +#include "Directory.h" + +typedef std::string str_t; +typedef std::pair pair_t; +typedef std::map pairmap_t; + +/* Clearing the tty */ +#define CLEAR (std::cout << "\033[2J\033[;H") // \033[2J\033[;H is an ANSI escape sequence to clear the screen, ANSI is supported on most OS's + + +pairmap_t /* Returns a std::map, constructed like the following: */ +display_directory_content() +{ + //chdir(path.c_str()); + Directory dir(get_current_dir_name()); + int count=1; + + pairmap_t content_map; + std::cout << "* Viewing contents of " << get_current_dir_name() << " *\n\n"; + + while ((dir.read()) != NULL) + { + if (dir.type() == Types::DIR) + { + str_t dir_name = "/" + dir.name() + "/"; + std::cout << "* " << count << ": " << dir_name << '\n'; /* Prepend directories with slash */ + content_map.insert(pair_t( count, dir_name )); + } + else if (dir.type() == Types::FILE) + { + std::cout << "* " << count << ": " << dir.name() << '\n'; + content_map.insert(pair_t( count, dir.name() )); + } + count++; + } + + return content_map; +} + +void +display_file_content(const str_t file_name) /* Displays the content of a file */ +{ + std::cout << "* Viewing contents of " << file_name << " *\n\n"; + std::ifstream file(file_name); + + if (file) + { + str_t line; + while (getline(file, line)) + std::cout << line << "\n"; + } + else std::cerr << "* Error viewing contents of " << file_name << " *\n"; +} + +int +main(int argc, char *argv[]) +{ + if (!argv[1]) + chdir( get_current_dir_name() ) ; /* If argv is not specified we will then view the contents of current working directory */ + else chdir(argv[1]); + + CLEAR; + pairmap_t map = display_directory_content(); + + /* Program loop */ + while (true) + { + std::cout << "\nNavigate directory (0 to quit): "; + unsigned option; + std::cin >> option; + + if ( option == 0 ) break; + if ( option > map.size() ) {std::cerr << "Invalid input"; continue;} + + if ( map[option][0] != '/' ) { /* If it doesn't start with a '/', we know it's a file */ + CLEAR; + display_file_content(map[option]); + + { + str_t temp; + std::cout << "Return (\\n)"; + std::cin.ignore(); //Flush newline char + getline(std::cin, temp); + } + + CLEAR; + display_directory_content(); + continue; + } + else + { + str_t path = get_current_dir_name() + map [option]; chdir( path.c_str() ); //Change current directory + CLEAR; + map = display_directory_content(); + } + } + + return 0; +} diff --git a/src/compile.sh b/src/compile.sh new file mode 100644 index 0000000..bd79b07 --- /dev/null +++ b/src/compile.sh @@ -0,0 +1,4 @@ +#!/bin/bash +g++ -c Directory.cpp -o Directory.o +g++ Main.cpp Directory.o -o File-viewer +