-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.c
147 lines (135 loc) · 2.53 KB
/
utilities.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
#include "utilities.h"
void initializeRules(void)
{
constrnts.mnwrds = 1;
constrnts.mxwrds = 22;
constrnts.minscore = 0;
constrnts.mnspeed = 3;
constrnts.mxspeed = 0;
constrnts.smooth = 1;
constrnts.step = 150;
constrnts.label = 1;
}
int get_random(int rng)
{
if (rng < 1)
{
return 0;
}
return (int)(random() % rng);
}
void my_strncpy(char *dst, char *src, size_t n)
{
(void)strncpy(dst, src, n);
dst[n] = '\0';
}
clock_t currTime(void)
{
struct timeval tval;
gettimeofday(&tval, NULL);
return ((clock_t)((tval.tv_sec * 100) + (tval.tv_usec / 10000)));
}
void getInput(int y, int x, char *bffr, int maxlen)
{
int chr, currlength;
currlength = 0;
memset(bffr, 0, maxlen + 1);
for (chr = 0; chr != 10;)
{
mvaddstr(y, x + currlength, " ");
move(y, x);
if (currlength)
{
addstr(bffr);
}
refresh();
switch (chr = getch())
{
case 10:
break;
case 27:
flushinp();
break;
case 4:
case 8:
case KEY_BACKSPACE:
case 127:
if (currlength)
{
currlength--;
echochar(chr);
bffr[currlength] = '\0';
}
break;
default:
if (currlength != maxlen && !iscntrl(chr))
bffr[currlength++] = chr;
break;
}
}
}
int compare(struct tInfo *ainf, struct tInfo *binf)
{
return ainf->sockfd - binf->sockfd;
}
void initList(struct list *strt)
{
strt->head = strt->tail = NULL;
strt->size = 0;
}
int insertList(struct list *strt, struct tInfo *thr_info)
{
if(strt->size == CLIENTS)
{
return -1;
}
if(strt->head == NULL)
{
strt->head = (struct listnode *)malloc(sizeof(struct listnode));
strt->head->threadinfo = *thr_info;
strt->head->next = NULL;
strt->tail = strt->head;
}
else
{
strt->tail->next = (struct listnode *)malloc(sizeof(struct listnode));
strt->tail->next->threadinfo = *thr_info;
strt->tail->next->next = NULL;
strt->tail = strt->tail->next;
}
strt->size++;
return 0;
}
int deleteList(struct list *strt, struct tInfo *thr_info)
{
struct listnode *curr, *temp;
if(strt->head == NULL)
{
return -1;
}
if(compare(thr_info, &strt->head->threadinfo) == 0)
{
temp = strt->head;
strt->head = strt->head->next;
if(strt->head == NULL) strt->tail = strt->head;
free(temp);
strt->size--;
return 0;
}
for(curr = strt->head; curr->next != NULL; curr = curr->next)
{
if(compare(thr_info, &curr->next->threadinfo) == 0)
{
temp = curr->next;
if(temp == strt->tail)
{
strt->tail = curr;
}
curr->next = curr->next->next;
free(temp);
strt->size--;
return 0;
}
}
return -1;
}