-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.cpp
102 lines (87 loc) · 2.92 KB
/
cmdline.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
93
94
95
96
97
98
99
100
101
102
#include "cmdline.h"
#include <string.h>
#include "crawler.h"
#include "terminal.h"
#include "tree.h"
#include <QDebug>
CommandLineInterPreter::CommandLineInterPreter()
{
}
CommandLineInterPreter &CommandLineInterPreter::getInstance()
{
static CommandLineInterPreter instance;
return instance;
}
void CommandLineInterPreter::list(QStringList params)
{
if(QString::compare(params[1], "-f", Qt::CaseInsensitive) == 0)
{
QStringList fileList = Crawler::getInstance().listFiles();
Terminal::getInstance().writeLine("List of files in the directory :");
for (int i = 0; i < fileList.size(); ++i) {
Terminal::getInstance().writeLine(fileList[i]);
}
}
else if(QString::compare(params[1], "-l", Qt::CaseInsensitive) == 0)
{
QStringList fileList = Crawler::getInstance().listOfCrawledFiles();
Terminal::getInstance().writeLine("List of files crawled into the dictionary :");
for (int i = 0; i < fileList.size(); ++i) {
Terminal::getInstance().writeLine(fileList[i]);
}
}
else if(QString::compare(params[1], "-w", Qt::CaseInsensitive) == 0)
{
QStringList wordList = TreeObject::getInstance().listAllWords();
Terminal::getInstance().writeLine("List of all the words in the dictionary : ");
for (int i = 0; i < wordList.count(); ++i) {
Terminal::getInstance().writeLine(wordList.at(i));
}
}
}
void CommandLineInterPreter::del(QStringList params)
{
}
void CommandLineInterPreter::add(QStringList params)
{
QString filePath = QString("%1/%2").arg(Crawler::getInstance().getDir()).arg(params[1]);
if(Crawler::getInstance().isFileExistedInList(filePath))
{
Terminal::getInstance().writeError(QString("err : File :\"%1\" already exists. You may want to update.").arg(filePath));
}
else
{
qDebug() << filePath;
Crawler::getInstance().add_file_to_list(filePath);
}
}
void CommandLineInterPreter::interpreteCommand(QString cmd)
{
QStringList tokens = cmd.split(' ');
if(QString::compare(tokens[0], QString("list"), Qt::CaseInsensitive) == 0)
{
list(tokens);
}
else if(QString::compare(tokens[0], QString("add"), Qt::CaseInsensitive) == 0)
{
add(tokens);
}
else if(QString::compare(tokens[0], QString("list"), Qt::CaseInsensitive) == 0)
{
}
else if(QString::compare(tokens[0], QString("search"), Qt::CaseInsensitive) == 0)
{
search(tokens);
}
}
void CommandLineInterPreter::search(QStringList params)
{
if(QString::compare(params[1], "-w", Qt::CaseInsensitive) == 0)
{
QStringList fileList = TreeObject::getInstance().searchWord(params[2]);
Terminal::getInstance().writeLine(QString("List of files that has the word %1 :").arg(params[2]));
for (int i = 0; i < fileList.size(); ++i) {
Terminal::getInstance().writeLine(fileList[i]);
}
}
}