-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat.cpp
executable file
·91 lines (78 loc) · 2.6 KB
/
cat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "command.h"
#include <QFile>
#include <QDir>
#include <QTextStream>
#include "globals.h"
#include <QCommandLineParser>
using namespace std;
cat::cat(QString input, int b){
cmd = input;
if(cmd.contains(" -h") or cmd.contains(" --help") or cmd.contains(" -?"))
this->cat_h();
else if(cmd.contains(">> "))
this->cat_a();
else if (cmd.contains("> "))
this->cat_o();
else
this->cat_n(b);
}
void cat::cat_h(){
parse.addHelpOption();
parse.setApplicationDescription("\nThis command extracts the contents of the specified file and displays it as standard output\nUse > followed by a filename to overwrite the contents of that file by the standard output of the current command\nUse >> followed by a filename to append the standard output of the current command into that file\nUse \'|\' to pipe the standard output to another command\n");
out << parse.helpText() << endl;
}
void cat::cat_o(){
index = cmd.indexOf(">");
length = cmd.length();
filename = cmd.right(length - index - 2);
QFile file(filename);
QString in = cmd.remove(index, length);
in = cmd.mid(4);
if (!file.exists())
out << "File does not exist!\n";
else
{
QFile reader(in);
reader.open(QIODevice::ReadOnly|QIODevice::Text);
QByteArray putt = reader.readAll();
out << putt << endl;
reader.close();
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.flush();
file.write(putt);
file.close();
}
}
void cat::cat_a(){
index = cmd.indexOf(">>");
length = cmd.length();
QString filename = cmd.right(length - index - 3);
QFile file(filename);
if (!file.exists())
out << "File does not exist!\n";
else
{
QString in = cmd.remove(index, length);
in = cmd.mid(4);
QFile reader(in);
reader.open(QIODevice::ReadOnly);
QByteArray putt = reader.readAll();
QString disp = putt;
out << disp << endl;
reader.close();
file.open(QIODevice::Append | QIODevice::Text);
file.write(putt);
file.close();
}
}
void cat::cat_n(int b){
if(!b)
filename = cmd.mid(4);
else
filename = cmd;
QFile file(filename);
file.open(QIODevice::ReadOnly);
output = file.readAll();
out << output << endl;
file.close();
}