forked from padi-g/bash-on-windows-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.cpp
executable file
·92 lines (76 loc) · 2.18 KB
/
echo.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
92
#include "command.h"
#include "globals.h"
#include <QString>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QCommandLineParser>
using namespace std;
echo::echo(QString input){
cmd = input;
if(cmd.contains(">>")){
this->echo_a();
}
else if (cmd.contains(">")){
this->echo_o();
}
else if(cmd.contains("|")){
this->echo_p();
}
else if(cmd.contains("-h") or cmd.contains("--help") or cmd.contains("-?")){
this->echo_h();
}
else {
QString sub = cmd.mid(5);
output = this->echo_n(sub);
}
out << output <<endl;
}
QString echo::echo_n(QString str){ // n = normal
return str;
}
void echo::echo_a(){
int index = cmd.indexOf(">>");
int length = cmd.length();
QString filename = cmd.right(length - index - 3);
QFile file(filename);
if (!file.exists())
out << "File does not exist!\n";
else
{
//out << index;
QString in = cmd.remove(index, cmd.length());
in = cmd.mid(5);
string s = in.toStdString();
QByteArray putt = s.c_str();
file.open(QIODevice::Append | QIODevice::Text);
file.write(putt);
file.flush();
file.close();
}
}
void echo::echo_o(){
int index = cmd.indexOf(">");
int length = cmd.length();
QString filename = cmd.right(length - index - 2);
QFile file(filename);
if (!file.exists())
out << "File does not exist!\n";
else
{
QString in = cmd.remove(index, length);
in = cmd.mid(5);
string s = in.toStdString();
QByteArray putt = s.c_str();
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(putt);
file.close();
}
}
void echo::echo_p(){
}
void echo::echo_h(){
parse.addHelpOption();
parse.setApplicationDescription("\nOutputs entered standard input back into the program\nUse >> followed by filename to append the standard input to that file\nUse > followed by the filename to overwrite contents of the file by the standard input\n ");
out << parse.helpText() << endl;
}