summaryrefslogtreecommitdiff
path: root/tools.cpp
blob: fff3483856688615d2112f9e0f8b8c65144ed98b (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdexcept>
#include <tnt/ecpp.h>
#include <tnt/htmlescostream.h>
#include <tnt/httprequest.h>
#include <tnt/httpreply.h>
#include "exception.h"
#include "live.h"
#include "setup.h"
#include "tools.h"

using namespace std;
using namespace tnt;

istream& operator>>( istream& is, tChannelID& ret )
{
	if ( is.rdbuf()->in_avail() > 0 ) {
		string line;
		if ( !getline( is, line ) || ( !line.empty() && !( ret = tChannelID::FromString( line.c_str() ) ).Valid() ) )
			is.setstate( ios::badbit );
	}
	return is;
}

namespace vdrlive {

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;
}

string StringRepeat(int times, const string& input)
{
	string result;
	for (int i = 0; i < times; i++) {
		result += input;
	}
	return result;
}

string StringWordTruncate(const string& input, size_t maxLen, bool& truncated)
{
	if (input.length() <= maxLen)
	{
		return input;
	}
	truncated = true;
	string result = input.substr(0, maxLen);
	size_t pos = result.find_last_of(" \t,;:.\n?!'\"/\\()[]{}*+-");
	return result.substr(0, pos);
}

string StringFormatBreak(string const& input)
{
	return StringReplace( input, "\n", "<br/>" );
}

string StringEscapeAndBreak( string const& input )
{
	stringstream plainBuilder;
	HtmlEscOstream builder( plainBuilder );
	builder << input;
	return StringReplace( plainBuilder.str(), "\n", "<br/>" );
}

string StringTrim(string const& str)
{
   string res = str;
   string::size_type pos = res.find_last_not_of(' ');
   if(pos != string::npos) {
      res.erase(pos + 1);
      pos = res.find_first_not_of(' ');
      if(pos != string::npos) res.erase(0, pos);
   }
   else res.erase(res.begin(), res.end());
   return res;
}

} // namespace vdrlive