From 033ec0bf7ada15433f5e6fa2fe9fc632f06d161c Mon Sep 17 00:00:00 2001 From: Isak Date: Sat, 17 Feb 2018 21:21:16 +0100 Subject: [PATCH] Update Main.cpp Now added support for specifying file editor and implemented functions from getopt to parse command line arguments. --- src/Main.cpp | 76 +++++++++++++--------------------------------------- 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index b7bf453..5d1536d 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,75 +1,29 @@ /* Program to navigate directories */ #include -#include //file I/O #include #include #include //for get_current_dir() and chdir() -#include "Directory.h" +#include "headers/Functions.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") //ANSI escape sequence to clear tty - -pairmap_t /* Returns a std::map, constructed like the following: */ -display_directory_content() -{ - 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]); + struct info prog_info = parse_command_line(argc, argv); /* Structure defined in headers/Functions.h */ + if ( chdir(prog_info.path) != 0) /* Changing directory was not succesful */ + chdir( get_current_dir_name() ); + CLEAR; - pairmap_t map = display_directory_content(); + std::map map = display_directory_content(); /* Program loop */ - while (true) - { + for(;;) + { std::cout << "\nNavigate directory (0 to quit): "; unsigned option; std::cin >> option; @@ -77,11 +31,19 @@ main(int argc, char *argv[]) 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]); + if ( map[option][0] != '/' ) /* If it doesn't start with a '/', we know it's a file */ + { + if (prog_info.editor != NULL) { + CLEAR; + str_t command = str_t(prog_info.editor) + " " + str_t(map[option]); + if ( system(command.c_str()) == -1 ) /* If opening file with specified editor failed, break */ + break; + } { + CLEAR; + display_file_content(map[option]); + str_t temp; std::cout << "Return (\\n)"; std::cin.ignore(); //Flush newline char