vdr  2.2.0
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 
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 
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 
68 int 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 
104 int cSource::FromData(eSourceType SourceType, int Position, bool East)
105 {
106  int code = SourceType;
107  if (SourceType == stSat) {
108  if (!East)
109  Position = -Position;
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 
128 bool 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 }
static cString ToString(int Code)
Definition: sources.c:55
static char ToChar(int Code)
Definition: sources.h:51
bool isempty(const char *s)
Definition: tools.c:297
int Code(void) const
Definition: sources.h:34
#define esyslog(a...)
Definition: tools.h:34
bool Parse(const char *s)
Definition: sources.c:31
~cSource()
Definition: sources.c:26
int code
Definition: sources.h:28
cListObject * Next(void) const
Definition: tools.h:468
cSources Sources
Definition: sources.c:117
static bool IsSat(int Code)
Definition: sources.h:57
eSourceType
Definition: sources.h:17
const char * Description(void) const
Definition: sources.h:44
bool ContainsSourceType(char SourceType)
Definition: sources.c:128
static int FromString(const char *s)
Definition: sources.c:68
static bool Matches(int Code1, int Code2)
Returns true if Code2 matches Code1.
Definition: sources.c:40
int Position(void)
Returns the orbital position of the satellite in case this is a DVB-S source (zero otherwise)...
Definition: sources.h:35
cSource(void)
Definition: sources.c:14
cSource * Get(int Code)
Definition: sources.c:119
static int FromData(eSourceType SourceType, int Position=0, bool East=false)
Definition: sources.c:104
char * description
Definition: sources.h:29
Definition: tools.h:168