-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusertable.c
More file actions
139 lines (110 loc) · 3.12 KB
/
Copy pathusertable.c
File metadata and controls
139 lines (110 loc) · 3.12 KB
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
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include "user.h"
Usertable* newUserTable(int count) {
Usertable *usertable = (Usertable*) malloc(sizeof(Usertable));
usertable->size = count;
usertable->users = (User**) calloc(count, sizeof(User*));
for (int i = 0; i < usertable->size; i++) {
usertable->users[i] = NULL;
}
return usertable;
}
User* getUser(Usertable* usertable, const char *username) {
uint64_t hashIndex = hashf(username), startIndex;
startIndex = hashIndex = hashIndex % usertable->size;
while (usertable->users[hashIndex] != NULL && (hashIndex != startIndex - 1)) {
if(!strcmp(usertable->users[hashIndex]->username, username)) {
return *(usertable->users + hashIndex);
}
++hashIndex;
hashIndex %= usertable->size;
}
return NULL;
}
int8_t addUser(Usertable* usertable, User *user)
{
uint64_t hashIndex = hashf(user->username), startIndex;
startIndex = hashIndex = hashIndex % usertable->size;
while (usertable->users[hashIndex] != NULL && hashIndex != startIndex - 1) {
hashIndex++;
hashIndex %= usertable->size;
}
if (hashIndex == startIndex - 1) {
return 1; // Dictionary full
} else {
usertable->users[hashIndex] = user;
initEventlist(&usertable->users[hashIndex]->eventlist);
}
return 0;
}
/*
* DESCR:
* Dumps table contents in json format.
*
* ARGS:
* Takes Usertable pointer as argument
*/
char* dumpTable(Usertable *usertable)
{
json_t *dump = json_array();
for (int i = 0; i < usertable->size; i++) {
if (usertable->users[i] != NULL) {
json_t *userHandle = json_object();
printf("Username: %s, password: %s\n", usertable->users[i]->username, usertable->users[i]->password);
json_object_set_new(userHandle, "username", json_string(usertable->users[i]->username));
json_object_set_new(userHandle, "password", json_string(usertable->users[i]->password));
json_array_append_new(dump, userHandle);
}
}
return json_dumps(dump, 0);
}
/*
* DESCR:
* Imports table from string in json format
*
* ARGS:
* Usertable reference to be filled
* String containing json data to be imported
*/
int importTable(Usertable *usertable, char *dump)
{
json_t *parsedDump = json_array();
parsedDump = json_loads(dump, 0, NULL);
if (parsedDump == NULL) {
fprintf(stderr, "Corrupted user database, failed to load\n");
return -1;
}
size_t index;
json_t *value;
printf("%s\n", json_dumps(parsedDump, 0));
json_array_foreach(parsedDump, index, value) {
User *user = malloc(sizeof(User));
user->username = (char*) json_string_value(json_object_get(value, "username"));
user->password = (char*) json_string_value(json_object_get(value, "password"));
addUser(usertable, user);
}
printTable(usertable);
return 0;
}
int printTable(Usertable *usertable)
{
printf("[");
for (int i = 0; i < usertable->size; i++) {
if (usertable->users[i] != NULL) {
printf("{ \"username\": \"%s\", \"password\": \"%s\" },", usertable->users[i]->username, usertable->users[i]->password);
}
}
printf("]");
return 0;
}
uint64_t hashf(const char *str)
{
uint64_t hash = 5381;
int32_t c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}