-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable_store.c
66 lines (63 loc) · 2.4 KB
/
variable_store.c
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
#include <stdlib.h>
#include <string.h>
#include "variable_store.h"
// Sets the value of a variable
void setVariable(struct VariableStore* varStore, char* name, char* value) {
// If variable exists, update its value
for (int i = 0; i < varStore->count; i++) {
if (!strcmp(varStore->variables[i].name, name)) {
varStore->variables[i].value = value;
return;
}
}
// Create a new variable if the variable does not exist
struct Variable* newVars = malloc((varStore->count + 1) * sizeof(struct Variable));
for (int i = 0; i < varStore->count; i++) {
newVars[i].name = varStore->variables[i].name;
newVars[i].value = varStore->variables[i].value;
}
newVars[varStore->count].name = name;
newVars[varStore->count].value = value;
free(varStore->variables);
varStore->variables = newVars;
varStore->count++;
}
// Deletes an environment variable
void removeVariable(struct VariableStore* varStore, char* cmdString) {
char* cmdStringCopy = malloc((strlen(cmdString) + 1) * sizeof(char));
strcpy(cmdStringCopy, cmdString);
char* commandName = strtok(cmdStringCopy, " ");
char* varName = strtok(NULL, " ");
// Check if environment variable exists
int varIndex = -1;
for (int i = 0; i < varStore->count; i++) {
if (!strcmp(varStore->variables[i].name, varName)) {
varIndex = i;
break;
}
}
if (varIndex == -1) return;
// Do nothing if variable does not exist, otherwise remove variable
struct Variable* newVars = malloc((varStore->count - 1) * sizeof(struct Variable));
for (int i = 0; i < varIndex; i++) {
newVars[i].name = varStore->variables[i].name;
newVars[i].value = varStore->variables[i].value;
}
for (int i = varIndex + 1; i < varStore->count; i++) {
newVars[i - 1].name = varStore->variables[i].name;
newVars[i - 1].value = varStore->variables[i].value;
}
free(varStore->variables);
varStore->variables = newVars;
varStore->count--;
}
// Returns the environment variable's value if found, or an empty string otherwise
char* getVariable(struct VariableStore* varStore, char* name) {
// If variable exists, update its value
for (int i = 0; i < varStore->count; i++) {
if (!strcmp(varStore->variables[i].name, name)) {
return varStore->variables[i].value;
}
}
return "";
}