forked from berolinaro/dvbv5-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.h
61 lines (56 loc) · 1.94 KB
/
Util.h
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
#pragma once
#include <functional>
#include <iostream>
extern "C" {
#include <sys/time.h>
}
namespace Util {
/**
* Wait for a condition to become true
* @param timeout Timeout in microseconds
* @param condition Condition to wait for (e.g. lambda function)
* @param delay Delay between checks for the condition
* @return @c true if the condition became true, @c false if the timeout was hit
*/
bool waitFor(uint32_t timeout, std::function<bool ()> const &condition, timespec delay = { 0, 1000000 });
bool waitForData(int fd, uint32_t timeout=10000);
/**
* Generate a hex dump of data
* @param data Data to dump
* @param size Size of data
* @param where Destination of hex dump
* @param bytesPerRow Bytes per row
*/
void hexdump(void const * const data, size_t size, std::ostream &where=std::cerr, std::string const &indent="", uint8_t bytesPerRow=16);
/**
* Calculate a DVB-Compatible CRC-32 checksum
* @param data Data to checksum
* @param size Size of data
* @param previous Previous CRC (for adding data to a previously calculated CRC)
* @return CRC-32 checksum
*/
uint32_t crc32(unsigned char const * const data, size_t size, uint32_t previous = 0xffffffff);
/**
* Get a UTF-8 encoded std::string from a DVB encoded string
* according to ETSI EN 300 468 V1.15.1 Annex A
*/
std::string StringFromDVB(char const * const c);
static std::string StringFromDVB(unsigned char const * const c) { return StringFromDVB(reinterpret_cast<char const * const>(c)); }
/**
* Helper that saves and restores std::iomanip format flags
* When manipulating iostream flags (std::hex, ...), simply construct a
* SaveIOState object earlier - flags will be restored as the object
* goes out of scope.
*/
class SaveIOState {
public:
SaveIOState(std::ostream &stream):_stream(stream),_flags(stream.flags()) {
}
~SaveIOState() {
_stream.flags(_flags);
}
private:
std::ostream & _stream;
std::ios::fmtflags _flags;
};
}