-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
48 lines (42 loc) · 1.39 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "parse.h"
LL_PAIRS* parse_url(char url[], int len, int* arr_length) {
// remove trailing http:// or https://
// skipping this step for now assuming all input is in the following format - www.google.com
int label_count = 0;
char* temp = strchr(url, '.');
while (temp!=NULL) {
label_count++;
temp = strchr(temp+1, '.');
}
label_count++; // number of labels
LL_PAIRS *pairs = (LL_PAIRS*)malloc(label_count * sizeof(LL_PAIRS));
*arr_length = label_count;
// tokenize based on period
char* token = strtok(url, ".");
for (int loop_index = 0; token != NULL; loop_index++) {
// printf("%s\n", token);
pairs[loop_index].label = strdup(token);
if (pairs[loop_index].label == NULL) {
// handle memory allocation failure
free_label_len_pairs(pairs, loop_index);
return NULL;
}
pairs[loop_index].len = strlen(token);
token = strtok(NULL, ".");
}
return pairs;
}
void free_label_len_pairs(LL_PAIRS* pairs, int count) {
for (int i = 0; i < count; i++) {
free(pairs[i].label);
}
free(pairs);
}
// int main() {
// char url[] = "www.google.com";
// int pair_count = 0;
// LL_PAIRS* pairs = parse(url, strlen(url), &pair_count);
// }