-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
108 lines (101 loc) · 2.84 KB
/
Copy pathparser.c
File metadata and controls
108 lines (101 loc) · 2.84 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include "parser.h"
InputParse *parseInput(char *input)
{
if (strlen(input) == 0)
return NULL;
bool allSpaces = true;
unsigned long long len = 1;
unsigned long long idx = 0;
char **parsedInputTemp = malloc(len * sizeof(char *));
unsigned long long inputLen = strlen(input);
for (unsigned long long i = 0; i < inputLen; i++)
{
if (isspace(input[i]))
continue;
unsigned long long strLen = 1;
unsigned long long strIdx = 0;
char *strTemp = malloc(strLen * sizeof(char));
unsigned long long j = i;
while (j < inputLen && input[j] != ' ')
{
allSpaces = false;
strTemp[strIdx] = input[j];
strIdx++;
if (strIdx == strLen)
{
strLen *= 2;
strTemp = realloc(strTemp, strLen * sizeof(char));
}
j++;
}
strTemp[strIdx] = '\0';
strIdx++;
char *str = malloc(strIdx * sizeof(char));
for (unsigned long long l = 0; l < strIdx; l++)
str[l] = strTemp[l];
free(strTemp);
parsedInputTemp[idx] = str;
idx++;
if (idx == len)
{
len *= 2;
parsedInputTemp = realloc(parsedInputTemp, len * sizeof(char *));
}
i = j;
}
if (allSpaces)
{
free(parsedInputTemp);
return NULL;
}
bool ampersandPresent = false;
if (parsedInputTemp[idx - 1][strlen(parsedInputTemp[idx - 1]) - 1] == '&')
ampersandPresent = true;
if (ampersandPresent && strlen(parsedInputTemp[idx - 1]) == 1)
{
free(parsedInputTemp[idx - 1]);
parsedInputTemp[idx - 1] = NULL;
}
else
{
if (ampersandPresent)
{
unsigned long long newStrLen = strlen(parsedInputTemp[idx - 1]);
char *newStr = malloc(newStrLen * sizeof(char));
for (unsigned long long l = 0; l < newStrLen; l++)
newStr[l] = parsedInputTemp[idx - 1][l];
newStr[newStrLen - 1] = '\0';
free(parsedInputTemp[idx - 1]);
parsedInputTemp[idx - 1] = newStr;
}
parsedInputTemp[idx] = NULL;
idx++;
}
char **parsedInput = malloc(idx * sizeof(char *));
for (unsigned long long l = 0; l < idx; l++)
parsedInput[l] = parsedInputTemp[l];
free(parsedInputTemp);
InputParse *inputParse = malloc(sizeof(InputParse));
inputParse->parsedInput = parsedInput;
inputParse->ampersandPresent = ampersandPresent;
inputParse->parseLen = idx;
return inputParse;
}
void freeInputParse(InputParse *inputParse)
{
for (unsigned long long i = 0; inputParse->parsedInput[i] != NULL; i++)
free(inputParse->parsedInput[i]);
free(inputParse->parsedInput);
free(inputParse);
}
void printParsedInput(InputParse *inputParse)
{
for (unsigned long long i = 0; i < inputParse->parseLen; i++)
printf("%lld: %s\n", i, inputParse->parsedInput[i]);
printf("ampersandPresent: %s\n\n", inputParse->ampersandPresent ? "true" : "false");
}