-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.c
177 lines (139 loc) · 3.1 KB
/
channel.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
#include "channel.h"
/**
* Free the memory held by a channel and its members.
* @param chan
*/
static void freeChannel(Channel* chan) {
if (!chan) {
return;
}
free(chan->id);
free(chan->name);
free(chan);
}
/**
* Create a channel from a JSON object.
* @param raw
*/
static Channel* channelFromJSON(cJSON* raw) {
Channel* chan = malloc(sizeof(*chan));
if (!chan) {
// out of memory
return NULL;
}
cJSON* id = cJSON_GetObjectItemCaseSensitive(raw, "id");
if (!cJSON_IsString(id) || !id->valuestring) {
// missing ID
freeChannel(chan);
return NULL;
}
cJSON* name = cJSON_GetObjectItemCaseSensitive(raw, "name");
if (!cJSON_IsString(name) || !name->valuestring) {
// missing name
freeChannel(chan);
return NULL;
}
// convert to wide strings
chan->id = strToWstr(id->valuestring);
chan->name = strToWstr(name->valuestring);
if (!chan->id || !chan->name) {
// out of memory
freeChannel(chan);
return NULL;
}
return chan;
}
/**
* Allocate memory for a channel node.
* @param chan
*/
static ChannelNode* createChannelNode(Channel* chan) {
ChannelNode* node = malloc(sizeof(*node));
if (!node) {
return NULL;
}
node->next = NULL;
node->chan = chan;
return node;
}
/**
* Free the memory held by a channel node and the channel object.
* @param node
*/
static void freeChannelNode(ChannelNode* node) {
freeChannel(node->chan);
free(node);
}
/**
* Create a Channel List from a JSON array.
* @param array
*/
ChannelList* channelListFromJSON(cJSON* array) {
ChannelList* list = malloc(sizeof(*list));
if (!list) {
return NULL;
}
list->length = 0;
list->head = NULL;
for (cJSON* raw = array->child; raw; raw = raw->next) {
Channel* chan = channelFromJSON(raw);
if (!chan || !insertChannel(list, chan)) {
freeChannel(chan);
freeChannelList(list);
return NULL;
}
}
return list;
}
/**
* Insert a channel into the list, sorted alphabetically.
* @param list
* @param chan
* @return False if out of memory and true otherwise.
*/
bool insertChannel(ChannelList* list, Channel* chan) {
ChannelNode* new = createChannelNode(chan);
if (!new) {
return false;
}
if (!list->head) {
// first insert
list->head = new;
} else {
ChannelNode* prev = NULL;
ChannelNode* curr = list->head;
// advance until the strings match or the new channel's name comes
// alphabetically before the current node's name or until the end
// of the list is reached
for (; curr && wcscmp(new->chan->name, curr->chan->name) > 0;
prev = curr, curr = curr->next);
if (!prev) {
// inserting at the start
list->head = new;
} else {
// inserting in the middle or at the end
prev->next = new;
}
// insert before the current node or the end
new->next = curr;
}
list->length++;
return true;
}
/**
* Free the memory allocated to a channel list.
* @param list
*/
void freeChannelList(ChannelList* list) {
if (!list) {
return;
}
// free all nodes and their respective channels
for (ChannelNode* node = list->head; node;) {
ChannelNode* next = node->next;
freeChannelNode(node);
node = next;
}
// free the list
free(list);
}