-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.cpp
157 lines (143 loc) Β· 3.83 KB
/
alias.cpp
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
//PANAGIOTIS KONTOEIDIS
//1115201900266
#include "libs.h"
void add_alias(tokens command)
{ // add command to alias table
char *cmd;
char *temp;
if (command.size() < 2)
{
cout << "Incorrect alias Syntax" << endl;
return;
}
else
{
if (command.size() == 3) //
{
temp = new char[strlen(command[2]) - 2]; // store the alias without the ""
int j = 0;
for (int i = 0; i < strlen(command[2]); i++)
{
if (command[2][i] != '"' && command[2][i] != ';')
{
temp[j++] = command[2][i];
}
}
temp[j] = '\0';
}
else if (command.size() > 3)
{
int k = 0;
temp = new char[100];
for (int i = 2; i < command.size(); i++)
{
for (int j = 0; j < strlen(command[i]); j++)
{
if (command[i][j] != '"' && command[i][j] != ';')
{
temp[k++] = command[i][j];
}
}
temp[k++] = ' ';
}
temp[k + 1] = '\0';
}
if (!alias_table.empty())
{
for (int i = 0; i < alias_table.size(); i++)
{ // if an alias with the same name is found delete it to update it with the new command
if (strcmp(alias_table[i].first, command[1]) == 0)
{
auto it = alias_table.begin() + i;
alias_table.erase(it);
}
}
}
alias_table.push_back(make_pair(command[1], temp)); // insert it to alias table
return;
}
}
void print_alias()
{ // prints alias table
if (alias_table.empty())
{
return;
}
for (auto &p : alias_table)
{
cout << p.first << " -> " << p.second << endl;
}
}
void remove_alias(tokens command)
{ // removes alias from alias table
char *temp = new char[strlen(command[1])];
int j = 0;
if (alias_table.empty())
{
return;
}
for (int i = 0; i < strlen(command[1]); i++)
{
if (command[1][i] != ';')
{
temp[j++] = command[1][i];
}
}
temp[j] = '\0';
for (int i = 0; i < alias_table.size(); i++) // if an alias with the same name is found delete it to update it with the new command
{
if (strcmp(alias_table[i].first, temp) == 0)
{
auto it = alias_table.begin() + i;
alias_table.erase(it);
}
}
delete[] temp;
return;
}
tokens check_for_aliases(tokens command) // parses command and if it finds alias it replaces it and returns updated command
{
tokens alias_command;
alias_command.clear();
int alias_flag = 0;
if (strcmp(command[0], "createalias") == 0)
{
return command;
}
else if (strcmp(command[0], "destroyalias") == 0)
{
return command;
}
else if (strcmp(command[0], "myHistory") == 0)
{
return command;
}
for (int i = 0; i < command.size(); i++)
{
for (int j = 0; j < alias_table.size(); j++)
{
alias_flag = 0;
if (strcmp(alias_table[j].first, command[i]) == 0)
{
alias_flag = 1;
tokens temp2;
char *temp = alias_table[j].second;
temp2 = split_command(temp);
for (int a = 0; a < temp2.size(); a++)
{
alias_command.push_back(temp2[a]);
}
break;
}
}
if (alias_flag == 0)
{
alias_command.push_back(command[i]);
}
else if (alias_flag == 1)
{
continue;
}
}
return alias_command;
}