forked from bkacjios/lua-mumble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
114 lines (94 loc) · 2.36 KB
/
Copy pathutil.c
File metadata and controls
114 lines (94 loc) · 2.36 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
#include "mumble.h"
double gettime()
{
struct timeval time;
gettimeofday(&time, (struct timezone *) NULL);
return time.tv_sec + time.tv_usec/1.0e6;
}
void bin_to_strhex(uint8_t *bin, size_t binsz, char **result)
{
char hex_str[]= "0123456789abcdef";
size_t i;
*result = (char *)malloc(binsz * 2 + 1);
(*result)[binsz * 2] = 0;
if (!binsz)
return;
for (i = 0; i < binsz; i++)
{
(*result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];
(*result)[i * 2 + 1] = hex_str[(bin[i] ) & 0x0F];
}
}
void debugstack(lua_State *l, const char* text)
{
for (int i=1; i<=lua_gettop(l); i++)
{
if (lua_isstring(l, i))
printf("%s [%d] = %s (%s)\n", text, i, eztype(l, i), lua_tostring(l, i));
else
printf("%s [%d] = %s\n", text, i, eztype(l, i));
}
}
int luaL_checkboolean(lua_State *L, int i){
if(!lua_isboolean(L,i))
luaL_typerror(L,i,"boolean");
return lua_toboolean(L,i);
}
int luaL_optboolean(lua_State *L, int i, int d){
if(lua_type(L, i) < 1)
return d;
else
return luaL_checkboolean(L, i);
}
const char* eztype(lua_State *L, int i)
{
return lua_typename(L, lua_type(L, i));
}
/* Function to add a node at the beginning of Linked List.
This function expects a pointer to the data to be added
and size of the data type */
void list_add(LinkNode** head_ref, uint32_t value)
{
LinkNode* new_node = malloc(sizeof(LinkNode));
new_node->data = value;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void list_remove(LinkNode **head_ref, uint32_t value)
{
LinkNode* temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == value)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != value)
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
void list_clear(LinkNode** head_ref)
{
/* deref head_ref to get the real head */
LinkNode* current = *head_ref;
LinkNode* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
/* deref head_ref to affect the real head back
in the caller. */
*head_ref = NULL;
}