summaryrefslogtreecommitdiff
path: root/tools.cpp
blob: 515418a7ea5d83ab5ba42e0e04a67c9b4d89f44f (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
#include <stdexcept>
#include <tnt/httprequest.h>
#include <tnt/httpreply.h>
#include <tnt/ecpp.h>
#include "exception.h"
#include "live.h"
#include "setup.h"
#include "tools.h"

namespace vdrlive {

using namespace std;
	
string FormatDateTime( char const* format, time_t time )
{
	struct tm tm_r;
	if ( localtime_r( &time, &tm_r ) == 0 ) {
		ostringstream builder;
		builder << "cannot represent timestamp " << time << " as local time";
		throw runtime_error( builder.str() );
	}
	
	char result[ 256 ];
	if ( strftime( result, sizeof( result ), format, &tm_r ) == 0 ) {
		ostringstream builder;
		builder << "representation of timestamp " << time << " exceeds " << sizeof( result ) << " bytes";
		throw runtime_error( builder.str() );
	}
	return result;
}

string StringReplace( string const& text, string const& substring, string const& replacement )
{
	string result = text;
	string::size_type pos = 0;
	while ( ( pos = result.find( substring, pos ) ) != string::npos ) {
		result.replace( pos, substring.length(), replacement );
		pos += replacement.length();
	}
	return result;
}

vector< string > StringSplit( string const& text, char delimiter )
{
	vector< string > result;
	string::size_type last = 0, pos;
	while ( ( pos = text.find( delimiter, last ) ) != string::npos ) {
		result.push_back( text.substr( last, pos - last ) );
		last = pos + 1;
	}
	if ( last < text.length() )
		result.push_back( text.substr( last ) );
	return result;
}

int StringToInt( std::string const& string, int base )
{
	char* end;
	int result = strtol( string.c_str(), &end, base );
	if ( *end == '\0' )
		return result;
	return 0;
}

} // namespace vdrlive