-
Notifications
You must be signed in to change notification settings - Fork 10
/
reddit.c
95 lines (70 loc) · 2.26 KB
/
reddit.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 <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <json.h>
#include <glib.h>
#include "reddit.h"
#include "jsonutil.h"
// The alphabet reddit uses for id encoding.
static const char kidAlphabet[] = "0123456789abcdefghijklmnopqrstuvwxyz";
const char *
reddit_object_id(json_object *object)
{
json_object *data;
if (json_object_object_get_ex(object, "data", &data)) {
return json_object_get_string_prop(data, "name");
}
g_warning("parsed a non-reddit object, expected a data property");
return NULL;
}
int
reddit_object_type(json_object *obj)
{
if (json_object_check_strprop(obj, "kind", "listing", false))
return REDDIT_OBJ_LISTING;
if (json_object_check_strprop(obj, "kind", "t1", false))
return REDDIT_OBJ_COMMENT;
if (json_object_check_strprop(obj, "kind", "t3", false))
return REDDIT_OBJ_LINK;
if (json_object_check_strprop(obj, "kind", "t2", false))
return REDDIT_OBJ_ACCOUNT;
if (json_object_check_strprop(obj, "kind", "t4", false))
return REDDIT_OBJ_MESSAGE;
if (json_object_check_strprop(obj, "kind", "t5", false))
return REDDIT_OBJ_SUBREDDIT;
if (json_object_check_strprop(obj, "kind", "t6", false))
return REDDIT_OBJ_AWARD;
if (json_object_check_strprop(obj, "kind", "t8", false))
return REDDIT_OBJ_PROMO;
if (json_object_check_strprop(obj, "kind", "more", false))
return REDDIT_OBJ_MORE;
g_warning("unexpected object kind found %s", json_object_get_string_prop(obj, "kind"));
return -1;
}
unsigned reddit_decode_id(const char *idstr)
{
long id;
for (id = 0; *idstr; idstr++) {
uint8_t val = index(kidAlphabet, *idstr) - kidAlphabet;
id = id * 36 + val;
}
return id;
}
int reddit_encode_id(unsigned id, char idstr[8])
{
ldiv_t result = {
.quot = id
};
// I copied this algorithm from str_to_base() in reddit code, just
// translated it into c.
for (*idstr = 0; result.quot != 0;) {
result = ldiv(result.quot, 36);
// Make room at the start of the string.
memmove(idstr + 1, idstr, strlen(idstr) + 1);
// Insert a new character.
*idstr = kidAlphabet[result.rem];
}
return 0;
}