vdr  2.0.7
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 2.2.1.1 2014/03/09 12:13:25 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, "%a[^ ] %a[^\n]", &codeBuf, &description))
35  code = FromString(codeBuf);
36  free(codeBuf);
37  return code != stNone && description && *description;
38 }
39 
41 {
42  char buffer[16];
43  char *q = buffer;
44  *q++ = (Code & st_Mask) >> 24;
45  int n = (Code & st_Pos);
46  if (n > 0x00007FFF)
47  n |= 0xFFFF0000;
48  if (n) {
49  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
50  *q++ = (n < 0) ? 'E' : 'W';
51  }
52  *q = 0;
53  return buffer;
54 }
55 
56 int cSource::FromString(const char *s)
57 {
58  if (!isempty(s)) {
59  if ('A' <= *s && *s <= 'Z') {
60  int code = int(*s) << 24;
61  if (code == stSat) {
62  int pos = 0;
63  bool dot = false;
64  bool neg = false;
65  while (*++s) {
66  switch (*s) {
67  case '0' ... '9': pos *= 10;
68  pos += *s - '0';
69  break;
70  case '.': dot = true;
71  break;
72  case 'E': neg = true; // fall through to 'W'
73  case 'W': if (!dot)
74  pos *= 10;
75  break;
76  default: esyslog("ERROR: unknown source character '%c'", *s);
77  return stNone;
78  }
79  }
80  if (neg)
81  pos = -pos;
82  code |= (pos & st_Pos);
83  }
84  return code;
85  }
86  else
87  esyslog("ERROR: unknown source key '%c'", *s);
88  }
89  return stNone;
90 }
91 
92 int cSource::FromData(eSourceType SourceType, int Position, bool East)
93 {
94  int code = SourceType;
95  if (SourceType == stSat) {
96  if (East)
97  Position = -Position;
98  code |= (Position & st_Pos);;
99  }
100  return code;
101 }
102 
103 // --- cSources --------------------------------------------------------------
104 
106 
108 {
109  for (cSource *p = First(); p; p = Next(p)) {
110  if (p->Code() == Code)
111  return p;
112  }
113  return NULL;
114 }
115 
116 bool cSources::ContainsSourceType(char SourceType)
117 {
118  for (cSource *p = First(); p; p = Next(p)) {
119  if (cSource::ToChar(p->Code()) == SourceType)
120  return true;
121  }
122  return false;
123 }
static cString ToString(int Code)
Definition: sources.c:40
static char ToChar(int Code)
Definition: sources.h:36
bool isempty(const char *s)
Definition: tools.c:248
#define esyslog(a...)
Definition: tools.h:34
bool Parse(const char *s)
Definition: sources.c:31
cSource * Next(const cSource *object) const
Definition: tools.h:485
~cSource()
Definition: sources.c:26
int code
Definition: sources.h:27
cSources Sources
Definition: sources.c:105
eSourceType
Definition: sources.h:17
cSource * First(void) const
Definition: tools.h:482
bool ContainsSourceType(char SourceType)
Definition: sources.c:116
static int FromString(const char *s)
Definition: sources.c:56
cSource(void)
Definition: sources.c:14
cSource * Get(int Code)
Definition: sources.c:107
static int FromData(eSourceType SourceType, int Position=0, bool East=false)
Definition: sources.c:92
char * description
Definition: sources.h:28
Definition: tools.h:166