From 3781959e23f79fec610e87cfbc3a43c6b24ea7ba Mon Sep 17 00:00:00 2001 From: Andrew Duggan Date: Mon, 27 Mar 2017 20:30:07 -0700 Subject: Consolidate utility functions into the rmidevice library --- rmidevice/util.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 rmidevice/util.cpp (limited to 'rmidevice/util.cpp') diff --git a/rmidevice/util.cpp b/rmidevice/util.cpp new file mode 100644 index 0000000..64b1c1c --- /dev/null +++ b/rmidevice/util.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +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; +} \ No newline at end of file -- cgit v1.2.3