1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef UTILS_H
6#define UTILS_H
7
8
9#include <dirent.h>
10#include <string.h>
11#include <time.h>
12
13#include <OS.h>
14
15
16inline bool operator<(const timespec& a, const timespec& b)
17{
18	return a.tv_sec < b.tv_sec
19		|| (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec);
20}
21
22
23inline bool operator>(const timespec& a, const timespec& b)
24{
25	return b < a;
26}
27
28
29static inline bool
30set_dirent_name(struct dirent* buffer, size_t bufferSize, const char* name,
31	size_t nameLen)
32{
33	size_t length = (buffer->d_name + nameLen + 1) - (char*)buffer;
34	if (length > bufferSize)
35		return false;
36
37	memcpy(buffer->d_name, name, nameLen);
38	buffer->d_name[nameLen] = '\0';
39	buffer->d_reclen = length;
40	return true;
41}
42
43
44static inline bool
45set_dirent_name(struct dirent* buffer, size_t bufferSize, const char* name)
46{
47	return set_dirent_name(buffer, bufferSize, name, strlen(name));
48}
49
50
51static inline void
52get_real_time(timespec& time)
53{
54	bigtime_t timeMicros = real_time_clock_usecs();
55
56	time.tv_sec = timeMicros / 1000000;
57	time.tv_nsec = (timeMicros % 1000000) * 1000;
58}
59
60
61#endif	// UTILS_H
62