vdr  1.7.27
sources.c
Go to the documentation of this file.
00001 /*
00002  * sources.c: Source handling
00003  *
00004  * See the main source file 'vdr.c' for copyright information and
00005  * how to reach the author.
00006  *
00007  * $Id: sources.c 2.2 2010/02/28 15:15:39 kls Exp $
00008  */
00009 
00010 #include "sources.h"
00011 
00012 // --- cSource ---------------------------------------------------------------
00013 
00014 cSource::cSource(void)
00015 {
00016   code = stNone;
00017   description = NULL;
00018 }
00019 
00020 cSource::cSource(char Source, const char *Description)
00021 {
00022   code = int(Source) << 24;
00023   description = strdup(Description);
00024 }
00025 
00026 cSource::~cSource()
00027 {
00028   free(description);
00029 }
00030 
00031 bool cSource::Parse(const char *s)
00032 {
00033   char *codeBuf = NULL;
00034   if (2 == sscanf(s, "%a[^ ] %a[^\n]", &codeBuf, &description))
00035      code = FromString(codeBuf);
00036   free(codeBuf);
00037   return code != stNone && description && *description;
00038 }
00039 
00040 cString cSource::ToString(int Code)
00041 {
00042   char buffer[16];
00043   char *q = buffer;
00044   *q++ = (Code & st_Mask) >> 24;
00045   int n = (Code & st_Pos);
00046   if (n > 0x00007FFF)
00047      n |= 0xFFFF0000;
00048   if (n) {
00049      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
00050      *q++ = (n < 0) ? 'E' : 'W';
00051      }
00052   *q = 0;
00053   return buffer;
00054 }
00055 
00056 int cSource::FromString(const char *s)
00057 {
00058   if (!isempty(s)) {
00059      if ('A' <= *s && *s <= 'Z') {
00060         int code = int(*s) << 24;
00061         if (code == stSat) {
00062            int pos = 0;
00063            bool dot = false;
00064            bool neg = false;
00065            while (*++s) {
00066                  switch (*s) {
00067                    case '0' ... '9': pos *= 10;
00068                                      pos += *s - '0';
00069                                      break;
00070                    case '.':         dot = true;
00071                                      break;
00072                    case 'E':         neg = true; // fall through to 'W'
00073                    case 'W':         if (!dot)
00074                                         pos *= 10;
00075                                      break;
00076                    default: esyslog("ERROR: unknown source character '%c'", *s);
00077                             return stNone;
00078                    }
00079                  }
00080            if (neg)
00081               pos = -pos;
00082            code |= (pos & st_Pos);
00083            }
00084         return code;
00085         }
00086      else
00087        esyslog("ERROR: unknown source key '%c'", *s);
00088      }
00089   return stNone;
00090 }
00091 
00092 int cSource::FromData(eSourceType SourceType, int Position, bool East)
00093 {
00094   int code = SourceType;
00095   if (SourceType == stSat) {
00096      if (East)
00097         Position = -Position;
00098      code |= (Position & st_Pos);;
00099      }
00100   return code;
00101 }
00102 
00103 // --- cSources --------------------------------------------------------------
00104 
00105 cSources Sources;
00106 
00107 cSource *cSources::Get(int Code)
00108 {
00109   for (cSource *p = First(); p; p = Next(p)) {
00110       if (p->Code() == Code)
00111          return p;
00112       }
00113   return NULL;
00114 }