-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.cpp
More file actions
294 lines (248 loc) · 7.29 KB
/
operations.cpp
File metadata and controls
294 lines (248 loc) · 7.29 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <filesystem>
#include <limits>
#include <vector>
// Including Operations Header File
#include "operations.hpp"
// Namespaces
using namespace std;
namespace fs = filesystem;
// Get Functions
bool fileExists(string filename){
string path = getBaseDir()+filename;
bool exists = fs::exists(path);
return exists;
}
string getBaseDir(){
#ifdef _WIN32
return string(getenv("USERPROFILE")) + "\\notes_cpp\\";
#else
return string(getenv("HOME")) + "/notes_cpp/";
#endif
}
int getChoice(){
int choice = 0;
if(cin>>choice) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return choice;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
choice = 0;
return choice;
}
return choice;
}
// Margin
void margin(){
cout << "==========================================\n";
}
//File Operations
void createFile(string filename){
if(filename == ""){
cout <<"\033[1;31m"" Name of the file can't be empty.""\033[0m"<<endl;
return;
}
bool exists = fileExists(filename);
if (exists){
cout << "\033[1;31m"" File already exists.""\033[0m"<<endl;
}
else{
ofstream file(getBaseDir()+filename);
if(!file){
cout << "\033[1;31m"" Can't create file.""\033[0m"<<endl;
}
else{
file.close();
cout << "\033[1;32m"" File created successfully.""\033[0m"<<endl;
}
}
}
void writeFile(string filename){
if(filename == ""){
cout <<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
bool exists = fileExists(filename);
if(!exists){
cout <<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
cout<<" This program doesn't support editing file"<<endl;
cout<<" Do you want to overwrite it? (y/n): ";
char *yn_ptr = new char;
cin>>*yn_ptr;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
margin();
if (*yn_ptr == 'y' || *yn_ptr == 'Y'){
string path = getBaseDir()+filename;
ofstream file(path);
if (!file){
cout <<"\033[1;31m"" Unexpected error occured""\033[0m"<<endl;
return;
}
cout<< " Write File content here (END for exit): "<<endl;
bool writting = true;
string *temp_line = new string;
int line_no = 1;
while(writting){
cout<<line_no<<" ";
getline(cin, *temp_line);
if(*temp_line == "END" || *temp_line == "end"){
writting = false;
break;
}
file<<*temp_line<<endl;
line_no++;
}
delete temp_line;
file.close();
margin();
cout<< "\033[1;32m"" File overwritten successfully.""\033[0m"<<endl;
}
else if(*yn_ptr == 'n' || *yn_ptr == 'N'){
cout<< "\033[1;32m"" File saved without any changes""\033[0m"<<endl;
}
else{
cout <<"\033[1;31m"" Invalid Input""\033[0m"<<endl;
}
delete yn_ptr;
}
void readFile(string filename){
if(filename == ""){
cout <<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
bool exists = fileExists(filename);
bool isEmpty = true;
if (!exists){
cout << "\033[1;31m"" File doesn't exist""\033[0m"<<endl;
return;
}
string path = getBaseDir()+filename;
ifstream file(path);
string line;
int line_no = 1;
while(getline(file, line)){
if(isEmpty){
cout<<"\033[1m""File content: ""\033[0m"<<endl;
isEmpty = false;
}
cout<<line_no<<" ";
cout<<line<<endl;
line_no++;
}
if(isEmpty){
cout<<"\033[1;31m""(File is empty)""\033[0m"<<endl;
return;
}
margin();
cout << "\033[1;32m"" File Read successfully.""\033[0m"<<endl;
}
void editFile(string filename){
if (filename == ""){
cout<<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
bool exists = fileExists(filename);
if(!exists){
cout<<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
string path = getBaseDir() + filename;
ifstream file(path);
bool isEmpty = true;
vector<string> file_content;
string line;
int line_no = 1;
while(getline(file, line)){
if(isEmpty){
cout<<"\033[1m""File content: ""\033[0m"<<endl;
isEmpty = false;
}
file_content.push_back(line);
cout<<line_no<<" ";
cout<<line<<endl;
line_no++;
}
if(isEmpty){
cout<<"\033[1;31m"" (File is empty)""\033[0m"<<endl;
return;
}
file.close();
margin();
int choice;
cout<<" Enter the line number to edit: ";
while(!(cin>>choice)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"\033[1;31m"" Invalid Input (Must be a number)""\033[0m"<<endl;
cout<<" Enter the line number to edit: ";
}
if(choice<=0 || choice > file_content.size()){
cout<<"\033[1;31m"" Line "<<choice<<" doesn't exist""\033[0m"<<endl;
return;
}
string old_content = file_content[choice-1];
cout<<endl;
cout<<" Old content: "<<endl;
cout<<" "<<old_content<<endl;
cout<<endl;
cout<<" New content: "<<endl;
cout<<" ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file_content[choice-1]);
if(file_content[choice-1] == ""){
file_content.erase(file_content.begin() + (choice - 1));
}
margin();
ofstream file_write(path);
for(int i = 0; i<file_content.size(); i++){
file_write<<file_content[i]<<endl;
}
cout<<"\033[1;32m"" File edited successfully.""\033[0m"<<endl;
}
void listFiles(){
string path = getBaseDir();
if(fs::is_empty(path)){
cout<<"There are no files saved.";
}
else{
cout<<"Listing Files:"<<endl;
for(const auto &entry : fs::directory_iterator(path)){
if(fs::is_regular_file(entry.status())){
cout<<"File: "<<entry.path().filename()<<endl;
}
else if(fs::is_directory(entry.status())){
cout<<"Folder: "<<entry.path().filename()<<endl;
}
}
string dummy;
cout << "\nPress Enter to continue...";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, dummy);
}
}
void deleteFile(string filename){
if(filename == ""){
cout <<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
bool exists = fileExists(filename);
if(!exists){
cout <<"\033[1;31m"" File doesn't exist.""\033[0m"<<endl;
return;
}
string path = getBaseDir() + filename;
if(fs::remove(path)){
cout <<"\033[1;32m"" File deleted successfully.""\033[0m"<<endl;
}
else{
cout <<"\033[1;31m"" Unexpected error occurred""\033[0m"<<endl;
}
}