-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
111 lines (105 loc) · 2.76 KB
/
parse.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
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
#include "parse.h"
char ** parse_args(char *string, char delim) {
int num = countDelimiters(string, delim) + 1;
char *current;
char ** result = malloc(num * sizeof(char *));
int i = 0;
char seperatorChars[2] = "\n";
seperatorChars[1] = delim;
while ((current = strsep(&string, seperatorChars)))
{
if (*current){
result[i] = current;
i++;
}
}
result[i] = NULL;
result = realloc(result, (i + 1) * sizeof(char *));
return result;
}
int countDelimiters(char *string, char delim) {
char *n = string;
int num = 1;
while (*n) {
if (*n == delim) {
num++;
}
n++;
}
return num;
}
int arrayOfStringsLength(char **array){
char **current = array;
int total = 0;
while (*current)
{
total += strlen(*current);
current++;
}
return total;
}
char *standardizeString(char *oldString){
int newStringLength = (countDelimiters(oldString, '>') - 1)*2 + (countDelimiters(oldString, '<')- 1)*2 + (countDelimiters(oldString, '|')-1)*2 + strlen(oldString);
char *newString = malloc(newStringLength);
char *current = oldString;
int indice = 0;
while (*current)
{
if (*current == '>' && *(current + 1) == '>'){
newString[indice] = ' ';
indice++;
newString[indice] = *current;
indice++;
newString[indice] = *current;
indice++;
newString[indice] = ' ';
current++;
}
else if (*current == '>' || *current == '<' || *current == '|')
{
newString[indice] = ' ';
indice++;
newString[indice] = *current;
indice++;
newString[indice] = ' ';
}
else
{
newString[indice] = *current;
}
indice++;
current++;
}
return newString;
}
int lengthOfArray(char **array){
int total = 0;
while(*array){
total++;
array++;
}
return total;
}
char* squigglyToHomeDirectory(char* string){
string = string + 1;
char *dir = string + 1;
char *homedir = getenv("HOME");
char *completeDir;
if (*string == '/')
{
completeDir = malloc((strlen(homedir) + strlen(dir - 1)) * sizeof(char));
strcat(completeDir, homedir);
strcat(completeDir, dir - 1);
}else if (*string == '\0'){
completeDir = malloc(strlen(homedir) * sizeof(char));
strcat(completeDir, homedir);
}
else
{
completeDir = malloc((strlen(homedir) + strlen("/../") + strlen(dir)) * sizeof(char));
strcat(completeDir, homedir);
strcat(completeDir, "/../");
strcat(completeDir, dir - 1);
}
return completeDir;
}