-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
64 lines (50 loc) · 1.14 KB
/
utils.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
#include "utils.h"
static int hasError = false;
void printErr(char * format,...)
{
va_list arg;
hasError = true;
va_start (arg, format);
vfprintf (stderr, format, arg);
va_end (arg);
}
int hasErrorInInput()
{
return hasError;
}
int cutByBits(const int number, int from, int to)
{
int i = 0, result = number, rangeSize = to - from;
char bitSign = '0';
/*cut all the bits until from*/
result >>= from;
/* check the status of the bit at to index*/
bitSign = ((result >> rangeSize) & 1) +'0';
/*flips all the other bits to 1 or 0 depends on bitSign*/
for (i = rangeSize ; i < 15; i++)
{
if(bitSign == '1')
result = result | 1 << i ;
else
result = result & ~(1 << i) ;
}
return result;
}
void resetErrorFlag()
{
hasError = false;
}
char *trimWhiteSpace(char *str)
{
char *end;
/* Trim leading space */
while(isspace(*str)) str++;
if(*str == 0)
return str;
/* Trim trailing space */
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
/* Write new null terminator */
*(end+1) = 0;
return str;
}