vdr  2.7.6
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 5.1 2024/10/09 10:36:16 kls Exp $
8  */
9 
10 #include "sources.h"
11 
12 // --- cSource ---------------------------------------------------------------
13 
15 {
16  code = stNone;
17  description = NULL;
18 }
19 
20 cSource::cSource(char Source, const char *Description)
21 {
22  code = int(Source) << 24;
23  description = strdup(Description);
24 }
25 
27 {
28  free(description);
29 }
30 
31 bool 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 
40 bool cSource::Matches(int Code1, int Code2)
41 {
42  if (Code1 == (stSat | st_Any))
43  return IsSat(Code2);
44  return Code1 == Code2;
45 }
46 
47 int cSource::Position(int Code)
48 {
49  return int16_t(Code & st_Pos);
50 }
51 
53 {
54  char buffer[16];
55  char *q = buffer;
56  *q++ = (Code & st_Mask) >> 24;
57  if (int n = Position(Code)) {
58  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
59  *q++ = (n < 0) ? 'W' : 'E';
60  }
61  *q = 0;
62  return buffer;
63 }
64 
65 int cSource::FromString(const char *s)
66 {
67  if (!isempty(s)) {
68  if ('A' <= *s && *s <= 'Z') {
69  int code = int(*s) << 24;
70  if (code == stSat) {
71  int pos = 0;
72  bool dot = false;
73  bool neg = false;
74  while (*++s) {
75  switch (*s) {
76  case '0' ... '9': pos *= 10;
77  pos += *s - '0';
78  break;
79  case '.': dot = true;
80  break;
81  case 'W': neg = true; // fall through to 'E'
82  case 'E': if (!dot)
83  pos *= 10;
84  break;
85  default: esyslog("ERROR: unknown source character '%c'", *s);
86  return stNone;
87  }
88  }
89  if (neg)
90  pos = -pos;
91  code |= (pos & st_Pos);
92  }
93  return code;
94  }
95  else
96  esyslog("ERROR: unknown source key '%c'", *s);
97  }
98  return stNone;
99 }
100 
101 int cSource::FromData(eSourceType SourceType, int Position, bool East)
102 {
103  int code = SourceType;
104  if (SourceType == stSat) {
105  if (!East)
106  Position = -Position;
107  code |= (Position & st_Pos);
108  }
109  return code;
110 }
111 
112 // --- cSources --------------------------------------------------------------
113 
115 
117 {
118  for (cSource *p = First(); p; p = Next(p)) {
119  if (p->Code() == Code)
120  return p;
121  }
122  return NULL;
123 }
124 
125 bool cSources::ContainsSourceType(char SourceType)
126 {
127  for (cSource *p = First(); p; p = Next(p)) {
128  if (cSource::ToChar(p->Code()) == SourceType)
129  return true;
130  }
131  return false;
132 }
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:650
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
Definition: tools.h:643
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:65
cSource(void)
Definition: sources.c:14
~cSource()
Definition: sources.c:26
static cString ToString(int Code)
Definition: sources.c:52
int code
Definition: sources.h:28
static char ToChar(int Code)
Definition: sources.h:51
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:101
const char * Description(void) const
Definition: sources.h:44
static bool Matches(int Code1, int Code2)
Returns true if Code2 matches Code1.
Definition: sources.c:40
bool ContainsSourceType(char SourceType)
Definition: sources.c:125
cSource * Get(int Code)
Definition: sources.c:116
Definition: tools.h:178
cSources Sources
Definition: sources.c:114
bool isempty(const char *s)
Definition: tools.c:357
#define esyslog(a...)
Definition: tools.h:35