Add files via upload

Added project files
This commit is contained in:
Isak 2018-02-12 22:06:21 +01:00 committed by GitHub
parent 9b6074ddd9
commit 7520a1da93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 164 additions and 0 deletions

25
src/Directory.cpp Normal file
View File

@ -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);
}

30
src/Directory.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include <string>
#include <dirent.h>
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

105
src/Main.cpp Normal file
View File

@ -0,0 +1,105 @@
/* Program to navigate directories */
#include <iostream>
#include <fstream> //file I/O
#include <string>
#include <map>
#include <unistd.h> //for get_current_dir() and chdir()
#include "Directory.h"
typedef std::string str_t;
typedef std::pair<int, str_t> pair_t;
typedef std::map<int, str_t> 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: <index, file_name>*/
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;
}

4
src/compile.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
g++ -c Directory.cpp -o Directory.o
g++ Main.cpp Directory.o -o File-viewer