vdr 2.6.3
sources.c
Go to the documentation of this file.
1/*
2 * sources.c: Source handling
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: sources.c 3.6 2014/03/09 12:05:42 kls Exp $
8 */
9
10#include "sources.h"
11
12// --- cSource ---------------------------------------------------------------
13
15{
16 code = stNone;
17 description = NULL;
18}
19
20cSource::cSource(char Source, const char *Description)
21{
22 code = int(Source) << 24;
23 description = strdup(Description);
24}
25
27{
28 free(description);
29}
30
31bool cSource::Parse(const char *s)
32{
33 char *codeBuf = NULL;
34 if (2 == sscanf(s, "%m[^ ] %m[^\n]", &codeBuf, &description))
35 code = FromString(codeBuf);
36 free(codeBuf);
37 return code != stNone && description && *description;
38}
39
40bool cSource::Matches(int Code1, int Code2)
41{
42 if (Code1 == (stSat | st_Any))
43 return IsSat(Code2);
44 return Code1 == Code2;
45}
46
47int cSource::Position(int Code)
48{
49 int n = (Code & st_Pos);
50 if (n > 0x00007FFF)
51 n |= 0xFFFF0000;
52 return n;
53}
54
56{
57 char buffer[16];
58 char *q = buffer;
59 *q++ = (Code & st_Mask) >> 24;
60 if (int n = Position(Code)) {
61 q += snprintf(q, sizeof(buffer) - 2, "%u.%u", abs(n) / 10, abs(n) % 10); // can't simply use "%g" here since the silly 'locale' messes up the decimal point
62 *q++ = (n < 0) ? 'W' : 'E';
63 }
64 *q = 0;
65 return buffer;
66}
67
68int cSource::FromString(const char *s)
69{
70 if (!isempty(s)) {
71 if ('A' <= *s && *s <= 'Z') {
72 int code = int(*s) << 24;
73 if (code == stSat) {
74 int pos = 0;
75 bool dot = false;
76 bool neg = false;
77 while (*++s) {
78 switch (*s) {
79 case '0' ... '9': pos *= 10;
80 pos += *s - '0';
81 break;
82 case '.': dot = true;
83 break;
84 case 'W': neg = true; // fall through to 'E'
85 case 'E': if (!dot)
86 pos *= 10;
87 break;
88 default: esyslog("ERROR: unknown source character '%c'", *s);
89 return stNone;
90 }
91 }
92 if (neg)
93 pos = -pos;
94 code |= (pos & st_Pos);
95 }
96 return code;
97 }
98 else
99 esyslog("ERROR: unknown source key '%c'", *s);
100 }
101 return stNone;
102}
103
104int cSource::FromData(eSourceType SourceType, int Position, bool East)
105{
106 int code = SourceType;
107 if (SourceType == stSat) {
108 if (!East)
110 code |= (Position & st_Pos);
111 }
112 return code;
113}
114
115// --- cSources --------------------------------------------------------------
116
118
120{
121 for (cSource *p = First(); p; p = Next(p)) {
122 if (p->Code() == Code)
123 return p;
124 }
125 return NULL;
126}
127
128bool cSources::ContainsSourceType(char SourceType)
129{
130 for (cSource *p = First(); p; p = Next(p)) {
131 if (cSource::ToChar(p->Code()) == SourceType)
132 return true;
133 }
134 return false;
135}
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
Definition: tools.h:653
const T * Next(const T *Object) const
< Returns the element immediately before Object in this list, or NULL if Object is the first element ...
Definition: tools.h:660
bool Parse(const char *s)
Definition: sources.c:31
int Code(void) const
Definition: sources.h:34
static int FromString(const char *s)
Definition: sources.c:68
cSource(void)
Definition: sources.c:14
~cSource()
Definition: sources.c:26
static cString ToString(int Code)
Definition: sources.c:55
int code
Definition: sources.h:28
static char ToChar(int Code)
Definition: sources.h:51
const char * Description(void) const
Definition: sources.h:44
int Position(void)
Returns the orbital position of the satellite in case this is a DVB-S source (zero otherwise).
Definition: sources.h:35
char * description
Definition: sources.h:29
static bool IsSat(int Code)
Definition: sources.h:57
eSourceType
Definition: sources.h:17
@ st_Mask
Definition: sources.h:23
@ stSat
Definition: sources.h:21
@ stNone
Definition: sources.h:18
@ st_Pos
Definition: sources.h:24
@ st_Any
Definition: sources.h:25
static int FromData(eSourceType SourceType, int Position=0, bool East=false)
Definition: sources.c:104
static bool Matches(int Code1, int Code2)
Returns true if Code2 matches Code1.
Definition: sources.c:40
bool ContainsSourceType(char SourceType)
Definition: sources.c:128
cSource * Get(int Code)
Definition: sources.c:119
Definition: tools.h:178
cSources Sources
Definition: sources.c:117
bool isempty(const char *s)
Definition: tools.c:349
#define esyslog(a...)
Definition: tools.h:35