-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
194 lines (185 loc) · 5.8 KB
/
parser.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "parser.h"
void chooseType(char type, char ** characteristics, int position, char ** arguments, int args)
{
switch (type)
{
case 'h':
header(characteristics, position);
break;
case 'b':
button(characteristics, position, arguments, args);
break;
case 'c':
createPost(characteristics, position, arguments, args);
break;
case 'd':
printf("<hr>\n");
break;
case 'e':
execute(characteristics, position);
break;
case 'i':
input(characteristics, position, arguments, args);
break;
case 'l':
html_link(characteristics, position);
break;
case 'p':
image(characteristics, position);
break;
case 'r':
radio(characteristics, position);
break;
case 'k': /* add author */
addAuthor(characteristics, position, arguments, args);
break;
case 't':
text(characteristics, position);
break;
case 'T':
textArea(characteristics, position);
break;
default:
break;
}
}
void freeArrayElements(char ** characteristics, int position)
{
int i;
for (i = 0; i < position; i++) free(characteristics[i]);
}
void readFile(FILE * input, char * filename, char ** arguments, int args)
{
startFile("Rhys's A4 Assignment For 2750");
char ** characteristics = malloc(sizeof(char *) * 20);
int bracketCount = 0, escaped = 0, count = 0, inString = 0, position = 0, firstFound = 0;
char last = 0, current, type = 0, next = 0;
if (args >= 5)
{
char username[50], stream[50], counter[50], limit[50], sorted[50];
concatenateArgs(arguments, args, username, stream, counter, limit, sorted);
printf("Logged in as: <b>%s</b>, your current stream is: <b>%s</b>.\n", username, stream);
}
char str[100];
while ((current = getc(input)) != EOF)
{
next = getc(input);
ungetc(next, input);
if ((current == '(' || current == ')') && inString == 0) /* bracket count */
{
if (current == ')' && bracketCount == 1)
{
str[count] = '\0';
if (str[0] != '\0')
{
characteristics[position] = malloc(sizeof(char) * count + 1);
strcpy(characteristics[position++], str);
}
count = 0;
}
if (current == '(') bracketCount++;
else bracketCount--;
last = current;
}
else if (current == ',' && inString == 0 && bracketCount == 1)
{
str[count] = '\0';
characteristics[position] = malloc(sizeof(char) * count + 1);
strcpy(characteristics[position++], str);
count = 0;
}
else if (current == '"')
{
if (inString == 0) inString = 1;
else if (escaped % 2 == 0 && inString == 1) inString = 0;
str[count++] = current;
escaped = 0;
}
else if (bracketCount == 1)
{
if (inString == 1 && current == '\\')
escaped++;
else if (inString == 1)
escaped = 0;
if (inString == 0 && current == ' ')
continue;
str[count++] = current;
}
else if ((current == '\n' || (bracketCount == 0 && last == '.' && firstFound == 1)) && type != 0)
{
chooseType(type, characteristics, position, arguments, args);
freeArrayElements(characteristics, position);
position = count = bracketCount = 0;
if (current == '\n') firstFound = last = type = 0;
}
if (next == EOF && type != 0)
{
chooseType(type, characteristics, position, arguments, args);
freeArrayElements(characteristics, position);
position = count = bracketCount = 0;
if (current == '\n') firstFound = last = type = 0;
}
if (bracketCount == 0 && last == '.') /* the type */
{
firstFound = 1;
type = current;
}
last = current;
}
freeArrayElements(characteristics, position);
free(characteristics);
if (strcmp(filename, "header") != 0
&& strcmp(filename,"mark_all") != 0
&& strcmp(filename, "authorcontrol") != 0
&& strcmp(filename,"choose") != 0)
{
endFile();
}
}
int main(int argc, char const *argv[]) {
char input[50], output[50];
char ** arguments = malloc(sizeof(char * ) * (argc - 2));
char * filename;
/* check to make sure arguements are right */
if (argc < 2)
{
printf("Invalid number of arguements...exiting\n");
exit(1);
}
strcpy(input, argv[1]);
if (argc > 2)
{
/* fetches the author then removes everything else */
int i, count = 0;
for (i = 2; i < argc; i++)
{
arguments[count] = malloc(sizeof(char) * strlen(argv[i]) + 1);
strcpy(arguments[count++], argv[i]);
}
}
/* check to make sure file opened okay */
FILE * f = fopen(argv[1], "r");
if (f == NULL)
{
printf("Failed to open file.. exiting\n");
exit(1);
}
/* set filename and output variables */
filename = strtok(input, ".");
strtok(NULL, "");
strcpy(output, filename);
strcat(output, ".html");
if (argc > 2)
readFile(f, filename, arguments, argc - 2);
else
readFile(f, filename, NULL, argc - 2);
if (argc > 2)
{
int j = 0;
for (j = 0; j < argc - 2; j++)
free(arguments[j]);
}
fclose(f);
free(arguments);
return 0;
}