summaryrefslogtreecommitdiff
path: root/ledsconf.c
blob: 5cbd98a7e36d9742b0de25e50d522b1d155c57c4 (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
120
121
122
123
124
125
126
127
/*
 * ledsconf.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: ledsconf.c,v 1.18 2012/11/20 19:04:09 wendel Exp $
 */

#include <ctype.h>

#include "common.h"
#include "config.h"
#include "ledsconf.h"

//***************************************************************************
// cLedConf
//***************************************************************************
//***************************************************************************
// Object
//***************************************************************************

cLedConf::cLedConf() 
{ 
   x = na;
   toX = na;
   y = na;
   toY = na;
   lp = na;
}

//***************************************************************************
// Parse like like "led 0-1 14-17"
//***************************************************************************

bool cLedConf::Parse(const char* s)
{
   const char* p = s;

   p = skipWs(p);

   // check keyword

   if (strncasecmp(p, "led ", 4) != 0)
      return false;

   p += 4;
   skipWs(p);

   // LED Position

   if (strncasecmp(p, "top ", 4) == 0)
      lp = lpTop;
   else if (strncasecmp(p, "left ", 5) == 0)
      lp = lpLeft;
   else if (strncasecmp(p, "bot ", 4) == 0)
      lp = lpBottom;
   else if (strncasecmp(p, "bottom ", 7) == 0)
      lp = lpBottom;
   else if (strncasecmp(p, "right ", 6) == 0)
      lp = lpRight;
   else
      return error("Missing location {top,left,bot(tom),right}");
   
   // skip to delemiter
   
   while (*p && *p != ' ' && *p != '\t')
      p++;

   // check

   if (!*p)
      return false;

   skipWs(p);

   // parse X 

   if (!parseRange(p, x, toX))
      return false;

   // parse Y
   
   if (!parseRange(p, y, toY))
      return false;
   
   return true;
}

//***************************************************************************
// Parse Range like "12-26"
//***************************************************************************

bool cLedConf::parseRange(const char*& p, int& from, int& to)
{
   p = skipWs(p);

   if (!isdigit(*p))
      return false;

   from = to = strtol(p, (char**)&p, 0);

   p = skipWs(p);

   if (*p != '-')
      return true;

   p++;

   if (!isdigit(*p))
      return false;

   to = strtol(p, (char**)&p, 0);

   return true;
}

//***************************************************************************
// Skip Whitespaces
//***************************************************************************

const char* cLedConf::skipWs(const char* p)
{
   while (*p && (*p == ' ' || *p == '\t'))
      p++;

   return p;
}