Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/controller/DreeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
void DreeController::print_dree(Args* args) {
DreeNode* rootNode = DreeLoaderInterface->load_dree(args);
DreePrinterInterface->depth_first_search(rootNode, args->MaxDepth);

delete rootNode;
rootNode = nullptr;
}

DreeController::DreeController(DreeLoaderI* DreeLoader, PrintDreeI* DreePrinter) {
Expand Down
1 change: 1 addition & 0 deletions src/controller/DreeControllerI.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace std;
class DreeControllerI {
public:
virtual void print_dree(Args* args) = 0;
virtual ~DreeControllerI() = default;
};

#endif
1 change: 1 addition & 0 deletions src/controller/HelpControllerI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class HelpControllerI {
public:
virtual void help() = 0;
virtual ~HelpControllerI() = default;
};

#endif
3 changes: 3 additions & 0 deletions src/controller/SearchController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ void SearchController::search(string& query, Args* args) {
DreeNode* root = new DreeNode(args->currentPath);
auto searchResult = searchModel->search(root, query, dreeHelpers);
searchResulPrinter->print_search_results(searchResult);

delete root;
root = nullptr;
}
1 change: 1 addition & 0 deletions src/controller/SearchControllerI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using namespace std;
class SearchControllerI {
public:
virtual void search(string& query, Args* args) = 0;
virtual ~SearchControllerI() = default;
};

#endif
3 changes: 3 additions & 0 deletions src/controller/navigate/DreeNavigate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
void DreeNavigate::display_dree(Args* args) {
DreeNode* rootNode = DreeLoaderInterface->load_dree(args);
DreePrinterInterface->navigate_dree(rootNode, args->MaxDepth);

delete rootNode;
rootNode = nullptr;
}

DreeNavigate::DreeNavigate(DreeLoaderI* DreeLoader, IDreeNavigateView* DreePrinter) {
Expand Down
1 change: 1 addition & 0 deletions src/controller/navigate/IDreeNavigate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace std;
class IDreeNavigate {
public:
virtual void display_dree(Args* args) = 0;
virtual ~IDreeNavigate() = default;
};

#endif
6 changes: 6 additions & 0 deletions src/data_structures/DreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class DreeNode {
DreeNode *right;

DreeNode(std::string &str);

~DreeNode() {
Copy link
Owner

@ujjwall-R ujjwall-R Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wont it increase the latency? Latency is already very high due to iterations. Lets not do this part. We will have to find some other way. Maybe concurrently. There is a separate issue for that.

Can we have some functionality like finally block in cpp (like what we have in python)? Lets figure out some doing it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will look for other possibilities and discuss

for (auto i : children) {
delete i;
}
}
};

#endif
26 changes: 26 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ int main(int argc, char* argv[]) {
AboutDree aboutView;
HelpControllerI* controller = new HelpController(&aboutView);
controller->help();

delete controller;
controller = nullptr;
}
else if (argc == 3 || (argc == 4 && strcmp(argv[3], "-n") != 0)) {
Args* arg = new Args(stoll(argv[2]), argv[1]);
Expand All @@ -73,6 +76,15 @@ int main(int argc, char* argv[]) {
DreeControllerI* controller = new DreeController(&dreeLoader, &dreePrinter);
controller->print_dree(arg);

delete arg;
arg = nullptr;

delete dreeIgnore;
dreeIgnore = nullptr;

delete controller;
controller = nullptr;

return 0;
}
else if (argc >= 3 && argc <= 5 && strcmp(argv[3], "-f") != 0) {
Expand All @@ -92,6 +104,15 @@ int main(int argc, char* argv[]) {
IDreeNavigate* controller = new DreeNavigate(&dreeLoader, dreeNavigateView);
controller->display_dree(arg);

delete arg;
arg = nullptr;

delete dreeIgnore;
dreeIgnore = nullptr;

delete controller;
controller = nullptr;

return 0;
}
}
Expand All @@ -106,6 +127,11 @@ int main(int argc, char* argv[]) {
string query = argv[4];
searchController->search(query, args);

delete args;
args = nullptr;

delete searchController;
searchController = nullptr;
}
else {
cout << "Command Not Found!\n Run: Dree --help to learn more\n";
Expand Down
21 changes: 15 additions & 6 deletions src/model/SearchDirectory.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
#include "SearchDirectory.h"
#include <iostream>
vector<pair<int, DreeNode *>> SearchDirectory::search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) {
vector<pair<int, DreeNode *>> searchResult;
vector<pair<int, pair<string, string>>> SearchDirectory::search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it decreasing the readability of the function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want the function signature to remain as - vector<pair<int, DreeNode *>> @ujjwall-R

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it make sure the function is readable.

vector<pair<int, pair<string, string>>> searchResult;
traverse_directories(root, args->MaxDepth, 0, query, searchResult, dreeHelpers);
return searchResult;
}

void SearchDirectory::traverse_directories(DreeNode *node, long long depth, long long currentDepth, const string &query,
vector<pair<int, DreeNode *>> &results, DreeHelpersI *dreehelpers) {
vector<pair<int, pair<string, string>>> &results, DreeHelpersI *dreehelpers) {
if (currentDepth > depth) {
return;
}
int score1 = dreehelpers->levenshtein_distance_between_strings(node->name, query);
int score2 = dreehelpers->levenshtein_distance_between_strings(node->path, query);

if (score1 * 100 <= (50 * query.length()) || score2 * 100 <= (50 * query.length())) {
results.push_back({min(score1, score2), node});
results.push_back({min(score1, score2), {node->name, node->path}});
}

if (!dreehelpers->string_is_a_directory(node->path)) {
return;
}

DreeNode *child;

try {

for (const auto &entry : filesystem::directory_iterator(node->path)) {
string childDirectory = entry.path().string();
DreeNode *child = new DreeNode(childDirectory);
child = new DreeNode(childDirectory);
node->children.push_back(child);
traverse_directories(child, depth, currentDepth + 1, query, results, dreehelpers);

delete child;
child = nullptr;
node->children.pop_back();
}
} catch (const std::exception &e) {
}
delete child;
child = nullptr;
}
}

SearchDirectory::SearchDirectory(Args *args) { this->args = args; }
4 changes: 2 additions & 2 deletions src/model/SearchDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class SearchDirectory : public SearchDirectoryI {
Args *args;

void traverse_directories(DreeNode *node, long long depth, long long currentDepth, const string &query,
vector<pair<int, DreeNode *>> &results, DreeHelpersI *dreehelpers);
vector<pair<int, pair<string, string>>> &results, DreeHelpersI *dreehelpers);

public:
vector<pair<int, DreeNode *>> search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) override;
vector<pair<int, pair<string, string>>> search(DreeNode *root, string &query, DreeHelpersI *dreeHelpers) override;

SearchDirectory(Args *args);
};
Expand Down
2 changes: 1 addition & 1 deletion src/model/SearchDirectoryI.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;

class SearchDirectoryI {
public:
virtual vector<pair<int, DreeNode*>> search(DreeNode* root, string& query, DreeHelpersI* dreeHelpers) = 0;
virtual vector<pair<int, pair<string, string>>> search(DreeNode* root, string& query, DreeHelpersI* dreeHelpers) = 0;
};

#endif
1 change: 1 addition & 0 deletions src/view/AboutDreeI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class AboutDreeI {
public:
virtual void print_help_instructions() = 0;
virtual ~AboutDreeI() = default;
};

#endif
6 changes: 3 additions & 3 deletions src/view/SearchResults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <algorithm>
#include <iostream>

void SearchResults::print_search_results(vector<pair<int, DreeNode *>> &searchResult) {
void SearchResults::print_search_results(vector<pair<int, pair<string, string>>> &searchResult) {
sort(searchResult.begin(), searchResult.end());
if (searchResult.empty()) {
cout << "No results found\n";
Expand All @@ -14,15 +14,15 @@ void SearchResults::print_search_results(vector<pair<int, DreeNode *>> &searchRe
if (res.first > 0) break;

cout << cnt + 1 << ".\t";
cout << res.second->name << "\t\t" << res.second->path << "\n";
cout << res.second.first << "\t\t" << res.second.second << "\n";
cnt++;
}
} else {
cout << "Couldn't find results. Did you mean?:\n";
int cnt = 0;
for (auto res : searchResult) {
cout << cnt + 1 << ".\t";
cout << res.second->name << "\t\t" << res.second->path << "\n";
cout << res.second.first << "\t\t" << res.second.second << "\n";
cnt++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/view/SearchResults.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace std;

class SearchResults : public SearchResultsI {
public:
void print_search_results(vector<pair<int, DreeNode *>> &searchResult) override;
void print_search_results(vector<pair<int, pair<string, string>>> &searchResult) override;
};

#endif
2 changes: 1 addition & 1 deletion src/view/SearchResultsI.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class SearchResultsI {
public:
virtual void print_search_results(std::vector<std::pair<int, DreeNode *>> &searchResult) = 0;
virtual void print_search_results(std::vector<std::pair<int, std::pair<std::string, std::string>>> &searchResult) = 0;
};

#endif