-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.c
More file actions
334 lines (274 loc) · 9.66 KB
/
Copy pathoperations.c
File metadata and controls
334 lines (274 loc) · 9.66 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int copy(const char* copyFrom, const char* copyTo);
int fileExists(const char* fileName);
int dirSplit(const char *target, char* dir, char* fileName);
int makeFile(const char* fileName);
int deleteFile(const char* fileName);
int readFile(const char* fileName);
int append(const char* fileName, const char* text);
int insert(const char* fileName, int line, const char* text);
int showLine(const char* fileName, int line);
int deleteLine(const char* fileName, int line);
int countLines(const char* fileName);
void displayHelp();
int compareFiles(const char* fileName1, const char* fileName2);
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int copy(const char* copyFrom, const char* copyTo){
// Checks that the file to copy exists and that you aren't overwriting a file by copying
if (!fileExists(copyFrom)){
printf("Cannot find file: %s \n", copyFrom);
return 1;
}
if (fileExists(copyTo)){
printf("File already exists: %s\n", copyTo);
return 1;
}
FILE *copyFile = fopen(copyFrom, "rb");
fseek(copyFile, 0L, SEEK_END);
int fileSize = ftell(copyFile);
rewind(copyFile);
char *fileBuffer = malloc(fileSize + 1);
if (fileBuffer == NULL){
printf("Malloc failed\n ");
fclose(copyFile);
return 1;
}
// Reads all of the file's data into a buffer
fread(fileBuffer, 1, fileSize, copyFile);
fileBuffer[fileSize] = '\0';
fclose(copyFile);
// Writes the Buffer into a new file of the name specified by the user
FILE *targetFile = fopen(copyTo, "wb");
//fprintf(targetFile, "%s", fileBuffer);
fwrite(fileBuffer, 1, fileSize, targetFile);
fclose(targetFile);
free(fileBuffer);
// Writes the user's operations to a log file.
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile,"Successfully Copied %d lines from %s to %s \n", countLines(copyFrom), copyFrom, copyTo);
printf("Successfully Copied %d lines from %s to %s \n", countLines(copyFrom), copyFrom, copyTo);
fclose(logFile);
return 0;
}
// This function tries to open the files that the user is trying to access. If they exist it returns 1 (true) otherwise, 0 (false)
int fileExists(const char* fileName){
FILE *file = fopen(fileName, "r");
if(file == NULL){
return 0;
}
else{
fclose(file);
return 1;
}
}
int makeFile(const char* fileName){
// Ensures that the file doesn't already exist
if(!fileExists(fileName)){
// Creates the File with the name the user gave
FILE *file = fopen(fileName, "w");
fclose(file);
// Checks that the file was correctly created
if (fileExists(fileName)){
printf("Successfully created file: %s\n", fileName);
// If it is correctly created the operation is appended to the log file
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile, "Successfully created file: %s with %d lines\n", fileName, countLines(fileName));
fclose(logFile);
return 1;
}
printf("Failed to create file %s\n", fileName);
return 0;
}
printf("File already exists: %s\n", fileName);
return 0;
}
int deleteFile(const char* fileName){
if (fileExists(fileName)){
remove(fileName);
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile, "File deleted: %s\n", fileName);
fclose(logFile);
printf("File deleted: %s\n", fileName);
return 0;
}
printf("Could not delete file\n");
return 2;
}
int readFile(const char* fileName){
FILE *file = fopen(fileName, "r");
fseek(file, 0L, SEEK_END);
size_t size = ftell(file);
rewind(file);
// Reads the file's content into a buffer and prints it out to the user
char *fileBuffer = malloc(size + 1);
fread(fileBuffer, 1, size, file);
fileBuffer[size] = '\0';
fclose(file);
printf("%s\n",fileBuffer);
free(fileBuffer);
return 1;
}
int append(const char* fileName, const char* text){
if (fileExists(fileName)){
FILE *file = fopen(fileName, "a");
fprintf(file,"%s\n", text);
fclose(file);
// Log append operation
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile, "Appended following text to %s: %s. There are now %d lines\n", fileName, text, countLines(fileName));
fclose(logFile);
return 1;
}
return 0;
}
int insert(const char* fileName, int line, const char* text){
if (fileExists(fileName)){
FILE *file = fopen(fileName, "r");
fseek(file, 0L, SEEK_END);
int fileSize = ftell(file);
rewind(file);
char *lineBuf = malloc(fileSize + 1);
char *fileBuf = malloc(fileSize + strnlen(text, strlen(text)) + 1);
// Iterates up to the user's specified line and appends each line to the file buffer
int i = 0;
for (i = 0; i < line; i++){
fgets(lineBuf, fileSize + 1, file);
strncat(fileBuf, lineBuf, strlen(lineBuf));
}
// When at the desired line, appends the user's text to the buffer
strncat(fileBuf, text, strlen(text));
fileBuf[strlen(fileBuf)] = '\n';
// Iterates through the remaining lines in the file and appends them to the filebuffer
while (fgets(lineBuf, fileSize+1, file)){
strncat(fileBuf, lineBuf, strlen(lineBuf));
}
// Prints the buffer out to the file (Overrwrites all data in the file with the buffer)
fclose(file);
file = fopen(fileName, "w");
fprintf(file, "%s", fileBuf);
fclose(file);
// Logs the operation
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile, "Inserted the following text at line %d of %s: %s\nThere are now %d lines", line, fileName, text, countLines(fileName));
fclose(logFile);
free(lineBuf);
free(fileBuf);
return 0;
}
return 1;
}
int showLine(const char* fileName, int line){
if (fileExists(fileName)){
FILE *file = fopen(fileName, "r");
fseek(file, 0L, SEEK_END);
int fileSize = ftell(file);
rewind(file);
// Iterates through each line in the file and stores it in a buffer (This way if the user's line is out of rage it will just read the last line in the file)
char* lineBuf = malloc(fileSize + 1);
for (int i = 0; i < line; i++){
fgets(lineBuf, fileSize + 1, file);
}
printf("%s", lineBuf);
free(lineBuf);
fclose(file);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
int deleteLine(const char* fileName, int line){
if (fileExists(fileName)){
FILE *file = fopen(fileName, "r");
fseek(file, 0L, SEEK_END);
int fileSize = ftell(file);
rewind(file);
char *lineBuf = malloc(fileSize + 1);
char *fileBuf = malloc(fileSize + 1);
// Iterates through each line in the file and stores it in a line buffer that is appended to the file buffer only if it is not from the line specified by the user
int i = 1;
while (fgets(lineBuf, fileSize+1, file)){
if (i != line)
strncat(fileBuf, lineBuf, strlen(lineBuf));
i++;
}
// The file buffer is printed out to file, missing the one line specified by the user
fclose(file);
file = fopen(fileName, "w");
fprintf(file, "%s", fileBuf);
fclose(file);
FILE *logFile = fopen("log.txt", "a");
fprintf(logFile, "Line %d deleted from %s. There are now %d lines\n", line, fileName, countLines(fileName));
fclose(logFile);
free(lineBuf);
free(fileBuf);
return 0;
}
return 1;
}
int countLines(const char* fileName){
FILE *file = fopen(fileName, "r");
if (file == NULL) {
printf("Could not find file\n");
return -1;
}
size_t fileSize = 0;
// Iterates through each line in the file and increments the counter until it reaches the end of the file
int count = 0;
for (char c = getc(file); c != EOF; c = getc(file)){
fileSize ++;
if (c == '\n')
count = count + 1;
}
fclose(file);
return fileSize == 0 ? 0 : count + 1;
}
void displayHelp(){
printf(" -n <File Name> : creates a new file\n -cpy <File to copy from> <File to copy to> : copy file\n -df <File Name> : Deletes the file \n -o <File Name> : Displays the content of the file \n "
"-a <File Name> <Text> : Appends the text to the end of the specified file \n -dl <File Name> <Line number> : Deletes the specified line from the file \n -i <File Name> <Line> <Text> : Inserts the text into the specified line of the file\n "
"-rl <File Name> <Line> : Displays the content of the specific line of a file.\n -cl <File Name> : Prints the number in the file \n "
"-r <Old File Name> <New File Name> : Renames a file from its old name to its new name \n -cmp <File 1> <File2> : Compares two files and checks if they are identical\n "
"-enc <File Name> : Encrypts the file\n -dec <File Name> : Decrypts the file\n -log : Displays the log of operations\n");
}
int compareFiles(const char* fileName1, const char* fileName2){
FILE *f1 = fopen(fileName1, "rb");
FILE *f2 = fopen(fileName2, "rb");
if (!f1 || !f2) {
printf("Error opening files");
fclose(f1);
fclose(f2);
return 2;
}
fseek(f1, 0L, SEEK_END);
size_t f1Size = ftell(f1);
rewind(f1);
fseek(f2, 0L, SEEK_END);
size_t f2Size = ftell(f2);
rewind(f2);
char* line1 = malloc(f1Size);
char* line2 = malloc(f2Size);
// This iterates through each line in both files and stores each line in a buffer. It uses strncmp to check if the two lines are identical.
int line = 1;
int differences = 0;
while(fgets(line1, f1Size, f1) && fgets(line2, f2Size, f2)){
if(strncmp(line1, line2, max(strlen(line1), strlen(line2))) != 0){
printf("Difference on line %d\n", line);
differences++;
}
line++;
}
free(line1);
free(line2);
if (differences != 0){
printf("%s and %s have differences on %d lines\n", fileName1, fileName2, differences);
return 1;
}
printf("%s and %s are identical\n", fileName1,fileName2);
return 0;
}