-
Notifications
You must be signed in to change notification settings - Fork 1
/
Options.hxx
160 lines (114 loc) · 3.56 KB
/
Options.hxx
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "tclled.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "UDPSender.h"
#include <stdlib.h>
#include <signal.h>
#include "TCLZoned.hxx"
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <iostream>
void prepPixels(unsigned int *raw, const tcl_buffer &tcl);
void initTclBuf(const tcl_buffer &tcl);
void interruptHandler(int param);
using namespace std;
class Options {
public:
Options(int argc, const char * argv[]):
leds(200),
port(54321)
{
buffersize ={20,10};
slurp(argc,argv);
findStart = args.begin();
findEnd = args.end();
buffersize = eatXY("size",buffersize);
leds = eatInt("leds",leds);
port = eatInt("port",port);
eatOmissions();
eatZones();
}
~Options() {
std::list<Zone*>::iterator first = zones.begin(), last = zones.end();
for ( ; first!=last; ++first )
delete (*first);
}
void dump() {
cout << "port:" << port << endl
<<"leds:" << leds << endl
<<"wd:" << buffersize.x << ", ht:" << buffersize.y << endl;
}
public:
XY buffersize;
int leds;
int port;
vector<string> args;
list<Zone *> zones;
list<XY> omissions;
vector<string>::iterator findStart, findEnd;
private:
int getInt(string &strVal) { return atoi(strVal.c_str());}
XY eatXY(const char *key, XY defaultVal) {
vector<string>::iterator where, keyloc,xloc,yloc;
where = find(findStart, findEnd,key);
if(where == findEnd) return defaultVal;
keyloc = where;
xloc = where + 1;
yloc = where + 2;
XY rval{getInt(*xloc),getInt(*yloc)};
args.erase(keyloc,yloc);
return rval;
}
int eatInt(const char *key, int defaultVal) {
vector<string>::iterator where;
where = find(findStart,findEnd,key);
if(where == findEnd) return defaultVal;
int rval = getInt(where[1]);
args.erase(where, where+1);
return rval;
}
int eatFlag(const char *key) {
vector<string>::iterator where;
where = find(findStart,findEnd,key);
if(where == findEnd) return 0;
// cerr << where[0] << endl;
// args.erase(where);
return 1;
}
void slurp(int argc, const char **argv) {
for(int i=0 ; i < argc; i++)
args.push_back(argv[i]);
}
void eatZones() {
while(findZone())
zones.push_back(eatZone());
}
bool findZone() {
findStart = find(args.begin(), args.end(), "zone");
if(findStart == args.end()) return false;
findStart++;
findEnd = find(findStart,args.end(),"zone");
}
Zone *eatZone() {
XY orig = eatXY("origin",XY{0,0});
XY size = eatXY("size",XY{10,10});
int offset = eatInt("offset",0);
bool vertical = eatFlag("vertical");
int flipX = eatFlag("flipX");
int flipY = eatFlag("flipY");
args.erase(findStart,findEnd);
Zone *rval = vertical ? new VZone(orig, size,offset, flipX, flipY) :
new Zone(orig,size,offset, flipX, flipY);
return rval;
}
void eatOmissions() {
while(true) {
XY omit = eatXY("omit",XY{-1,-1});
if(omit.x == -1) return;
omissions.push_back(omit);
}
}
};