aboutsummaryrefslogtreecommitdiff
path: root/rmidevice/util.cpp
blob: 64b1c1c5433276eb327921ee8fb0ebbd539a19e0 (plain)
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
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>

long long diff_time(struct timespec *start, struct timespec *end)
{
	long long diff;
	diff = (end->tv_sec - start->tv_sec) * 1000 * 1000;
	diff += (end->tv_nsec - start->tv_nsec) / 1000;
	return diff;
}

int Sleep(int ms)
{
	struct timespec ts;
	struct timespec rem;

	ts.tv_sec = ms / 1000;
	ts.tv_nsec = (ms % 1000) * 1000 * 1000;
	for (;;) {
		if (nanosleep(&ts, &rem) == 0) {
			break;
		} else {
			if (errno == EINTR) {
				ts = rem;
				continue;
			}
			return -1;
		}
	}
	return 0;
}

void print_buffer(const unsigned char *buf, unsigned int len)
{
	for (unsigned int i = 0; i < len; ++i) {
		fprintf(stdout, "0x%02X ", buf[i]);
		if (i % 8 == 7)
			fprintf(stdout, "\n");
	}
	fprintf(stdout, "\n");
}


const char * StripPath(const char * path, ssize_t size)
{
	int i;
	const char * str;

	for (i = size - 1, str = &path[size - 1]; i > 0; --i, --str)
		if (path[i - 1] == '/')
			break;

	return str;
}

unsigned long extract_long(const unsigned char *data)
{
	return (unsigned long)data [0]
		+ (unsigned long)data [1] * 0x100
		+ (unsigned long)data [2] * 0x10000
		+ (unsigned long)data [3] * 0x1000000;
}

unsigned short extract_short(const unsigned char *data)
{
	return (unsigned long)data [0]
		+ (unsigned long)data [1] * 0x100;
}