summaryrefslogtreecommitdiff
path: root/common.c
blob: 42e4e4f64571ee5d124245d34f43c4f6bbfd2f39 (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
125
126
127
128
129
130
131
132
133
134
/*
 * $Id: common.c,v 1.2 2004/12/06 22:35:54 lordjaxom Exp $
 */

#include "common.h"
#include <vdr/plugin.h>

std::string SkinPath(void) {
	return cPlugin::ConfigDirectory(PLUGIN_NAME_I18N);
}

const char *ChannelNumber(const cChannel *Channel, int Number) {
	static char buffer[256];
	buffer[0] = '\0';
  if (Channel) {
     if (!Channel->GroupSep())
        snprintf(buffer, sizeof(buffer), "%d%s", Channel->Number(), Number ? "-" : "");
     }
  else if (Number)
     snprintf(buffer, sizeof(buffer), "%d-", Number);
  return buffer;
}

const char *ChannelName(const cChannel *Channel, int Number) {
	static char buffer[256];
	buffer[0] = '\0';
	if (Channel) 
		snprintf(buffer, sizeof(buffer), "%s", Channel->Name());
	else if (!Number)
		snprintf(buffer, sizeof(buffer), "%s", tr("*** Invalid Channel ***"));

	char *ptr;
	if ((ptr = strchr(buffer, ',')) != NULL
			|| (ptr = strchr(buffer, ';')) != NULL)
		*ptr = '\0';
	return buffer;
}

const char *ChannelShortName(const cChannel *Channel, int Number) {
	static char buffer[256];
	buffer[0] = '\0';
	if (Channel) 
		snprintf(buffer, sizeof(buffer), "%s", Channel->Name());
	else if (!Number)
		snprintf(buffer, sizeof(buffer), "%s", tr("*** Invalid Channel ***"));

	char *ptr;
	if ((ptr = strchr(buffer, ',')) != NULL) {
		char *start = ptr + 1;
		if ((ptr = strchr(start, ';')) != NULL)
			*ptr = '\0';
		return start;
	} else if ((ptr = strchr(buffer, ';')) != NULL)
		*ptr = '\0';
	
	return buffer;
}

const char *ChannelBouquet(const cChannel *Channel, int Number) {
	static char buffer[256];
	buffer[0] = '\0';
	if (Channel) 
		snprintf(buffer, sizeof(buffer), "%s", Channel->Name());
	else if (!Number)
		snprintf(buffer, sizeof(buffer), "%s", tr("*** Invalid Channel ***"));

	char *ptr;
	if ((ptr = strchr(buffer, ';')) != NULL)
		return ptr + 1;
	else
		return "";
}

cxType TimeType(time_t Time, const std::string &Format) {
	static char result[1000];
	struct tm tm_r, *tm;
	tm = localtime_r(&Time, &tm_r);

	if (Time > 0) {
		if (Format.length() > 0) {
			strftime(result, sizeof(result), Format.c_str(), tm);
			return result;
		} else
			return Time;
	}
	return "";
}

bool ParseVar(const char *Text, const char *Name, std::string &Value) {
	const char *ptr1, *ptr2;
	char *str;
	bool res = false;
	asprintf(&str, ",%s=", Name);
	if ((ptr1 = strstr(Text, str)) || (strncmp(ptr1 = Text, str + 1, strlen(str) - 1) == 0)) {
		if (ptr1 == Text)
			--ptr1;
		ptr1 += strlen(str);
		if ((ptr2 = strchr(ptr1, ',')) || (ptr2 = strchr(ptr1, ';'))) {
			int pos;
			Value = ptr1;
			Value.erase(ptr2 - ptr1);
			if (Value[0] == '"')
				SkipQuotes(Value);
			while ((pos = Value.find('$')) != -1)
				Value.replace(pos, 1, "{*}");
			res = true;
		}
	}
	free(str);
	return res;
}

bool ParseVar(const char *Text, const char *Name, tColor *Value) {
	std::string value;
	if (ParseVar(Text, Name, value) && value[0] == '#') {
		*Value = (tColor)strtoul(value.c_str() + 1, NULL, 16);
		return true;
	}
	return false;
}

void SkipQuotes(std::string &Value) {
	char quote = Value[0];
	int i;
	Value.erase(0, 1);
	for (i = 0; i < (int)Value.length() && Value[i] != quote; ++i) {
		if (Value[i] == '\\')
			Value.erase(i, 1);
	}
	if (Value[i] == quote)
		Value.erase(i, 1);
	else
		esyslog("ERROR: text2skin: missing closing %c", quote);
}