summaryrefslogtreecommitdiff
path: root/tools.h
blob: 7db052e44c8238036bb7f1dc2cfffd5f03b64bdc (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
#ifndef VDR_LIVE_TOOLS_H
#define VDR_LIVE_TOOLS_H

#include <ctime>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <vdr/thread.h>

namespace vdrlive {

std::string FormatDateTime( char const* format, time_t time );
std::string StringReplace( std::string const& text, std::string const& substring, std::string const& replacement );
std::vector< std::string > StringSplit( std::string const& text, char delimiter );
int StringToInt( std::string const& string, int base = 10 );

struct bad_lexical_cast: std::runtime_error 
{
	bad_lexical_cast(): std::runtime_error( "bad lexical cast" ) {}
};

template< typename To, typename From >
To lexical_cast( From const& from )
{
	std::stringstream parser;
	parser << from;
	To result;
	parser >> result;
	if ( !parser )
		throw bad_lexical_cast();
	return result;
}

class ReadLock
{
public:
	ReadLock( cRwLock& lock, int timeout = 100 ): m_lock( lock ), m_locked( false ) { if ( m_lock.Lock( false, timeout ) ) m_locked = true; }
	~ReadLock() { if ( m_locked ) m_lock.Unlock(); }

	operator bool() { return m_locked; }
	bool operator!() { return !m_locked; }

private:
	ReadLock( ReadLock const& );

	cRwLock& m_lock;
	bool m_locked;
};

} // namespace vdrlive

#endif // VDR_LIVE_TOOLS_H