-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.c
95 lines (82 loc) · 2.13 KB
/
badge.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "badge.h"
/**
* @brief Print all bridges from a given badge linked-list
*
* @param badge
*/
void print_badges(struct badge_t* badge) {
struct badge_t* curr = badge;
while(curr) {
printf("[ BADGE %p:%p] %s = %s\n", (void *) curr, (void *) curr->next, curr->key, curr->value);
curr = curr->next;
}
}
/**
* @brief Free all badges from a given badge linked-list
*
* @param tag
*/
void free_badges(struct badge_t* tag) {
struct badge_t* curr = tag;
while(curr) {
struct badge_t* tmp = curr;
curr = curr->next;
free(tmp);
printf("Freeing badge %p\n", (void *) tmp);
}
}
/**
* @brief Dissamble badge and create badge linked-list
*
* @param raw
* @return struct badge_t*
*/
struct badge_t* disassemble_badge(char* raw) {
char badge_str[1024];
strcpy(badge_str, raw);
char* badge_split;
char* badge_result;
char* token_pair;
char* token_result;
char key[50];
char value[50];
struct badge_t* head = 0;
struct badge_t* curr = 0;
for(badge_split = strtok_r(badge_str, ",", &badge_result); badge_split != 0; badge_split = strtok_r(0, ",", &badge_result)) {
int pair_count = 0;
for(token_pair = strtok_r(badge_split, "/", &token_result); token_pair != 0; token_pair = strtok_r(0, "/", &token_result)) {
if(pair_count == 0) {
strcpy(key, token_pair);
} else {
strcpy(value, token_pair);
}
pair_count++;
}
struct badge_t* next = create_badge_pair(key, value);
if(!head) {
head = next;
curr = next;
} else {
curr->next = next;
curr = curr->next;
}
}
return head;
}
/**
* @brief Create a badge pair object
*
* @param key
* @param value
* @return struct badge_t*
*/
struct badge_t* create_badge_pair(char* key, char* value) {
struct badge_t* badge = malloc(sizeof(struct badge_t));
strcpy(badge->key, key);
strcpy(badge->value, value);
badge->next = 0;
return badge;
}