-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
92 lines (77 loc) · 1.91 KB
/
utils.c
File metadata and controls
92 lines (77 loc) · 1.91 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
/*
* Copyright (c) 2024, <>
*/
#include "utils.h"
unsigned int hash_uint(void *key)
{
unsigned int uint_key = *((unsigned int *)key);
uint_key = ((uint_key >> 16u) ^ uint_key) * 0x45d9f3b;
uint_key = ((uint_key >> 16u) ^ uint_key) * 0x45d9f3b;
uint_key = (uint_key >> 16u) ^ uint_key;
return uint_key;
}
unsigned int hash_string(void *key)
{
unsigned char *key_string = (unsigned char *)key;
unsigned int hash = 5381;
int c;
while ((c = *key_string++))
hash = ((hash << 5u) + hash) + c;
return hash;
}
char *get_request_type_str(request_type req_type)
{
switch (req_type)
{
case ADD_SERVER:
return ADD_SERVER_REQUEST;
case REMOVE_SERVER:
return REMOVE_SERVER_REQUEST;
case EDIT_DOCUMENT:
return EDIT_REQUEST;
case GET_DOCUMENT:
return GET_REQUEST;
}
return NULL;
}
request_type get_request_type(char *request_type_str)
{
request_type type;
if (!strncmp(request_type_str,
ADD_SERVER_REQUEST, strlen(ADD_SERVER_REQUEST)))
type = ADD_SERVER;
else if (!strncmp(request_type_str,
REMOVE_SERVER_REQUEST, strlen(REMOVE_SERVER_REQUEST)))
type = REMOVE_SERVER;
else if (!strncmp(request_type_str,
EDIT_REQUEST, strlen(EDIT_REQUEST)))
type = EDIT_DOCUMENT;
else if (!strncmp(request_type_str,
GET_REQUEST, strlen(GET_REQUEST)))
type = GET_DOCUMENT;
else
DIE(1, "unknown request type");
return type;
}
int compare_strings(void *st1, void *st2)
{
return strcmp(st1, st2);
}
char *strdup(const char *src)
{
char *dst = malloc(strlen(src) + 1);
if (dst == NULL)
return NULL;
strcpy(dst, src);
return dst;
}
unsigned int number_digits(unsigned int number)
{
unsigned int c = 0;
while (number)
{
c++;
number /= 10;
}
return c;
}