-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_delete_files.cpp
More file actions
78 lines (70 loc) · 1.87 KB
/
Copy pathcreate_delete_files.cpp
File metadata and controls
78 lines (70 loc) · 1.87 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
#include "myheader.h"
/// @brief Removes a single file from the given path
/// @param path The path of the file to remove
void removeSingleFile(char *path)
{
int status = remove(path);
if (status != 0)
{
showError("Error in removing the file with path: " + string(path));
}
}
/// @brief Removes multiple files specified by the user in the argument
/// @param list The list of files to remove
void removeFiles(vector<string> list)
{
if (list.size() < 2)
{
showError("Less number of arguments in delete_file command");
}
for (unsigned int i = 1; i < list.size(); i++)
{
char *path = new char[list[i].length() + 1];
strcpy(path, list[i].c_str());
removeSingleFile(path);
}
}
/// @brief Creates a single file at the given path
/// @param path The path of the file to create
void createSingleFile(char *path)
{
int status = open(path, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (status == -1)
{
showError("Error in creating new file with path: " + string(path));
}
}
/// @brief Creates multiple files as specified by the user
/// @param list The list of files to create
void createNewFiles(vector<string> list)
{
if (list.size() < 3)
{
showError("Less number of arguments in renaming!");
return;
}
unsigned int len = list.size();
string destinationPath = pathProcessing(list[len - 1]);
for (unsigned int i = 1; i < len - 1; i++)
{
string fileName = destinationPath + "/" + list[i];
char *path = new char[fileName.length() + 1];
strcpy(path, fileName.c_str());
createSingleFile(path);
}
}
/// @brief Renames a file or directory
/// @param list The list containing the initial name and the final name
void renameFiles(vector<string> list)
{
if (list.size() != 3)
{
showError("Invalid arguments in renaming!");
}
else
{
string initialName = list[1];
string finalName = list[2];
rename(initialName.c_str(), finalName.c_str());
}
}