Home

Dokumentation

Impressum

Dokumentation VDR
 

Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

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 1.1 2002/10/06 09:27:10 kls Exp $
00008  */
00009 
00010 #include "sources.h"
00011 #include <ctype.h>
00012 
00013 // -- cSource ----------------------------------------------------------------
00014 
00015 cSource::cSource(void)
00016 {
00017   code = stNone;
00018   description = NULL;
00019 }
00020 
00021 cSource::~cSource()
00022 {
00023   free(description);
00024 }
00025 
00026 bool cSource::Parse(const char *s)
00027 {
00028   char *codeBuf = NULL;
00029   if (2 == sscanf(s, "%a[^ ] %a[^\n]", &codeBuf, &description))
00030      code = FromString(codeBuf);
00031   free(codeBuf);
00032   return code != stNone && description && *description;
00033 }
00034 
00035 const char *cSource::ToString(int Code)
00036 {
00037   static char buffer[16];
00038   char *q = buffer;
00039   switch (Code & st_Mask) {
00040     case stCable: *q++ = 'C'; break;
00041     case stSat:   *q++ = 'S';
00042                   {
00043                     int pos = Code & ~st_Mask;
00044                     q += snprintf(q, sizeof(buffer) - 2, "%u.%u", (pos & ~st_Neg) / 10, (pos & ~st_Neg) % 10); // can't simply use "%g" here since the silly 'locale' messes up the decimal point
00045                     *q++ = (Code & st_Neg) ? 'E' : 'W';
00046                   }
00047                   break;
00048     case stTerr:  *q++ = 'T'; break;
00049     default:      *q++ = Code + '0'; // backward compatibility
00050     }
00051   *q = 0;
00052   return buffer;
00053 }
00054 
00055 int cSource::FromString(const char *s)
00056 {
00057   int type = stNone;
00058   switch (toupper(*s)) {
00059     case 'C': type = stCable; break;
00060     case 'S': type = stSat;   break;
00061     case 'T': type = stTerr;  break;
00062     case '0' ... '9': type = *s - '0'; break; // backward compatibility
00063     default: esyslog("ERROR: unknown source key '%c'", *s);
00064              return stNone;
00065     }
00066   int code = type;
00067   if (type == stSat) {
00068      int pos = 0;
00069      bool dot = false;
00070      bool neg = false;
00071      while (*++s) {
00072            switch (toupper(*s)) {
00073              case '0' ... '9': pos *= 10;
00074                                pos += *s - '0';
00075                                break;
00076              case '.':         dot = true;
00077                                break;
00078              case 'E':         neg = true; // fall through to 'W'
00079              case 'W':         if (!dot)
00080                                   pos *= 10;
00081                                break;
00082              default: esyslog("ERROR: unknown source character '%c'", *s);
00083                       return stNone;
00084              }
00085            }
00086      if (neg)
00087         pos |= st_Neg;
00088      code |= pos;
00089      }
00090   return code;
00091 }
00092 
00093 // -- cSources ---------------------------------------------------------------
00094 
00095 cSources Sources;
00096 
00097 cSource *cSources::Get(int Code)
00098 {
00099   for (cSource *p = First(); p; p = Next(p)) {
00100       if (p->Code() == Code)
00101          return p;
00102       }
00103   return NULL;
00104 }

Generated on Wed Feb 5 23:30:11 2003 for VDR by doxygen1.3-rc2