summaryrefslogtreecommitdiff
path: root/sources.c
blob: 94df64c63054abeb0a6a298715f697c50698721d (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
/*
 * sources.c: Source handling
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: sources.c 3.2 2013/04/11 10:24:05 kls Exp $
 */

#include "sources.h"

// --- cSource ---------------------------------------------------------------

cSource::cSource(void)
{
  code = stNone;
  description = NULL;
}

cSource::cSource(char Source, const char *Description)
{
  code = int(Source) << 24;
  description = strdup(Description);
}

cSource::~cSource()
{
  free(description);
}

bool cSource::Parse(const char *s)
{
  char *codeBuf = NULL;
  if (2 == sscanf(s, "%a[^ ] %a[^\n]", &codeBuf, &description))
     code = FromString(codeBuf);
  free(codeBuf);
  return code != stNone && description && *description;
}

int cSource::Position(int Code)
{
  int n = (Code & st_Pos);
  if (n > 0x00007FFF)
     n |= 0xFFFF0000;
  return n;
}

cString cSource::ToString(int Code)
{
  char buffer[16];
  char *q = buffer;
  *q++ = (Code & st_Mask) >> 24;
  if (int n = Position(Code)) {
     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
     *q++ = (n < 0) ? 'W' : 'E';
     }
  *q = 0;
  return buffer;
}

int cSource::FromString(const char *s)
{
  if (!isempty(s)) {
     if ('A' <= *s && *s <= 'Z') {
        int code = int(*s) << 24;
        if (code == stSat) {
           int pos = 0;
           bool dot = false;
           bool neg = false;
           while (*++s) {
                 switch (*s) {
                   case '0' ... '9': pos *= 10;
                                     pos += *s - '0';
                                     break;
                   case '.':         dot = true;
                                     break;
                   case 'W':         neg = true; // fall through to 'E'
                   case 'E':         if (!dot)
                                        pos *= 10;
                                     break;
                   default: esyslog("ERROR: unknown source character '%c'", *s);
                            return stNone;
                   }
                 }
           if (neg)
              pos = -pos;
           code |= (pos & st_Pos);
           }
        return code;
        }
     else
       esyslog("ERROR: unknown source key '%c'", *s);
     }
  return stNone;
}

int cSource::FromData(eSourceType SourceType, int Position, bool East)
{
  int code = SourceType;
  if (SourceType == stSat) {
     if (!East)
        Position = -Position;
     code |= (Position & st_Pos);
     }
  return code;
}

// --- cSources --------------------------------------------------------------

cSources Sources;

cSource *cSources::Get(int Code)
{
  for (cSource *p = First(); p; p = Next(p)) {
      if (p->Code() == Code)
         return p;
      }
  return NULL;
}