From 7df6269e327ef2023971b1352b5e0ee4787800cb Mon Sep 17 00:00:00 2001 From: Isak Date: Sat, 17 Feb 2018 21:16:00 +0100 Subject: [PATCH] Add files via upload --- headers/Directory.h | 30 ++++++++++++++++ headers/Functions.h | 85 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 headers/Directory.h create mode 100644 headers/Functions.h diff --git a/headers/Directory.h b/headers/Directory.h new file mode 100644 index 0000000..651930c --- /dev/null +++ b/headers/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/headers/Functions.h b/headers/Functions.h new file mode 100644 index 0000000..43c4e1f --- /dev/null +++ b/headers/Functions.h @@ -0,0 +1,85 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +#include +#include +#include +#include +#include "Directory.h" + +struct info +{ + const char *path; + const char *editor; +}; + +info +parse_command_line(int argc, char **argv) +{ + info optinfo {NULL, NULL}; + int opt; + + while ((opt = getopt(argc, argv, "e:p:h")) != -1) + { + switch (opt) { + case 'e': + optinfo.editor = optarg; + break; + case 'p': + optinfo.path = optarg; + break; + case 'h': + std::cout << "A simple program for navigating directories and outputting files.\n"; + std::cout << "Optional switches [-p PATH] [-e EDITOR]\n"; + exit(1); + default: + exit(1); + } + } + return optinfo; +} + +std::map /* Returns a std::map, constructed like the following: */ +display_directory_content() +{ + Directory dir( get_current_dir_name() ); + int count=1; + + std::map content_map; + std::cout << "* Viewing contents of " << get_current_dir_name() << " *\n\n"; + + while ((dir.read()) != NULL) + { + if (dir.type() == Types::DIR) + { + std::string dir_name = "/" + dir.name() + "/"; + std::cout << "* " << count << ": " << dir_name << '\n'; /* Prepend directories with slash */ + content_map.insert(std::pair( count, dir_name )); + } + else if (dir.type() == Types::FILE) + { + std::cout << "* " << count << ": " << dir.name() << '\n'; + content_map.insert(std::pair( count, dir.name() )); + } + count++; + } + + return content_map; +} + +void +display_file_content(const std::string file_name) /* Displays the content of a file */ +{ + std::cout << "* Viewing contents of " << file_name << " *\n\n"; + std::ifstream file(file_name); + + if (file) + { + std::string line; + while (getline(file, line)) + std::cout << line << "\n"; + } + else std::cerr << "* Error viewing contents of " << file_name << " *\n"; +} + +#endif