-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (180 loc) · 6.06 KB
/
main.cpp
File metadata and controls
195 lines (180 loc) · 6.06 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "CodeSnippetManager.h"
#include <iostream>
using namespace std;
std::string toLowerCase(std::string input_string)
{
std::string lowercased_string = "";
for (char &c : input_string)
{
if (c >= 'A' && c <= 'Z')
{
lowercased_string += c + ('a' - 'A');
}
else
{
lowercased_string += c;
}
}
return lowercased_string;
}
int main()
{
CodeSnippetManager manager;
if (manager.LoadFromFile())
{
cout << "Snippets are loaded from the file" << endl;
manager.LoadInvertedIndex();
}
else
{
cout << "No saved snippets found.Starting with an empty manager" << endl;
}
while (true)
{
cout << ("------------------------------------") << endl;
cout << ("| START |") << endl;
cout << ("------------------------------------") << endl;
cout << ("| 1.Add Snippet |") << endl;
cout << ("| |") << endl;
cout << ("| 2.Retrieve Snippet |") << endl;
cout << ("| |") << endl;
cout << ("| 3.Deleting Snippet |") << endl;
cout << ("| |") << endl;
cout << ("| 4.Updating Snippet |") << endl;
cout << ("| |") << endl;
cout << ("| 5.Search By Tag |") << endl;
cout << ("| |") << endl;
cout << ("| 6.Search By Contents |") << endl;
cout << ("| |") << endl;
cout << ("| 7.Exit the Program |") << endl;
cout << ("| |") << endl;
cout << ("------------------------------------") << endl;
cout << ("------------------------------------") << endl;
cout << ("| Choose an option: |") << endl;
cout << ("------------------------------------") << endl;
int choice;
cin >> choice;
cin.ignore();
if (choice == 1)
{
string tag, code;
cout << "Enter the tag: ";
cin >> tag;
tag = toLowerCase(tag);
cin.ignore();
cout << "Enter the code snippet:\n";
string line;
code = "";
while (getline(cin, line) && line != "%%")
{
code = code + line + "\n";
}
manager.AddSnippet(tag, code);
cout << "Snippets are added." << endl;
}
else if (choice == 2)
{
string tag;
cout << "Enter the tag to retrieve snippet: ";
cin >> tag;
tag = toLowerCase(tag);
manager.RetrieveSnippet(tag);
}
else if (choice == 3)
{
string tag;
cout << "Enter the tag to Delete the snippet: ";
cin >> tag;
tag = toLowerCase(tag);
manager.DeleteSnippet(tag);
manager.SaveToFile();
}
else if (choice == 4)
{
string tag, code;
cout << "Enter the tag to Update the snippet: ";
cin >> tag;
tag = toLowerCase(tag);
/**
* Search for the snippet before asking for the update
*/
if (!manager.RetrieveSnippet(tag))
{
// File is not available
cout << "File does not exist for a given tag" << endl;
}
else
{
cout << "Enter the updated code snippet:\n";
string line;
code = "";
while (getline(cin, line) && line != "%%")
{
code = code + line + "\n";
}
manager.UpdateSnippet(tag, code);
}
}
else if (choice == 5)
{
string tagPrefix;
cout << "Tell me which snippet you want to search for: ";
getline(cin,tagPrefix);
tagPrefix = toLowerCase(tagPrefix);
vector<string> Results = manager.SearchSnippetsByTag(tagPrefix);
if (!Results.empty())
{
cout << "Matching snippets:" << endl;
for (const auto &tag : Results)
{
cout << "- " << tag << endl;
}
}
else
{
cout << "No snippets found for the given query." << endl;
}
}
else if (choice == 6)
{
string input;
cout << "Enter keywords to search for snippets (separated by spaces): ";
cin.ignore();
getline(cin, input);
input = toLowerCase(input);
istringstream iss(input);
vector<string> keywords;
string keyword;
while (iss >> keyword)
{
keywords.push_back(keyword);
}
vector<string> matchingTags = manager.SearchSnippetsByContent(keywords);
if (!matchingTags.empty())
{
cout << "Matching snippets:" << endl;
for (const auto &tag : matchingTags)
{
cout << "- " << tag << endl;
}
}
else
{
cout << "No snippets found for the given query." << endl;
}
}
else if (choice == 7)
{
manager.SaveToFile();
manager.SaveInvertedIndex();
cout
<< "Snippets saved to file. Exiting the program." << endl;
break;
}
else
{
cout << "Invalid choice.Choose a given option" << endl;
}
}
return 0;
}