remaining parts of config parser implemented
Stefan Schuermans

Stefan Schuermans commited on 2011-09-11 10:46:13
Showing 4 changed files, with 119 additions and 32 deletions.

... ...
@@ -33,7 +33,7 @@ PKG_PATH=org/blinkenarea/JFlexiPix
33 33
 PKG_PATH_EX=$(PKG_PATH)/examples
34 34
 
35 35
 CLASSES=AddrParser Config Constants Display Distri \
36
-        Mapping MessageIf MsgType Pixel PixelParser
36
+        Mapping MessageIf MsgType Pixel PixelParser Version
37 37
 CLASSES_EX=Blink Msg
38 38
 CLASS_FILES=$(addprefix $(PKG_PATH)/,$(addsuffix .class,$(CLASSES)))
39 39
 CLASS_FILES_EX=$(addprefix $(PKG_PATH_EX)/,$(addsuffix .class,$(CLASSES_EX)))
... ...
@@ -1 +1,2 @@
1 1
 *.class
2
+Version.java
... ...
@@ -72,33 +72,33 @@ class Config
72 72
       dist = Constants.distriMaxCnt; // force error in next line
73 73
     }
74 74
     if (dist >= Constants.distriMaxCnt)
75
-      error("invalid distributor number \"" + settingPart2 + "\"" +
75
+      errorExc("invalid distributor number \"" + settingPart2 + "\"" +
76 76
                String.format(" in line %d of config file", m_lineNo));
77 77
 
78 78
     // get number of outputs and pixels
79 79
     px = PixelParser.parsePixel(value); // abuse for "outputs,pixels"
80 80
     if (px == null)
81
-      error("invalid distributor size \"" + value + "\"" +
81
+      errorExc("invalid distributor size \"" + value + "\"" +
82 82
                String.format(" in line %d of config file", m_lineNo));
83 83
     out = px.m_x;
84 84
     if (out >= Constants.outputMaxCnt)
85
-      error(String.format("invalid number of outputs \"%d\"" +
85
+      errorExc(String.format("invalid number of outputs \"%d\"" +
86 86
                              " in line %d of config file", out, m_lineNo));
87 87
     pix = px.m_y;
88 88
     if (pix >= Constants.pixelMaxCnt)
89
-      error(String.format("invalid number of pixels \"%d\"" +
89
+      errorExc(String.format("invalid number of pixels \"%d\"" +
90 90
                              " in line %d of config file", pix, m_lineNo));
91 91
 
92 92
     // check if distributor is already present
93 93
     if (m_display.m_distris[dist] != null)
94
-      error(String.format("duplicate definition of distributor \"%d\"" +
94
+      errorExc(String.format("duplicate definition of distributor \"%d\"" +
95 95
                              " in line %d of config file", dist, m_lineNo));
96 96
 
97 97
     // create new distributor
98 98
     m_display.m_distris[dist] = new Distri(dist, out, pix);
99 99
 
100 100
     // count distributors
101
-    ++m_display.m_distriCnt;
101
+    m_display.m_distriCnt++;
102 102
   }
103 103
 
104 104
   /**
... ...
@@ -122,7 +122,7 @@ class Config
122 122
     pattern = Pattern.compile("^([0-9]+) +([a-z]+)$");
123 123
     matcher = pattern.matcher(settingPart2);
124 124
     if (!matcher.find())
125
-      error("invalid mapping specifier \"" + settingPart2 + "\"" +
125
+      errorExc("invalid mapping specifier \"" + settingPart2 + "\"" +
126 126
                String.format(" in line %d of config file", m_lineNo));
127 127
     strDistri  = matcher.group(1);
128 128
     strChannel = matcher.group(2);
... ...
@@ -134,13 +134,13 @@ class Config
134 134
       dist = Constants.distriMaxCnt; // force error in next line
135 135
     }
136 136
     if (dist >= Constants.distriMaxCnt)
137
-      error("invalid distributor number \"" + strDistri + "\"" +
137
+      errorExc("invalid distributor number \"" + strDistri + "\"" +
138 138
                String.format(" in line %d of config file", m_lineNo));
139 139
 
140 140
     // get distributor
141 141
     distri = m_display.m_distris[dist];
142 142
     if (distri == null)
143
-      error(String.format("no distributor with number \"%d\"" +
143
+      errorExc(String.format("no distributor with number \"%d\"" +
144 144
                              " in line %d of config file", dist, m_lineNo));
145 145
 
146 146
     // get channel
... ...
@@ -151,14 +151,14 @@ class Config
151 151
     else if (strChannel.equals("blue"))
152 152
       mapping = distri.m_mapBlue;
153 153
     else
154
-      error("invalid channel \"" + strChannel + "\"" +
154
+      errorExc("invalid channel \"" + strChannel + "\"" +
155 155
                String.format(" in line %d of config file", m_lineNo));
156 156
 
157 157
     // split mapping parameters
158 158
     pattern = Pattern.compile("^([-+.eE0-9]+) +([-+.eE0-9]+) +([-+.eE0-9]+)$");
159 159
     matcher = pattern.matcher(value);
160 160
     if (!matcher.find())
161
-      error("invalid mapping parameters \"" + value + "\"" +
161
+      errorExc("invalid mapping parameters \"" + value + "\"" +
162 162
                String.format(" in line %d of config file", m_lineNo));
163 163
     strBase   = matcher.group(1);
164 164
     strFactor = matcher.group(2);
... ...
@@ -170,11 +170,11 @@ class Config
170 170
       factor = Double.parseDouble(strFactor);
171 171
       gamma  = Double.parseDouble(strGamma);
172 172
     } catch (NumberFormatException e) {
173
-      error("invalid mapping parameters \"" + value + "\"" +
173
+      errorExc("invalid mapping parameters \"" + value + "\"" +
174 174
                String.format(" in line %d of config file", m_lineNo), e);
175 175
     }
176 176
     if (gamma <= 0.0)
177
-      error(String.format("invalid gamma value \"%f\"" +
177
+      errorExc(String.format("invalid gamma value \"%f\"" +
178 178
                              " in line %d of config file", gamma, m_lineNo));
179 179
 
180 180
     // update mapping parameters
... ...
@@ -185,14 +185,51 @@ class Config
185 185
    * @brief process pixel from config file
186 186
    *
187 187
    * @param[in] txtPixel text of pixel to process
188
+   * @param[in] distri distributor
188 189
    * @param[in] dist number of distributor
189 190
    * @param[in] out number of output
190 191
    * @param[in] pix number of pixel
192
+   * @param return if parsing was successful
191 193
    */
192
-  void procPixel(String txtPixel, int dist, int out, int pix)
193
-    throws Exception
194
+  boolean procPixel(String txtPixel, Distri distri, int dist, int out, int pix)
194 195
   {
195
-    // TODO
196
+    Pixel pixel;
197
+    int idx;
198
+
199
+    // get coordinates of pixel
200
+    pixel = PixelParser.parsePixel(txtPixel);
201
+    if (pixel == null) {
202
+      error("invalid pixel \"" + txtPixel + "\"" +
203
+            String.format(" in line %d of config file", m_lineNo));
204
+      return false;
205
+    }
206
+
207
+    // check pixel number
208
+    if (pix >= Constants.pixelMaxCnt || pix >= distri.m_pixelCnt) {
209
+      error(String.format("too many pixels (more than %d)" +
210
+                          " in line %d of config file",
211
+                          distri.m_pixelCnt, m_lineNo));
212
+      return false;
213
+    }
214
+
215
+    // check that pixel is not yet set
216
+    idx = out * distri.m_pixelCnt + pix;
217
+    if (distri.m_pixels[idx] != null) {
218
+      error(String.format("pixel %d of output %d of distributor %d" +
219
+                          " already set to pixel %d,%d" +
220
+                          " in line %d of config file",
221
+                          pix, out, dist, distri.m_pixels[idx].m_x,
222
+                          distri.m_pixels[idx].m_y, m_lineNo));
223
+      return false;
224
+    }
225
+
226
+    // create pixel
227
+    distri.m_pixels[idx] = pixel;
228
+
229
+    // count pixels in total
230
+    m_display.m_pixelCnt++;
231
+
232
+    return true;
196 233
   }
197 234
 
198 235
   /**
... ...
@@ -204,7 +241,49 @@ class Config
204 241
   void procOutput(String settingPart2, String value)
205 242
     throws Exception
206 243
   {
207
-    // TODO
244
+    Pixel px;
245
+    int dist, out, pix;
246
+    Distri distri;
247
+    String [] pixels;
248
+    boolean err;
249
+
250
+    // get number of distributor and output
251
+    px = PixelParser.parsePixel(settingPart2); // abuse for "dist,out"
252
+    if (px == null)
253
+      errorExc("invalid output specifier \"" + settingPart2 + "\"" +
254
+               String.format(" in line %d of config file", m_lineNo));
255
+    dist = px.m_x;
256
+    if (dist >= Constants.distriMaxCnt)
257
+      errorExc(String.format("invalid distributor number \"%d\"" +
258
+                             " in line %d of config file", dist, m_lineNo));
259
+    out = px.m_y;
260
+    if (out >= Constants.outputMaxCnt)
261
+      errorExc(String.format("invalid output number \"%d\"" +
262
+                             " in line %d of config file", out, m_lineNo));
263
+
264
+    // get distributor
265
+    distri = m_display.m_distris[dist];
266
+    if (distri == null)
267
+      errorExc(String.format("no distributor with number \"%d\"" +
268
+                             " in line %d of config file", dist, m_lineNo));
269
+
270
+    // check output number
271
+    if (out >= distri.m_outputCnt)
272
+      errorExc(String.format("no output with output number \"%d\"" +
273
+                             " in line %d of config file", out, m_lineNo));
274
+
275
+    // count outputs
276
+    m_display.m_outputCnt++;
277
+
278
+    // process pixels
279
+    pixels = value.split("\\s+");
280
+    err = false;
281
+    for (pix = 0; pix < pixels.length; ++pix)
282
+      if (!procPixel(pixels[pix], distri, dist, out, pix))
283
+        err = true;
284
+    if (err)
285
+      errorExc(String.format("invalid pixels in line %d of config file",
286
+                             m_lineNo));
208 287
   }
209 288
 
210 289
   /**
... ...
@@ -227,7 +306,7 @@ class Config
227 306
     if (setting.equals("bindAddr")) {
228 307
       addr = AddrParser.parseAddr(value);
229 308
       if (addr == null) {
230
-        error("invalid addess \"" + value + "\" for \"" + setting +
309
+        errorExc("invalid addess \"" + value + "\" for \"" + setting +
231 310
                  String.format(" in line %d of config file", m_lineNo));
232 311
       }
233 312
       info("bind address: " + addr.toString());
... ...
@@ -239,7 +318,7 @@ class Config
239 318
     if (setting.equals("size")) {
240 319
       pix = PixelParser.parsePixel(value);
241 320
       if (pix == null || pix.m_x <= 0 || pix.m_y <= 0) {
242
-        error("invalid value \"" + value + "\" for \"" + setting +
321
+        errorExc("invalid value \"" + value + "\" for \"" + setting +
243 322
                  String.format(" in line %d of config file", m_lineNo));
244 323
       }
245 324
       m_display.m_size = pix;
... ...
@@ -317,7 +396,7 @@ class Config
317 396
 
318 397
     // check file name
319 398
     if (configFileName.length() == 0)
320
-      error("no config file specified");
399
+      errorExc("no config file specified");
321 400
 
322 401
     info("using config file \"" + configFileName + "\"");
323 402
 
... ...
@@ -326,7 +405,7 @@ class Config
326 405
       fr = new FileReader(configFileName);
327 406
       br = new BufferedReader(fr);
328 407
     } catch (FileNotFoundException e) {
329
-      error("cannot open config file \"" + configFileName +
408
+      errorExc("cannot open config file \"" + configFileName +
330 409
                "\" for reading", e);
331 410
     }
332 411
 
... ...
@@ -345,19 +424,27 @@ class Config
345 424
                                                m_display.m_size.m_y) +
346 425
          String.format("%d distributors, ",    m_display.m_distriCnt ) +
347 426
          String.format("%d outputs, ",         m_display.m_outputCnt ) +
348
-         String.format("%d pixels, ",          m_display.m_pixelCnt) +
349
-         "\n");
427
+         String.format("%d pixels\n",          m_display.m_pixelCnt));
350 428
   }
351 429
 
352 430
   /**
353
-   * @brief report error message and throw exception
431
+   * @brief report error message
354 432
    * @param[in] txt error text
355 433
    */
356 434
   private void error(String txt)
357
-    throws Exception
358 435
   {
359 436
     if (m_messageIf != null)
360 437
       m_messageIf.message(MsgType.Err, txt + "\n");
438
+  }
439
+
440
+  /**
441
+   * @brief report error message and throw exception
442
+   * @param[in] txt error text
443
+   */
444
+  private void errorExc(String txt)
445
+    throws Exception
446
+  {
447
+    error(txt);
361 448
     throw new Exception(txt);
362 449
   }
363 450
 
... ...
@@ -366,11 +453,10 @@ class Config
366 453
    * @param[in] txt error text
367 454
    * @param[in] e reason for error
368 455
    */
369
-  private void error(String txt, Exception e)
456
+  private void errorExc(String txt, Exception e)
370 457
     throws Exception
371 458
   {
372
-    if (m_messageIf != null)
373
-      m_messageIf.message(MsgType.Err, txt + ": " + e.getMessage() + "\n");
459
+    error(txt + ": " + e.getMessage());
374 460
     throw new Exception(txt, e);
375 461
   }
376 462
 
... ...
@@ -142,9 +142,9 @@ public class Display
142 142
               if (rx >= 0 && rx < width && ry >= 0 && ry < height) {
143 143
                 // get pixel data and map it
144 144
                 src = rx * strideX + ry * strideY;
145
-                distri.m_msgBuf[dest+0] = distri.m_mapRed.m_table[data[src+0]];
146
-                distri.m_msgBuf[dest+1] = distri.m_mapGreen.m_table[data[src+1]];
147
-                distri.m_msgBuf[dest+2] = distri.m_mapBlue.m_table[data[src+2]];
145
+                distri.m_msgBuf[dest+0] = distri.m_mapRed.m_table[data[src+0] & 0xFF];
146
+                distri.m_msgBuf[dest+1] = distri.m_mapGreen.m_table[data[src+1] & 0xFF];
147
+                distri.m_msgBuf[dest+2] = distri.m_mapBlue.m_table[data[src+2] & 0xFF];
148 148
               }
149 149
 
150 150
             } // if pixel
151 151